Blame linkhash.c

Packit ea8578
/*
Packit ea8578
 * $Id: linkhash.c,v 1.4 2006/01/26 02:16:28 mclark Exp $
Packit ea8578
 *
Packit ea8578
 * Copyright (c) 2004, 2005 Metaparadigm Pte. Ltd.
Packit ea8578
 * Michael Clark <michael@metaparadigm.com>
Packit ea8578
 * Copyright (c) 2009 Hewlett-Packard Development Company, L.P.
Packit ea8578
 *
Packit ea8578
 * This library is free software; you can redistribute it and/or modify
Packit ea8578
 * it under the terms of the MIT license. See COPYING for details.
Packit ea8578
 *
Packit ea8578
 */
Packit ea8578
Packit ea8578
#include "config.h"
Packit ea8578
Packit ea8578
#include <stdio.h>
Packit ea8578
#include <string.h>
Packit ea8578
#include <stdlib.h>
Packit ea8578
#include <stdarg.h>
Packit ea8578
#include <stddef.h>
Packit ea8578
#include <limits.h>
Packit ea8578
Packit ea8578
#ifdef HAVE_ENDIAN_H
Packit ea8578
# include <endian.h>    /* attempt to define endianness */
Packit ea8578
#endif
Packit ea8578
Packit ea8578
#if defined(_MSC_VER) || defined(__MINGW32__)
Packit ea8578
# define WIN32_LEAN_AND_MEAN
Packit ea8578
# include <windows.h>   /* Get InterlockedCompareExchange */
Packit ea8578
#endif
Packit ea8578
Packit ea8578
#include "random_seed.h"
Packit ea8578
#include "linkhash.h"
Packit ea8578
Packit ea8578
/* hash functions */
Packit ea8578
static unsigned long lh_char_hash(const void *k);
Packit ea8578
static unsigned long lh_perllike_str_hash(const void *k);
Packit ea8578
static lh_hash_fn *char_hash_fn = lh_char_hash;
Packit ea8578
Packit ea8578
int
Packit ea8578
json_global_set_string_hash(const int h)
Packit ea8578
{
Packit ea8578
	switch(h) {
Packit ea8578
	case JSON_C_STR_HASH_DFLT:
Packit ea8578
		char_hash_fn = lh_char_hash;
Packit ea8578
		break;
Packit ea8578
	case JSON_C_STR_HASH_PERLLIKE:
Packit ea8578
		char_hash_fn = lh_perllike_str_hash;
Packit ea8578
		break;
Packit ea8578
	default:
Packit ea8578
		return -1;
Packit ea8578
	}
Packit ea8578
	return 0;
Packit ea8578
}
Packit ea8578
Packit ea8578
void lh_abort(const char *msg, ...)
Packit ea8578
{
Packit ea8578
	va_list ap;
Packit ea8578
	va_start(ap, msg);
Packit ea8578
	vprintf(msg, ap);
Packit ea8578
	va_end(ap);
Packit ea8578
	exit(1);
Packit ea8578
}
Packit ea8578
Packit ea8578
static unsigned long lh_ptr_hash(const void *k)
Packit ea8578
{
Packit ea8578
	/* CAW: refactored to be 64bit nice */
Packit ea8578
	return (unsigned long)((((ptrdiff_t)k * LH_PRIME) >> 4) & ULONG_MAX);
Packit ea8578
}
Packit ea8578
Packit ea8578
int lh_ptr_equal(const void *k1, const void *k2)
Packit ea8578
{
Packit ea8578
	return (k1 == k2);
Packit ea8578
}
Packit ea8578
Packit ea8578
/*
Packit ea8578
 * hashlittle from lookup3.c, by Bob Jenkins, May 2006, Public Domain.
Packit ea8578
 * http://burtleburtle.net/bob/c/lookup3.c
Packit ea8578
 * minor modifications to make functions static so no symbols are exported
Packit ea8578
 * minor mofifications to compile with -Werror
Packit ea8578
 */
