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