1 /* Based on work Copyright 2002 Christopher Clark */
2 /* Copyright 2005-2012 Nick Mathewson */
3 /* Copyright 2009-2012 Niels Provos and Nick Mathewson */
4 /* See license at end. */
6 /* Based on ideas by Christopher Clark and interfaces from Niels Provos. */
11 #define HT_HEAD(name, type) \
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. */ \
25 #define HT_INITIALIZER() \
28 #ifdef HT_CACHE_HASH_VALUES
29 #define HT_ENTRY(type) \
31 struct type *hte_next; \
35 #define HT_ENTRY(type) \
37 struct type *hte_next; \
41 #define HT_EMPTY(head) \
42 ((head)->hth_n_entries == 0)
44 /* How many elements in 'head'? */
45 #define HT_SIZE(head) \
46 ((head)->hth_n_entries)
48 #define HT_FIND(name, head, elm) name##_HT_FIND((head), (elm))
49 #define HT_INSERT(name, head, elm) name##_HT_INSERT((head), (elm))
50 #define HT_REPLACE(name, head, elm) name##_HT_REPLACE((head), (elm))
51 #define HT_REMOVE(name, head, elm) name##_HT_REMOVE((head), (elm))
52 #define HT_START(name, head) name##_HT_START(head)
53 #define HT_NEXT(name, head, elm) name##_HT_NEXT((head), (elm))
54 #define HT_NEXT_RMV(name, head, elm) name##_HT_NEXT_RMV((head), (elm))
55 #define HT_CLEAR(name, head) name##_HT_CLEAR(head)
56 #define HT_INIT(name, head) name##_HT_INIT(head)
58 static inline unsigned
59 ht_improve_hash(unsigned h
)
61 /* Aim to protect against poor hash functions by adding logic here
62 * - logic taken from java 1.4 hashtable source */
64 h
^= ((h
>> 14) | (h
<< 18)); /* >>> */
66 h
^= ((h
>> 10) | (h
<< 22)); /* >>> */
71 /** Basic string hash function, from Java standard String.hashCode(). */
72 static inline unsigned
73 ht_string_hash(const char *s
)
78 h
+= ((signed char)*s
++)*m
;
79 m
= (m
<<5)-1; /* m *= 31 */
85 /** Basic string hash function, from Python's str.__hash__() */
86 static inline unsigned
87 ht_string_hash(const char *s
)
90 const unsigned char *cp
= (const unsigned char *)s
;
93 h
= (1000003*h
) ^ *cp
++;
95 /* This conversion truncates the length of the string, but that's ok. */
96 h
^= (unsigned)(cp
-(const unsigned char*)s
);
100 #ifdef HT_CACHE_HASH_VALUES
101 #define _HT_SET_HASH(elm, field, hashfn) \
102 do { (elm)->field.hte_hash = hashfn(elm); } while (0)
103 #define _HT_SET_HASHVAL(elm, field, val) \
104 do { (elm)->field.hte_hash = (val); } while (0)
105 #define _HT_ELT_HASH(elm, field, hashfn) \
106 ((elm)->field.hte_hash)
108 #define _HT_SET_HASH(elm, field, hashfn) \
110 #define _HT_ELT_HASH(elm, field, hashfn) \
112 #define _HT_SET_HASHVAL(elm, field, val) \
116 /* Helper: alias for the bucket containing 'elm'. */
117 #define _HT_BUCKET(head, field, elm, hashfn) \
118 ((head)->hth_table[_HT_ELT_HASH(elm,field,hashfn) % head->hth_table_length])
120 #define HT_FOREACH(x, name, head) \
121 for ((x) = HT_START(name, head); \
123 (x) = HT_NEXT(name, head, x))
125 #define HT_PROTOTYPE(name, type, field, hashfn, eqfn) \
126 int name##_HT_GROW(struct name *ht, unsigned min_capacity); \
127 void name##_HT_CLEAR(struct name *ht); \
128 int _##name##_HT_REP_IS_BAD(const struct name *ht); \
130 name##_HT_INIT(struct name *head) { \
131 head->hth_table_length = 0; \
132 head->hth_table = NULL; \
133 head->hth_n_entries = 0; \
134 head->hth_load_limit = 0; \
135 head->hth_prime_idx = -1; \
137 /* Helper: returns a pointer to the right location in the table \
138 * 'head' to find or insert the element 'elm'. */ \
139 static inline struct type ** \
140 _##name##_HT_FIND_P(struct name *head, struct type *elm) \
143 if (!head->hth_table) \
145 p = &_HT_BUCKET(head, field, elm, hashfn); \
149 p = &(*p)->field.hte_next; \
153 /* Return a pointer to the element in the table 'head' matching 'elm', \
154 * or NULL if no such element exists */ \
155 static inline struct type * \
156 name##_HT_FIND(const struct name *head, struct type *elm) \
159 struct name *h = (struct name *) head; \
160 _HT_SET_HASH(elm, field, hashfn); \
161 p = _##name##_HT_FIND_P(h, elm); \
162 return p ? *p : NULL; \
164 /* Insert the element 'elm' into the table 'head'. Do not call this \
165 * function if the table might already contain a matching element. */ \
167 name##_HT_INSERT(struct name *head, struct type *elm) \
170 if (!head->hth_table || head->hth_n_entries >= head->hth_load_limit) \
171 name##_HT_GROW(head, head->hth_n_entries+1); \
172 ++head->hth_n_entries; \
173 _HT_SET_HASH(elm, field, hashfn); \
174 p = &_HT_BUCKET(head, field, elm, hashfn); \
175 elm->field.hte_next = *p; \
178 /* Insert the element 'elm' into the table 'head'. If there already \
179 * a matching element in the table, replace that element and return \
181 static inline struct type * \
182 name##_HT_REPLACE(struct name *head, struct type *elm) \
184 struct type **p, *r; \
185 if (!head->hth_table || head->hth_n_entries >= head->hth_load_limit) \
186 name##_HT_GROW(head, head->hth_n_entries+1); \
187 _HT_SET_HASH(elm, field, hashfn); \
188 p = _##name##_HT_FIND_P(head, elm); \
191 if (r && (r!=elm)) { \
192 elm->field.hte_next = r->field.hte_next; \
193 r->field.hte_next = NULL; \
196 ++head->hth_n_entries; \
200 /* Remove any element matching 'elm' from the table 'head'. If such \
201 * an element is found, return it; otherwise return NULL. */ \
202 static inline struct type * \
203 name##_HT_REMOVE(struct name *head, struct type *elm) \
205 struct type **p, *r; \
206 _HT_SET_HASH(elm, field, hashfn); \
207 p = _##name##_HT_FIND_P(head,elm); \
211 *p = r->field.hte_next; \
212 r->field.hte_next = NULL; \
213 --head->hth_n_entries; \
216 /* Invoke the function 'fn' on every element of the table 'head', \
217 * using 'data' as its second argument. If the function returns \
218 * nonzero, remove the most recently examined element before invoking \
219 * the function again. */ \
221 name##_HT_FOREACH_FN(struct name *head, \
222 int (*fn)(struct type *, void *), \
226 struct type **p, **nextp, *next; \
227 if (!head->hth_table) \
229 for (idx=0; idx < head->hth_table_length; ++idx) { \
230 p = &head->hth_table[idx]; \
232 nextp = &(*p)->field.hte_next; \
234 if (fn(*p, data)) { \
235 --head->hth_n_entries; \
243 /* Return a pointer to the first element in the table 'head', under \
244 * an arbitrary order. This order is stable under remove operations, \
245 * but not under others. If the table is empty, return NULL. */ \
246 static inline struct type ** \
247 name##_HT_START(struct name *head) \
250 while (b < head->hth_table_length) { \
251 if (head->hth_table[b]) \
252 return &head->hth_table[b]; \
257 /* Return the next element in 'head' after 'elm', under the arbitrary \
258 * order used by HT_START. If there are no more elements, return \
259 * NULL. If 'elm' is to be removed from the table, you must call \
260 * this function for the next value before you remove it. \
262 static inline struct type ** \
263 name##_HT_NEXT(struct name *head, struct type **elm) \
265 if ((*elm)->field.hte_next) { \
266 return &(*elm)->field.hte_next; \
268 unsigned b = (_HT_ELT_HASH(*elm, field, hashfn) % head->hth_table_length)+1; \
269 while (b < head->hth_table_length) { \
270 if (head->hth_table[b]) \
271 return &head->hth_table[b]; \
277 static inline struct type ** \
278 name##_HT_NEXT_RMV(struct name *head, struct type **elm) \
280 unsigned h = _HT_ELT_HASH(*elm, field, hashfn); \
281 *elm = (*elm)->field.hte_next; \
282 --head->hth_n_entries; \
286 unsigned b = (h % head->hth_table_length)+1; \
287 while (b < head->hth_table_length) { \
288 if (head->hth_table[b]) \
289 return &head->hth_table[b]; \
296 #define HT_GENERATE(name, type, field, hashfn, eqfn, load, mallocfn, \
298 static unsigned name##_PRIMES[] = { \
300 769, 1543, 3079, 6151, \
301 12289, 24593, 49157, 98317, \
302 196613, 393241, 786433, 1572869, \
303 3145739, 6291469, 12582917, 25165843, \
304 50331653, 100663319, 201326611, 402653189, \
305 805306457, 1610612741 \
307 static unsigned name##_N_PRIMES = \
308 (unsigned)(sizeof(name##_PRIMES)/sizeof(name##_PRIMES[0])); \
309 /* Expand the internal table of 'head' until it is large enough to \
310 * hold 'size' elements. Return 0 on success, -1 on allocation \
313 name##_HT_GROW(struct name *head, unsigned size) \
315 unsigned new_len, new_load_limit; \
317 struct type **new_table; \
318 if (head->hth_prime_idx == (int)name##_N_PRIMES - 1) \
320 if (head->hth_load_limit > size) \
322 prime_idx = head->hth_prime_idx; \
324 new_len = name##_PRIMES[++prime_idx]; \
325 new_load_limit = (unsigned)(load*new_len); \
326 } while (new_load_limit <= size && \
327 prime_idx < (int)name##_N_PRIMES); \
328 if ((new_table = mallocfn(new_len*sizeof(struct type*)))) { \
330 memset(new_table, 0, new_len*sizeof(struct type*)); \
331 for (b = 0; b < head->hth_table_length; ++b) { \
332 struct type *elm, *next; \
334 elm = head->hth_table[b]; \
336 next = elm->field.hte_next; \
337 b2 = _HT_ELT_HASH(elm, field, hashfn) % new_len; \
338 elm->field.hte_next = new_table[b2]; \
339 new_table[b2] = elm; \
343 if (head->hth_table) \
344 freefn(head->hth_table); \
345 head->hth_table = new_table; \
348 new_table = reallocfn(head->hth_table, new_len*sizeof(struct type*)); \
349 if (!new_table) return -1; \
350 memset(new_table + head->hth_table_length, 0, \
351 (new_len - head->hth_table_length)*sizeof(struct type*)); \
352 for (b=0; b < head->hth_table_length; ++b) { \
353 struct type *e, **pE; \
354 for (pE = &new_table[b], e = *pE; e != NULL; e = *pE) { \
355 b2 = _HT_ELT_HASH(e, field, hashfn) % new_len; \
357 pE = &e->field.hte_next; \
359 *pE = e->field.hte_next; \
360 e->field.hte_next = new_table[b2]; \
365 head->hth_table = new_table; \
367 head->hth_table_length = new_len; \
368 head->hth_prime_idx = prime_idx; \
369 head->hth_load_limit = new_load_limit; \
372 /* Free all storage held by 'head'. Does not free 'head' itself, or \
373 * individual elements. */ \
375 name##_HT_CLEAR(struct name *head) \
377 if (head->hth_table) \
378 freefn(head->hth_table); \
379 head->hth_table_length = 0; \
380 name##_HT_INIT(head); \
382 /* Debugging helper: return false iff the representation of 'head' is \
383 * internally consistent. */ \
385 _##name##_HT_REP_IS_BAD(const struct name *head) \
389 if (!head->hth_table_length) { \
390 if (!head->hth_table && !head->hth_n_entries && \
391 !head->hth_load_limit && head->hth_prime_idx == -1) \
396 if (!head->hth_table || head->hth_prime_idx < 0 || \
397 !head->hth_load_limit) \
399 if (head->hth_n_entries > head->hth_load_limit) \
401 if (head->hth_table_length != name##_PRIMES[head->hth_prime_idx]) \
403 if (head->hth_load_limit != (unsigned)(load*head->hth_table_length)) \
405 for (n = i = 0; i < head->hth_table_length; ++i) { \
406 for (elm = head->hth_table[i]; elm; elm = elm->field.hte_next) { \
407 if (_HT_ELT_HASH(elm, field, hashfn) != hashfn(elm)) \
409 if ((_HT_ELT_HASH(elm, field, hashfn) % head->hth_table_length) != i) \
414 if (n != head->hth_n_entries) \
419 /** Implements an over-optimized "find and insert if absent" block;
420 * not meant for direct usage by typical code, or usage outside the critical
422 #define _HT_FIND_OR_INSERT(name, field, hashfn, head, eltype, elm, var, y, n) \
424 struct name *_##var##_head = head; \
425 struct eltype **var; \
426 if (!_##var##_head->hth_table || \
427 _##var##_head->hth_n_entries >= _##var##_head->hth_load_limit) \
428 name##_HT_GROW(_##var##_head, _##var##_head->hth_n_entries+1); \
429 _HT_SET_HASH((elm), field, hashfn); \
430 var = _##name##_HT_FIND_P(_##var##_head, (elm)); \
437 #define _HT_FOI_INSERT(field, head, elm, newent, var) \
439 _HT_SET_HASHVAL(newent, field, (elm)->field.hte_hash); \
440 newent->field.hte_next = NULL; \
442 ++((head)->hth_n_entries); \
446 * Copyright 2005, Nick Mathewson. Implementation logic is adapted from code
447 * by Cristopher Clark, retrofit to allow drop-in memory management, and to
448 * use the same interface as Niels Provos's tree.h. This is probably still
449 * a derived work, so the original license below still applies.
451 * Copyright (c) 2002, Christopher Clark
452 * All rights reserved.
454 * Redistribution and use in source and binary forms, with or without
455 * modification, are permitted provided that the following conditions
458 * * Redistributions of source code must retain the above copyright
459 * notice, this list of conditions and the following disclaimer.
461 * * Redistributions in binary form must reproduce the above copyright
462 * notice, this list of conditions and the following disclaimer in the
463 * documentation and/or other materials provided with the distribution.
465 * * Neither the name of the original author; nor the names of any contributors
466 * may be used to endorse or promote products derived from this software
467 * without specific prior written permission.
470 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
471 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
472 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
473 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
474 * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
475 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
476 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
477 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
478 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
479 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
480 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.