Use a simpler fix for the byte-reversing warning
[tor/rransom.git] / src / common / ht.h
blobb31492ec3c17c7c91e301a714e657a7a9837a868
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 #define HT_FIND(name, head, elm) name##_HT_FIND((head), (elm))
46 #define HT_INSERT(name, head, elm) name##_HT_INSERT((head), (elm))
47 #define HT_REPLACE(name, head, elm) name##_HT_REPLACE((head), (elm))
48 #define HT_REMOVE(name, head, elm) name##_HT_REMOVE((head), (elm))
49 #define HT_START(name, head) name##_HT_START(head)
50 #define HT_NEXT(name, head, elm) name##_HT_NEXT((head), (elm))
51 #define HT_NEXT_RMV(name, head, elm) name##_HT_NEXT_RMV((head), (elm))
52 #define HT_CLEAR(name, head) name##_HT_CLEAR(head)
53 #define HT_INIT(name, head) name##_HT_INIT(head)
54 /* Helper: */
55 static INLINE unsigned
56 ht_improve_hash(unsigned h)
58 /* Aim to protect against poor hash functions by adding logic here
59 * - logic taken from java 1.4 hashtable source */
60 h += ~(h << 9);
61 h ^= ((h >> 14) | (h << 18)); /* >>> */
62 h += (h << 4);
63 h ^= ((h >> 10) | (h << 22)); /* >>> */
64 return h;
67 #if 0
68 /** Basic string hash function, from Java standard String.hashCode(). */
69 static INLINE unsigned
70 ht_string_hash(const char *s)
72 unsigned h = 0;
73 int m = 1;
74 while (*s) {
75 h += ((signed char)*s++)*m;
76 m = (m<<5)-1; /* m *= 31 */
78 return h;
80 #endif
82 /** Basic string hash function, from Python's str.__hash__() */
83 static INLINE unsigned
84 ht_string_hash(const char *s)
86 unsigned h;
87 const unsigned char *cp = (const unsigned char *)s;
88 h = *cp << 7;
89 while (*cp) {
90 h = (1000003*h) ^ *cp++;
92 /* This conversion truncates the length of the string, but that's ok. */
93 h ^= (unsigned)(cp-(const unsigned char*)s);
94 return h;
97 #define _HT_SET_HASH(elm, field, hashfn) \
98 (elm)->field.hte_hash = hashfn(elm)
100 #define HT_FOREACH(x, name, head) \
101 for ((x) = HT_START(name, head); \
102 (x) != NULL; \
103 (x) = HT_NEXT(name, head, x))
105 #define HT_PROTOTYPE(name, type, field, hashfn, eqfn) \
106 int name##_HT_GROW(struct name *ht, unsigned min_capacity); \
107 void name##_HT_CLEAR(struct name *ht); \
108 int _##name##_HT_REP_IS_BAD(const struct name *ht); \
109 static INLINE void \
110 name##_HT_INIT(struct name *head) { \
111 head->hth_table_length = 0; \
112 head->hth_table = NULL; \
113 head->hth_n_entries = 0; \
114 head->hth_load_limit = 0; \
115 head->hth_prime_idx = -1; \
117 /* Helper: returns a pointer to the right location in the table \
118 * 'head' to find or insert the element 'elm'. */ \
119 static INLINE struct type ** \
120 _##name##_HT_FIND_P(struct name *head, struct type *elm) \
122 struct type **p; \
123 if (!head->hth_table) \
124 return NULL; \
125 p = &_HT_BUCKET(head, field, elm); \
126 while (*p) { \
127 if (eqfn(*p, elm)) \
128 return p; \
129 p = &(*p)->field.hte_next; \
131 return p; \
133 /* Return a pointer to the element in the table 'head' matching 'elm', \
134 * or NULL if no such element exists */ \
135 static INLINE struct type * \
136 name##_HT_FIND(const struct name *head, struct type *elm) \
138 struct type **p; \
139 struct name *h = (struct name *) head; \
140 _HT_SET_HASH(elm, field, hashfn); \
141 p = _##name##_HT_FIND_P(h, elm); \
142 return p ? *p : NULL; \
144 /* Insert the element 'elm' into the table 'head'. Do not call this \
145 * function if the table might already contain a matching element. */ \
146 static INLINE void \
147 name##_HT_INSERT(struct name *head, struct type *elm) \
149 struct type **p; \
150 if (!head->hth_table || head->hth_n_entries >= head->hth_load_limit) \
151 name##_HT_GROW(head, head->hth_n_entries+1); \
152 ++head->hth_n_entries; \
153 _HT_SET_HASH(elm, field, hashfn); \
154 p = &_HT_BUCKET(head, field, elm); \
155 elm->field.hte_next = *p; \
156 *p = elm; \
158 /* Insert the element 'elm' into the table 'head'. If there already \
159 * a matching element in the table, replace that element and return \
160 * it. */ \
161 static INLINE struct type * \
162 name##_HT_REPLACE(struct name *head, struct type *elm) \
164 struct type **p, *r; \
165 if (!head->hth_table || head->hth_n_entries >= head->hth_load_limit) \
166 name##_HT_GROW(head, head->hth_n_entries+1); \
167 _HT_SET_HASH(elm, field, hashfn); \
168 p = _##name##_HT_FIND_P(head, elm); \
169 r = *p; \
170 *p = elm; \
171 if (r && (r!=elm)) { \
172 elm->field.hte_next = r->field.hte_next; \
173 r->field.hte_next = NULL; \
174 return r; \
175 } else { \
176 ++head->hth_n_entries; \
177 return NULL; \
180 /* Remove any element matching 'elm' from the table 'head'. If such \
181 * an element is found, return it; otherwise return NULL. */ \
182 static INLINE struct type * \
183 name##_HT_REMOVE(struct name *head, struct type *elm) \
185 struct type **p, *r; \
186 _HT_SET_HASH(elm, field, hashfn); \
187 p = _##name##_HT_FIND_P(head,elm); \
188 if (!p || !*p) \
189 return NULL; \
190 r = *p; \
191 *p = r->field.hte_next; \
192 r->field.hte_next = NULL; \
193 --head->hth_n_entries; \
194 return r; \
196 /* Invoke the function 'fn' on every element of the table 'head', \
197 * using 'data' as its second argument. If the function returns \
198 * nonzero, remove the most recently examined element before invoking \
199 * the function again. */ \
200 static INLINE void \
201 name##_HT_FOREACH_FN(struct name *head, \
202 int (*fn)(struct type *, void *), \
203 void *data) \
205 unsigned idx; \
206 int remove; \
207 struct type **p, **nextp, *next; \
208 if (!head->hth_table) \
209 return; \
210 for (idx=0; idx < head->hth_table_length; ++idx) { \
211 p = &head->hth_table[idx]; \
212 while (*p) { \
213 nextp = &(*p)->field.hte_next; \
214 next = *nextp; \
215 remove = fn(*p, data); \
216 if (remove) { \
217 --head->hth_n_entries; \
218 *p = next; \
219 } else { \
220 p = nextp; \
225 /* Return a pointer to the first element in the table 'head', under \
226 * an arbitrary order. This order is stable under remove operations, \
227 * but not under others. If the table is empty, return NULL. */ \
228 static INLINE struct type ** \
229 name##_HT_START(struct name *head) \
231 unsigned b = 0; \
232 while (b < head->hth_table_length) { \
233 if (head->hth_table[b]) \
234 return &head->hth_table[b]; \
235 ++b; \
237 return NULL; \
239 /* Return the next element in 'head' after 'elm', under the arbitrary \
240 * order used by HT_START. If there are no more elements, return \
241 * NULL. If 'elm' is to be removed from the table, you must call \
242 * this function for the next value before you remove it. \
243 */ \
244 static INLINE struct type ** \
245 name##_HT_NEXT(struct name *head, struct type **elm) \
247 if ((*elm)->field.hte_next) { \
248 return &(*elm)->field.hte_next; \
249 } else { \
250 unsigned b = ((*elm)->field.hte_hash % head->hth_table_length)+1; \
251 while (b < head->hth_table_length) { \
252 if (head->hth_table[b]) \
253 return &head->hth_table[b]; \
254 ++b; \
256 return NULL; \
259 static INLINE struct type ** \
260 name##_HT_NEXT_RMV(struct name *head, struct type **elm) \
262 unsigned h = (*elm)->field.hte_hash; \
263 *elm = (*elm)->field.hte_next; \
264 --head->hth_n_entries; \
265 if (*elm) { \
266 return elm; \
267 } else { \
268 unsigned b = (h % head->hth_table_length)+1; \
269 while (b < head->hth_table_length) { \
270 if (head->hth_table[b]) \
271 return &head->hth_table[b]; \
272 ++b; \
274 return NULL; \
278 #define HT_GENERATE(name, type, field, hashfn, eqfn, load, mallocfn, \
279 reallocfn, freefn) \
280 static unsigned name##_PRIMES[] = { \
281 53, 97, 193, 389, \
282 769, 1543, 3079, 6151, \
283 12289, 24593, 49157, 98317, \
284 196613, 393241, 786433, 1572869, \
285 3145739, 6291469, 12582917, 25165843, \
286 50331653, 100663319, 201326611, 402653189, \
287 805306457, 1610612741 \
288 }; \
289 static unsigned name##_N_PRIMES = \
290 (unsigned)(sizeof(name##_PRIMES)/sizeof(name##_PRIMES[0])); \
291 /* Expand the internal table of 'head' until it is large enough to \
292 * hold 'size' elements. Return 0 on success, -1 on allocation \
293 * failure. */ \
294 int \
295 name##_HT_GROW(struct name *head, unsigned size) \
297 unsigned new_len, new_load_limit; \
298 int prime_idx; \
299 struct type **new_table; \
300 if (head->hth_prime_idx == (int)name##_N_PRIMES - 1) \
301 return 0; \
302 if (head->hth_load_limit > size) \
303 return 0; \
304 prime_idx = head->hth_prime_idx; \
305 do { \
306 new_len = name##_PRIMES[++prime_idx]; \
307 new_load_limit = (unsigned)(load*new_len); \
308 } while (new_load_limit <= size && \
309 prime_idx < (int)name##_N_PRIMES); \
310 if ((new_table = mallocfn(new_len*sizeof(struct type*)))) { \
311 unsigned b; \
312 memset(new_table, 0, new_len*sizeof(struct type*)); \
313 for (b = 0; b < head->hth_table_length; ++b) { \
314 struct type *elm, *next; \
315 unsigned b2; \
316 elm = head->hth_table[b]; \
317 while (elm) { \
318 next = elm->field.hte_next; \
319 b2 = elm->field.hte_hash % new_len; \
320 elm->field.hte_next = new_table[b2]; \
321 new_table[b2] = elm; \
322 elm = next; \
325 if (head->hth_table) \
326 freefn(head->hth_table); \
327 head->hth_table = new_table; \
328 } else { \
329 unsigned b, b2; \
330 new_table = reallocfn(head->hth_table, new_len*sizeof(struct type*)); \
331 if (!new_table) return -1; \
332 memset(new_table + head->hth_table_length, 0, \
333 (new_len - head->hth_table_length)*sizeof(struct type*)); \
334 for (b=0; b < head->hth_table_length; ++b) { \
335 struct type *e, **pE; \
336 for (pE = &new_table[b], e = *pE; e != NULL; e = *pE) { \
337 b2 = e->field.hte_hash % new_len; \
338 if (b2 == b) { \
339 pE = &e->field.hte_next; \
340 } else { \
341 *pE = e->field.hte_next; \
342 e->field.hte_next = new_table[b2]; \
343 new_table[b2] = e; \
347 head->hth_table = new_table; \
349 head->hth_table_length = new_len; \
350 head->hth_prime_idx = prime_idx; \
351 head->hth_load_limit = new_load_limit; \
352 return 0; \
354 /* Free all storage held by 'head'. Does not free 'head' itself, or \
355 * individual elements. */ \
356 void \
357 name##_HT_CLEAR(struct name *head) \
359 if (head->hth_table) \
360 freefn(head->hth_table); \
361 head->hth_table_length = 0; \
362 name##_HT_INIT(head); \
364 /* Debugging helper: return false iff the representation of 'head' is \
365 * internally consistent. */ \
366 int \
367 _##name##_HT_REP_IS_BAD(const struct name *head) \
369 unsigned n, i; \
370 struct type *elm; \
371 if (!head->hth_table_length) { \
372 if (!head->hth_table && !head->hth_n_entries && \
373 !head->hth_load_limit && head->hth_prime_idx == -1) \
374 return 0; \
375 else \
376 return 1; \
378 if (!head->hth_table || head->hth_prime_idx < 0 || \
379 !head->hth_load_limit) \
380 return 2; \
381 if (head->hth_n_entries > head->hth_load_limit) \
382 return 3; \
383 if (head->hth_table_length != name##_PRIMES[head->hth_prime_idx]) \
384 return 4; \
385 if (head->hth_load_limit != (unsigned)(load*head->hth_table_length)) \
386 return 5; \
387 for (n = i = 0; i < head->hth_table_length; ++i) { \
388 for (elm = head->hth_table[i]; elm; elm = elm->field.hte_next) { \
389 if (elm->field.hte_hash != hashfn(elm)) \
390 return 1000 + i; \
391 if ((elm->field.hte_hash % head->hth_table_length) != i) \
392 return 10000 + i; \
393 ++n; \
396 if (n != head->hth_n_entries) \
397 return 6; \
398 return 0; \
401 /** Implements an over-optimized "find and insert if absent" block;
402 * not meant for direct usage by typical code, or usage outside the critical
403 * path.*/
404 #define _HT_FIND_OR_INSERT(name, field, hashfn, head, eltype, elm, var, y, n) \
406 struct name *_##var##_head = head; \
407 eltype **var; \
408 if (!_##var##_head->hth_table || \
409 _##var##_head->hth_n_entries >= _##var##_head->hth_load_limit) \
410 name##_HT_GROW(_##var##_head, _##var##_head->hth_n_entries+1); \
411 _HT_SET_HASH((elm), field, hashfn); \
412 var = _##name##_HT_FIND_P(_##var##_head, (elm)); \
413 if (*var) { \
414 y; \
415 } else { \
416 n; \
419 #define _HT_FOI_INSERT(field, head, elm, newent, var) \
421 newent->field.hte_hash = (elm)->field.hte_hash; \
422 newent->field.hte_next = NULL; \
423 *var = newent; \
424 ++((head)->hth_n_entries); \
428 * Copyright 2005, Nick Mathewson. Implementation logic is adapted from code
429 * by Christopher Clark, retrofit to allow drop-in memory management, and to
430 * use the same interface as Niels Provos's HT_H. I'm not sure whether this
431 * is a derived work any more, but whether it is or not, the license below
432 * applies.
434 * Copyright (c) 2002, Christopher Clark
435 * All rights reserved.
437 * Redistribution and use in source and binary forms, with or without
438 * modification, are permitted provided that the following conditions
439 * are met:
441 * * Redistributions of source code must retain the above copyright
442 * notice, this list of conditions and the following disclaimer.
444 * * Redistributions in binary form must reproduce the above copyright
445 * notice, this list of conditions and the following disclaimer in the
446 * documentation and/or other materials provided with the distribution.
448 * * Neither the name of the original author; nor the names of any contributors
449 * may be used to endorse or promote products derived from this software
450 * without specific prior written permission.
453 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
454 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
455 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
456 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
457 * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
458 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
459 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
460 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
461 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
462 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
463 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
466 #endif