Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * $Id: linkhash.c,v 1.4 2006/01/26 02:16:28 mclark Exp $ |
3 | | * |
4 | | * Copyright (c) 2004, 2005 Metaparadigm Pte. Ltd. |
5 | | * Michael Clark <michael@metaparadigm.com> |
6 | | * Copyright (c) 2009 Hewlett-Packard Development Company, L.P. |
7 | | * |
8 | | * This library is free software; you can redistribute it and/or modify |
9 | | * it under the terms of the MIT license. See COPYING for details. |
10 | | * |
11 | | */ |
12 | | |
13 | | #include "config.h" |
14 | | |
15 | | #include <assert.h> |
16 | | #include <limits.h> |
17 | | #include <stdarg.h> |
18 | | #include <stddef.h> |
19 | | #include <stdio.h> |
20 | | #include <stdlib.h> |
21 | | #include <string.h> |
22 | | |
23 | | #ifdef HAVE_ENDIAN_H |
24 | | #include <endian.h> /* attempt to define endianness */ |
25 | | #endif |
26 | | |
27 | | #if defined(_MSC_VER) || defined(__MINGW32__) |
28 | | #define WIN32_LEAN_AND_MEAN |
29 | | #include <windows.h> /* Get InterlockedCompareExchange */ |
30 | | #endif |
31 | | |
32 | | #include "linkhash.h" |
33 | | #include "random_seed.h" |
34 | | |
35 | | /* hash functions */ |
36 | | static unsigned long lh_char_hash(const void *k); |
37 | | static unsigned long lh_perllike_str_hash(const void *k); |
38 | | static lh_hash_fn *char_hash_fn = lh_char_hash; |
39 | | |
40 | | /* comparison functions */ |
41 | | int lh_char_equal(const void *k1, const void *k2); |
42 | | int lh_ptr_equal(const void *k1, const void *k2); |
43 | | |
44 | | int json_global_set_string_hash(const int h) |
45 | 0 | { |
46 | 0 | switch (h) |
47 | 0 | { |
48 | 0 | case JSON_C_STR_HASH_DFLT: char_hash_fn = lh_char_hash; break; |
49 | 0 | case JSON_C_STR_HASH_PERLLIKE: char_hash_fn = lh_perllike_str_hash; break; |
50 | 0 | default: return -1; |
51 | 0 | } |
52 | 0 | return 0; |
53 | 0 | } |
54 | | |
55 | | static unsigned long lh_ptr_hash(const void *k) |
56 | 0 | { |
57 | | /* CAW: refactored to be 64bit nice */ |
58 | 0 | return (unsigned long)((((ptrdiff_t)k * LH_PRIME) >> 4) & ULONG_MAX); |
59 | 0 | } |
60 | | |
61 | | int lh_ptr_equal(const void *k1, const void *k2) |
62 | 0 | { |
63 | 0 | return (k1 == k2); |
64 | 0 | } |
65 | | |
66 | | /* |
67 | | * hashlittle from lookup3.c, by Bob Jenkins, May 2006, Public Domain. |
68 | | * https://burtleburtle.net/bob/c/lookup3.c |
69 | | * minor modifications to make functions static so no symbols are exported |
70 | | * minor modifications to compile with -Werror |
71 | | */ |
72 | | |
73 | | /* |
74 | | ------------------------------------------------------------------------------- |
75 | | lookup3.c, by Bob Jenkins, May 2006, Public Domain. |
76 | | |
77 | | These are functions for producing 32-bit hashes for hash table lookup. |
78 | | hashword(), hashlittle(), hashlittle2(), hashbig(), mix(), and final() |
79 | | are externally useful functions. Routines to test the hash are included |
80 | | if SELF_TEST is defined. You can use this free for any purpose. It's in |
81 | | the public domain. It has no warranty. |
82 | | |
83 | | You probably want to use hashlittle(). hashlittle() and hashbig() |
84 | | hash byte arrays. hashlittle() is faster than hashbig() on |
85 | | little-endian machines. Intel and AMD are little-endian machines. |
86 | | On second thought, you probably want hashlittle2(), which is identical to |
87 | | hashlittle() except it returns two 32-bit hashes for the price of one. |
88 | | You could implement hashbig2() if you wanted but I haven't bothered here. |
89 | | |
90 | | If you want to find a hash of, say, exactly 7 integers, do |
91 | | a = i1; b = i2; c = i3; |
92 | | mix(a,b,c); |
93 | | a += i4; b += i5; c += i6; |
94 | | mix(a,b,c); |
95 | | a += i7; |
96 | | final(a,b,c); |
97 | | then use c as the hash value. If you have a variable length array of |
98 | | 4-byte integers to hash, use hashword(). If you have a byte array (like |
99 | | a character string), use hashlittle(). If you have several byte arrays, or |
100 | | a mix of things, see the comments above hashlittle(). |
101 | | |
102 | | Why is this so big? I read 12 bytes at a time into 3 4-byte integers, |
103 | | then mix those integers. This is fast (you can do a lot more thorough |
104 | | mixing with 12*3 instructions on 3 integers than you can with 3 instructions |
105 | | on 1 byte), but shoehorning those bytes into integers efficiently is messy. |
106 | | ------------------------------------------------------------------------------- |
107 | | */ |
108 | | |
109 | | /* |
110 | | * My best guess at if you are big-endian or little-endian. This may |
111 | | * need adjustment. |
112 | | */ |
113 | | #if (defined(__BYTE_ORDER) && defined(__LITTLE_ENDIAN) && __BYTE_ORDER == __LITTLE_ENDIAN) || \ |
114 | | (defined(i386) || defined(__i386__) || defined(__i486__) || defined(__i586__) || \ |
115 | | defined(__i686__) || defined(vax) || defined(MIPSEL)) |
116 | 100k | #define HASH_LITTLE_ENDIAN 1 |
117 | | #define HASH_BIG_ENDIAN 0 |
118 | | #elif (defined(__BYTE_ORDER) && defined(__BIG_ENDIAN) && __BYTE_ORDER == __BIG_ENDIAN) || \ |
119 | | (defined(sparc) || defined(POWERPC) || defined(mc68000) || defined(sel)) |
120 | | #define HASH_LITTLE_ENDIAN 0 |
121 | | #define HASH_BIG_ENDIAN 1 |
122 | | #else |
123 | | #define HASH_LITTLE_ENDIAN 0 |
124 | | #define HASH_BIG_ENDIAN 0 |
125 | | #endif |
126 | | |
127 | | #define hashsize(n) ((uint32_t)1 << (n)) |
128 | | #define hashmask(n) (hashsize(n) - 1) |
129 | 4.11M | #define rot(x, k) (((x) << (k)) | ((x) >> (32 - (k)))) |
130 | | |
131 | | /* |
132 | | ------------------------------------------------------------------------------- |
133 | | mix -- mix 3 32-bit values reversibly. |
134 | | |
135 | | This is reversible, so any information in (a,b,c) before mix() is |
136 | | still in (a,b,c) after mix(). |
137 | | |
138 | | If four pairs of (a,b,c) inputs are run through mix(), or through |
139 | | mix() in reverse, there are at least 32 bits of the output that |
140 | | are sometimes the same for one pair and different for another pair. |
141 | | This was tested for: |
142 | | * pairs that differed by one bit, by two bits, in any combination |
143 | | of top bits of (a,b,c), or in any combination of bottom bits of |
144 | | (a,b,c). |
145 | | * "differ" is defined as +, -, ^, or ~^. For + and -, I transformed |
146 | | the output delta to a Gray code (a^(a>>1)) so a string of 1's (as |
147 | | is commonly produced by subtraction) look like a single 1-bit |
148 | | difference. |
149 | | * the base values were pseudorandom, all zero but one bit set, or |
150 | | all zero plus a counter that starts at zero. |
151 | | |
152 | | Some k values for my "a-=c; a^=rot(c,k); c+=b;" arrangement that |
153 | | satisfy this are |
154 | | 4 6 8 16 19 4 |
155 | | 9 15 3 18 27 15 |
156 | | 14 9 3 7 17 3 |
157 | | Well, "9 15 3 18 27 15" didn't quite get 32 bits diffing |
158 | | for "differ" defined as + with a one-bit base and a two-bit delta. I |
159 | | used https://burtleburtle.net/bob/hash/avalanche.html to choose |
160 | | the operations, constants, and arrangements of the variables. |
161 | | |
162 | | This does not achieve avalanche. There are input bits of (a,b,c) |
163 | | that fail to affect some output bits of (a,b,c), especially of a. The |
164 | | most thoroughly mixed value is c, but it doesn't really even achieve |
165 | | avalanche in c. |
166 | | |
167 | | This allows some parallelism. Read-after-writes are good at doubling |
168 | | the number of bits affected, so the goal of mixing pulls in the opposite |
169 | | direction as the goal of parallelism. I did what I could. Rotates |
170 | | seem to cost as much as shifts on every machine I could lay my hands |
171 | | on, and rotates are much kinder to the top and bottom bits, so I used |
172 | | rotates. |
173 | | ------------------------------------------------------------------------------- |
174 | | */ |
175 | | /* clang-format off */ |
176 | 631k | #define mix(a,b,c) \ |
177 | 631k | { \ |
178 | 631k | a -= c; a ^= rot(c, 4); c += b; \ |
179 | 631k | b -= a; b ^= rot(a, 6); a += c; \ |
180 | 631k | c -= b; c ^= rot(b, 8); b += a; \ |
181 | 631k | a -= c; a ^= rot(c,16); c += b; \ |
182 | 631k | b -= a; b ^= rot(a,19); a += c; \ |
183 | 631k | c -= b; c ^= rot(b, 4); b += a; \ |
184 | 631k | } |
185 | | /* clang-format on */ |
186 | | |
187 | | /* |
188 | | ------------------------------------------------------------------------------- |
189 | | final -- final mixing of 3 32-bit values (a,b,c) into c |
190 | | |
191 | | Pairs of (a,b,c) values differing in only a few bits will usually |
192 | | produce values of c that look totally different. This was tested for |
193 | | * pairs that differed by one bit, by two bits, in any combination |
194 | | of top bits of (a,b,c), or in any combination of bottom bits of |
195 | | (a,b,c). |
196 | | * "differ" is defined as +, -, ^, or ~^. For + and -, I transformed |
197 | | the output delta to a Gray code (a^(a>>1)) so a string of 1's (as |
198 | | is commonly produced by subtraction) look like a single 1-bit |
199 | | difference. |
200 | | * the base values were pseudorandom, all zero but one bit set, or |
201 | | all zero plus a counter that starts at zero. |
202 | | |
203 | | These constants passed: |
204 | | 14 11 25 16 4 14 24 |
205 | | 12 14 25 16 4 14 24 |
206 | | and these came close: |
207 | | 4 8 15 26 3 22 24 |
208 | | 10 8 15 26 3 22 24 |
209 | | 11 8 15 26 3 22 24 |
210 | | ------------------------------------------------------------------------------- |
211 | | */ |
212 | | /* clang-format off */ |
213 | 46.4k | #define final(a,b,c) \ |
214 | 46.4k | { \ |
215 | 46.4k | c ^= b; c -= rot(b,14); \ |
216 | 46.4k | a ^= c; a -= rot(c,11); \ |
217 | 46.4k | b ^= a; b -= rot(a,25); \ |
218 | 46.4k | c ^= b; c -= rot(b,16); \ |
219 | 46.4k | a ^= c; a -= rot(c,4); \ |
220 | 46.4k | b ^= a; b -= rot(a,14); \ |
221 | 46.4k | c ^= b; c -= rot(b,24); \ |
222 | 46.4k | } |
223 | | /* clang-format on */ |
224 | | |
225 | | /* |
226 | | ------------------------------------------------------------------------------- |
227 | | hashlittle() -- hash a variable-length key into a 32-bit value |
228 | | k : the key (the unaligned variable-length array of bytes) |
229 | | length : the length of the key, counting by bytes |
230 | | initval : can be any 4-byte value |
231 | | Returns a 32-bit value. Every bit of the key affects every bit of |
232 | | the return value. Two keys differing by one or two bits will have |
233 | | totally different hash values. |
234 | | |
235 | | The best hash table sizes are powers of 2. There is no need to do |
236 | | mod a prime (mod is sooo slow!). If you need less than 32 bits, |
237 | | use a bitmask. For example, if you need only 10 bits, do |
238 | | h = (h & hashmask(10)); |
239 | | In which case, the hash table should have hashsize(10) elements. |
240 | | |
241 | | If you are hashing n strings (uint8_t **)k, do it like this: |
242 | | for (i=0, h=0; i<n; ++i) h = hashlittle( k[i], len[i], h); |
243 | | |
244 | | By Bob Jenkins, 2006. bob_jenkins@burtleburtle.net. You may use this |
245 | | code any way you wish, private, educational, or commercial. It's free. |
246 | | |
247 | | Use for hash table lookup, or anything where one collision in 2^^32 is |
248 | | acceptable. Do NOT use for cryptographic purposes. |
249 | | ------------------------------------------------------------------------------- |
250 | | */ |
251 | | |
252 | | /* clang-format off */ |
253 | | static uint32_t hashlittle(const void *key, size_t length, uint32_t initval) |
254 | 50.3k | { |
255 | 50.3k | uint32_t a,b,c; /* internal state */ |
256 | 50.3k | union |
257 | 50.3k | { |
258 | 50.3k | const void *ptr; |
259 | 50.3k | size_t i; |
260 | 50.3k | } u; /* needed for Mac Powerbook G4 */ |
261 | | |
262 | | /* Set up the internal state */ |
263 | 50.3k | a = b = c = 0xdeadbeef + ((uint32_t)length) + initval; |
264 | | |
265 | 50.3k | u.ptr = key; |
266 | 50.3k | if (HASH_LITTLE_ENDIAN && ((u.i & 0x3) == 0)) { |
267 | 50.3k | const uint32_t *k = (const uint32_t *)key; /* read 32-bit chunks */ |
268 | | |
269 | | /*------ all but last block: aligned reads and affect 32 bits of (a,b,c) */ |
270 | 681k | while (length > 12) |
271 | 631k | { |
272 | 631k | a += k[0]; |
273 | 631k | b += k[1]; |
274 | 631k | c += k[2]; |
275 | 631k | mix(a,b,c); |
276 | 631k | length -= 12; |
277 | 631k | k += 3; |
278 | 631k | } |
279 | | |
280 | | /*----------------------------- handle the last (probably partial) block */ |
281 | | /* |
282 | | * "k[2]&0xffffff" actually reads beyond the end of the string, but |
283 | | * then masks off the part it's not allowed to read. Because the |
284 | | * string is aligned, the masked-off tail is in the same word as the |
285 | | * rest of the string. Every machine with memory protection I've seen |
286 | | * does it on word boundaries, so is OK with this. But VALGRIND will |
287 | | * still catch it and complain. The masking trick does make the hash |
288 | | * noticeably faster for short strings (like English words). |
289 | | * AddressSanitizer is similarly picky about overrunning |
290 | | * the buffer. (https://clang.llvm.org/docs/AddressSanitizer.html) |
291 | | */ |
292 | | #ifdef VALGRIND |
293 | | #define PRECISE_MEMORY_ACCESS 1 |
294 | | #elif defined(__SANITIZE_ADDRESS__) /* GCC's ASAN */ |
295 | | #define PRECISE_MEMORY_ACCESS 1 |
296 | | #elif defined(__has_feature) |
297 | | #if __has_feature(address_sanitizer) /* Clang's ASAN */ |
298 | | #define PRECISE_MEMORY_ACCESS 1 |
299 | | #endif |
300 | 50.3k | #endif |
301 | 50.3k | #ifndef PRECISE_MEMORY_ACCESS |
302 | | |
303 | 50.3k | switch(length) |
304 | 50.3k | { |
305 | 792 | case 12: c+=k[2]; b+=k[1]; a+=k[0]; break; |
306 | 2.16k | case 11: c+=k[2]&0xffffff; b+=k[1]; a+=k[0]; break; |
307 | 1.80k | case 10: c+=k[2]&0xffff; b+=k[1]; a+=k[0]; break; |
308 | 1.82k | case 9 : c+=k[2]&0xff; b+=k[1]; a+=k[0]; break; |
309 | 1.72k | case 8 : b+=k[1]; a+=k[0]; break; |
310 | 9.93k | case 7 : b+=k[1]&0xffffff; a+=k[0]; break; |
311 | 763 | case 6 : b+=k[1]&0xffff; a+=k[0]; break; |
312 | 1.04k | case 5 : b+=k[1]&0xff; a+=k[0]; break; |
313 | 1.79k | case 4 : a+=k[0]; break; |
314 | 1.53k | case 3 : a+=k[0]&0xffffff; break; |
315 | 3.99k | case 2 : a+=k[0]&0xffff; break; |
316 | 19.0k | case 1 : a+=k[0]&0xff; break; |
317 | 3.93k | case 0 : return c; /* zero length strings require no mixing */ |
318 | 50.3k | } |
319 | | |
320 | | #else /* make valgrind happy */ |
321 | | |
322 | | const uint8_t *k8 = (const uint8_t *)k; |
323 | | switch(length) |
324 | | { |
325 | | case 12: c+=k[2]; b+=k[1]; a+=k[0]; break; |
326 | | case 11: c+=((uint32_t)k8[10])<<16; /* fall through */ |
327 | | case 10: c+=((uint32_t)k8[9])<<8; /* fall through */ |
328 | | case 9 : c+=k8[8]; /* fall through */ |
329 | | case 8 : b+=k[1]; a+=k[0]; break; |
330 | | case 7 : b+=((uint32_t)k8[6])<<16; /* fall through */ |
331 | | case 6 : b+=((uint32_t)k8[5])<<8; /* fall through */ |
332 | | case 5 : b+=k8[4]; /* fall through */ |
333 | | case 4 : a+=k[0]; break; |
334 | | case 3 : a+=((uint32_t)k8[2])<<16; /* fall through */ |
335 | | case 2 : a+=((uint32_t)k8[1])<<8; /* fall through */ |
336 | | case 1 : a+=k8[0]; break; |
337 | | case 0 : return c; |
338 | | } |
339 | | |
340 | | #endif /* !valgrind */ |
341 | | |
342 | 50.3k | } |
343 | 0 | else if (HASH_LITTLE_ENDIAN && ((u.i & 0x1) == 0)) |
344 | 0 | { |
345 | 0 | const uint16_t *k = (const uint16_t *)key; /* read 16-bit chunks */ |
346 | 0 | const uint8_t *k8; |
347 | | |
348 | | /*--------------- all but last block: aligned reads and different mixing */ |
349 | 0 | while (length > 12) |
350 | 0 | { |
351 | 0 | a += k[0] + (((uint32_t)k[1])<<16); |
352 | 0 | b += k[2] + (((uint32_t)k[3])<<16); |
353 | 0 | c += k[4] + (((uint32_t)k[5])<<16); |
354 | 0 | mix(a,b,c); |
355 | 0 | length -= 12; |
356 | 0 | k += 6; |
357 | 0 | } |
358 | | |
359 | | /*----------------------------- handle the last (probably partial) block */ |
360 | 0 | k8 = (const uint8_t *)k; |
361 | 0 | switch(length) |
362 | 0 | { |
363 | 0 | case 12: c+=k[4]+(((uint32_t)k[5])<<16); |
364 | 0 | b+=k[2]+(((uint32_t)k[3])<<16); |
365 | 0 | a+=k[0]+(((uint32_t)k[1])<<16); |
366 | 0 | break; |
367 | 0 | case 11: c+=((uint32_t)k8[10])<<16; /* fall through */ |
368 | 0 | case 10: c+=k[4]; |
369 | 0 | b+=k[2]+(((uint32_t)k[3])<<16); |
370 | 0 | a+=k[0]+(((uint32_t)k[1])<<16); |
371 | 0 | break; |
372 | 0 | case 9 : c+=k8[8]; /* fall through */ |
373 | 0 | case 8 : b+=k[2]+(((uint32_t)k[3])<<16); |
374 | 0 | a+=k[0]+(((uint32_t)k[1])<<16); |
375 | 0 | break; |
376 | 0 | case 7 : b+=((uint32_t)k8[6])<<16; /* fall through */ |
377 | 0 | case 6 : b+=k[2]; |
378 | 0 | a+=k[0]+(((uint32_t)k[1])<<16); |
379 | 0 | break; |
380 | 0 | case 5 : b+=k8[4]; /* fall through */ |
381 | 0 | case 4 : a+=k[0]+(((uint32_t)k[1])<<16); |
382 | 0 | break; |
383 | 0 | case 3 : a+=((uint32_t)k8[2])<<16; /* fall through */ |
384 | 0 | case 2 : a+=k[0]; |
385 | 0 | break; |
386 | 0 | case 1 : a+=k8[0]; |
387 | 0 | break; |
388 | 0 | case 0 : return c; /* zero length requires no mixing */ |
389 | 0 | } |
390 | |
|
391 | 0 | } |
392 | 0 | else |
393 | 0 | { |
394 | | /* need to read the key one byte at a time */ |
395 | 0 | const uint8_t *k = (const uint8_t *)key; |
396 | | |
397 | | /*--------------- all but the last block: affect some 32 bits of (a,b,c) */ |
398 | 0 | while (length > 12) |
399 | 0 | { |
400 | 0 | a += k[0]; |
401 | 0 | a += ((uint32_t)k[1])<<8; |
402 | 0 | a += ((uint32_t)k[2])<<16; |
403 | 0 | a += ((uint32_t)k[3])<<24; |
404 | 0 | b += k[4]; |
405 | 0 | b += ((uint32_t)k[5])<<8; |
406 | 0 | b += ((uint32_t)k[6])<<16; |
407 | 0 | b += ((uint32_t)k[7])<<24; |
408 | 0 | c += k[8]; |
409 | 0 | c += ((uint32_t)k[9])<<8; |
410 | 0 | c += ((uint32_t)k[10])<<16; |
411 | 0 | c += ((uint32_t)k[11])<<24; |
412 | 0 | mix(a,b,c); |
413 | 0 | length -= 12; |
414 | 0 | k += 12; |
415 | 0 | } |
416 | | |
417 | | /*-------------------------------- last block: affect all 32 bits of (c) */ |
418 | 0 | switch(length) /* all the case statements fall through */ |
419 | 0 | { |
420 | 0 | case 12: c+=((uint32_t)k[11])<<24; /* FALLTHRU */ |
421 | 0 | case 11: c+=((uint32_t)k[10])<<16; /* FALLTHRU */ |
422 | 0 | case 10: c+=((uint32_t)k[9])<<8; /* FALLTHRU */ |
423 | 0 | case 9 : c+=k[8]; /* FALLTHRU */ |
424 | 0 | case 8 : b+=((uint32_t)k[7])<<24; /* FALLTHRU */ |
425 | 0 | case 7 : b+=((uint32_t)k[6])<<16; /* FALLTHRU */ |
426 | 0 | case 6 : b+=((uint32_t)k[5])<<8; /* FALLTHRU */ |
427 | 0 | case 5 : b+=k[4]; /* FALLTHRU */ |
428 | 0 | case 4 : a+=((uint32_t)k[3])<<24; /* FALLTHRU */ |
429 | 0 | case 3 : a+=((uint32_t)k[2])<<16; /* FALLTHRU */ |
430 | 0 | case 2 : a+=((uint32_t)k[1])<<8; /* FALLTHRU */ |
431 | 0 | case 1 : a+=k[0]; |
432 | 0 | break; |
433 | 0 | case 0 : return c; |
434 | 0 | } |
435 | 0 | } |
436 | | |
437 | 46.4k | final(a,b,c); |
438 | 46.4k | return c; |
439 | 50.3k | } |
440 | | /* clang-format on */ |
441 | | |
442 | | /* a simple hash function similar to what perl does for strings. |
443 | | * for good results, the string should not be excessively large. |
444 | | */ |
445 | | static unsigned long lh_perllike_str_hash(const void *k) |
446 | 0 | { |
447 | 0 | const char *rkey = (const char *)k; |
448 | 0 | unsigned hashval = 1; |
449 | |
|
450 | 0 | while (*rkey) |
451 | 0 | hashval = hashval * 33 + *rkey++; |
452 | |
|
453 | 0 | return hashval; |
454 | 0 | } |
455 | | |
456 | | static unsigned long lh_char_hash(const void *k) |
457 | 50.3k | { |
458 | | #if defined _MSC_VER || defined __MINGW32__ |
459 | | #define RANDOM_SEED_TYPE LONG |
460 | | #else |
461 | 50.3k | #define RANDOM_SEED_TYPE int |
462 | 50.3k | #endif |
463 | 50.3k | static volatile RANDOM_SEED_TYPE random_seed = -1; |
464 | | |
465 | 50.3k | if (random_seed == -1) |
466 | 1 | { |
467 | 1 | RANDOM_SEED_TYPE seed; |
468 | | /* we can't use -1 as it is the uninitialized sentinel */ |
469 | 1 | while ((seed = json_c_get_random_seed()) == -1) {} |
470 | | #if SIZEOF_INT == 8 && defined __GCC_HAVE_SYNC_COMPARE_AND_SWAP_8 |
471 | | #define USE_SYNC_COMPARE_AND_SWAP 1 |
472 | | #endif |
473 | 1 | #if SIZEOF_INT == 4 && defined __GCC_HAVE_SYNC_COMPARE_AND_SWAP_4 |
474 | 1 | #define USE_SYNC_COMPARE_AND_SWAP 1 |
475 | 1 | #endif |
476 | | #if SIZEOF_INT == 2 && defined __GCC_HAVE_SYNC_COMPARE_AND_SWAP_2 |
477 | | #define USE_SYNC_COMPARE_AND_SWAP 1 |
478 | | #endif |
479 | 1 | #if defined USE_SYNC_COMPARE_AND_SWAP |
480 | 1 | (void)__sync_val_compare_and_swap(&random_seed, -1, seed); |
481 | | #elif defined _MSC_VER || defined __MINGW32__ |
482 | | InterlockedCompareExchange(&random_seed, seed, -1); |
483 | | #else |
484 | | //#warning "racy random seed initialization if used by multiple threads" |
485 | | random_seed = seed; /* potentially racy */ |
486 | | #endif |
487 | 1 | } |
488 | | |
489 | 50.3k | return hashlittle((const char *)k, strlen((const char *)k), (uint32_t)random_seed); |
490 | 50.3k | } |
491 | | |
492 | | int lh_char_equal(const void *k1, const void *k2) |
493 | 34.6k | { |
494 | 34.6k | return (strcmp((const char *)k1, (const char *)k2) == 0); |
495 | 34.6k | } |
496 | | |
497 | | struct lh_table *lh_table_new(int size, lh_entry_free_fn *free_fn, lh_hash_fn *hash_fn, |
498 | | lh_equal_fn *equal_fn) |
499 | 5.15k | { |
500 | 5.15k | int i; |
501 | 5.15k | struct lh_table *t; |
502 | | |
503 | | /* Allocate space for elements to avoid divisions by zero. */ |
504 | 5.15k | assert(size > 0); |
505 | 5.15k | t = (struct lh_table *)calloc(1, sizeof(struct lh_table)); |
506 | 5.15k | if (!t) |
507 | 0 | return NULL; |
508 | | |
509 | 5.15k | t->count = 0; |
510 | 5.15k | t->size = size; |
511 | 5.15k | t->table = (struct lh_entry *)calloc(size, sizeof(struct lh_entry)); |
512 | 5.15k | if (!t->table) |
513 | 0 | { |
514 | 0 | free(t); |
515 | 0 | return NULL; |
516 | 0 | } |
517 | 5.15k | t->free_fn = free_fn; |
518 | 5.15k | t->hash_fn = hash_fn; |
519 | 5.15k | t->equal_fn = equal_fn; |
520 | 131k | for (i = 0; i < size; i++) |
521 | 125k | t->table[i].k = LH_EMPTY; |
522 | 5.15k | return t; |
523 | 5.15k | } |
524 | | |
525 | | struct lh_table *lh_kchar_table_new(int size, lh_entry_free_fn *free_fn) |
526 | 3.80k | { |
527 | 3.80k | return lh_table_new(size, free_fn, char_hash_fn, lh_char_equal); |
528 | 3.80k | } |
529 | | |
530 | | struct lh_table *lh_kptr_table_new(int size, lh_entry_free_fn *free_fn) |
531 | 0 | { |
532 | 0 | return lh_table_new(size, free_fn, lh_ptr_hash, lh_ptr_equal); |
533 | 0 | } |
534 | | |
535 | | int lh_table_resize(struct lh_table *t, int new_size) |
536 | 1.35k | { |
537 | 1.35k | struct lh_table *new_t; |
538 | 1.35k | struct lh_entry *ent; |
539 | | |
540 | 1.35k | new_t = lh_table_new(new_size, NULL, t->hash_fn, t->equal_fn); |
541 | 1.35k | if (new_t == NULL) |
542 | 0 | return -1; |
543 | | |
544 | 23.6k | for (ent = t->head; ent != NULL; ent = ent->next) |
545 | 22.2k | { |
546 | 22.2k | unsigned long h = lh_get_hash(new_t, ent->k); |
547 | 22.2k | unsigned int opts = 0; |
548 | 22.2k | if (ent->k_is_constant) |
549 | 0 | opts = JSON_C_OBJECT_ADD_CONSTANT_KEY; |
550 | 22.2k | if (lh_table_insert_w_hash(new_t, ent->k, ent->v, h, opts) != 0) |
551 | 0 | { |
552 | 0 | lh_table_free(new_t); |
553 | 0 | return -1; |
554 | 0 | } |
555 | 22.2k | } |
556 | 1.35k | free(t->table); |
557 | 1.35k | t->table = new_t->table; |
558 | 1.35k | t->size = new_size; |
559 | 1.35k | t->head = new_t->head; |
560 | 1.35k | t->tail = new_t->tail; |
561 | 1.35k | free(new_t); |
562 | | |
563 | 1.35k | return 0; |
564 | 1.35k | } |
565 | | |
566 | | void lh_table_free(struct lh_table *t) |
567 | 3.80k | { |
568 | 3.80k | struct lh_entry *c; |
569 | 3.80k | if (t->free_fn) |
570 | 3.80k | { |
571 | 25.2k | for (c = t->head; c != NULL; c = c->next) |
572 | 21.4k | t->free_fn(c); |
573 | 3.80k | } |
574 | 3.80k | free(t->table); |
575 | 3.80k | free(t); |
576 | 3.80k | } |
577 | | |
578 | | int lh_table_insert_w_hash(struct lh_table *t, const void *k, const void *v, const unsigned long h, |
579 | | const unsigned opts) |
580 | 43.6k | { |
581 | 43.6k | unsigned long n; |
582 | | |
583 | 43.6k | if (t->count >= t->size * LH_LOAD_FACTOR) |
584 | 1.35k | { |
585 | | /* Avoid signed integer overflow with large tables. */ |
586 | 1.35k | int new_size = (t->size > INT_MAX / 2) ? INT_MAX : (t->size * 2); |
587 | 1.35k | if (t->size == INT_MAX || lh_table_resize(t, new_size) != 0) |
588 | 0 | return -1; |
589 | 1.35k | } |
590 | | |
591 | 43.6k | n = h % t->size; |
592 | | |
593 | 71.4k | while (1) |
594 | 71.4k | { |
595 | 71.4k | if (t->table[n].k == LH_EMPTY || t->table[n].k == LH_FREED) |
596 | 43.6k | break; |
597 | 27.7k | if ((int)++n == t->size) |
598 | 374 | n = 0; |
599 | 27.7k | } |
600 | | |
601 | 43.6k | t->table[n].k = k; |
602 | 43.6k | t->table[n].k_is_constant = (opts & JSON_C_OBJECT_ADD_CONSTANT_KEY); |
603 | 43.6k | t->table[n].v = v; |
604 | 43.6k | t->count++; |
605 | | |
606 | 43.6k | if (t->head == NULL) |
607 | 3.34k | { |
608 | 3.34k | t->head = t->tail = &t->table[n]; |
609 | 3.34k | t->table[n].next = t->table[n].prev = NULL; |
610 | 3.34k | } |
611 | 40.3k | else |
612 | 40.3k | { |
613 | 40.3k | t->tail->next = &t->table[n]; |
614 | 40.3k | t->table[n].prev = t->tail; |
615 | 40.3k | t->table[n].next = NULL; |
616 | 40.3k | t->tail = &t->table[n]; |
617 | 40.3k | } |
618 | | |
619 | 43.6k | return 0; |
620 | 43.6k | } |
621 | | int lh_table_insert(struct lh_table *t, const void *k, const void *v) |
622 | 0 | { |
623 | 0 | return lh_table_insert_w_hash(t, k, v, lh_get_hash(t, k), 0); |
624 | 0 | } |
625 | | |
626 | | struct lh_entry *lh_table_lookup_entry_w_hash(struct lh_table *t, const void *k, |
627 | | const unsigned long h) |
628 | 28.0k | { |
629 | 28.0k | unsigned long n = h % t->size; |
630 | 28.0k | int count = 0; |
631 | | |
632 | 56.0k | while (count < t->size) |
633 | 56.0k | { |
634 | 56.0k | if (t->table[n].k == LH_EMPTY) |
635 | 21.4k | return NULL; |
636 | 34.6k | if (t->table[n].k != LH_FREED && t->equal_fn(t->table[n].k, k)) |
637 | 6.64k | return &t->table[n]; |
638 | 28.0k | if ((int)++n == t->size) |
639 | 408 | n = 0; |
640 | 28.0k | count++; |
641 | 28.0k | } |
642 | 0 | return NULL; |
643 | 28.0k | } |
644 | | |
645 | | struct lh_entry *lh_table_lookup_entry(struct lh_table *t, const void *k) |
646 | 0 | { |
647 | 0 | return lh_table_lookup_entry_w_hash(t, k, lh_get_hash(t, k)); |
648 | 0 | } |
649 | | |
650 | | json_bool lh_table_lookup_ex(struct lh_table *t, const void *k, void **v) |
651 | 0 | { |
652 | 0 | struct lh_entry *e = lh_table_lookup_entry(t, k); |
653 | 0 | if (e != NULL) |
654 | 0 | { |
655 | 0 | if (v != NULL) |
656 | 0 | *v = lh_entry_v(e); |
657 | 0 | return 1; /* key found */ |
658 | 0 | } |
659 | 0 | if (v != NULL) |
660 | 0 | *v = NULL; |
661 | 0 | return 0; /* key not found */ |
662 | 0 | } |
663 | | |
664 | | int lh_table_delete_entry(struct lh_table *t, struct lh_entry *e) |
665 | 0 | { |
666 | | /* CAW: fixed to be 64bit nice, still need the crazy negative case... */ |
667 | 0 | ptrdiff_t n = (ptrdiff_t)(e - t->table); |
668 | | |
669 | | /* CAW: this is bad, really bad, maybe stack goes other direction on this machine... */ |
670 | 0 | if (n < 0) |
671 | 0 | { |
672 | 0 | return -2; |
673 | 0 | } |
674 | | |
675 | 0 | if (t->table[n].k == LH_EMPTY || t->table[n].k == LH_FREED) |
676 | 0 | return -1; |
677 | 0 | t->count--; |
678 | 0 | if (t->free_fn) |
679 | 0 | t->free_fn(e); |
680 | 0 | t->table[n].v = NULL; |
681 | 0 | t->table[n].k = LH_FREED; |
682 | 0 | if (t->tail == &t->table[n] && t->head == &t->table[n]) |
683 | 0 | { |
684 | 0 | t->head = t->tail = NULL; |
685 | 0 | } |
686 | 0 | else if (t->head == &t->table[n]) |
687 | 0 | { |
688 | 0 | t->head->next->prev = NULL; |
689 | 0 | t->head = t->head->next; |
690 | 0 | } |
691 | 0 | else if (t->tail == &t->table[n]) |
692 | 0 | { |
693 | 0 | t->tail->prev->next = NULL; |
694 | 0 | t->tail = t->tail->prev; |
695 | 0 | } |
696 | 0 | else |
697 | 0 | { |
698 | 0 | t->table[n].prev->next = t->table[n].next; |
699 | 0 | t->table[n].next->prev = t->table[n].prev; |
700 | 0 | } |
701 | 0 | t->table[n].next = t->table[n].prev = NULL; |
702 | 0 | return 0; |
703 | 0 | } |
704 | | |
705 | | int lh_table_delete(struct lh_table *t, const void *k) |
706 | 0 | { |
707 | 0 | struct lh_entry *e = lh_table_lookup_entry(t, k); |
708 | 0 | if (!e) |
709 | 0 | return -1; |
710 | 0 | return lh_table_delete_entry(t, e); |
711 | 0 | } |
712 | | |
713 | | int lh_table_length(struct lh_table *t) |
714 | 0 | { |
715 | 0 | return t->count; |
716 | 0 | } |