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 19 kB

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