use-system-uthash.patch 62 KB

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