You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

linkhash.c 21 kB

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