Packit ea8578
Packit ea8578
/*
Packit ea8578
-------------------------------------------------------------------------------
Packit ea8578
lookup3.c, by Bob Jenkins, May 2006, Public Domain.
Packit ea8578
Packit ea8578
These are functions for producing 32-bit hashes for hash table lookup.
Packit ea8578
hashword(), hashlittle(), hashlittle2(), hashbig(), mix(), and final()
Packit ea8578
are externally useful functions.  Routines to test the hash are included
Packit ea8578
if SELF_TEST is defined.  You can use this free for any purpose.  It's in
Packit ea8578
the public domain.  It has no warranty.
Packit ea8578
Packit ea8578
You probably want to use hashlittle().  hashlittle() and hashbig()
Packit ea8578
hash byte arrays.  hashlittle() is is faster than hashbig() on
Packit ea8578
little-endian machines.  Intel and AMD are little-endian machines.
Packit ea8578
On second thought, you probably want hashlittle2(), which is identical to
Packit ea8578
hashlittle() except it returns two 32-bit hashes for the price of one.
Packit ea8578
You could implement hashbig2() if you wanted but I haven't bothered here.
Packit ea8578
Packit ea8578
If you want to find a hash of, say, exactly 7 integers, do
Packit ea8578
  a = i1;  b = i2;  c = i3;
Packit ea8578
  mix(a,b,c);
Packit ea8578
  a += i4; b += i5; c += i6;
Packit ea8578
  mix(a,b,c);
Packit ea8578
  a += i7;
Packit ea8578
  final(a,b,c);
Packit ea8578
then use c as the hash value.  If you have a variable length array of
Packit ea8578
4-byte integers to hash, use hashword().  If you have a byte array (like
Packit ea8578
a character string), use hashlittle().  If you have several byte arrays, or
Packit ea8578
a mix of things, see the comments above hashlittle().
Packit ea8578
Packit ea8578
Why is this so big?  I read 12 bytes at a time into 3 4-byte integers,
Packit ea8578
then mix those integers.  This is fast (you can do a lot more thorough
Packit ea8578
mixing with 12*3 instructions on 3 integers than you can with 3 instructions
Packit ea8578
on 1 byte), but shoehorning those bytes into integers efficiently is messy.
Packit ea8578
-------------------------------------------------------------------------------
Packit ea8578
*/
Packit ea8578
Packit ea8578
/*
Packit ea8578
 * My best guess at if you are big-endian or little-endian.  This may
Packit ea8578
 * need adjustment.
Packit ea8578
 */
Packit ea8578
#if (defined(__BYTE_ORDER) && defined(__LITTLE_ENDIAN) && \
Packit ea8578
     __BYTE_ORDER == __LITTLE_ENDIAN) || \
Packit ea8578
    (defined(i386) || defined(__i386__) || defined(__i486__) || \
Packit ea8578
     defined(__i586__) || defined(__i686__) || defined(vax) || defined(MIPSEL))
Packit ea8578
# define HASH_LITTLE_ENDIAN 1
Packit ea8578
# define HASH_BIG_ENDIAN 0
Packit ea8578
#elif (defined(__BYTE_ORDER) && defined(__BIG_ENDIAN) && \
Packit ea8578
       __BYTE_ORDER == __BIG_ENDIAN) || \
Packit ea8578
      (defined(sparc) || defined(POWERPC) || defined(mc68000) || defined(sel))
