Coverage Report

Created: 2026-08-30 07:14

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