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