Packit ea8578
# define HASH_LITTLE_ENDIAN 0
Packit ea8578
# define HASH_BIG_ENDIAN 1
Packit ea8578
#else
Packit ea8578
# define HASH_LITTLE_ENDIAN 0
Packit ea8578
# define HASH_BIG_ENDIAN 0
Packit ea8578
#endif
Packit ea8578
Packit ea8578
#define hashsize(n) ((uint32_t)1<<(n))
Packit ea8578
#define hashmask(n) (hashsize(n)-1)
Packit ea8578
#define rot(x,k) (((x)<<(k)) | ((x)>>(32-(k))))
Packit ea8578
Packit ea8578
/*
Packit ea8578
-------------------------------------------------------------------------------
Packit ea8578
mix -- mix 3 32-bit values reversibly.
Packit ea8578
Packit ea8578
This is reversible, so any information in (a,b,c) before mix() is
Packit ea8578
still in (a,b,c) after mix().
Packit ea8578
Packit ea8578
If four pairs of (a,b,c) inputs are run through mix(), or through
Packit ea8578
mix() in reverse, there are at least 32 bits of the output that
Packit ea8578
are sometimes the same for one pair and different for another pair.
Packit ea8578
This was tested for:
Packit ea8578
* pairs that differed by one bit, by two bits, in any combination
Packit ea8578
  of top bits of (a,b,c), or in any combination of bottom bits of
Packit ea8578
  (a,b,c).
Packit ea8578
* "differ" is defined as +, -, ^, or ~^.  For + and -, I transformed
Packit ea8578
  the output delta to a Gray code (a^(a>>1)) so a string of 1's (as
Packit ea8578
  is commonly produced by subtraction) look like a single 1-bit
Packit ea8578
  difference.
Packit ea8578
* the base values were pseudorandom, all zero but one bit set, or
Packit ea8578
  all zero plus a counter that starts at zero.
Packit ea8578
Packit ea8578
Some k values for my "a-=c; a^=rot(c,k); c+=b;" arrangement that
Packit ea8578
satisfy this are
Packit ea8578
    4  6  8 16 19  4
Packit ea8578
    9 15  3 18 27 15
Packit ea8578
   14  9  3  7 17  3
Packit ea8578
Well, "9 15 3 18 27 15" didn't quite get 32 bits diffing
Packit ea8578
for "differ" defined as + with a one-bit base and a two-bit delta.  I
Packit ea8578
used http://burtleburtle.net/bob/hash/avalanche.html to choose
Packit ea8578
the operations, constants, and arrangements of the variables.
Packit ea8578
Packit ea8578
This does not achieve avalanche.  There are input bits of (a,b,c)
Packit ea8578
that fail to affect some output bits of (a,b,c), especially of a.  The
Packit ea8578
most thoroughly mixed value is c, but it doesn't really even achieve
Packit ea8578
avalanche in c.
Packit ea8578
Packit ea8578
This allows some parallelism.  Read-after-writes are good at doubling
Packit ea8578
the number of bits affected, so the goal of mixing pulls in the opposite
Packit ea8578
direction as the goal of parallelism.  I did what I could.  Rotates
Packit ea8578
seem to cost as much as shifts on every machine I could lay my hands
Packit ea8578
on, and rotates are much kinder to the top and bottom bits, so I used
Packit ea8578
rotates.
Packit ea8578
-------------------------------------------------------------------------------
Packit ea8578
*/
Packit ea8578
#define mix(a,b,c) \
Packit ea8578
{ \
Packit ea8578
  a -= c;  a ^= rot(c, 4);  c += b; \
Packit ea8578
  b -= a;  b ^= rot(a, 6);  a += c; \
Packit ea8578
  c -= b;  c ^= rot(b, 8);  b += a; \
Packit ea8578
  a -= c;  a ^= rot(c,16);  c += b; \
Packit ea8578
  b -= a;  b ^= rot(a,19);  a += c; \
Packit ea8578
  c -= b;  c ^= rot(b, 4);  b += a; \
Packit ea8578
}
Packit ea8578
Packit ea8578
/*
Packit ea8578
-------------------------------------------------------------------------------
Packit ea8578
final -- final mixing of 3 32-bit values (a,b,c) into c
Packit ea8578
Packit ea8578
Pairs of (a,b,c) values differing in only a few bits will usually
Packit ea8578
produce values of c that look totally different.  This was tested for
Packit ea8578
* pairs that differed by one bit, by two bits, in any combination
Packit ea8578
  of top bits of (a,b,c), or in any combination of bottom bits of
Packit ea8578
  (a,b,c).
Packit ea8578
* "differ" is defined as +, -, ^, or ~^.  For + and -, I transformed
Packit ea8578
  the output delta to a Gray code (a^(a>>1)) so a string of 1's (as
Packit ea8578
  is commonly produced by subtraction) look like a single 1-bit
Packit ea8578
  difference.
Packit ea8578
* the base values were pseudorandom, all zero but one bit set, or
Packit ea8578
  all zero plus a counter that starts at zero.
Packit ea8578
Packit ea8578
These constants passed:
Packit ea8578
 14 11 25 16 4 14 24
Packit ea8578
 12 14 25 16 4 14 24
Packit ea8578
and these came close:
Packit ea8578
  4  8 15 26 3 22 24
Packit ea8578
 10  8 15 26 3 22 24
Packit ea8578
 11  8 15 26 3 22 24
Packit ea8578
-------------------------------------------------------------------------------
Packit ea8578
*/
Packit ea8578
#define final(a,b,c) \
Packit ea8578
{ \
Packit ea8578
  c ^= b; c -= rot(b,14); \
Packit ea8578
  a ^= c; a -= rot(c,11); \
Packit ea8578
  b ^= a; b -= rot(a,25); \
Packit ea8578
  c ^= b; c -= rot(b,16); \
Packit ea8578
  a ^= c; a -= rot(c,4);  \
Packit ea8578
  b ^= a; b -= rot(a,14); \
Packit ea8578
  c ^= b; c -= rot(b,24); \
Packit ea8578
}
Packit ea8578
Packit ea8578
Packit ea8578
/*
Packit ea8578
-------------------------------------------------------------------------------
Packit ea8578
hashlittle() -- hash a variable-length key into a 32-bit value
Packit ea8578
  k       : the key (the unaligned variable-length array of bytes)
Packit ea8578
  length  : the length of the key, counting by bytes
Packit ea8578
  initval : can be any 4-byte value
Packit ea8578
Returns a 32-bit value.  Every bit of the key affects every bit of
Packit ea8578
the return value.  Two keys differing by one or two bits will have
Packit ea8578
totally different hash values.
Packit ea8578
Packit ea8578
The best hash table sizes are powers of 2.  There is no need to do
Packit ea8578
mod a prime (mod is sooo slow!).  If you need less than 32 bits,
Packit ea8578
use a bitmask.  For example, if you need only 10 bits, do
Packit ea8578
  h = (h & hashmask(10));
Packit ea8578
In which case, the hash table should have hashsize(10) elements.
Packit ea8578
Packit ea8578
If you are hashing n strings (uint8_t **)k, do it like this:
Packit ea8578
  for (i=0, h=0; i
Packit ea8578
Packit ea8578
By Bob Jenkins, 2006.  bob_jenkins@burtleburtle.net.  You may use this
Packit ea8578
code any way you wish, private, educational, or commercial.  It's free.
Packit ea8578
Packit ea8578
Use for hash table lookup, or anything where one collision in 2^^32 is
Packit ea8578
acceptable.  Do NOT use for cryptographic purposes.
Packit ea8578
-------------------------------------------------------------------------------
Packit ea8578
*/
Packit ea8578
Packit ea8578
static uint32_t hashlittle( const void *key, size_t length, uint32_t initval)
Packit ea8578
{
Packit ea8578
  uint32_t a,b,c;                                          /* internal state */
Packit ea8578
  union { const void *ptr; size_t i; } u;     /* needed for Mac Powerbook G4 */
Packit ea8578
Packit ea8578
  /* Set up the internal state */
Packit ea8578
  a = b = c = 0xdeadbeef + ((uint32_t)length) + initval;
Packit ea8578
Packit ea8578
  u.ptr = key;
Packit ea8578
  if (HASH_LITTLE_ENDIAN && ((u.i & 0x3) == 0)) {
Packit ea8578
    const uint32_t *k = (const uint32_t *)key;         /* read 32-bit chunks */
Packit ea8578
Packit ea8578
    /*------ all but last block: aligned reads and affect 32 bits of (a,b,c) */
Packit ea8578
    while (length > 12)
Packit ea8578
    {
Packit ea8578
      a += k[0];
Packit ea8578
      b += k[1];
Packit ea8578
      c += k[2];
Packit ea8578
      mix(a,b,c);
Packit ea8578
      length -= 12;
Packit ea8578
      k += 3;
Packit ea8578
    }
Packit ea8578
Packit ea8578
    /*----------------------------- handle the last (probably partial) block */
Packit ea8578
    /*
Packit ea8578
     * "k[2]&0xffffff" actually reads beyond the end of the string, but
Packit ea8578
     * then masks off the part it's not allowed to read.  Because the
Packit ea8578
     * string is aligned, the masked-off tail is in the same word as the
Packit ea8578
     * rest of the string.  Every machine with memory protection I've seen
Packit ea8578
     * does it on word boundaries, so is OK with this.  But VALGRIND will
Packit ea8578
     * still catch it and complain.  The masking trick does make the hash
Packit ea8578
     * noticably faster for short strings (like English words).
Packit ea8578
     * AddressSanitizer is similarly picky about overrunning
Packit ea8578
	 * the buffer. (http://clang.llvm.org/docs/AddressSanitizer.html
Packit ea8578
     */
Packit ea8578
#ifdef VALGRIND
Packit ea8578
#    define PRECISE_MEMORY_ACCESS 1
Packit ea8578
#elif defined(__SANITIZE_ADDRESS__) /* GCC's ASAN */
Packit ea8578
#    define PRECISE_MEMORY_ACCESS 1
Packit ea8578
#elif defined(__has_feature)
Packit ea8578
#  if __has_feature(address_sanitizer) /* Clang's ASAN */
Packit ea8578
#    define PRECISE_MEMORY_ACCESS 1
Packit ea8578
#  endif
Packit ea8578
#endif
Packit ea8578
#ifndef PRECISE_MEMORY_ACCESS
Packit ea8578
Packit ea8578
    switch(length)
Packit ea8578
    {
Packit ea8578
    case 12: c+=k[2]; b+=k[1]; a+=k[0]; break;
Packit ea8578
    case 11: c+=k[2]&0xffffff; b+=k[1]; a+=k[0]; break;
Packit ea8578
    case 10: c+=k[2]&0xffff; b+=k[1]; a+=k[0]; break;
Packit ea8578
    case 9 : c+=k[2]&0xff; b+=k[1]; a+=k[0]; break;
Packit ea8578
    case 8 : b+=k[1]; a+=k[0]; break;
Packit ea8578
    case 7 : b+=k[1]&0xffffff; a+=k[0]; break;
Packit ea8578
    case 6 : b+=k[1]&0xffff; a+=k[0]; break;
Packit ea8578
    case 5 : b+=k[1]&0xff; a+=k[0]; break;
Packit ea8578
    case 4 : a+=k[0]; break;
Packit ea8578
    case 3 : a+=k[0]&0xffffff; break;
Packit ea8578
    case 2 : a+=k[0]&0xffff; break;
Packit ea8578
    case 1 : a+=k[0]&0xff; break;
Packit ea8578
    case 0 : return c;              /* zero length strings require no mixing */
Packit ea8578
    }
Packit ea8578
Packit ea8578
#else /* make valgrind happy */
Packit ea8578
Packit ea8578
    const uint8_t  *k8 = (const uint8_t *)k;
Packit ea8578
    switch(length)
Packit ea8578
    {
Packit ea8578
    case 12: c+=k[2]; b+=k[1]; a+=k[0]; break;
Packit ea8578
    case 11: c+=((uint32_t)k8[10])<<16;  /* fall through */
Packit ea8578
    case 10: c+=((uint32_t)k8[9])<<8;    /* fall through */
Packit ea8578
    case 9 : c+=k8[8];                   /* fall through */
Packit ea8578
    case 8 : b+=k[1]; a+=k[0]; break;
Packit ea8578
    case 7 : b+=((uint32_t)k8[6])<<16;   /* fall through */
Packit ea8578
    case 6 : b+=((uint32_t)k8[5])<<8;    /* fall through */
Packit ea8578
    case 5 : b+=k8[4];                   /* fall through */
Packit ea8578
    case 4 : a+=k[0]; break;
Packit ea8578
    case 3 : a+=((uint32_t)k8[2])<<16;   /* fall through */
Packit ea8578
    case 2 : a+=((uint32_t)k8[1])<<8;    /* fall through */
Packit ea8578
    case 1 : a+=k8[0]; break;
Packit ea8578
    case 0 : return c;
Packit ea8578
    }
Packit ea8578
Packit ea8578
#endif /* !valgrind */
Packit ea8578
Packit ea8578
  } else if (HASH_LITTLE_ENDIAN && ((u.i & 0x1) == 0)) {
Packit ea8578
    const uint16_t *k = (const uint16_t *)key;         /* read 16-bit chunks */
Packit ea8578
    const uint8_t  *k8;
Packit ea8578
Packit ea8578
    /*--------------- all but last block: aligned reads and different mixing */
Packit ea8578
    while (length > 12)
Packit ea8578
    {
Packit ea8578
      a += k[0] + (((uint32_t)k[1])<<16);
Packit ea8578
      b += k[2] + (((uint32_t)k[3])<<16);
Packit ea8578
      c += k[4] + (((uint32_t)k[5])<<16);
Packit ea8578
      mix(a,b,c);
Packit ea8578
      length -= 12;
Packit ea8578
      k += 6;
Packit ea8578
    }
Packit ea8578
Packit ea8578
    /*----------------------------- handle the last (probably partial) block */
Packit ea8578
    k8 = (const uint8_t *)k;
Packit ea8578
    switch(length)
Packit ea8578
    {
Packit ea8578
    case 12: c+=k[4]+(((uint32_t)k[5])<<16);
Packit ea8578
             b+=k[2]+(((uint32_t)k[3])<<16);
Packit ea8578
             a+=k[0]+(((uint32_t)k[1])<<16);
Packit ea8578
             break;
Packit ea8578
    case 11: c+=((uint32_t)k8[10])<<16;     /* fall through */
Packit ea8578
    case 10: c+=k[4];
Packit ea8578
             b+=k[2]+(((uint32_t)k[3])<<16);
Packit ea8578
             a+=k[0]+(((uint32_t)k[1])<<16);
Packit ea8578
             break;
Packit ea8578
    case 9 : c+=k8[8];                      /* fall through */
Packit ea8578
    case 8 : b+=k[2]+(((uint32_t)k[3])<<16);
Packit ea8578
             a+=k[0]+(((uint32_t)k[1])<<16);
Packit ea8578
             break;
Packit ea8578
    case 7 : b+=((uint32_t)k8[6])<<16;      /* fall through */
Packit ea8578
    case 6 : b+=k[2];
Packit ea8578
             a+=k[0]+(((uint32_t)k[1])<<16);
Packit ea8578
             break;
Packit ea8578
    case 5 : b+=k8[4];                      /* fall through */
Packit ea8578
    case 4 : a+=k[0]+(((uint32_t)k[1])<<16);
Packit ea8578
             break;
Packit ea8578
    case 3 : a+=((uint32_t)k8[2])<<16;      /* fall through */
Packit ea8578
    case 2 : a+=k[0];
Packit ea8578
             break;
Packit ea8578
    case 1 : a+=k8[0];
Packit ea8578
             break;
Packit ea8578
    case 0 : return c;                     /* zero length requires no mixing */
Packit ea8578
    }
Packit ea8578
Packit ea8578
  } else {                        /* need to read the key one byte at a time */
Packit ea8578
    const uint8_t *k = (const uint8_t *)key;
Packit ea8578
Packit ea8578
    /*--------------- all but the last block: affect some 32 bits of (a,b,c) */
Packit ea8578
    while (length > 12)
Packit ea8578
    {
Packit ea8578
      a += k[0];
Packit ea8578
      a += ((uint32_t)k[1])<<8;
Packit ea8578
      a += ((uint32_t)k[2])<<16;
Packit ea8578
      a += ((uint32_t)k[3])<<24;
Packit ea8578
      b += k[4];
Packit ea8578
      b += ((uint32_t)k[5])<<8;
Packit ea8578
      b += ((uint32_t)k[6])<<16;
Packit ea8578
      b += ((uint32_t)k[7])<<24;
Packit ea8578
      c += k[8];
Packit ea8578
      c += ((uint32_t)k[9])<<8;
Packit ea8578
      c += ((uint32_t)k[10])<<16;
Packit ea8578
      c += ((uint32_t)k[11])<<24;
Packit ea8578
      mix(a,b,c);
Packit ea8578
      length -= 12;
Packit ea8578
      k += 12;
Packit ea8578
    }
Packit ea8578
Packit ea8578
    /*-------------------------------- last block: affect all 32 bits of (c) */
Packit ea8578
    switch(length)                   /* all the case statements fall through */
Packit ea8578
    {
Packit ea8578
    case 12: c+=((uint32_t)k[11])<<24; /* FALLTHRU */
Packit ea8578
    case 11: c+=((uint32_t)k[10])<<16; /* FALLTHRU */
Packit ea8578
    case 10: c+=((uint32_t)k[9])<<8; /* FALLTHRU */
Packit ea8578
    case 9 : c+=k[8]; /* FALLTHRU */
Packit ea8578
    case 8 : b+=((uint32_t)k[7])<<24; /* FALLTHRU */
Packit ea8578
    case 7 : b+=((uint32_t)k[6])<<16; /* FALLTHRU */
Packit ea8578
    case 6 : b+=((uint32_t)k[5])<<8; /* FALLTHRU */
Packit ea8578
    case 5 : b+=k[4]; /* FALLTHRU */
Packit ea8578
    case 4 : a+=((uint32_t)k[3])<<24; /* FALLTHRU */
Packit ea8578
    case 3 : a+=((uint32_t)k[2])<<16; /* FALLTHRU */
Packit ea8578
    case 2 : a+=((uint32_t)k[1])<<8; /* FALLTHRU */
Packit ea8578
    case 1 : a+=k[0];
Packit ea8578
             break;
Packit ea8578
    case 0 : return c;
Packit ea8578
    }
Packit ea8578
  }
Packit ea8578
Packit ea8578
  final(a,b,c);
Packit ea8578
  return c;
Packit ea8578
}
Packit ea8578
Packit ea8578
/* a simple hash function similiar to what perl does for strings.
Packit ea8578
 * for good results, the string should not be excessivly large.
Packit ea8578
 */
