Implement queue with O(1) operations, and correct some math.
[tor/rransom.git] / src / common / ht.h
blob5187c90e6f540e3b103d88d22818072c3cebac91
1 /* Copyright (c) 2002, Christopher Clark.
2 * Copyright (c) 2005-2006, Nick Mathewson.
3 * Copyright (c) 2007-2009, The Tor Project, Inc. */
4 /* See license at end. */
6 /* Based on ideas by Christopher Clark and interfaces from Niels Provos. */
8 #ifndef _TOR_HT_H
9 #define _TOR_HT_H
11 #define HT_HEAD(name, type) \
12 struct name { \
13 /* The hash table itself. */ \
14 struct type **hth_table; \
15 /* How long is the hash table? */ \
16 unsigned hth_table_length; \
17 /* How many elements does the table contain? */ \
18 unsigned hth_n_entries; \
19 /* How many elements will we allow in the table before resizing it? */ \
20 unsigned hth_load_limit; \
21 /* Position of hth_table_length in the primes table. */ \
22 int hth_prime_idx; \
25 #define HT_INITIALIZER() \
26 { NULL, 0, 0, 0, -1 }
28 #define HT_ENTRY(type) \
29 struct { \
30 struct type *hte_next; \
31 unsigned hte_hash; \
34 #define HT_EMPTY(head) \
35 ((head)->hth_n_entries == 0)
37 /* Helper: alias for the bucket containing 'elm'. */
38 #define _HT_BUCKET(head, field, elm) \
39 ((head)->hth_table[elm->field.hte_hash % head->hth_table_length])
41 /* How many elements in 'head'? */
42 #define HT_SIZE(head) \
43 ((head)->hth_n_entries)
45 /* Return memory usage for a hashtable (not counting the entries themselves) */
46 #define HT_MEM_USAGE(head) \
47 (sizeof(*head) + (head)->hth_table_length * sizeof(void*))
49 #define HT_FIND(name, head, elm) name##_HT_FIND((head), (elm))
50 #define HT_INSERT(name, head, elm) name##_HT_INSERT((head), (elm))
51 #define HT_REPLACE(name, head, elm) name##_HT_REPLACE((head), (elm))
52 #define HT_REMOVE(name, head, elm) name##_HT_REMOVE((head), (elm))
53 #define HT_START(name, head) name##_HT_START(head)
54 #define HT_NEXT(name, head, elm) name##_HT_NEXT((head), (elm))
55 #define HT_NEXT_RMV(name, head, elm) name##_HT_NEXT_RMV((head), (elm))
56 #define HT_CLEAR(name, head) name##_HT_CLEAR(head)
57 #define HT_INIT(name, head) name##_HT_INIT(head)
58 /* Helper: */
59 static INLINE unsigned
60 ht_improve_hash(unsigned h)
62 /* Aim to protect against poor hash functions by adding logic here
63 * - logic taken from java 1.4 hashtable source */
64 h += ~(h << 9);
65 h ^= ((h >> 14) | (h << 18)); /* >>> */
66 h += (h << 4);
67 h ^= ((h >> 10) | (h << 22)); /* >>> */
68 return h;
71 #if 0
72 /** Basic string hash function, from Java standard String.hashCode(). */
73 static INLINE unsigned
74 ht_string_hash(const char *s)
76 unsigned h = 0;
77 int m = 1;
78 while (*s) {
79 h += ((signed char)*s++)*m;
80 m = (m<<5)-1; /* m *= 31 */
82 return h;
84 #endif
86 /** Basic string hash function, from Python's str.__hash__() */
87 static INLINE unsigned
88 ht_string_hash(const char *s)
90 unsigned h;
91 const unsigned char *cp = (const unsigned char *)s;
92 h = *cp << 7;
93 while (*cp) {
94 h = (1000003*h) ^ *cp++;
96 /* This conversion truncates the length of the string, but that's ok. */
97 h ^= (unsigned)(cp-(const unsigned char*)s);
98 return h;
101 #define _HT_SET_HASH(elm, field, hashfn) \
102 (elm)->field.hte_hash = hashfn(elm)
104 #define HT_FOREACH(x, name, head) \
105 for ((x) = HT_START(name, head); \
106 (x) != NULL; \
107 (x) = HT_NEXT(name, head, x))
109 #define HT_PROTOTYPE(name, type, field, hashfn, eqfn) \
110 int name##_HT_GROW(struct name *ht, unsigned min_capacity); \
111 void name##_HT_CLEAR(struct name *ht); \
112 int _##name##_HT_REP_IS_BAD(const struct name *ht); \
113 static INLINE void \
114 name##_HT_INIT(struct name *head) { \
115 head->hth_table_length = 0; \
116 head->hth_table = NULL; \
117 head->hth_n_entries = 0; \
118 head->hth_load_limit = 0; \
119 head->hth_prime_idx = -1; \
121 /* Helper: returns a pointer to the right location in the table \
122 * 'head' to find or insert the element 'elm'. */ \
123 static INLINE struct type ** \
124 _##name##_HT_FIND_P(struct name *head, struct type *elm) \
126 struct type **p; \
127 if (!head->hth_table) \
128 return NULL; \
129 p = &_HT_BUCKET(head, field, elm); \
130 while (*p) { \
131 if (eqfn(*p, elm)) \
132 return p; \
133 p = &(*p)->field.hte_next; \
135 return p; \
137 /* Return a pointer to the element in the table 'head' matching 'elm', \
138 * or NULL if no such element exists */ \
139 static INLINE struct type * \
140 name##_HT_FIND(const struct name *head, struct type *elm) \
142 struct type **p; \
143 struct name *h = (struct name *) head; \
144 _HT_SET_HASH(elm, field, hashfn); \
145 p = _##name##_HT_FIND_P(h, elm); \
146 return p ? *p : NULL; \
148 /* Insert the element 'elm' into the table 'head'. Do not call this \
149 * function if the table might already contain a matching element. */ \
150 static INLINE void \
151 name##_HT_INSERT(struct name *head, struct type *elm) \
153 struct type **p; \
154 if (!head->hth_table || head->hth_n_entries >= head->hth_load_limit) \
155 name##_HT_GROW(head, head->hth_n_entries+1); \
156 ++head->hth_n_entries; \
157 _HT_SET_HASH(elm, field, hashfn); \
158 p = &_HT_BUCKET(head, field, elm); \
159 elm->field.hte_next = *p; \
160 *p = elm; \
162 /* Insert the element 'elm' into the table 'head'. If there already \
163 * a matching element in the table, replace that element and return \
164 * it. */ \
165 static INLINE struct type * \
166 name##_HT_REPLACE(struct name *head, struct type *elm) \
168 struct type **p, *r; \
169 if (!head->hth_table || head->hth_n_entries >= head->hth_load_limit) \
170 name##_HT_GROW(head, head->hth_n_entries+1); \
171 _HT_SET_HASH(elm, field, hashfn); \
172 p = _##name##_HT_FIND_P(head, elm); \
173 r = *p; \
174 *p = elm; \
175 if (r && (r!=elm)) { \
176 elm->field.hte_next = r->field.hte_next; \
177 r->field.hte_next = NULL; \
178 return r; \
179 } else { \
180 ++head->hth_n_entries; \
181 return NULL; \
184 /* Remove any element matching 'elm' from the table 'head'. If such \
185 * an element is found, return it; otherwise return NULL. */ \
186 static INLINE struct type * \
187 name##_HT_REMOVE(struct name *head, struct type *elm) \
189 struct type **p, *r; \
190 _HT_SET_HASH(elm, field, hashfn); \
191 p = _##name##_HT_FIND_P(head,elm); \
192 if (!p || !*p) \
193 return NULL; \
194 r = *p; \
195 *p = r->field.hte_next; \
196 r->field.hte_next = NULL; \
197 --head->hth_n_entries; \
198 return r; \
200 /* Invoke the function 'fn' on every element of the table 'head', \
201 * using 'data' as its second argument. If the function returns \
202 * nonzero, remove the most recently examined element before invoking \
203 * the function again. */ \
204 static INLINE void \
205 name##_HT_FOREACH_FN(struct name *head, \
206 int (*fn)(struct type *, void *), \
207 void *data) \
209 unsigned idx; \
210 int remove; \
211 struct type **p, **nextp, *next; \
212 if (!head->hth_table) \
213 return; \
214 for (idx=0; idx < head->hth_table_length; ++idx) { \
215 p = &head->hth_table[idx]; \
216 while (*p) { \
217 nextp = &(*p)->field.hte_next; \
218 next = *nextp; \
219 remove = fn(*p, data); \
220 if (remove) { \
221 --head->hth_n_entries; \
222 *p = next; \
223 } else { \
224 p = nextp; \
229 /* Return a pointer to the first element in the table 'head', under \
230 * an arbitrary order. This order is stable under remove operations, \
231 * but not under others. If the table is empty, return NULL. */ \
232 static INLINE struct type ** \
233 name##_HT_START(struct name *head) \
235 unsigned b = 0; \
236 while (b < head->hth_table_length) { \
237 if (head->hth_table[b]) \
238 return &head->hth_table[b]; \
239 ++b; \
241 return NULL; \
243 /* Return the next element in 'head' after 'elm', under the arbitrary \
244 * order used by HT_START. If there are no more elements, return \
245 * NULL. If 'elm' is to be removed from the table, you must call \
246 * this function for the next value before you remove it. \
247 */ \
248 static INLINE struct type ** \
249 name##_HT_NEXT(struct name *head, struct type **elm) \
251 if ((*elm)->field.hte_next) { \
252 return &(*elm)->field.hte_next; \
253 } else { \
254 unsigned b = ((*elm)->field.hte_hash % head->hth_table_length)+1; \
255 while (b < head->hth_table_length) { \
256 if (head->hth_table[b]) \
257 return &head->hth_table[b]; \
258 ++b; \
260 return NULL; \
263 static INLINE struct type ** \
264 name##_HT_NEXT_RMV(struct name *head, struct type **elm) \
266 unsigned h = (*elm)->field.hte_hash; \
267 *elm = (*elm)->field.hte_next; \
268 --head->hth_n_entries; \
269 if (*elm) { \
270 return elm; \
271 } else { \
272 unsigned b = (h % head->hth_table_length)+1; \
273 while (b < head->hth_table_length) { \
274 if (head->hth_table[b]) \
275 return &head->hth_table[b]; \
276 ++b; \
278 return NULL; \
282 #define HT_GENERATE(name, type, field, hashfn, eqfn, load, mallocfn, \
283 reallocfn, freefn) \
284 static unsigned name##_PRIMES[] = { \
285 53, 97, 193, 389, \
286 769, 1543, 3079, 6151, \
287 12289, 24593, 49157, 98317, \
288 196613, 393241, 786433, 1572869, \
289 3145739, 6291469, 12582917, 25165843, \
290 50331653, 100663319, 201326611, 402653189, \
291 805306457, 1610612741 \
292 }; \
293 static unsigned name##_N_PRIMES = \
294 (unsigned)(sizeof(name##_PRIMES)/sizeof(name##_PRIMES[0])); \
295 /* Expand the internal table of 'head' until it is large enough to \
296 * hold 'size' elements. Return 0 on success, -1 on allocation \
297 * failure. */ \
298 int \
299 name##_HT_GROW(struct name *head, unsigned size) \
301 unsigned new_len, new_load_limit; \
302 int prime_idx; \
303 struct type **new_table; \
304 if (head->hth_prime_idx == (int)name##_N_PRIMES - 1) \
305 return 0; \
306 if (head->hth_load_limit > size) \
307 return 0; \
308 prime_idx = head->hth_prime_idx; \
309 do { \
310 new_len = name##_PRIMES[++prime_idx]; \
311 new_load_limit = (unsigned)(load*new_len); \
312 } while (new_load_limit <= size && \
313 prime_idx < (int)name##_N_PRIMES); \
314 if ((new_table = mallocfn(new_len*sizeof(struct type*)))) { \
315 unsigned b; \
316 memset(new_table, 0, new_len*sizeof(struct type*)); \
317 for (b = 0; b < head->hth_table_length; ++b) { \
318 struct type *elm, *next; \
319 unsigned b2; \
320 elm = head->hth_table[b]; \
321 while (elm) { \
322 next = elm->field.hte_next; \
323 b2 = elm->field.hte_hash % new_len; \
324 elm->field.hte_next = new_table[b2]; \
325 new_table[b2] = elm; \
326 elm = next; \
329 if (head->hth_table) \
330 freefn(head->hth_table); \
331 head->hth_table = new_table; \
332 } else { \
333 unsigned b, b2; \
334 new_table = reallocfn(head->hth_table, new_len*sizeof(struct type*)); \
335 if (!new_table) return -1; \
336 memset(new_table + head->hth_table_length, 0, \
337 (new_len - head->hth_table_length)*sizeof(struct type*)); \
338 for (b=0; b < head->hth_table_length; ++b) { \
339 struct type *e, **pE; \
340 for (pE = &new_table[b], e = *pE; e != NULL; e = *pE) { \
341 b2 = e->field.hte_hash % new_len; \
342 if (b2 == b) { \
343 pE = &e->field.hte_next; \
344 } else { \
345 *pE = e->field.hte_next; \
346 e->field.hte_next = new_table[b2]; \
347 new_table[b2] = e; \
351 head->hth_table = new_table; \
353 head->hth_table_length = new_len; \
354 head->hth_prime_idx = prime_idx; \
355 head->hth_load_limit = new_load_limit; \
356 return 0; \
358 /* Free all storage held by 'head'. Does not free 'head' itself, or \
359 * individual elements. */ \
360 void \
361 name##_HT_CLEAR(struct name *head) \
363 if (head->hth_table) \
364 freefn(head->hth_table); \
365 head->hth_table_length = 0; \
366 name##_HT_INIT(head); \
368 /* Debugging helper: return false iff the representation of 'head' is \
369 * internally consistent. */ \
370 int \
371 _##name##_HT_REP_IS_BAD(const struct name *head) \
373 unsigned n, i; \
374 struct type *elm; \
375 if (!head->hth_table_length) { \
376 if (!head->hth_table && !head->hth_n_entries && \
377 !head->hth_load_limit && head->hth_prime_idx == -1) \
378 return 0; \
379 else \
380 return 1; \
382 if (!head->hth_table || head->hth_prime_idx < 0 || \
383 !head->hth_load_limit) \
384 return 2; \
385 if (head->hth_n_entries > head->hth_load_limit) \
386 return 3; \
387 if (head->hth_table_length != name##_PRIMES[head->hth_prime_idx]) \
388 return 4; \
389 if (head->hth_load_limit != (unsigned)(load*head->hth_table_length)) \
390 return 5; \
391 for (n = i = 0; i < head->hth_table_length; ++i) { \
392 for (elm = head->hth_table[i]; elm; elm = elm->field.hte_next) { \
393 if (elm->field.hte_hash != hashfn(elm)) \
394 return 1000 + i; \
395 if ((elm->field.hte_hash % head->hth_table_length) != i) \
396 return 10000 + i; \
397 ++n; \
400 if (n != head->hth_n_entries) \
401 return 6; \
402 return 0; \
405 /** Implements an over-optimized "find and insert if absent" block;
406 * not meant for direct usage by typical code, or usage outside the critical
407 * path.*/
408 #define _HT_FIND_OR_INSERT(name, field, hashfn, head, eltype, elm, var, y, n) \
410 struct name *_##var##_head = head; \
411 eltype **var; \
412 if (!_##var##_head->hth_table || \
413 _##var##_head->hth_n_entries >= _##var##_head->hth_load_limit) \
414 name##_HT_GROW(_##var##_head, _##var##_head->hth_n_entries+1); \
415 _HT_SET_HASH((elm), field, hashfn); \
416 var = _##name##_HT_FIND_P(_##var##_head, (elm)); \
417 if (*var) { \
418 y; \
419 } else { \
420 n; \
423 #define _HT_FOI_INSERT(field, head, elm, newent, var) \
425 newent->field.hte_hash = (elm)->field.hte_hash; \
426 newent->field.hte_next = NULL; \
427 *var = newent; \
428 ++((head)->hth_n_entries); \
432 * Copyright 2005, Nick Mathewson. Implementation logic is adapted from code
433 * by Christopher Clark, retrofit to allow drop-in memory management, and to
434 * use the same interface as Niels Provos's HT_H. I'm not sure whether this
435 * is a derived work any more, but whether it is or not, the license below
436 * applies.
438 * Copyright (c) 2002, Christopher Clark
439 * All rights reserved.
441 * Redistribution and use in source and binary forms, with or without
442 * modification, are permitted provided that the following conditions
443 * are met:
445 * * Redistributions of source code must retain the above copyright
446 * notice, this list of conditions and the following disclaimer.
448 * * Redistributions in binary form must reproduce the above copyright
449 * notice, this list of conditions and the following disclaimer in the
450 * documentation and/or other materials provided with the distribution.
452 * * Neither the name of the original author; nor the names of any contributors
453 * may be used to endorse or promote products derived from this software
454 * without specific prior written permission.
457 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
458 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
459 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
460 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
461 * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
462 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
463 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
464 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
465 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
466 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
467 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
470 #endif