uthash.h 60 KB

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