Packit ea8578
static unsigned long lh_perllike_str_hash(const void *k) 
Packit ea8578
{
Packit ea8578
    const char *rkey = (const char *)k;
Packit ea8578
    unsigned hashval = 1;
Packit ea8578
Packit ea8578
    while (*rkey)
Packit ea8578
        hashval = hashval * 33 + *rkey++;
Packit ea8578
Packit ea8578
    return hashval;
Packit ea8578
}
Packit ea8578
Packit ea8578
static unsigned long lh_char_hash(const void *k)
Packit ea8578
{
Packit ea8578
#if defined _MSC_VER || defined __MINGW32__
Packit ea8578
#define RANDOM_SEED_TYPE LONG
Packit ea8578
#else
Packit ea8578
#define RANDOM_SEED_TYPE int
Packit ea8578
#endif
Packit ea8578
	static volatile RANDOM_SEED_TYPE random_seed = -1;
Packit ea8578
Packit ea8578
	if (random_seed == -1) {
Packit ea8578
		RANDOM_SEED_TYPE seed;
Packit ea8578
		/* we can't use -1 as it is the unitialized sentinel */
Packit ea8578
		while ((seed = json_c_get_random_seed()) == -1);
Packit ea8578
#if SIZEOF_INT == 8 && defined __GCC_HAVE_SYNC_COMPARE_AND_SWAP_8
Packit ea8578
#define USE_SYNC_COMPARE_AND_SWAP 1
Packit ea8578
#endif
Packit ea8578
#if SIZEOF_INT == 4 && defined __GCC_HAVE_SYNC_COMPARE_AND_SWAP_4
Packit ea8578
#define USE_SYNC_COMPARE_AND_SWAP 1
Packit ea8578
#endif
Packit ea8578
#if SIZEOF_INT == 2 && defined __GCC_HAVE_SYNC_COMPARE_AND_SWAP_2
Packit ea8578
#define USE_SYNC_COMPARE_AND_SWAP 1
Packit ea8578
#endif
Packit ea8578
#if defined USE_SYNC_COMPARE_AND_SWAP
Packit ea8578
		(void)__sync_val_compare_and_swap(&random_seed, -1, seed);
Packit ea8578
#elif defined _MSC_VER || defined __MINGW32__
Packit ea8578
		InterlockedCompareExchange(&random_seed, seed, -1);
Packit ea8578
#else
Packit ea8578
//#warning "racy random seed initializtion if used by multiple threads"
Packit ea8578
		random_seed = seed; /* potentially racy */
Packit ea8578
#endif
Packit ea8578
	}
Packit ea8578
Packit ea8578
	return hashlittle((const char*)k, strlen((const char*)k), random_seed);
Packit ea8578
}
Packit ea8578
Packit ea8578
int lh_char_equal(const void *k1, const void *k2)
Packit ea8578
{
Packit ea8578
	return (strcmp((const char*)k1, (const char*)k2) == 0);
Packit ea8578
}
Packit ea8578
Packit ea8578
struct lh_table* lh_table_new(int size,
Packit ea8578
			      lh_entry_free_fn *free_fn,
Packit ea8578
			      lh_hash_fn *hash_fn,
Packit ea8578
			      lh_equal_fn *equal_fn)
