123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690 |
- #include "config.h"
- #include <stdio.h>
- #include <string.h>
- #include <stdlib.h>
- #include <stdarg.h>
- #include <stddef.h>
- #include <limits.h>
- #ifdef HAVE_ENDIAN_H
- # include <endian.h> /* attempt to define endianness */
- #endif
- #if defined(_MSC_VER) || defined(__MINGW32__)
- # define WIN32_LEAN_AND_MEAN
- # include <windows.h> /* Get InterlockedCompareExchange */
- #endif
- #include "random_seed.h"
- #include "linkhash.h"
- static unsigned long lh_char_hash(const void *k);
- static unsigned long lh_perllike_str_hash(const void *k);
- static lh_hash_fn *char_hash_fn = lh_char_hash;
- int
- json_global_set_string_hash(const int h)
- {
- switch(h) {
- case JSON_C_STR_HASH_DFLT:
- char_hash_fn = lh_char_hash;
- break;
- case JSON_C_STR_HASH_PERLLIKE:
- char_hash_fn = lh_perllike_str_hash;
- break;
- default:
- return -1;
- }
- return 0;
- }
- void lh_abort(const char *msg, ...)
- {
- va_list ap;
- va_start(ap, msg);
- vprintf(msg, ap);
- va_end(ap);
- exit(1);
- }
- static unsigned long lh_ptr_hash(const void *k)
- {
-
- return (unsigned long)((((ptrdiff_t)k * LH_PRIME) >> 4) & ULONG_MAX);
- }
- int lh_ptr_equal(const void *k1, const void *k2)
- {
- return (k1 == k2);
- }
- #if (defined(__BYTE_ORDER) && defined(__LITTLE_ENDIAN) && \
- __BYTE_ORDER == __LITTLE_ENDIAN) || \
- (defined(i386) || defined(__i386__) || defined(__i486__) || \
- defined(__i586__) || defined(__i686__) || defined(vax) || defined(MIPSEL))
- # define HASH_LITTLE_ENDIAN 1
- # define HASH_BIG_ENDIAN 0
- #elif (defined(__BYTE_ORDER) && defined(__BIG_ENDIAN) && \
- __BYTE_ORDER == __BIG_ENDIAN) || \
- (defined(sparc) || defined(POWERPC) || defined(mc68000) || defined(sel))
- # define HASH_LITTLE_ENDIAN 0
- # define HASH_BIG_ENDIAN 1
- #else
- # define HASH_LITTLE_ENDIAN 0
- # define HASH_BIG_ENDIAN 0
- #endif
- #define hashsize(n) ((uint32_t)1<<(n))
- #define hashmask(n) (hashsize(n)-1)
- #define rot(x,k) (((x)<<(k)) | ((x)>>(32-(k))))
- #define mix(a,b,c) \
- { \
- a -= c; a ^= rot(c, 4); c += b; \
- b -= a; b ^= rot(a, 6); a += c; \
- c -= b; c ^= rot(b, 8); b += a; \
- a -= c; a ^= rot(c,16); c += b; \
- b -= a; b ^= rot(a,19); a += c; \
- c -= b; c ^= rot(b, 4); b += a; \
- }
- #define final(a,b,c) \
- { \
- c ^= b; c -= rot(b,14); \
- a ^= c; a -= rot(c,11); \
- b ^= a; b -= rot(a,25); \
- c ^= b; c -= rot(b,16); \
- a ^= c; a -= rot(c,4); \
- b ^= a; b -= rot(a,14); \
- c ^= b; c -= rot(b,24); \
- }
- static uint32_t hashlittle( const void *key, size_t length, uint32_t initval)
- {
- uint32_t a,b,c;
- union { const void *ptr; size_t i; } u;
-
- a = b = c = 0xdeadbeef + ((uint32_t)length) + initval;
- u.ptr = key;
- if (HASH_LITTLE_ENDIAN && ((u.i & 0x3) == 0)) {
- const uint32_t *k = (const uint32_t *)key;
-
- while (length > 12)
- {
- a += k[0];
- b += k[1];
- c += k[2];
- mix(a,b,c);
- length -= 12;
- k += 3;
- }
-
-
- #ifdef VALGRIND
- # define PRECISE_MEMORY_ACCESS 1
- #elif defined(__SANITIZE_ADDRESS__)
- # define PRECISE_MEMORY_ACCESS 1
- #elif defined(__has_feature)
- # if __has_feature(address_sanitizer)
- # define PRECISE_MEMORY_ACCESS 1
- # endif
- #endif
- #ifndef PRECISE_MEMORY_ACCESS
- switch(length)
- {
- case 12: c+=k[2]; b+=k[1]; a+=k[0]; break;
- case 11: c+=k[2]&0xffffff; b+=k[1]; a+=k[0]; break;
- case 10: c+=k[2]&0xffff; b+=k[1]; a+=k[0]; break;
- case 9 : c+=k[2]&0xff; b+=k[1]; a+=k[0]; break;
- case 8 : b+=k[1]; a+=k[0]; break;
- case 7 : b+=k[1]&0xffffff; a+=k[0]; break;
- case 6 : b+=k[1]&0xffff; a+=k[0]; break;
- case 5 : b+=k[1]&0xff; a+=k[0]; break;
- case 4 : a+=k[0]; break;
- case 3 : a+=k[0]&0xffffff; break;
- case 2 : a+=k[0]&0xffff; break;
- case 1 : a+=k[0]&0xff; break;
- case 0 : return c;
- }
- #else
- const uint8_t *k8 = (const uint8_t *)k;
- switch(length)
- {
- case 12: c+=k[2]; b+=k[1]; a+=k[0]; break;
- case 11: c+=((uint32_t)k8[10])<<16;
- case 10: c+=((uint32_t)k8[9])<<8;
- case 9 : c+=k8[8];
- case 8 : b+=k[1]; a+=k[0]; break;
- case 7 : b+=((uint32_t)k8[6])<<16;
- case 6 : b+=((uint32_t)k8[5])<<8;
- case 5 : b+=k8[4];
- case 4 : a+=k[0]; break;
- case 3 : a+=((uint32_t)k8[2])<<16;
- case 2 : a+=((uint32_t)k8[1])<<8;
- case 1 : a+=k8[0]; break;
- case 0 : return c;
- }
- #endif
- } else if (HASH_LITTLE_ENDIAN && ((u.i & 0x1) == 0)) {
- const uint16_t *k = (const uint16_t *)key;
- const uint8_t *k8;
-
- while (length > 12)
- {
- a += k[0] + (((uint32_t)k[1])<<16);
- b += k[2] + (((uint32_t)k[3])<<16);
- c += k[4] + (((uint32_t)k[5])<<16);
- mix(a,b,c);
- length -= 12;
- k += 6;
- }
-
- k8 = (const uint8_t *)k;
- switch(length)
- {
- case 12: c+=k[4]+(((uint32_t)k[5])<<16);
- b+=k[2]+(((uint32_t)k[3])<<16);
- a+=k[0]+(((uint32_t)k[1])<<16);
- break;
- case 11: c+=((uint32_t)k8[10])<<16;
- case 10: c+=k[4];
- b+=k[2]+(((uint32_t)k[3])<<16);
- a+=k[0]+(((uint32_t)k[1])<<16);
- break;
- case 9 : c+=k8[8];
- case 8 : b+=k[2]+(((uint32_t)k[3])<<16);
- a+=k[0]+(((uint32_t)k[1])<<16);
- break;
- case 7 : b+=((uint32_t)k8[6])<<16;
- case 6 : b+=k[2];
- a+=k[0]+(((uint32_t)k[1])<<16);
- break;
- case 5 : b+=k8[4];
- case 4 : a+=k[0]+(((uint32_t)k[1])<<16);
- break;
- case 3 : a+=((uint32_t)k8[2])<<16;
- case 2 : a+=k[0];
- break;
- case 1 : a+=k8[0];
- break;
- case 0 : return c;
- }
- } else {
- const uint8_t *k = (const uint8_t *)key;
-
- while (length > 12)
- {
- a += k[0];
- a += ((uint32_t)k[1])<<8;
- a += ((uint32_t)k[2])<<16;
- a += ((uint32_t)k[3])<<24;
- b += k[4];
- b += ((uint32_t)k[5])<<8;
- b += ((uint32_t)k[6])<<16;
- b += ((uint32_t)k[7])<<24;
- c += k[8];
- c += ((uint32_t)k[9])<<8;
- c += ((uint32_t)k[10])<<16;
- c += ((uint32_t)k[11])<<24;
- mix(a,b,c);
- length -= 12;
- k += 12;
- }
-
- switch(length)
- {
- case 12: c+=((uint32_t)k[11])<<24;
- case 11: c+=((uint32_t)k[10])<<16;
- case 10: c+=((uint32_t)k[9])<<8;
- case 9 : c+=k[8];
- case 8 : b+=((uint32_t)k[7])<<24;
- case 7 : b+=((uint32_t)k[6])<<16;
- case 6 : b+=((uint32_t)k[5])<<8;
- case 5 : b+=k[4];
- case 4 : a+=((uint32_t)k[3])<<24;
- case 3 : a+=((uint32_t)k[2])<<16;
- case 2 : a+=((uint32_t)k[1])<<8;
- case 1 : a+=k[0];
- break;
- case 0 : return c;
- }
- }
- final(a,b,c);
- return c;
- }
- static unsigned long lh_perllike_str_hash(const void *k)
- {
- const char *rkey = (const char *)k;
- unsigned hashval = 1;
- while (*rkey)
- hashval = hashval * 33 + *rkey++;
- return hashval;
- }
- static unsigned long lh_char_hash(const void *k)
- {
- #if defined _MSC_VER || defined __MINGW32__
- #define RANDOM_SEED_TYPE LONG
- #else
- #define RANDOM_SEED_TYPE int
- #endif
- static volatile RANDOM_SEED_TYPE random_seed = -1;
- if (random_seed == -1) {
- RANDOM_SEED_TYPE seed;
-
- while ((seed = json_c_get_random_seed()) == -1);
- #if SIZEOF_INT == 8 && defined __GCC_HAVE_SYNC_COMPARE_AND_SWAP_8
- #define USE_SYNC_COMPARE_AND_SWAP 1
- #endif
- #if SIZEOF_INT == 4 && defined __GCC_HAVE_SYNC_COMPARE_AND_SWAP_4
- #define USE_SYNC_COMPARE_AND_SWAP 1
- #endif
- #if SIZEOF_INT == 2 && defined __GCC_HAVE_SYNC_COMPARE_AND_SWAP_2
- #define USE_SYNC_COMPARE_AND_SWAP 1
- #endif
- #if defined USE_SYNC_COMPARE_AND_SWAP
- (void)__sync_val_compare_and_swap(&random_seed, -1, seed);
- #elif defined _MSC_VER || defined __MINGW32__
- InterlockedCompareExchange(&random_seed, seed, -1);
- #else
- random_seed = seed;
- #endif
- }
- return hashlittle((const char*)k, strlen((const char*)k), random_seed);
- }
- int lh_char_equal(const void *k1, const void *k2)
- {
- return (strcmp((const char*)k1, (const char*)k2) == 0);
- }
- struct lh_table* lh_table_new(int size,
- lh_entry_free_fn *free_fn,
- lh_hash_fn *hash_fn,
- lh_equal_fn *equal_fn)
- {
- int i;
- struct lh_table *t;
- t = (struct lh_table*)calloc(1, sizeof(struct lh_table));
- if (!t)
- return NULL;
- t->count = 0;
- t->size = size;
- t->table = (struct lh_entry*)calloc(size, sizeof(struct lh_entry));
- if (!t->table)
- {
- free(t);
- return NULL;
- }
- t->free_fn = free_fn;
- t->hash_fn = hash_fn;
- t->equal_fn = equal_fn;
- for(i = 0; i < size; i++) t->table[i].k = LH_EMPTY;
- return t;
- }
- struct lh_table* lh_kchar_table_new(int size,
- lh_entry_free_fn *free_fn)
- {
- return lh_table_new(size, free_fn, char_hash_fn, lh_char_equal);
- }
- struct lh_table* lh_kptr_table_new(int size,
- lh_entry_free_fn *free_fn)
- {
- return lh_table_new(size, free_fn, lh_ptr_hash, lh_ptr_equal);
- }
- int lh_table_resize(struct lh_table *t, int new_size)
- {
- struct lh_table *new_t;
- struct lh_entry *ent;
- new_t = lh_table_new(new_size, NULL, t->hash_fn, t->equal_fn);
- if (new_t == NULL)
- return -1;
- for (ent = t->head; ent != NULL; ent = ent->next)
- {
- unsigned long h = lh_get_hash(new_t, ent->k);
- unsigned int opts = 0;
- if (ent->k_is_constant)
- opts = JSON_C_OBJECT_KEY_IS_CONSTANT;
- if (lh_table_insert_w_hash(new_t, ent->k, ent->v, h, opts) != 0)
- {
- lh_table_free(new_t);
- return -1;
- }
- }
- free(t->table);
- t->table = new_t->table;
- t->size = new_size;
- t->head = new_t->head;
- t->tail = new_t->tail;
- free(new_t);
- return 0;
- }
- void lh_table_free(struct lh_table *t)
- {
- struct lh_entry *c;
- if(t->free_fn) {
- for(c = t->head; c != NULL; c = c->next)
- t->free_fn(c);
- }
- free(t->table);
- free(t);
- }
- int lh_table_insert_w_hash(struct lh_table *t, const void *k, const void *v, const unsigned long h, const unsigned opts)
- {
- unsigned long n;
- if (t->count >= t->size * LH_LOAD_FACTOR)
- if (lh_table_resize(t, t->size * 2) != 0)
- return -1;
- n = h % t->size;
- while( 1 ) {
- if(t->table[n].k == LH_EMPTY || t->table[n].k == LH_FREED) break;
- if ((int)++n == t->size) n = 0;
- }
- t->table[n].k = k;
- t->table[n].k_is_constant = (opts & JSON_C_OBJECT_KEY_IS_CONSTANT);
- t->table[n].v = v;
- t->count++;
- if(t->head == NULL) {
- t->head = t->tail = &t->table[n];
- t->table[n].next = t->table[n].prev = NULL;
- } else {
- t->tail->next = &t->table[n];
- t->table[n].prev = t->tail;
- t->table[n].next = NULL;
- t->tail = &t->table[n];
- }
- return 0;
- }
- int lh_table_insert(struct lh_table *t, const void *k, const void *v)
- {
- return lh_table_insert_w_hash(t, k, v, lh_get_hash(t, k), 0);
- }
- struct lh_entry* lh_table_lookup_entry_w_hash(struct lh_table *t, const void *k, const unsigned long h)
- {
- unsigned long n = h % t->size;
- int count = 0;
- while( count < t->size ) {
- if(t->table[n].k == LH_EMPTY) return NULL;
- if(t->table[n].k != LH_FREED &&
- t->equal_fn(t->table[n].k, k)) return &t->table[n];
- if ((int)++n == t->size) n = 0;
- count++;
- }
- return NULL;
- }
- struct lh_entry* lh_table_lookup_entry(struct lh_table *t, const void *k)
- {
- return lh_table_lookup_entry_w_hash(t, k, lh_get_hash(t, k));
- }
- const void* lh_table_lookup(struct lh_table *t, const void *k)
- {
- void *result;
- lh_table_lookup_ex(t, k, &result);
- return result;
- }
- json_bool lh_table_lookup_ex(struct lh_table* t, const void* k, void **v)
- {
- struct lh_entry *e = lh_table_lookup_entry(t, k);
- if (e != NULL) {
- if (v != NULL) *v = lh_entry_v(e);
- return TRUE;
- }
- if (v != NULL) *v = NULL;
- return FALSE;
- }
- int lh_table_delete_entry(struct lh_table *t, struct lh_entry *e)
- {
- ptrdiff_t n = (ptrdiff_t)(e - t->table);
-
- if(n < 0) { return -2; }
- if(t->table[n].k == LH_EMPTY || t->table[n].k == LH_FREED) return -1;
- t->count--;
- if(t->free_fn) t->free_fn(e);
- t->table[n].v = NULL;
- t->table[n].k = LH_FREED;
- if(t->tail == &t->table[n] && t->head == &t->table[n]) {
- t->head = t->tail = NULL;
- } else if (t->head == &t->table[n]) {
- t->head->next->prev = NULL;
- t->head = t->head->next;
- } else if (t->tail == &t->table[n]) {
- t->tail->prev->next = NULL;
- t->tail = t->tail->prev;
- } else {
- t->table[n].prev->next = t->table[n].next;
- t->table[n].next->prev = t->table[n].prev;
- }
- t->table[n].next = t->table[n].prev = NULL;
- return 0;
- }
- int lh_table_delete(struct lh_table *t, const void *k)
- {
- struct lh_entry *e = lh_table_lookup_entry(t, k);
- if(!e) return -1;
- return lh_table_delete_entry(t, e);
- }
- int lh_table_length(struct lh_table *t)
- {
- return t->count;
- }
|