LCOV - code coverage report
Current view: top level - ogr/ogrsf_frmts/geojson/libjson - linkhash.c (source / functions) Hit Total Coverage
Test: gdal_filtered.info Lines: 214 246 87.0 %
Date: 2024-05-06 16:27:07 Functions: 14 20 70.0 %

          Line data    Source code
       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 "cpl_port.h"
      16             : 
      17             : #include <assert.h>
      18             : #include <limits.h>
      19             : #include <stdarg.h>
      20             : #include <stddef.h>
      21             : #include <stdio.h>
      22             : #include <stdlib.h>
      23             : #include <string.h>
      24             : 
      25             : #ifdef HAVE_ENDIAN_H
      26             : #include <endian.h> /* attempt to define endianness */
      27             : #endif
      28             : 
      29             : #if defined(_MSC_VER) || defined(__MINGW32__)
      30             : #define WIN32_LEAN_AND_MEAN
      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           0 : int json_global_set_string_hash(const int h)
      47             : {
      48           0 :   switch (h)
      49             :   {
      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             :   }
      54           0 :   return 0;
      55             : }
      56             : 
      57           0 : static unsigned long lh_ptr_hash(const void *k)
      58             : {
      59             :   /* CAW: refactored to be 64bit nice */
      60           0 :   return (unsigned long)((((ptrdiff_t)k * LH_PRIME) >> 4) & ULONG_MAX);
      61             : }
      62             : 
      63           0 : int lh_ptr_equal(const void *k1, const void *k2)
      64             : {
      65           0 :   return (k1 == k2);
      66             : }
      67             : 
      68             : /*
      69             :  * hashlittle from lookup3.c, by Bob Jenkins, May 2006, Public Domain.
      70             :  * http://burtleburtle.net/bob/c/lookup3.c
      71             :  * minor modifications to make functions static so no symbols are exported
      72             :  * minor mofifications 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 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             : #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             : #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 http://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             : #define mix(a,b,c) \
     179             : { \
     180             :   a -= c;  a ^= rot(c, 4);  c += b; \
     181             :   b -= a;  b ^= rot(a, 6);  a += c; \
     182             :   c -= b;  c ^= rot(b, 8);  b += a; \
     183             :   a -= c;  a ^= rot(c,16);  c += b; \
     184             :   b -= a;  b ^= rot(a,19);  a += c; \
     185             :   c -= b;  c ^= rot(b, 4);  b += a; \
     186             : }
     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             : #define final(a,b,c) \
     216             : { \
     217             :   c ^= b; c -= rot(b,14); \
     218             :   a ^= c; a -= rot(c,11); \
     219             :   b ^= a; b -= rot(a,25); \
     220             :   c ^= b; c -= rot(b,16); \
     221             :   a ^= c; a -= rot(c,4);  \
     222             :   b ^= a; b -= rot(a,14); \
     223             :   c ^= b; c -= rot(b,24); \
     224             : }
     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             : 
     256             : CPL_NOSANITIZE_UNSIGNED_INT_OVERFLOW
     257     2396530 : static uint32_t hashlittle(const void *key, size_t length, uint32_t initval)
     258             : {
     259             :   uint32_t a,b,c; /* internal state */
     260             :   union
     261             :   {
     262             :     const void *ptr;
     263             :     size_t i;
     264             :   } u; /* needed for Mac Powerbook G4 */
     265             : 
     266             :   /* Set up the internal state */
     267     2396530 :   a = b = c = 0xdeadbeef + ((uint32_t)length) + initval;
     268             : 
     269     2396530 :   u.ptr = key;
     270     2396530 :   if (HASH_LITTLE_ENDIAN && ((u.i & 0x3) == 0)) {
     271     2360550 :     const uint32_t *k = (const uint32_t *)key; /* read 32-bit chunks */
     272             : 
     273             :     /*------ all but last block: aligned reads and affect 32 bits of (a,b,c) */
     274     2494740 :     while (length > 12)
     275             :     {
     276      134189 :       a += k[0];
     277      134189 :       b += k[1];
     278      134189 :       c += k[2];
     279      134189 :       mix(a,b,c);
     280      134189 :       length -= 12;
     281      134189 :       k += 3;
     282             :     }
     283             : 
     284             :     /*----------------------------- handle the last (probably partial) block */
     285             :     /*
     286             :      * "k[2]&0xffffff" actually reads beyond the end of the string, but
     287             :      * then masks off the part it's not allowed to read.  Because the
     288             :      * string is aligned, the masked-off tail is in the same word as the
     289             :      * rest of the string.  Every machine with memory protection I've seen
     290             :      * does it on word boundaries, so is OK with this.  But VALGRIND will
     291             :      * still catch it and complain.  The masking trick does make the hash
     292             :      * noticeably faster for short strings (like English words).
     293             :      * AddressSanitizer is similarly picky about overrunning
     294             :      * the buffer. (http://clang.llvm.org/docs/AddressSanitizer.html
     295             :      */
     296             : #ifdef VALGRIND
     297             : #define PRECISE_MEMORY_ACCESS 1
     298             : #elif defined(__SANITIZE_ADDRESS__) /* GCC's ASAN */
     299             : #define PRECISE_MEMORY_ACCESS 1
     300             : #elif defined(__SANITIZE_HWADDRESS__) /* GCC's HWASAN */
     301             : #define PRECISE_MEMORY_ACCESS 1
     302             : #elif defined(__has_feature)
     303             : #if __has_feature(address_sanitizer) /* Clang's ASAN */
     304             : #define PRECISE_MEMORY_ACCESS 1
     305             : #elif __has_feature(hwaddress_sanitizer) /* Clang's HWASAN */
     306             : #define PRECISE_MEMORY_ACCESS 1
     307             : #endif
     308             : #endif
     309             : #ifndef PRECISE_MEMORY_ACCESS
     310             : 
     311     2360550 :     switch(length)
     312             :     {
     313       32388 :     case 12: c+=k[2]; b+=k[1]; a+=k[0]; break;
     314       45120 :     case 11: c+=k[2]&0xffffff; b+=k[1]; a+=k[0]; break;
     315       74568 :     case 10: c+=k[2]&0xffff; b+=k[1]; a+=k[0]; break;
     316      489361 :     case 9 : c+=k[2]&0xff; b+=k[1]; a+=k[0]; break;
     317       52975 :     case 8 : b+=k[1]; a+=k[0]; break;
     318       60139 :     case 7 : b+=k[1]&0xffffff; a+=k[0]; break;
     319       59055 :     case 6 : b+=k[1]&0xffff; a+=k[0]; break;
     320      365830 :     case 5 : b+=k[1]&0xff; a+=k[0]; break;
     321      641404 :     case 4 : a+=k[0]; break;
     322      424445 :     case 3 : a+=k[0]&0xffffff; break;
     323       36700 :     case 2 : a+=k[0]&0xffff; break;
     324       78430 :     case 1 : a+=k[0]&0xff; break;
     325         136 :     case 0 : return c; /* zero length strings require no mixing */
     326             :     }
     327             : 
     328             : #else /* make valgrind happy */
     329             : 
     330             :     const uint8_t  *k8 = (const uint8_t *)k;
     331             :     switch(length)
     332             :     {
     333             :     case 12: c+=k[2]; b+=k[1]; a+=k[0]; break;
     334             :     case 11: c+=((uint32_t)k8[10])<<16;  /* fall through */
     335             :     case 10: c+=((uint32_t)k8[9])<<8;    /* fall through */
     336             :     case 9 : c+=k8[8];                   /* fall through */
     337             :     case 8 : b+=k[1]; a+=k[0]; break;
     338             :     case 7 : b+=((uint32_t)k8[6])<<16;   /* fall through */
     339             :     case 6 : b+=((uint32_t)k8[5])<<8;    /* fall through */
     340             :     case 5 : b+=k8[4];                   /* fall through */
     341             :     case 4 : a+=k[0]; break;
     342             :     case 3 : a+=((uint32_t)k8[2])<<16;   /* fall through */
     343             :     case 2 : a+=((uint32_t)k8[1])<<8;    /* fall through */
     344             :     case 1 : a+=k8[0]; break;
     345             :     case 0 : return c;
     346             :     }
     347             : 
     348             : #endif /* !valgrind */
     349             : 
     350     2360420 :   }
     351       35978 :   else if (HASH_LITTLE_ENDIAN && ((u.i & 0x1) == 0))
     352             :   {
     353        6817 :     const uint16_t *k = (const uint16_t *)key; /* read 16-bit chunks */
     354             :     const uint8_t  *k8;
     355             : 
     356             :     /*--------------- all but last block: aligned reads and different mixing */
     357        7073 :     while (length > 12)
     358             :     {
     359         256 :       a += k[0] + (((uint32_t)k[1])<<16);
     360         256 :       b += k[2] + (((uint32_t)k[3])<<16);
     361         256 :       c += k[4] + (((uint32_t)k[5])<<16);
     362         256 :       mix(a,b,c);
     363         256 :       length -= 12;
     364         256 :       k += 6;
     365             :     }
     366             : 
     367             :     /*----------------------------- handle the last (probably partial) block */
     368        6817 :     k8 = (const uint8_t *)k;
     369        6817 :     switch(length)
     370             :     {
     371          49 :     case 12: c+=k[4]+(((uint32_t)k[5])<<16);
     372          49 :        b+=k[2]+(((uint32_t)k[3])<<16);
     373          49 :        a+=k[0]+(((uint32_t)k[1])<<16);
     374          49 :        break;
     375        2613 :     case 11: c+=((uint32_t)k8[10])<<16;     /* fall through */
     376        3149 :     case 10: c+=k[4];
     377        3149 :        b+=k[2]+(((uint32_t)k[3])<<16);
     378        3149 :        a+=k[0]+(((uint32_t)k[1])<<16);
     379        3149 :        break;
     380          14 :     case 9 : c+=k8[8];                      /* fall through */
     381        1863 :     case 8 : b+=k[2]+(((uint32_t)k[3])<<16);
     382        1863 :        a+=k[0]+(((uint32_t)k[1])<<16);
     383        1863 :        break;
     384         240 :     case 7 : b+=((uint32_t)k8[6])<<16;      /* fall through */
     385         430 :     case 6 : b+=k[2];
     386         430 :        a+=k[0]+(((uint32_t)k[1])<<16);
     387         430 :        break;
     388         249 :     case 5 : b+=k8[4];                      /* fall through */
     389        1019 :     case 4 : a+=k[0]+(((uint32_t)k[1])<<16);
     390        1019 :        break;
     391         181 :     case 3 : a+=((uint32_t)k8[2])<<16;      /* fall through */
     392         214 :     case 2 : a+=k[0];
     393         214 :        break;
     394          42 :     case 1 : a+=k8[0];
     395          42 :        break;
     396          51 :     case 0 : return c;                     /* zero length requires no mixing */
     397             :     }
     398             : 
     399        6766 :   }
     400             :   else
     401             :   {
     402             :     /* need to read the key one byte at a time */
     403       29161 :     const uint8_t *k = (const uint8_t *)key;
     404             : 
     405             :     /*--------------- all but the last block: affect some 32 bits of (a,b,c) */
     406       30531 :     while (length > 12)
     407             :     {
     408        1370 :       a += k[0];
     409        1370 :       a += ((uint32_t)k[1])<<8;
     410        1370 :       a += ((uint32_t)k[2])<<16;
     411        1370 :       a += ((uint32_t)k[3])<<24;
     412        1370 :       b += k[4];
     413        1370 :       b += ((uint32_t)k[5])<<8;
     414        1370 :       b += ((uint32_t)k[6])<<16;
     415        1370 :       b += ((uint32_t)k[7])<<24;
     416        1370 :       c += k[8];
     417        1370 :       c += ((uint32_t)k[9])<<8;
     418        1370 :       c += ((uint32_t)k[10])<<16;
     419        1370 :       c += ((uint32_t)k[11])<<24;
     420        1370 :       mix(a,b,c);
     421        1370 :       length -= 12;
     422        1370 :       k += 12;
     423             :     }
     424             : 
     425             :     /*-------------------------------- last block: affect all 32 bits of (c) */
     426       29161 :     switch(length) /* all the case statements fall through */
     427             :     {
     428         123 :     case 12: c+=((uint32_t)k[11])<<24; /* FALLTHRU */
     429        4125 :     case 11: c+=((uint32_t)k[10])<<16; /* FALLTHRU */
     430        7030 :     case 10: c+=((uint32_t)k[9])<<8; /* FALLTHRU */
     431        7762 :     case 9 : c+=k[8]; /* FALLTHRU */
     432       11211 :     case 8 : b+=((uint32_t)k[7])<<24; /* FALLTHRU */
     433       11255 :     case 7 : b+=((uint32_t)k[6])<<16; /* FALLTHRU */
     434       11498 :     case 6 : b+=((uint32_t)k[5])<<8; /* FALLTHRU */
     435       13051 :     case 5 : b+=k[4]; /* FALLTHRU */
     436       21532 :     case 4 : a+=((uint32_t)k[3])<<24; /* FALLTHRU */
     437       21680 :     case 3 : a+=((uint32_t)k[2])<<16; /* FALLTHRU */
     438       28858 :     case 2 : a+=((uint32_t)k[1])<<8; /* FALLTHRU */
     439       29161 :     case 1 : a+=k[0];
     440       29161 :        break;
     441           0 :     case 0 : return c;
     442             :     }
     443     2396340 :   }
     444             : 
     445     2396340 :   final(a,b,c);
     446     2396340 :   return c;
     447             : }
     448             : /* clang-format on */
     449             : 
     450             : /* a simple hash function similar to what perl does for strings.
     451             :  * for good results, the string should not be excessivly large.
     452             :  */
     453           0 : static unsigned long lh_perllike_str_hash(const void *k)
     454             : {
     455           0 :   const char *rkey = (const char *)k;
     456           0 :   unsigned hashval = 1;
     457             : 
     458           0 :   while (*rkey)
     459           0 :     hashval = hashval * 33 + *rkey++;
     460             : 
     461           0 :   return hashval;
     462             : }
     463             : 
     464     2396530 : static unsigned long lh_char_hash(const void *k)
     465             : {
     466             : #if defined _MSC_VER || defined __MINGW32__
     467             : #define RANDOM_SEED_TYPE LONG
     468             : #else
     469             : #define RANDOM_SEED_TYPE int
     470             : #endif
     471             :   static volatile RANDOM_SEED_TYPE random_seed = -1;
     472             : 
     473     2396530 :   if (random_seed == -1)
     474             :   {
     475             :     RANDOM_SEED_TYPE seed;
     476             :     /* we can't use -1 as it is the uninitialized sentinel */
     477         136 :     while ((seed = json_c_get_random_seed()) == -1) {}
     478             : #if SIZEOF_INT == 8 && defined __GCC_HAVE_SYNC_COMPARE_AND_SWAP_8
     479             : #define USE_SYNC_COMPARE_AND_SWAP 1
     480             : #endif
     481             : #if SIZEOF_INT == 4 && defined __GCC_HAVE_SYNC_COMPARE_AND_SWAP_4
     482             : #define USE_SYNC_COMPARE_AND_SWAP 1
     483             : #endif
     484             : #if SIZEOF_INT == 2 && defined __GCC_HAVE_SYNC_COMPARE_AND_SWAP_2
     485             : #define USE_SYNC_COMPARE_AND_SWAP 1
     486             : #endif
     487             : #if defined USE_SYNC_COMPARE_AND_SWAP
     488         136 :     (void)__sync_val_compare_and_swap(&random_seed, -1, seed);
     489             : #elif defined _MSC_VER || defined __MINGW32__
     490             :     InterlockedCompareExchange(&random_seed, seed, -1);
     491             : #else
     492             :     //#warning "racy random seed initializtion if used by multiple threads"
     493             :     random_seed = seed; /* potentially racy */
     494             : #endif
     495             :   }
     496             : 
     497     2396530 :   return hashlittle((const char *)k, strlen((const char *)k), random_seed);
     498             : }
     499             : 
     500     1035050 : int lh_char_equal(const void *k1, const void *k2)
     501             : {
     502     1035050 :   return (strcmp((const char *)k1, (const char *)k2) == 0);
     503             : }
     504             : 
     505      443559 : struct lh_table *lh_table_new(int size, lh_entry_free_fn *free_fn, lh_hash_fn *hash_fn,
     506             :                               lh_equal_fn *equal_fn)
     507             : {
     508             :   int i;
     509             :   struct lh_table *t;
     510             : 
     511             :   /* Allocate space for elements to avoid divisions by zero. */
     512      443559 :   assert(size > 0);
     513      443559 :   t = (struct lh_table *)calloc(1, sizeof(struct lh_table));
     514      443559 :   if (!t)
     515           0 :     return NULL;
     516             : 
     517      443559 :   t->count = 0;
     518      443559 :   t->size = size;
     519      443559 :   t->table = (struct lh_entry *)calloc(size, sizeof(struct lh_entry));
     520      443559 :   if (!t->table)
     521             :   {
     522           0 :     free(t);
     523           0 :     return NULL;
     524             :   }
     525      443559 :   t->free_fn = free_fn;
     526      443559 :   t->hash_fn = hash_fn;
     527      443559 :   t->equal_fn = equal_fn;
     528     7627830 :   for (i = 0; i < size; i++)
     529     7184270 :     t->table[i].k = LH_EMPTY;
     530      443559 :   return t;
     531             : }
     532             : 
     533      441413 : struct lh_table *lh_kchar_table_new(int size, lh_entry_free_fn *free_fn)
     534             : {
     535      441413 :   return lh_table_new(size, free_fn, char_hash_fn, lh_char_equal);
     536             : }
     537             : 
     538           0 : struct lh_table *lh_kptr_table_new(int size, lh_entry_free_fn *free_fn)
     539             : {
     540           0 :   return lh_table_new(size, free_fn, lh_ptr_hash, lh_ptr_equal);
     541             : }
     542             : 
     543        2146 : int lh_table_resize(struct lh_table *t, int new_size)
     544             : {
     545             :   struct lh_table *new_t;
     546             :   struct lh_entry *ent;
     547             : 
     548        2146 :   new_t = lh_table_new(new_size, NULL, t->hash_fn, t->equal_fn);
     549        2146 :   if (new_t == NULL)
     550           0 :     return -1;
     551             : 
     552       43595 :   for (ent = t->head; ent != NULL; ent = ent->next)
     553             :   {
     554       41449 :     unsigned long h = lh_get_hash(new_t, ent->k);
     555       41449 :     unsigned int opts = 0;
     556       41449 :     if (ent->k_is_constant)
     557           0 :       opts = JSON_C_OBJECT_KEY_IS_CONSTANT;
     558       41449 :     if (lh_table_insert_w_hash(new_t, ent->k, ent->v, h, opts) != 0)
     559             :     {
     560           0 :       lh_table_free(new_t);
     561           0 :       return -1;
     562             :     }
     563             :   }
     564        2146 :   free(t->table);
     565        2146 :   t->table = new_t->table;
     566        2146 :   t->size = new_size;
     567        2146 :   t->head = new_t->head;
     568        2146 :   t->tail = new_t->tail;
     569        2146 :   free(new_t);
     570             : 
     571        2146 :   return 0;
     572             : }
     573             : 
     574      441413 : void lh_table_free(struct lh_table *t)
     575             : {
     576             :   struct lh_entry *c;
     577      441413 :   if (t->free_fn)
     578             :   {
     579     1840930 :     for (c = t->head; c != NULL; c = c->next)
     580     1399520 :       t->free_fn(c);
     581             :   }
     582      441413 :   free(t->table);
     583      441413 :   free(t);
     584      441413 : }
     585             : 
     586     1442210 : int lh_table_insert_w_hash(struct lh_table *t, const void *k, const void *v, const unsigned long h,
     587             :                            const unsigned opts)
     588             : {
     589             :   unsigned long n;
     590             : 
     591     1442210 :   if (t->count >= t->size * LH_LOAD_FACTOR)
     592             :   {
     593             :     /* Avoid signed integer overflow with large tables. */
     594        2146 :     int new_size = (t->size > INT_MAX / 2) ? INT_MAX : (t->size * 2);
     595        2146 :     if (t->size == INT_MAX || lh_table_resize(t, new_size) != 0)
     596           0 :       return -1;
     597             :   }
     598             : 
     599     1442210 :   n = h % t->size;
     600             : 
     601             :   while (1)
     602             :   {
     603     1582830 :     if (t->table[n].k == LH_EMPTY || t->table[n].k == LH_FREED)
     604             :       break;
     605      140618 :     if ((int)++n == t->size)
     606        8219 :       n = 0;
     607             :   }
     608             : 
     609     1442210 :   t->table[n].k = k;
     610     1442210 :   t->table[n].k_is_constant = (opts & JSON_C_OBJECT_KEY_IS_CONSTANT);
     611     1442210 :   t->table[n].v = v;
     612     1442210 :   t->count++;
     613             : 
     614     1442210 :   if (t->head == NULL)
     615             :   {
     616      354672 :     t->head = t->tail = &t->table[n];
     617      354672 :     t->table[n].next = t->table[n].prev = NULL;
     618             :   }
     619             :   else
     620             :   {
     621     1087540 :     t->tail->next = &t->table[n];
     622     1087540 :     t->table[n].prev = t->tail;
     623     1087540 :     t->table[n].next = NULL;
     624     1087540 :     t->tail = &t->table[n];
     625             :   }
     626             : 
     627     1442210 :   return 0;
     628             : }
     629           0 : int lh_table_insert(struct lh_table *t, const void *k, const void *v)
     630             : {
     631           0 :   return lh_table_insert_w_hash(t, k, v, lh_get_hash(t, k), 0);
     632             : }
     633             : 
     634     2355080 : struct lh_entry *lh_table_lookup_entry_w_hash(struct lh_table *t, const void *k,
     635             :                                               const unsigned long h)
     636             : {
     637     2355080 :   unsigned long n = h % t->size;
     638     2355080 :   int count = 0;
     639             : 
     640     2630320 :   while (count < t->size)
     641             :   {
     642     2630320 :     if (t->table[n].k == LH_EMPTY)
     643     1593660 :       return NULL;
     644     1036660 :     if (t->table[n].k != LH_FREED && t->equal_fn(t->table[n].k, k))
     645      761416 :       return &t->table[n];
     646      275242 :     if ((int)++n == t->size)
     647       18907 :       n = 0;
     648      275242 :     count++;
     649             :   }
     650           0 :   return NULL;
     651             : }
     652             : 
     653      954181 : struct lh_entry *lh_table_lookup_entry(struct lh_table *t, const void *k)
     654             : {
     655      954181 :   return lh_table_lookup_entry_w_hash(t, k, lh_get_hash(t, k));
     656             : }
     657             : 
     658      944505 : json_bool lh_table_lookup_ex(struct lh_table *t, const void *k, void **v)
     659             : {
     660      944505 :   struct lh_entry *e = lh_table_lookup_entry(t, k);
     661      944505 :   if (e != NULL)
     662             :   {
     663      760035 :     if (v != NULL)
     664      760035 :       *v = lh_entry_v(e);
     665      760035 :     return 1; /* key found */
     666             :   }
     667      184470 :   if (v != NULL)
     668      184470 :     *v = NULL;
     669      184470 :   return 0; /* key not found */
     670             : }
     671             : 
     672        1243 : int lh_table_delete_entry(struct lh_table *t, struct lh_entry *e)
     673             : {
     674             :   /* CAW: fixed to be 64bit nice, still need the crazy negative case... */
     675        1243 :   ptrdiff_t n = (ptrdiff_t)(e - t->table);
     676             : 
     677             :   /* CAW: this is bad, really bad, maybe stack goes other direction on this machine... */
     678        1243 :   if (n < 0)
     679             :   {
     680           0 :     return -2;
     681             :   }
     682             : 
     683        1243 :   if (t->table[n].k == LH_EMPTY || t->table[n].k == LH_FREED)
     684           0 :     return -1;
     685        1243 :   t->count--;
     686        1243 :   if (t->free_fn)
     687        1243 :     t->free_fn(e);
     688        1243 :   t->table[n].v = NULL;
     689        1243 :   t->table[n].k = LH_FREED;
     690        1243 :   if (t->tail == &t->table[n] && t->head == &t->table[n])
     691             :   {
     692         301 :     t->head = t->tail = NULL;
     693             :   }
     694         942 :   else if (t->head == &t->table[n])
     695             :   {
     696         376 :     t->head->next->prev = NULL;
     697         376 :     t->head = t->head->next;
     698             :   }
     699         566 :   else if (t->tail == &t->table[n])
     700             :   {
     701          83 :     t->tail->prev->next = NULL;
     702          83 :     t->tail = t->tail->prev;
     703             :   }
     704             :   else
     705             :   {
     706         483 :     t->table[n].prev->next = t->table[n].next;
     707         483 :     t->table[n].next->prev = t->table[n].prev;
     708             :   }
     709        1243 :   t->table[n].next = t->table[n].prev = NULL;
     710        1243 :   return 0;
     711             : }
     712             : 
     713        9676 : int lh_table_delete(struct lh_table *t, const void *k)
     714             : {
     715        9676 :   struct lh_entry *e = lh_table_lookup_entry(t, k);
     716        9676 :   if (!e)
     717        8433 :     return -1;
     718        1243 :   return lh_table_delete_entry(t, e);
     719             : }
     720             : 
     721           3 : int lh_table_length(struct lh_table *t)
     722             : {
     723           3 :   return t->count;
     724             : }

Generated by: LCOV version 1.14