Coverage Report

Created: 2024-05-21 06:33

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