Packit ea8578
{
Packit ea8578
	int i;
Packit ea8578
	struct lh_table *t;
Packit ea8578
Packit ea8578
	t = (struct lh_table*)calloc(1, sizeof(struct lh_table));
Packit ea8578
	if (!t)
Packit ea8578
		return NULL;
Packit ea8578
Packit ea8578
	t->count = 0;
Packit ea8578
	t->size = size;
Packit ea8578
	t->table = (struct lh_entry*)calloc(size, sizeof(struct lh_entry));
Packit ea8578
	if (!t->table)
Packit ea8578
	{
Packit ea8578
		free(t);
Packit ea8578
		return NULL;
Packit ea8578
	}
Packit ea8578
	t->free_fn = free_fn;
Packit ea8578
	t->hash_fn = hash_fn;
Packit ea8578
	t->equal_fn = equal_fn;
Packit ea8578
	for(i = 0; i < size; i++) t->table[i].k = LH_EMPTY;
Packit ea8578
	return t;
Packit ea8578
}
Packit ea8578
Packit ea8578
struct lh_table* lh_kchar_table_new(int size,
Packit ea8578
				    lh_entry_free_fn *free_fn)
Packit ea8578
{
Packit ea8578
	return lh_table_new(size, free_fn, char_hash_fn, lh_char_equal);
Packit ea8578
}
Packit ea8578
Packit ea8578
struct lh_table* lh_kptr_table_new(int size,
Packit ea8578
				   lh_entry_free_fn *free_fn)
