Chameleon

Chameleon Svn Source Tree

Root/branches/cparm/i386/libsaio/uthash.h

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

Archive Download this file

Revision: 1919