uthash.h 60 KB

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