uthash.h 72 KB

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