Packit ea8578
{
Packit ea8578
	return lh_table_new(size, free_fn, lh_ptr_hash, lh_ptr_equal);
Packit ea8578
}
Packit ea8578
Packit ea8578
int lh_table_resize(struct lh_table *t, int new_size)
Packit ea8578
{
Packit ea8578
	struct lh_table *new_t;
Packit ea8578
	struct lh_entry *ent;
Packit ea8578
Packit ea8578
	new_t = lh_table_new(new_size, NULL, t->hash_fn, t->equal_fn);
Packit ea8578
	if (new_t == NULL)
Packit ea8578
		return -1;
Packit ea8578
Packit ea8578
	for (ent = t->head; ent != NULL; ent = ent->next)
Packit ea8578
	{
Packit ea8578
		unsigned long h = lh_get_hash(new_t, ent->k);
Packit ea8578
		unsigned int opts = 0;
Packit ea8578
		if (ent->k_is_constant)
Packit ea8578
			opts = JSON_C_OBJECT_KEY_IS_CONSTANT;
Packit ea8578
		if (lh_table_insert_w_hash(new_t, ent->k, ent->v, h, opts) != 0)
Packit ea8578
		{
Packit ea8578
			lh_table_free(new_t);
Packit ea8578
			return -1;
Packit ea8578
		}
Packit ea8578
	}
Packit ea8578
	free(t->table);
Packit ea8578
	t->table = new_t->table;
Packit ea8578
	t->size = new_size;
Packit ea8578
	t->head = new_t->head;
Packit ea8578
	t->tail = new_t->tail;
Packit ea8578
	free(new_t);
Packit ea8578
Packit ea8578
	return 0;
Packit ea8578
}
Packit ea8578
Packit ea8578
void lh_table_free(struct lh_table *t)
Packit ea8578
{
Packit ea8578
	struct lh_entry *c;
Packit ea8578
	if(t->free_fn) {
Packit ea8578
		for(c = t->head; c != NULL; c = c->next)
Packit ea8578
			t->free_fn(c);
Packit ea8578
	}
Packit ea8578
	free(t->table);
Packit ea8578
	free(t);
Packit ea8578
}
Packit ea8578
Packit ea8578
Packit ea8578
int lh_table_insert_w_hash(struct lh_table *t, const void *k, const void *v, const unsigned long h, const unsigned opts)
Packit ea8578
{
Packit ea8578
	unsigned long n;
Packit ea8578
Packit ea8578
	if (t->count >= t->size * LH_LOAD_FACTOR)
Packit ea8578
		if (lh_table_resize(t, t->size * 2) != 0)
Packit ea8578
			return -1;
Packit ea8578
Packit ea8578
	n = h % t->size;
Packit ea8578
Packit ea8578
	while( 1 ) {
Packit ea8578
		if(t->table[n].k == LH_EMPTY || t->table[n].k == LH_FREED) break;
Packit ea8578
		if ((int)++n == t->size) n = 0;
Packit ea8578
	}
Packit ea8578
Packit ea8578
	t->table[n].k = k;
Packit ea8578
	t->table[n].k_is_constant = (opts & JSON_C_OBJECT_KEY_IS_CONSTANT);
Packit ea8578
	t->table[n].v = v;
Packit ea8578
	t->count++;
Packit ea8578
Packit ea8578
	if(t->head == NULL) {
Packit ea8578
		t->head = t->tail = &t->table[n];
Packit ea8578
		t->table[n].next = t->table[n].prev = NULL;
Packit ea8578
	} else {
Packit ea8578
		t->tail->next = &t->table[n];
Packit ea8578
		t->table[n].prev = t->tail;
Packit ea8578
		t->table[n].next = NULL;
Packit ea8578
		t->tail = &t->table[n];
Packit ea8578
	}
Packit ea8578
Packit ea8578
	return 0;
Packit ea8578
}
Packit ea8578
int lh_table_insert(struct lh_table *t, const void *k, const void *v)
Packit ea8578
{
Packit ea8578
	return lh_table_insert_w_hash(t, k, v, lh_get_hash(t, k), 0);
Packit ea8578
}
Packit ea8578
Packit ea8578
Packit ea8578
struct lh_entry* lh_table_lookup_entry_w_hash(struct lh_table *t, const void *k, const unsigned long h)
Packit ea8578
{
Packit ea8578
	unsigned long n = h % t->size;
Packit ea8578
	int count = 0;
Packit ea8578
Packit ea8578
	while( count < t->size ) {
Packit ea8578
		if(t->table[n].k == LH_EMPTY) return NULL;
Packit ea8578
		if(t->table[n].k != LH_FREED &&
Packit ea8578
		   t->equal_fn(t->table[n].k, k)) return &t->table[n];
Packit ea8578
		if ((int)++n == t->size) n = 0;
Packit ea8578
		count++;
Packit ea8578
	}
Packit ea8578
	return NULL;
Packit ea8578
}
Packit ea8578
Packit ea8578
struct lh_entry* lh_table_lookup_entry(struct lh_table *t, const void *k)
Packit ea8578
{
Packit ea8578
	return lh_table_lookup_entry_w_hash(t, k, lh_get_hash(t, k));
Packit ea8578
}
Packit ea8578
Packit ea8578
const void* lh_table_lookup(struct lh_table *t, const void *k)
Packit ea8578
{
Packit ea8578
	void *result;
Packit ea8578
	lh_table_lookup_ex(t, k, &result);
Packit ea8578
	return result;
Packit ea8578
}
Packit ea8578
Packit ea8578
json_bool lh_table_lookup_ex(struct lh_table* t, const void* k, void **v)
Packit ea8578
{
Packit ea8578
	struct lh_entry *e = lh_table_lookup_entry(t, k);
Packit ea8578
	if (e != NULL) {
Packit ea8578
		if (v != NULL) *v = lh_entry_v(e);
Packit ea8578
		return TRUE; /* key found */
Packit ea8578
	}
Packit ea8578
	if (v != NULL) *v = NULL;
Packit ea8578
		return FALSE; /* key not found */
Packit ea8578
}
Packit ea8578
Packit ea8578
int lh_table_delete_entry(struct lh_table *t, struct lh_entry *e)
Packit ea8578
{
Packit ea8578
	ptrdiff_t n = (ptrdiff_t)(e - t->table); /* CAW: fixed to be 64bit nice, still need the crazy negative case... */
Packit ea8578
Packit ea8578
	/* CAW: this is bad, really bad, maybe stack goes other direction on this machine... */
Packit ea8578
	if(n < 0) { return -2; }
Packit ea8578
Packit ea8578
	if(t->table[n].k == LH_EMPTY || t->table[n].k == LH_FREED) return -1;
Packit ea8578
	t->count--;
Packit ea8578
	if(t->free_fn) t->free_fn(e);
Packit ea8578
	t->table[n].v = NULL;
Packit ea8578
	t->table[n].k = LH_FREED;
Packit ea8578
	if(t->tail == &t->table[n] && t->head == &t->table[n]) {
Packit ea8578
		t->head = t->tail = NULL;
Packit ea8578
	} else if (t->head == &t->table[n]) {
Packit ea8578
		t->head->next->prev = NULL;
Packit ea8578
		t->head = t->head->next;
Packit ea8578
	} else if (t->tail == &t->table[n]) {
Packit ea8578
		t->tail->prev->next = NULL;
Packit ea8578
		t->tail = t->tail->prev;
Packit ea8578
	} else {
Packit ea8578
		t->table[n].prev->next = t->table[n].next;
Packit ea8578
		t->table[n].next->prev = t->table[n].prev;
Packit ea8578
	}
Packit ea8578
	t->table[n].next = t->table[n].prev = NULL;
Packit ea8578
	return 0;
Packit ea8578
}
Packit ea8578
Packit ea8578
Packit ea8578
int lh_table_delete(struct lh_table *t, const void *k)
Packit ea8578
{
Packit ea8578
	struct lh_entry *e = lh_table_lookup_entry(t, k);
Packit ea8578
	if(!e) return -1;
Packit ea8578
	return lh_table_delete_entry(t, e);
Packit ea8578
}
Packit ea8578
Packit ea8578
int lh_table_length(struct lh_table *t)
Packit ea8578
{
Packit ea8578
	return t->count;
Packit ea8578
}