All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
uthash.h
1 // Copyright eeGeo Ltd (2012-2014), All Rights Reserved
2 
3 #ifndef UTHASH_H
4 #define UTHASH_H
5 
6 #include <string.h> /* memcmp,strlen */
7 #include <stddef.h> /* ptrdiff_t */
8 
9 /* These macros use decltype or the earlier __typeof GNU extension.
10  As decltype is only available in newer compilers (VS2010 or gcc 4.3+
11  when compiling c++ source) this code uses whatever method is needed
12  or, for VS2008 where neither is available, uses casting workarounds. */
13 #ifdef _MSC_VER /* MS compiler */
14 #if _MSC_VER >= 1600 && __cplusplus /* VS2010 or newer in C++ mode */
15 #define DECLTYPE(x) (decltype(x))
16 #else /* VS2008 or older (or VS2010 in C mode) */
17 #define NO_DECLTYPE
18 #define DECLTYPE(x)
19 #endif
20 #else /* GNU, Sun and other compilers */
21 #define DECLTYPE(x) (__typeof(x))
22 #endif
23 
24 #ifdef NO_DECLTYPE
25 #define DECLTYPE_ASSIGN(dst,src) \
26 do { \
27  char **_da_dst = (char**)(&(dst)); \
28  *_da_dst = (char*)(src); \
29 } while(0)
30 #else
31 #define DECLTYPE_ASSIGN(dst,src) \
32 do { \
33  (dst) = DECLTYPE(dst)(src); \
34 } while(0)
35 #endif
36 
37 /* a number of the hash function use uint32_t which isn't defined on win32 */
38 #ifdef _MSC_VER
39 typedef unsigned int uint32_t;
40 #else
41 #include <inttypes.h> /* uint32_t */
42 #endif
43 
44 #define UTHASH_VERSION 1.9.1
45 
46 #define uthash_fatal(msg) exit(-1) /* fatal error (out of memory,etc) */
47 #define uthash_malloc(sz) malloc(sz) /* malloc fcn */
48 #define uthash_free(ptr) free(ptr) /* free fcn */
49 
50 #define uthash_noexpand_fyi(tbl) /* can be defined to log noexpand */
51 #define uthash_expand_fyi(tbl) /* can be defined to log expands */
52 
53 /* initial number of buckets */
54 #define HASH_INITIAL_NUM_BUCKETS 32 /* initial number of buckets */
55 #define HASH_INITIAL_NUM_BUCKETS_LOG2 5 /* lg2 of initial number of buckets */
56 #define HASH_BKT_CAPACITY_THRESH 10 /* expand when bucket count reaches */
57 
58 /* calculate the element whose hash handle address is hhe */
59 #define ELMT_FROM_HH(tbl,hhp) ((void*)(((char*)(hhp)) - ((tbl)->hho)))
60 
61 #define HASH_FIND(hh,head,keyptr,keylen,out) \
62 do { \
63  unsigned _hf_bkt,_hf_hashv; \
64  out=NULL; \
65  if (head) { \
66  HASH_FCN(keyptr,keylen, (head)->hh.tbl->num_buckets, _hf_hashv, _hf_bkt); \
67  if (HASH_BLOOM_TEST((head)->hh.tbl, _hf_hashv)) { \
68  HASH_FIND_IN_BKT((head)->hh.tbl, hh, (head)->hh.tbl->buckets[ _hf_bkt ], \
69  keyptr,keylen,out); \
70  } \
71  } \
72 } while (0)
73 
74 #ifdef HASH_BLOOM
75 #define HASH_BLOOM_BITLEN (1ULL << HASH_BLOOM)
76 #define HASH_BLOOM_BYTELEN (HASH_BLOOM_BITLEN/8) + ((HASH_BLOOM_BITLEN%8) ? 1:0)
77 #define HASH_BLOOM_MAKE(tbl) \
78 do { \
79  (tbl)->bloom_nbits = HASH_BLOOM; \
80  (tbl)->bloom_bv = (uint8_t*)uthash_malloc(HASH_BLOOM_BYTELEN); \
81  if (!((tbl)->bloom_bv)) { uthash_fatal( "out of memory"); } \
82  memset((tbl)->bloom_bv, 0, HASH_BLOOM_BYTELEN); \
83  (tbl)->bloom_sig = HASH_BLOOM_SIGNATURE; \
84 } while (0);
85 
86 #define HASH_BLOOM_FREE(tbl) \
87 do { \
88  uthash_free((tbl)->bloom_bv); \
89 } while (0);
90 
91 #define HASH_BLOOM_BITSET(bv,idx) (bv[(idx)/8] |= (1U << ((idx)%8)))
92 #define HASH_BLOOM_BITTEST(bv,idx) (bv[(idx)/8] & (1U << ((idx)%8)))
93 
94 #define HASH_BLOOM_ADD(tbl,hashv) \
95  HASH_BLOOM_BITSET((tbl)->bloom_bv, (hashv & (uint32_t)((1ULL << (tbl)->bloom_nbits) - 1)))
96 
97 #define HASH_BLOOM_TEST(tbl,hashv) \
98  HASH_BLOOM_BITTEST((tbl)->bloom_bv, (hashv & (uint32_t)((1ULL << (tbl)->bloom_nbits) - 1)))
99 
100 #else
101 #define HASH_BLOOM_MAKE(tbl)
102 #define HASH_BLOOM_FREE(tbl)
103 #define HASH_BLOOM_ADD(tbl,hashv)
104 #define HASH_BLOOM_TEST(tbl,hashv) (1)
105 #endif
106 
107 #define HASH_MAKE_TABLE(hh,head) \
108 do { \
109  (head)->hh.tbl = (UT_hash_table*)uthash_malloc( \
110  sizeof(UT_hash_table)); \
111  if (!((head)->hh.tbl)) { uthash_fatal( "out of memory"); } \
112  memset((head)->hh.tbl, 0, sizeof(UT_hash_table)); \
113  (head)->hh.tbl->tail = &((head)->hh); \
114  (head)->hh.tbl->num_buckets = HASH_INITIAL_NUM_BUCKETS; \
115  (head)->hh.tbl->log2_num_buckets = HASH_INITIAL_NUM_BUCKETS_LOG2; \
116  (head)->hh.tbl->hho = (char*)(&(head)->hh) - (char*)(head); \
117  (head)->hh.tbl->buckets = (UT_hash_bucket*)uthash_malloc( \
118  HASH_INITIAL_NUM_BUCKETS*sizeof(struct UT_hash_bucket)); \
119  if (! (head)->hh.tbl->buckets) { uthash_fatal( "out of memory"); } \
120  memset((head)->hh.tbl->buckets, 0, \
121  HASH_INITIAL_NUM_BUCKETS*sizeof(struct UT_hash_bucket)); \
122  HASH_BLOOM_MAKE((head)->hh.tbl); \
123  (head)->hh.tbl->signature = HASH_SIGNATURE; \
124 } while(0)
125 
126 #define HASH_ADD(hh,head,fieldname,keylen_in,add) \
127  HASH_ADD_KEYPTR(hh,head,&add->fieldname,keylen_in,add)
128 
129 #define HASH_ADD_KEYPTR(hh,head,keyptr,keylen_in,add) \
130 do { \
131  unsigned _ha_bkt; \
132  (add)->hh.next = NULL; \
133  (add)->hh.key = (char*)keyptr; \
134  (add)->hh.keylen = keylen_in; \
135  if (!(head)) { \
136  head = (add); \
137  (head)->hh.prev = NULL; \
138  HASH_MAKE_TABLE(hh,head); \
139  } else { \
140  (head)->hh.tbl->tail->next = (add); \
141  (add)->hh.prev = ELMT_FROM_HH((head)->hh.tbl, (head)->hh.tbl->tail); \
142  (head)->hh.tbl->tail = &((add)->hh); \
143  } \
144  (head)->hh.tbl->num_items++; \
145  (add)->hh.tbl = (head)->hh.tbl; \
146  HASH_FCN(keyptr,keylen_in, (head)->hh.tbl->num_buckets, \
147  (add)->hh.hashv, _ha_bkt); \
148  HASH_ADD_TO_BKT((head)->hh.tbl->buckets[_ha_bkt],&(add)->hh); \
149  HASH_BLOOM_ADD((head)->hh.tbl,(add)->hh.hashv); \
150  HASH_EMIT_KEY(hh,head,keyptr,keylen_in); \
151  HASH_FSCK(hh,head); \
152 } while(0)
153 
154 #define HASH_TO_BKT( hashv, num_bkts, bkt ) \
155 do { \
156  bkt = ((hashv) & ((num_bkts) - 1)); \
157 } while(0)
158 
159 /* delete "delptr" from the hash table.
160  * "the usual" patch-up process for the app-order doubly-linked-list.
161  * The use of _hd_hh_del below deserves special explanation.
162  * These used to be expressed using (delptr) but that led to a bug
163  * if someone used the same symbol for the head and deletee, like
164  * HASH_DELETE(hh,users,users);
165  * We want that to work, but by changing the head (users) below
166  * we were forfeiting our ability to further refer to the deletee (users)
167  * in the patch-up process. Solution: use scratch space to
168  * copy the deletee pointer, then the latter references are via that
169  * scratch pointer rather than through the repointed (users) symbol.
170  */
171 #define HASH_DELETE(hh,head,delptr) \
172 do { \
173  unsigned _hd_bkt; \
174  struct UT_hash_handle *_hd_hh_del; \
175  if ( ((delptr)->hh.prev == NULL) && ((delptr)->hh.next == NULL) ) { \
176  uthash_free((head)->hh.tbl->buckets ); \
177  HASH_BLOOM_FREE((head)->hh.tbl); \
178  uthash_free((head)->hh.tbl); \
179  head = NULL; \
180  } else { \
181  _hd_hh_del = &((delptr)->hh); \
182  if ((delptr) == ELMT_FROM_HH((head)->hh.tbl,(head)->hh.tbl->tail)) { \
183  (head)->hh.tbl->tail = \
184  (UT_hash_handle*)((char*)((delptr)->hh.prev) + \
185  (head)->hh.tbl->hho); \
186  } \
187  if ((delptr)->hh.prev) { \
188  ((UT_hash_handle*)((char*)((delptr)->hh.prev) + \
189  (head)->hh.tbl->hho))->next = (delptr)->hh.next; \
190  } else { \
191  DECLTYPE_ASSIGN(head,(delptr)->hh.next); \
192  } \
193  if (_hd_hh_del->next) { \
194  ((UT_hash_handle*)((char*)_hd_hh_del->next + \
195  (head)->hh.tbl->hho))->prev = \
196  _hd_hh_del->prev; \
197  } \
198  HASH_TO_BKT( _hd_hh_del->hashv, (head)->hh.tbl->num_buckets, _hd_bkt); \
199  HASH_DEL_IN_BKT(hh,(head)->hh.tbl->buckets[_hd_bkt], _hd_hh_del); \
200  (head)->hh.tbl->num_items--; \
201  } \
202  HASH_FSCK(hh,head); \
203 } while (0)
204 
205 
206 /* convenience forms of HASH_FIND/HASH_ADD/HASH_DEL */
207 #define HASH_FIND_STR(head,findstr,out) \
208  HASH_FIND(hh,head,findstr,strlen(findstr),out)
209 #define HASH_ADD_STR(head,strfield,add) \
210  HASH_ADD(hh,head,strfield,strlen(add->strfield),add)
211 #define HASH_FIND_INT(head,findint,out) \
212  HASH_FIND(hh,head,findint,sizeof(int),out)
213 #define HASH_ADD_INT(head,intfield,add) \
214  HASH_ADD(hh,head,intfield,sizeof(int),add)
215 #define HASH_FIND_PTR(head,findptr,out) \
216  HASH_FIND(hh,head,findptr,sizeof(void *),out)
217 #define HASH_ADD_PTR(head,ptrfield,add) \
218  HASH_ADD(hh,head,ptrfield,sizeof(void *),add)
219 #define HASH_DEL(head,delptr) \
220  HASH_DELETE(hh,head,delptr)
221 
222 /* HASH_FSCK checks hash integrity on every add/delete when HASH_DEBUG is defined.
223  * This is for uthash developer only; it compiles away if HASH_DEBUG isn't defined.
224  */
225 #ifdef HASH_DEBUG
226 #define HASH_OOPS(...) do { fprintf(stderr,__VA_ARGS__); exit(-1); } while (0)
227 #define HASH_FSCK(hh,head) \
228 do { \
229  unsigned _bkt_i; \
230  unsigned _count, _bkt_count; \
231  char *_prev; \
232  struct UT_hash_handle *_thh; \
233  if (head) { \
234  _count = 0; \
235  for( _bkt_i = 0; _bkt_i < (head)->hh.tbl->num_buckets; _bkt_i++) { \
236  _bkt_count = 0; \
237  _thh = (head)->hh.tbl->buckets[_bkt_i].hh_head; \
238  _prev = NULL; \
239  while (_thh) { \
240  if (_prev != (char*)(_thh->hh_prev)) { \
241  HASH_OOPS("invalid hh_prev %p, actual %p\n", \
242  _thh->hh_prev, _prev ); \
243  } \
244  _bkt_count++; \
245  _prev = (char*)(_thh); \
246  _thh = _thh->hh_next; \
247  } \
248  _count += _bkt_count; \
249  if ((head)->hh.tbl->buckets[_bkt_i].count != _bkt_count) { \
250  HASH_OOPS("invalid bucket count %d, actual %d\n", \
251  (head)->hh.tbl->buckets[_bkt_i].count, _bkt_count); \
252  } \
253  } \
254  if (_count != (head)->hh.tbl->num_items) { \
255  HASH_OOPS("invalid hh item count %d, actual %d\n", \
256  (head)->hh.tbl->num_items, _count ); \
257  } \
258  /* traverse hh in app order; check next/prev integrity, count */ \
259  _count = 0; \
260  _prev = NULL; \
261  _thh = &(head)->hh; \
262  while (_thh) { \
263  _count++; \
264  if (_prev !=(char*)(_thh->prev)) { \
265  HASH_OOPS("invalid prev %p, actual %p\n", \
266  _thh->prev, _prev ); \
267  } \
268  _prev = (char*)ELMT_FROM_HH((head)->hh.tbl, _thh); \
269  _thh = ( _thh->next ? (UT_hash_handle*)((char*)(_thh->next) + \
270  (head)->hh.tbl->hho) : NULL ); \
271  } \
272  if (_count != (head)->hh.tbl->num_items) { \
273  HASH_OOPS("invalid app item count %d, actual %d\n", \
274  (head)->hh.tbl->num_items, _count ); \
275  } \
276  } \
277 } while (0)
278 #else
279 #define HASH_FSCK(hh,head)
280 #endif
281 
282 /* When compiled with -DHASH_EMIT_KEYS, length-prefixed keys are emitted to
283  * the descriptor to which this macro is defined for tuning the hash function.
284  * The app can #include <unistd.h> to get the prototype for write(2). */
285 #ifdef HASH_EMIT_KEYS
286 #define HASH_EMIT_KEY(hh,head,keyptr,fieldlen) \
287 do { \
288  unsigned _klen = fieldlen; \
289  write(HASH_EMIT_KEYS, &_klen, sizeof(_klen)); \
290  write(HASH_EMIT_KEYS, keyptr, fieldlen); \
291 } while (0)
292 #else
293 #define HASH_EMIT_KEY(hh,head,keyptr,fieldlen)
294 #endif
295 
296 /* default to Jenkin's hash unless overridden e.g. DHASH_FUNCTION=HASH_SAX */
297 #ifdef HASH_FUNCTION
298 #define HASH_FCN HASH_FUNCTION
299 #else
300 #define HASH_FCN HASH_JEN
301 #endif
302 
303 /* The Bernstein hash function, used in Perl prior to v5.6 */
304 #define HASH_BER(key,keylen,num_bkts,hashv,bkt) \
305 do { \
306  unsigned _hb_keylen=keylen; \
307  char *_hb_key=(char*)key; \
308  (hashv) = 0; \
309  while (_hb_keylen--) { (hashv) = ((hashv) * 33) + *_hb_key++; } \
310  bkt = (hashv) & (num_bkts-1); \
311 } while (0)
312 
313 
314 /* SAX/FNV/OAT/JEN hash functions are macro variants of those listed at
315  * http://eternallyconfuzzled.com/tuts/algorithms/jsw_tut_hashing.aspx */
316 #define HASH_SAX(key,keylen,num_bkts,hashv,bkt) \
317 do { \
318  unsigned _sx_i; \
319  char *_hs_key=(char*)key; \
320  hashv = 0; \
321  for(_sx_i=0; _sx_i < keylen; _sx_i++) \
322  hashv ^= (hashv << 5) + (hashv >> 2) + _hs_key[_sx_i]; \
323  bkt = hashv & (num_bkts-1); \
324 } while (0)
325 
326 #define HASH_FNV(key,keylen,num_bkts,hashv,bkt) \
327 do { \
328  unsigned _fn_i; \
329  char *_hf_key=(char*)key; \
330  hashv = 2166136261UL; \
331  for(_fn_i=0; _fn_i < keylen; _fn_i++) \
332  hashv = (hashv * 16777619) ^ _hf_key[_fn_i]; \
333  bkt = hashv & (num_bkts-1); \
334 } while(0);
335 
336 #define HASH_OAT(key,keylen,num_bkts,hashv,bkt) \
337 do { \
338  unsigned _ho_i; \
339  char *_ho_key=(char*)key; \
340  hashv = 0; \
341  for(_ho_i=0; _ho_i < keylen; _ho_i++) { \
342  hashv += _ho_key[_ho_i]; \
343  hashv += (hashv << 10); \
344  hashv ^= (hashv >> 6); \
345  } \
346  hashv += (hashv << 3); \
347  hashv ^= (hashv >> 11); \
348  hashv += (hashv << 15); \
349  bkt = hashv & (num_bkts-1); \
350 } while(0)
351 
352 #define HASH_JEN_MIX(a,b,c) \
353 do { \
354  a -= b; a -= c; a ^= ( c >> 13 ); \
355  b -= c; b -= a; b ^= ( a << 8 ); \
356  c -= a; c -= b; c ^= ( b >> 13 ); \
357  a -= b; a -= c; a ^= ( c >> 12 ); \
358  b -= c; b -= a; b ^= ( a << 16 ); \
359  c -= a; c -= b; c ^= ( b >> 5 ); \
360  a -= b; a -= c; a ^= ( c >> 3 ); \
361  b -= c; b -= a; b ^= ( a << 10 ); \
362  c -= a; c -= b; c ^= ( b >> 15 ); \
363 } while (0)
364 
365 #define HASH_JEN(key,keylen,num_bkts,hashv,bkt) \
366 do { \
367  unsigned _hj_i,_hj_j,_hj_k; \
368  char *_hj_key=(char*)key; \
369  hashv = 0xfeedbeef; \
370  _hj_i = _hj_j = 0x9e3779b9; \
371  _hj_k = keylen; \
372  while (_hj_k >= 12) { \
373  _hj_i += (_hj_key[0] + ( (unsigned)_hj_key[1] << 8 ) \
374  + ( (unsigned)_hj_key[2] << 16 ) \
375  + ( (unsigned)_hj_key[3] << 24 ) ); \
376  _hj_j += (_hj_key[4] + ( (unsigned)_hj_key[5] << 8 ) \
377  + ( (unsigned)_hj_key[6] << 16 ) \
378  + ( (unsigned)_hj_key[7] << 24 ) ); \
379  hashv += (_hj_key[8] + ( (unsigned)_hj_key[9] << 8 ) \
380  + ( (unsigned)_hj_key[10] << 16 ) \
381  + ( (unsigned)_hj_key[11] << 24 ) ); \
382  \
383  HASH_JEN_MIX(_hj_i, _hj_j, hashv); \
384  \
385  _hj_key += 12; \
386  _hj_k -= 12; \
387  } \
388  hashv += keylen; \
389  switch ( _hj_k ) { \
390  case 11: hashv += ( (unsigned)_hj_key[10] << 24 ); \
391  case 10: hashv += ( (unsigned)_hj_key[9] << 16 ); \
392  case 9: hashv += ( (unsigned)_hj_key[8] << 8 ); \
393  case 8: _hj_j += ( (unsigned)_hj_key[7] << 24 ); \
394  case 7: _hj_j += ( (unsigned)_hj_key[6] << 16 ); \
395  case 6: _hj_j += ( (unsigned)_hj_key[5] << 8 ); \
396  case 5: _hj_j += _hj_key[4]; \
397  case 4: _hj_i += ( (unsigned)_hj_key[3] << 24 ); \
398  case 3: _hj_i += ( (unsigned)_hj_key[2] << 16 ); \
399  case 2: _hj_i += ( (unsigned)_hj_key[1] << 8 ); \
400  case 1: _hj_i += _hj_key[0]; \
401  } \
402  HASH_JEN_MIX(_hj_i, _hj_j, hashv); \
403  bkt = hashv & (num_bkts-1); \
404 } while(0)
405 
406 /* The Paul Hsieh hash function */
407 #undef get16bits
408 #if (defined(__GNUC__) && defined(__i386__)) || defined(__WATCOMC__) \
409  || defined(_MSC_VER) || defined (__BORLANDC__) || defined (__TURBOC__)
410 #define get16bits(d) (*((const uint16_t *) (d)))
411 #endif
412 
413 #if !defined (get16bits)
414 #define get16bits(d) ((((uint32_t)(((const uint8_t *)(d))[1])) << 8) \
415  +(uint32_t)(((const uint8_t *)(d))[0]) )
416 #endif
417 #define HASH_SFH(key,keylen,num_bkts,hashv,bkt) \
418 do { \
419  char *_sfh_key=(char*)key; \
420  uint32_t _sfh_tmp, _sfh_len = keylen; \
421  \
422  int _sfh_rem = _sfh_len & 3; \
423  _sfh_len >>= 2; \
424  hashv = 0xcafebabe; \
425  \
426  /* Main loop */ \
427  for (;_sfh_len > 0; _sfh_len--) { \
428  hashv += get16bits (_sfh_key); \
429  _sfh_tmp = (get16bits (_sfh_key+2) << 11) ^ hashv; \
430  hashv = (hashv << 16) ^ _sfh_tmp; \
431  _sfh_key += 2*sizeof (uint16_t); \
432  hashv += hashv >> 11; \
433  } \
434  \
435  /* Handle end cases */ \
436  switch (_sfh_rem) { \
437  case 3: hashv += get16bits (_sfh_key); \
438  hashv ^= hashv << 16; \
439  hashv ^= _sfh_key[sizeof (uint16_t)] << 18; \
440  hashv += hashv >> 11; \
441  break; \
442  case 2: hashv += get16bits (_sfh_key); \
443  hashv ^= hashv << 11; \
444  hashv += hashv >> 17; \
445  break; \
446  case 1: hashv += *_sfh_key; \
447  hashv ^= hashv << 10; \
448  hashv += hashv >> 1; \
449  } \
450  \
451  /* Force "avalanching" of final 127 bits */ \
452  hashv ^= hashv << 3; \
453  hashv += hashv >> 5; \
454  hashv ^= hashv << 4; \
455  hashv += hashv >> 17; \
456  hashv ^= hashv << 25; \
457  hashv += hashv >> 6; \
458  bkt = hashv & (num_bkts-1); \
459 } while(0);
460 
461 #ifdef HASH_USING_NO_STRICT_ALIASING
462 /* The MurmurHash exploits some CPU's (e.g. x86) tolerance for unaligned reads.
463  * For other types of CPU's (e.g. Sparc) an unaligned read causes a bus error.
464  * So MurmurHash comes in two versions, the faster unaligned one and the slower
465  * aligned one. We only use the faster one on CPU's where we know it's safe.
466  *
467  * Note the preprocessor built-in defines can be emitted using:
468  *
469  * gcc -m64 -dM -E - < /dev/null (on gcc)
470  * cc -## a.c (where a.c is a simple test file) (Sun Studio)
471  */
472 #if (defined(__i386__) || defined(__x86_64__))
473 #define HASH_MUR HASH_MUR_UNALIGNED
474 #else
475 #define HASH_MUR HASH_MUR_ALIGNED
476 #endif
477 
478 /* Appleby's MurmurHash fast version for unaligned-tolerant archs like i386 */
479 #define HASH_MUR_UNALIGNED(key,keylen,num_bkts,hashv,bkt) \
480 do { \
481  const unsigned int _mur_m = 0x5bd1e995; \
482  const int _mur_r = 24; \
483  hashv = 0xcafebabe ^ keylen; \
484  char *_mur_key = (char *)key; \
485  uint32_t _mur_tmp, _mur_len = keylen; \
486  \
487  for (;_mur_len >= 4; _mur_len-=4) { \
488  _mur_tmp = *(uint32_t *)_mur_key; \
489  _mur_tmp *= _mur_m; \
490  _mur_tmp ^= _mur_tmp >> _mur_r; \
491  _mur_tmp *= _mur_m; \
492  hashv *= _mur_m; \
493  hashv ^= _mur_tmp; \
494  _mur_key += 4; \
495  } \
496  \
497  switch(_mur_len) \
498  { \
499  case 3: hashv ^= _mur_key[2] << 16; \
500  case 2: hashv ^= _mur_key[1] << 8; \
501  case 1: hashv ^= _mur_key[0]; \
502  hashv *= _mur_m; \
503  }; \
504  \
505  hashv ^= hashv >> 13; \
506  hashv *= _mur_m; \
507  hashv ^= hashv >> 15; \
508  \
509  bkt = hashv & (num_bkts-1); \
510 } while(0)
511 
512 /* Appleby's MurmurHash version for alignment-sensitive archs like Sparc */
513 #define HASH_MUR_ALIGNED(key,keylen,num_bkts,hashv,bkt) \
514 do { \
515  const unsigned int _mur_m = 0x5bd1e995; \
516  const int _mur_r = 24; \
517  hashv = 0xcafebabe ^ keylen; \
518  char *_mur_key = (char *)key; \
519  uint32_t _mur_len = keylen; \
520  int _mur_align = (int)_mur_key & 3; \
521  \
522  if (_mur_align && (_mur_len >= 4)) { \
523  unsigned _mur_t = 0, _mur_d = 0; \
524  switch(_mur_align) { \
525  case 1: _mur_t |= _mur_key[2] << 16; \
526  case 2: _mur_t |= _mur_key[1] << 8; \
527  case 3: _mur_t |= _mur_key[0]; \
528  } \
529  _mur_t <<= (8 * _mur_align); \
530  _mur_key += 4-_mur_align; \
531  _mur_len -= 4-_mur_align; \
532  int _mur_sl = 8 * (4-_mur_align); \
533  int _mur_sr = 8 * _mur_align; \
534  \
535  for (;_mur_len >= 4; _mur_len-=4) { \
536  _mur_d = *(unsigned *)_mur_key; \
537  _mur_t = (_mur_t >> _mur_sr) | (_mur_d << _mur_sl); \
538  unsigned _mur_k = _mur_t; \
539  _mur_k *= _mur_m; \
540  _mur_k ^= _mur_k >> _mur_r; \
541  _mur_k *= _mur_m; \
542  hashv *= _mur_m; \
543  hashv ^= _mur_k; \
544  _mur_t = _mur_d; \
545  _mur_key += 4; \
546  } \
547  _mur_d = 0; \
548  if(_mur_len >= _mur_align) { \
549  switch(_mur_align) { \
550  case 3: _mur_d |= _mur_key[2] << 16; \
551  case 2: _mur_d |= _mur_key[1] << 8; \
552  case 1: _mur_d |= _mur_key[0]; \
553  } \
554  unsigned _mur_k = (_mur_t >> _mur_sr) | (_mur_d << _mur_sl); \
555  _mur_k *= _mur_m; \
556  _mur_k ^= _mur_k >> _mur_r; \
557  _mur_k *= _mur_m; \
558  hashv *= _mur_m; \
559  hashv ^= _mur_k; \
560  _mur_k += _mur_align; \
561  _mur_len -= _mur_align; \
562  \
563  switch(_mur_len) \
564  { \
565  case 3: hashv ^= _mur_key[2] << 16; \
566  case 2: hashv ^= _mur_key[1] << 8; \
567  case 1: hashv ^= _mur_key[0]; \
568  hashv *= _mur_m; \
569  } \
570  } else { \
571  switch(_mur_len) \
572  { \
573  case 3: _mur_d ^= _mur_key[2] << 16; \
574  case 2: _mur_d ^= _mur_key[1] << 8; \
575  case 1: _mur_d ^= _mur_key[0]; \
576  case 0: hashv ^= (_mur_t >> _mur_sr) | (_mur_d << _mur_sl); \
577  hashv *= _mur_m; \
578  } \
579  } \
580  \
581  hashv ^= hashv >> 13; \
582  hashv *= _mur_m; \
583  hashv ^= hashv >> 15; \
584  } else { \
585  for (;_mur_len >= 4; _mur_len-=4) { \
586  unsigned _mur_k = *(unsigned*)_mur_key; \
587  _mur_k *= _mur_m; \
588  _mur_k ^= _mur_k >> _mur_r; \
589  _mur_k *= _mur_m; \
590  hashv *= _mur_m; \
591  hashv ^= _mur_k; \
592  _mur_key += 4; \
593  } \
594  switch(_mur_len) \
595  { \
596  case 3: hashv ^= _mur_key[2] << 16; \
597  case 2: hashv ^= _mur_key[1] << 8; \
598  case 1: hashv ^= _mur_key[0]; \
599  hashv *= _mur_m; \
600  } \
601  \
602  hashv ^= hashv >> 13; \
603  hashv *= _mur_m; \
604  hashv ^= hashv >> 15; \
605  } \
606  bkt = hashv & (num_bkts-1); \
607 } while(0)
608 #endif /* HASH_USING_NO_STRICT_ALIASING */
609 
610 /* key comparison function; return 0 if keys equal */
611 #define HASH_KEYCMP(a,b,len) memcmp(a,b,len)
612 
613 /* iterate over items in a known bucket to find desired item */
614 #define HASH_FIND_IN_BKT(tbl,hh,head,keyptr,keylen_in,out) \
615 do { \
616  if (head.hh_head) DECLTYPE_ASSIGN(out,ELMT_FROM_HH(tbl,head.hh_head)); \
617  else out=NULL; \
618  while (out) { \
619  if (out->hh.keylen == keylen_in) { \
620  if ((HASH_KEYCMP(out->hh.key,keyptr,keylen_in)) == 0) break; \
621  } \
622  if (out->hh.hh_next) DECLTYPE_ASSIGN(out,ELMT_FROM_HH(tbl,out->hh.hh_next)); \
623  else out = NULL; \
624  } \
625 } while(0)
626 
627 /* add an item to a bucket */
628 #define HASH_ADD_TO_BKT(head,addhh) \
629 do { \
630  head.count++; \
631  (addhh)->hh_next = head.hh_head; \
632  (addhh)->hh_prev = NULL; \
633  if (head.hh_head) { (head).hh_head->hh_prev = (addhh); } \
634  (head).hh_head=addhh; \
635  if (head.count >= ((head.expand_mult+1) * HASH_BKT_CAPACITY_THRESH) \
636  && (addhh)->tbl->noexpand != 1) { \
637  HASH_EXPAND_BUCKETS((addhh)->tbl); \
638  } \
639 } while(0)
640 
641 /* remove an item from a given bucket */
642 #define HASH_DEL_IN_BKT(hh,head,hh_del) \
643  (head).count--; \
644  if ((head).hh_head == hh_del) { \
645  (head).hh_head = hh_del->hh_next; \
646  } \
647  if (hh_del->hh_prev) { \
648  hh_del->hh_prev->hh_next = hh_del->hh_next; \
649  } \
650  if (hh_del->hh_next) { \
651  hh_del->hh_next->hh_prev = hh_del->hh_prev; \
652  }
653 
654 /* Bucket expansion has the effect of doubling the number of buckets
655  * and redistributing the items into the new buckets. Ideally the
656  * items will distribute more or less evenly into the new buckets
657  * (the extent to which this is true is a measure of the quality of
658  * the hash function as it applies to the key domain).
659  *
660  * With the items distributed into more buckets, the chain length
661  * (item count) in each bucket is reduced. Thus by expanding buckets
662  * the hash keeps a bound on the chain length. This bounded chain
663  * length is the essence of how a hash provides constant time lookup.
664  *
665  * The calculation of tbl->ideal_chain_maxlen below deserves some
666  * explanation. First, keep in mind that we're calculating the ideal
667  * maximum chain length based on the *new* (doubled) bucket count.
668  * In fractions this is just n/b (n=number of items,b=new num buckets).
669  * Since the ideal chain length is an integer, we want to calculate
670  * ceil(n/b). We don't depend on floating point arithmetic in this
671  * hash, so to calculate ceil(n/b) with integers we could write
672  *
673  * ceil(n/b) = (n/b) + ((n%b)?1:0)
674  *
675  * and in fact a previous version of this hash did just that.
676  * But now we have improved things a bit by recognizing that b is
677  * always a power of two. We keep its base 2 log handy (call it lb),
678  * so now we can write this with a bit shift and logical AND:
679  *
680  * ceil(n/b) = (n>>lb) + ( (n & (b-1)) ? 1:0)
681  *
682  */
683 #define HASH_EXPAND_BUCKETS(tbl) \
684 do { \
685  unsigned _he_bkt; \
686  unsigned _he_bkt_i; \
687  struct UT_hash_handle *_he_thh, *_he_hh_nxt; \
688  UT_hash_bucket *_he_new_buckets, *_he_newbkt; \
689  _he_new_buckets = (UT_hash_bucket*)uthash_malloc( \
690  2 * tbl->num_buckets * sizeof(struct UT_hash_bucket)); \
691  if (!_he_new_buckets) { uthash_fatal( "out of memory"); } \
692  memset(_he_new_buckets, 0, \
693  2 * tbl->num_buckets * sizeof(struct UT_hash_bucket)); \
694  tbl->ideal_chain_maxlen = \
695  (tbl->num_items >> (tbl->log2_num_buckets+1)) + \
696  ((tbl->num_items & ((tbl->num_buckets*2)-1)) ? 1 : 0); \
697  tbl->nonideal_items = 0; \
698  for(_he_bkt_i = 0; _he_bkt_i < tbl->num_buckets; _he_bkt_i++) \
699  { \
700  _he_thh = tbl->buckets[ _he_bkt_i ].hh_head; \
701  while (_he_thh) { \
702  _he_hh_nxt = _he_thh->hh_next; \
703  HASH_TO_BKT( _he_thh->hashv, tbl->num_buckets*2, _he_bkt); \
704  _he_newbkt = &(_he_new_buckets[ _he_bkt ]); \
705  if (++(_he_newbkt->count) > tbl->ideal_chain_maxlen) { \
706  tbl->nonideal_items++; \
707  _he_newbkt->expand_mult = _he_newbkt->count / \
708  tbl->ideal_chain_maxlen; \
709  } \
710  _he_thh->hh_prev = NULL; \
711  _he_thh->hh_next = _he_newbkt->hh_head; \
712  if (_he_newbkt->hh_head) _he_newbkt->hh_head->hh_prev = \
713  _he_thh; \
714  _he_newbkt->hh_head = _he_thh; \
715  _he_thh = _he_hh_nxt; \
716  } \
717  } \
718  tbl->num_buckets *= 2; \
719  tbl->log2_num_buckets++; \
720  uthash_free( tbl->buckets ); \
721  tbl->buckets = _he_new_buckets; \
722  tbl->ineff_expands = (tbl->nonideal_items > (tbl->num_items >> 1)) ? \
723  (tbl->ineff_expands+1) : 0; \
724  if (tbl->ineff_expands > 1) { \
725  tbl->noexpand=1; \
726  uthash_noexpand_fyi(tbl); \
727  } \
728  uthash_expand_fyi(tbl); \
729 } while(0)
730 
731 
732 /* This is an adaptation of Simon Tatham's O(n log(n)) mergesort */
733 /* Note that HASH_SORT assumes the hash handle name to be hh.
734  * HASH_SRT was added to allow the hash handle name to be passed in. */
735 #define HASH_SORT(head,cmpfcn) HASH_SRT(hh,head,cmpfcn)
736 #define HASH_SRT(hh,head,cmpfcn) \
737 do { \
738  unsigned _hs_i; \
739  unsigned _hs_looping,_hs_nmerges,_hs_insize,_hs_psize,_hs_qsize; \
740  struct UT_hash_handle *_hs_p, *_hs_q, *_hs_e, *_hs_list, *_hs_tail; \
741  if (head) { \
742  _hs_insize = 1; \
743  _hs_looping = 1; \
744  _hs_list = &((head)->hh); \
745  while (_hs_looping) { \
746  _hs_p = _hs_list; \
747  _hs_list = NULL; \
748  _hs_tail = NULL; \
749  _hs_nmerges = 0; \
750  while (_hs_p) { \
751  _hs_nmerges++; \
752  _hs_q = _hs_p; \
753  _hs_psize = 0; \
754  for ( _hs_i = 0; _hs_i < _hs_insize; _hs_i++ ) { \
755  _hs_psize++; \
756  _hs_q = (UT_hash_handle*)((_hs_q->next) ? \
757  ((void*)((char*)(_hs_q->next) + \
758  (head)->hh.tbl->hho)) : NULL); \
759  if (! (_hs_q) ) break; \
760  } \
761  _hs_qsize = _hs_insize; \
762  while ((_hs_psize > 0) || ((_hs_qsize > 0) && _hs_q )) { \
763  if (_hs_psize == 0) { \
764  _hs_e = _hs_q; \
765  _hs_q = (UT_hash_handle*)((_hs_q->next) ? \
766  ((void*)((char*)(_hs_q->next) + \
767  (head)->hh.tbl->hho)) : NULL); \
768  _hs_qsize--; \
769  } else if ( (_hs_qsize == 0) || !(_hs_q) ) { \
770  _hs_e = _hs_p; \
771  _hs_p = (UT_hash_handle*)((_hs_p->next) ? \
772  ((void*)((char*)(_hs_p->next) + \
773  (head)->hh.tbl->hho)) : NULL); \
774  _hs_psize--; \
775  } else if (( \
776  cmpfcn(DECLTYPE(head)(ELMT_FROM_HH((head)->hh.tbl,_hs_p)), \
777  DECLTYPE(head)(ELMT_FROM_HH((head)->hh.tbl,_hs_q))) \
778  ) <= 0) { \
779  _hs_e = _hs_p; \
780  _hs_p = (UT_hash_handle*)((_hs_p->next) ? \
781  ((void*)((char*)(_hs_p->next) + \
782  (head)->hh.tbl->hho)) : NULL); \
783  _hs_psize--; \
784  } else { \
785  _hs_e = _hs_q; \
786  _hs_q = (UT_hash_handle*)((_hs_q->next) ? \
787  ((void*)((char*)(_hs_q->next) + \
788  (head)->hh.tbl->hho)) : NULL); \
789  _hs_qsize--; \
790  } \
791  if ( _hs_tail ) { \
792  _hs_tail->next = ((_hs_e) ? \
793  ELMT_FROM_HH((head)->hh.tbl,_hs_e) : NULL); \
794  } else { \
795  _hs_list = _hs_e; \
796  } \
797  _hs_e->prev = ((_hs_tail) ? \
798  ELMT_FROM_HH((head)->hh.tbl,_hs_tail) : NULL); \
799  _hs_tail = _hs_e; \
800  } \
801  _hs_p = _hs_q; \
802  } \
803  _hs_tail->next = NULL; \
804  if ( _hs_nmerges <= 1 ) { \
805  _hs_looping=0; \
806  (head)->hh.tbl->tail = _hs_tail; \
807  DECLTYPE_ASSIGN(head,ELMT_FROM_HH((head)->hh.tbl, _hs_list)); \
808  } \
809  _hs_insize *= 2; \
810  } \
811  HASH_FSCK(hh,head); \
812  } \
813 } while (0)
814 
815 /* This function selects items from one hash into another hash.
816  * The end result is that the selected items have dual presence
817  * in both hashes. There is no copy of the items made; rather
818  * they are added into the new hash through a secondary hash
819  * hash handle that must be present in the structure. */
820 #define HASH_SELECT(hh_dst, dst, hh_src, src, cond) \
821 do { \
822  unsigned _src_bkt, _dst_bkt; \
823  void *_last_elt=NULL, *_elt; \
824  UT_hash_handle *_src_hh, *_dst_hh, *_last_elt_hh=NULL; \
825  ptrdiff_t _dst_hho = ((char*)(&(dst)->hh_dst) - (char*)(dst)); \
826  if (src) { \
827  for(_src_bkt=0; _src_bkt < (src)->hh_src.tbl->num_buckets; _src_bkt++) { \
828  for(_src_hh = (src)->hh_src.tbl->buckets[_src_bkt].hh_head; \
829  _src_hh; \
830  _src_hh = _src_hh->hh_next) { \
831  _elt = ELMT_FROM_HH((src)->hh_src.tbl, _src_hh); \
832  if (cond(_elt)) { \
833  _dst_hh = (UT_hash_handle*)(((char*)_elt) + _dst_hho); \
834  _dst_hh->key = _src_hh->key; \
835  _dst_hh->keylen = _src_hh->keylen; \
836  _dst_hh->hashv = _src_hh->hashv; \
837  _dst_hh->prev = _last_elt; \
838  _dst_hh->next = NULL; \
839  if (_last_elt_hh) { _last_elt_hh->next = _elt; } \
840  if (!dst) { \
841  DECLTYPE_ASSIGN(dst,_elt); \
842  HASH_MAKE_TABLE(hh_dst,dst); \
843  } else { \
844  _dst_hh->tbl = (dst)->hh_dst.tbl; \
845  } \
846  HASH_TO_BKT(_dst_hh->hashv, _dst_hh->tbl->num_buckets, _dst_bkt); \
847  HASH_ADD_TO_BKT(_dst_hh->tbl->buckets[_dst_bkt],_dst_hh); \
848  (dst)->hh_dst.tbl->num_items++; \
849  _last_elt = _elt; \
850  _last_elt_hh = _dst_hh; \
851  } \
852  } \
853  } \
854  } \
855  HASH_FSCK(hh_dst,dst); \
856 } while (0)
857 
858 #define HASH_CLEAR(hh,head) \
859 do { \
860  if (head) { \
861  uthash_free((head)->hh.tbl->buckets ); \
862  uthash_free((head)->hh.tbl); \
863  (head)=NULL; \
864  } \
865 } while(0)
866 
867 /* obtain a count of items in the hash */
868 #define HASH_COUNT(head) HASH_CNT(hh,head)
869 #define HASH_CNT(hh,head) (head?(head->hh.tbl->num_items):0)
870 
871 typedef struct UT_hash_bucket {
872  struct UT_hash_handle *hh_head;
873  unsigned count;
874 
875  /* expand_mult is normally set to 0. In this situation, the max chain length
876  * threshold is enforced at its default value, HASH_BKT_CAPACITY_THRESH. (If
877  * the bucket's chain exceeds this length, bucket expansion is triggered).
878  * However, setting expand_mult to a non-zero value delays bucket expansion
879  * (that would be triggered by additions to this particular bucket)
880  * until its chain length reaches a *multiple* of HASH_BKT_CAPACITY_THRESH.
881  * (The multiplier is simply expand_mult+1). The whole idea of this
882  * multiplier is to reduce bucket expansions, since they are expensive, in
883  * situations where we know that a particular bucket tends to be overused.
884  * It is better to let its chain length grow to a longer yet-still-bounded
885  * value, than to do an O(n) bucket expansion too often.
886  */
887  unsigned expand_mult;
888 
890 
891 /* random signature used only to find hash tables in external analysis */
892 #define HASH_SIGNATURE 0xa0111fe1
893 #define HASH_BLOOM_SIGNATURE 0xb12220f2
894 
895 typedef struct UT_hash_table {
896  UT_hash_bucket *buckets;
897  unsigned num_buckets, log2_num_buckets;
898  unsigned num_items;
899  struct UT_hash_handle *tail; /* tail hh in app order, for fast append */
900  ptrdiff_t hho; /* hash handle offset (byte pos of hash handle in element */
901 
902  /* in an ideal situation (all buckets used equally), no bucket would have
903  * more than ceil(#items/#buckets) items. that's the ideal chain length. */
904  unsigned ideal_chain_maxlen;
905 
906  /* nonideal_items is the number of items in the hash whose chain position
907  * exceeds the ideal chain maxlen. these items pay the penalty for an uneven
908  * hash distribution; reaching them in a chain traversal takes >ideal steps */
909  unsigned nonideal_items;
910 
911  /* ineffective expands occur when a bucket doubling was performed, but
912  * afterward, more than half the items in the hash had nonideal chain
913  * positions. If this happens on two consecutive expansions we inhibit any
914  * further expansion, as it's not helping; this happens when the hash
915  * function isn't a good fit for the key domain. When expansion is inhibited
916  * the hash will still work, albeit no longer in constant time. */
917  unsigned ineff_expands, noexpand;
918 
919  uint32_t signature; /* used only to find hash tables in external analysis */
920 #ifdef HASH_BLOOM
921  uint32_t bloom_sig; /* used only to test bloom exists in external analysis */
922  uint8_t *bloom_bv;
923  char bloom_nbits;
924 #endif
925 
926 } UT_hash_table;
927 
928 typedef struct UT_hash_handle {
929  struct UT_hash_table *tbl;
930  void *prev; /* prev element in app order */
931  void *next; /* next element in app order */
932  struct UT_hash_handle *hh_prev; /* previous hh in bucket order */
933  struct UT_hash_handle *hh_next; /* next hh in bucket order */
934  void *key; /* ptr to enclosing struct's key */
935  unsigned keylen; /* enclosing struct's key len */
936  unsigned hashv; /* result of hash-fcn(key) */
938 
939 #endif /* UTHASH_H */