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

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