uthash.h 68 KB

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