erlang各种有用的函数包括一些有用nif封装,还有一些性能测试case。
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1074 lines
68 KiB

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