uthash.h 76 KB

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