uthash.h 77 KB

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