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