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

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