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