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