2009-02-07 02:26:27 +08:00
|
|
|
/*
|
2014-01-28 15:16:05 +08:00
|
|
|
* Copyright (c) 2009-2014 Petri Lehtinen <petri@digip.org>
|
2009-02-07 02:26:27 +08:00
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the MIT license. See LICENSE for details.
|
|
|
|
*/
|
|
|
|
|
2014-01-14 17:16:39 +08:00
|
|
|
#if HAVE_CONFIG_H
|
2014-02-11 20:49:15 +08:00
|
|
|
#include <jansson_private_config.h>
|
2014-01-14 17:16:39 +08:00
|
|
|
#endif
|
|
|
|
|
2009-02-07 02:26:27 +08:00
|
|
|
#include <stdlib.h>
|
2012-01-25 02:36:31 +08:00
|
|
|
#include <string.h>
|
2014-01-14 17:16:39 +08:00
|
|
|
|
|
|
|
#if HAVE_STDINT_H
|
|
|
|
#include <stdint.h>
|
|
|
|
#endif
|
|
|
|
|
2012-04-30 19:39:45 +08:00
|
|
|
#include <jansson_config.h> /* for JSON_INLINE */
|
2010-09-05 22:13:21 +08:00
|
|
|
#include "jansson_private.h" /* for container_of() */
|
2009-02-07 02:26:27 +08:00
|
|
|
#include "hashtable.h"
|
|
|
|
|
2014-12-18 20:43:07 +08:00
|
|
|
#ifndef INITIAL_HASHTABLE_ORDER
|
|
|
|
#define INITIAL_HASHTABLE_ORDER 3
|
|
|
|
#endif
|
|
|
|
|
2009-04-30 21:38:39 +08:00
|
|
|
typedef struct hashtable_list list_t;
|
|
|
|
typedef struct hashtable_pair pair_t;
|
|
|
|
typedef struct hashtable_bucket bucket_t;
|
|
|
|
|
2014-01-14 17:16:39 +08:00
|
|
|
extern volatile uint32_t hashtable_seed;
|
2012-01-25 02:36:31 +08:00
|
|
|
|
2014-01-14 17:16:39 +08:00
|
|
|
/* Implementation of the hash function */
|
|
|
|
#include "lookup3.h"
|
2012-01-25 02:36:31 +08:00
|
|
|
|
2014-01-14 17:16:39 +08:00
|
|
|
#define list_to_pair(list_) container_of(list_, pair_t, list)
|
|
|
|
#define hash_str(key) ((size_t)hashlittle((key), strlen(key), hashtable_seed))
|
2012-01-25 02:36:31 +08:00
|
|
|
|
2010-09-05 22:13:21 +08:00
|
|
|
static JSON_INLINE void list_init(list_t *list)
|
2009-02-07 02:26:27 +08:00
|
|
|
{
|
|
|
|
list->next = list;
|
|
|
|
list->prev = list;
|
|
|
|
}
|
|
|
|
|
2010-09-05 22:13:21 +08:00
|
|
|
static JSON_INLINE void list_insert(list_t *list, list_t *node)
|
2009-02-07 02:26:27 +08:00
|
|
|
{
|
|
|
|
node->next = list;
|
|
|
|
node->prev = list->prev;
|
|
|
|
list->prev->next = node;
|
|
|
|
list->prev = node;
|
|
|
|
}
|
|
|
|
|
2010-09-05 22:13:21 +08:00
|
|
|
static JSON_INLINE void list_remove(list_t *list)
|
2009-02-07 02:26:27 +08:00
|
|
|
{
|
|
|
|
list->prev->next = list->next;
|
|
|
|
list->next->prev = list->prev;
|
|
|
|
}
|
|
|
|
|
2010-09-05 22:13:21 +08:00
|
|
|
static JSON_INLINE int bucket_is_empty(hashtable_t *hashtable, bucket_t *bucket)
|
2009-02-07 02:26:27 +08:00
|
|
|
{
|
|
|
|
return bucket->first == &hashtable->list && bucket->first == bucket->last;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void insert_to_bucket(hashtable_t *hashtable, bucket_t *bucket,
|
|
|
|
list_t *list)
|
|
|
|
{
|
|
|
|
if(bucket_is_empty(hashtable, bucket))
|
|
|
|
{
|
|
|
|
list_insert(&hashtable->list, list);
|
|
|
|
bucket->first = bucket->last = list;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
list_insert(bucket->first, list);
|
|
|
|
bucket->first = list;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static pair_t *hashtable_find_pair(hashtable_t *hashtable, bucket_t *bucket,
|
2012-01-25 02:36:31 +08:00
|
|
|
const char *key, size_t hash)
|
2009-02-07 02:26:27 +08:00
|
|
|
{
|
|
|
|
list_t *list;
|
|
|
|
pair_t *pair;
|
|
|
|
|
|
|
|
if(bucket_is_empty(hashtable, bucket))
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
list = bucket->first;
|
|
|
|
while(1)
|
|
|
|
{
|
|
|
|
pair = list_to_pair(list);
|
2012-01-25 02:36:31 +08:00
|
|
|
if(pair->hash == hash && strcmp(pair->key, key) == 0)
|
2009-02-07 02:26:27 +08:00
|
|
|
return pair;
|
|
|
|
|
|
|
|
if(list == bucket->last)
|
|
|
|
break;
|
|
|
|
|
|
|
|
list = list->next;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* returns 0 on success, -1 if key was not found */
|
|
|
|
static int hashtable_do_del(hashtable_t *hashtable,
|
2012-01-25 02:36:31 +08:00
|
|
|
const char *key, size_t hash)
|
2009-02-07 02:26:27 +08:00
|
|
|
{
|
|
|
|
pair_t *pair;
|
|
|
|
bucket_t *bucket;
|
2010-06-15 20:27:35 +08:00
|
|
|
size_t index;
|
2009-02-07 02:26:27 +08:00
|
|
|
|
2014-01-14 17:16:39 +08:00
|
|
|
index = hash & hashmask(hashtable->order);
|
2009-02-07 02:26:27 +08:00
|
|
|
bucket = &hashtable->buckets[index];
|
|
|
|
|
|
|
|
pair = hashtable_find_pair(hashtable, bucket, key, hash);
|
|
|
|
if(!pair)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
if(&pair->list == bucket->first && &pair->list == bucket->last)
|
|
|
|
bucket->first = bucket->last = &hashtable->list;
|
|
|
|
|
|
|
|
else if(&pair->list == bucket->first)
|
|
|
|
bucket->first = pair->list.next;
|
|
|
|
|
|
|
|
else if(&pair->list == bucket->last)
|
|
|
|
bucket->last = pair->list.prev;
|
|
|
|
|
|
|
|
list_remove(&pair->list);
|
2012-01-25 02:36:31 +08:00
|
|
|
json_decref(pair->value);
|
2009-02-07 02:26:27 +08:00
|
|
|
|
2011-03-31 21:37:41 +08:00
|
|
|
jsonp_free(pair);
|
2009-02-07 02:26:27 +08:00
|
|
|
hashtable->size--;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-10-12 01:42:43 +08:00
|
|
|
static void hashtable_do_clear(hashtable_t *hashtable)
|
|
|
|
{
|
|
|
|
list_t *list, *next;
|
|
|
|
pair_t *pair;
|
|
|
|
|
|
|
|
for(list = hashtable->list.next; list != &hashtable->list; list = next)
|
|
|
|
{
|
|
|
|
next = list->next;
|
|
|
|
pair = list_to_pair(list);
|
2012-01-25 02:36:31 +08:00
|
|
|
json_decref(pair->value);
|
2011-02-17 16:10:53 +08:00
|
|
|
jsonp_free(pair);
|
2009-10-12 01:42:43 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-02-07 02:26:27 +08:00
|
|
|
static int hashtable_do_rehash(hashtable_t *hashtable)
|
|
|
|
{
|
|
|
|
list_t *list, *next;
|
|
|
|
pair_t *pair;
|
2010-06-15 20:27:35 +08:00
|
|
|
size_t i, index, new_size;
|
2009-02-07 02:26:27 +08:00
|
|
|
|
2011-02-17 16:10:53 +08:00
|
|
|
jsonp_free(hashtable->buckets);
|
2009-02-07 02:26:27 +08:00
|
|
|
|
2014-01-14 17:16:39 +08:00
|
|
|
hashtable->order++;
|
|
|
|
new_size = hashsize(hashtable->order);
|
2009-02-07 02:26:27 +08:00
|
|
|
|
2011-02-17 16:10:53 +08:00
|
|
|
hashtable->buckets = jsonp_malloc(new_size * sizeof(bucket_t));
|
2009-02-07 02:26:27 +08:00
|
|
|
if(!hashtable->buckets)
|
|
|
|
return -1;
|
|
|
|
|
2014-01-14 17:16:39 +08:00
|
|
|
for(i = 0; i < hashsize(hashtable->order); i++)
|
2009-02-07 02:26:27 +08:00
|
|
|
{
|
|
|
|
hashtable->buckets[i].first = hashtable->buckets[i].last =
|
|
|
|
&hashtable->list;
|
|
|
|
}
|
|
|
|
|
|
|
|
list = hashtable->list.next;
|
|
|
|
list_init(&hashtable->list);
|
|
|
|
|
|
|
|
for(; list != &hashtable->list; list = next) {
|
|
|
|
next = list->next;
|
|
|
|
pair = list_to_pair(list);
|
|
|
|
index = pair->hash % new_size;
|
|
|
|
insert_to_bucket(hashtable, &hashtable->buckets[index], &pair->list);
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-01-25 02:36:31 +08:00
|
|
|
int hashtable_init(hashtable_t *hashtable)
|
2009-04-30 21:38:39 +08:00
|
|
|
{
|
2010-06-15 20:27:35 +08:00
|
|
|
size_t i;
|
2009-04-30 21:38:39 +08:00
|
|
|
|
|
|
|
hashtable->size = 0;
|
2014-12-18 20:43:07 +08:00
|
|
|
hashtable->order = INITIAL_HASHTABLE_ORDER;
|
2014-01-14 17:16:39 +08:00
|
|
|
hashtable->buckets = jsonp_malloc(hashsize(hashtable->order) * sizeof(bucket_t));
|
2009-04-30 21:38:39 +08:00
|
|
|
if(!hashtable->buckets)
|
|
|
|
return -1;
|
|
|
|
|
2009-02-07 02:26:27 +08:00
|
|
|
list_init(&hashtable->list);
|
|
|
|
|
2014-01-14 17:16:39 +08:00
|
|
|
for(i = 0; i < hashsize(hashtable->order); i++)
|
2009-02-07 02:26:27 +08:00
|
|
|
{
|
|
|
|
hashtable->buckets[i].first = hashtable->buckets[i].last =
|
|
|
|
&hashtable->list;
|
|
|
|
}
|
|
|
|
|
2009-04-30 21:38:39 +08:00
|
|
|
return 0;
|
2009-02-07 02:26:27 +08:00
|
|
|
}
|
|
|
|
|
2009-04-30 21:38:39 +08:00
|
|
|
void hashtable_close(hashtable_t *hashtable)
|
2009-02-07 02:26:27 +08:00
|
|
|
{
|
2009-10-12 01:42:43 +08:00
|
|
|
hashtable_do_clear(hashtable);
|
2011-02-17 16:10:53 +08:00
|
|
|
jsonp_free(hashtable->buckets);
|
2009-02-07 02:26:27 +08:00
|
|
|
}
|
|
|
|
|
2012-01-25 02:36:31 +08:00
|
|
|
int hashtable_set(hashtable_t *hashtable,
|
|
|
|
const char *key, size_t serial,
|
|
|
|
json_t *value)
|
2009-02-07 02:26:27 +08:00
|
|
|
{
|
|
|
|
pair_t *pair;
|
|
|
|
bucket_t *bucket;
|
2010-06-15 20:27:35 +08:00
|
|
|
size_t hash, index;
|
2009-02-07 02:26:27 +08:00
|
|
|
|
|
|
|
/* rehash if the load ratio exceeds 1 */
|
2014-01-14 17:16:39 +08:00
|
|
|
if(hashtable->size >= hashsize(hashtable->order))
|
2009-02-07 02:26:27 +08:00
|
|
|
if(hashtable_do_rehash(hashtable))
|
|
|
|
return -1;
|
|
|
|
|
2012-01-25 02:36:31 +08:00
|
|
|
hash = hash_str(key);
|
2014-01-14 17:16:39 +08:00
|
|
|
index = hash & hashmask(hashtable->order);
|
2009-02-07 02:26:27 +08:00
|
|
|
bucket = &hashtable->buckets[index];
|
2010-02-10 02:51:25 +08:00
|
|
|
pair = hashtable_find_pair(hashtable, bucket, key, hash);
|
|
|
|
|
|
|
|
if(pair)
|
|
|
|
{
|
2012-01-25 02:36:31 +08:00
|
|
|
json_decref(pair->value);
|
2010-02-10 02:51:25 +08:00
|
|
|
pair->value = value;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-01-25 02:36:31 +08:00
|
|
|
/* offsetof(...) returns the size of pair_t without the last,
|
|
|
|
flexible member. This way, the correct amount is
|
|
|
|
allocated. */
|
2013-10-15 13:44:04 +08:00
|
|
|
|
|
|
|
size_t len = strlen(key);
|
2013-10-15 13:49:40 +08:00
|
|
|
if(len >= (size_t)-1 - offsetof(pair_t, key)) {
|
2013-10-15 13:44:04 +08:00
|
|
|
/* Avoid an overflow if the key is very long */
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2013-10-15 13:46:16 +08:00
|
|
|
pair = jsonp_malloc(offsetof(pair_t, key) + len + 1);
|
2010-02-10 02:51:25 +08:00
|
|
|
if(!pair)
|
|
|
|
return -1;
|
2009-02-07 02:26:27 +08:00
|
|
|
|
2010-02-10 02:51:25 +08:00
|
|
|
pair->hash = hash;
|
2012-01-25 02:36:31 +08:00
|
|
|
pair->serial = serial;
|
|
|
|
strcpy(pair->key, key);
|
|
|
|
pair->value = value;
|
2010-02-10 02:51:25 +08:00
|
|
|
list_init(&pair->list);
|
2009-02-07 02:26:27 +08:00
|
|
|
|
2010-02-10 02:51:25 +08:00
|
|
|
insert_to_bucket(hashtable, bucket, &pair->list);
|
|
|
|
|
|
|
|
hashtable->size++;
|
|
|
|
}
|
2009-02-07 02:26:27 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-01-25 02:36:31 +08:00
|
|
|
void *hashtable_get(hashtable_t *hashtable, const char *key)
|
2009-02-07 02:26:27 +08:00
|
|
|
{
|
|
|
|
pair_t *pair;
|
2010-06-15 20:27:35 +08:00
|
|
|
size_t hash;
|
2009-02-07 02:26:27 +08:00
|
|
|
bucket_t *bucket;
|
|
|
|
|
2012-01-25 02:36:31 +08:00
|
|
|
hash = hash_str(key);
|
2014-01-14 17:16:39 +08:00
|
|
|
bucket = &hashtable->buckets[hash & hashmask(hashtable->order)];
|
2009-02-07 02:26:27 +08:00
|
|
|
|
|
|
|
pair = hashtable_find_pair(hashtable, bucket, key, hash);
|
|
|
|
if(!pair)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
return pair->value;
|
|
|
|
}
|
|
|
|
|
2012-01-25 02:36:31 +08:00
|
|
|
int hashtable_del(hashtable_t *hashtable, const char *key)
|
2009-02-07 02:26:27 +08:00
|
|
|
{
|
2012-01-25 02:36:31 +08:00
|
|
|
size_t hash = hash_str(key);
|
2009-02-07 02:26:27 +08:00
|
|
|
return hashtable_do_del(hashtable, key, hash);
|
|
|
|
}
|
|
|
|
|
2009-10-12 01:42:43 +08:00
|
|
|
void hashtable_clear(hashtable_t *hashtable)
|
|
|
|
{
|
2010-06-15 20:27:35 +08:00
|
|
|
size_t i;
|
2009-10-12 01:42:43 +08:00
|
|
|
|
|
|
|
hashtable_do_clear(hashtable);
|
|
|
|
|
2014-01-14 17:16:39 +08:00
|
|
|
for(i = 0; i < hashsize(hashtable->order); i++)
|
2009-10-12 01:42:43 +08:00
|
|
|
{
|
|
|
|
hashtable->buckets[i].first = hashtable->buckets[i].last =
|
|
|
|
&hashtable->list;
|
|
|
|
}
|
|
|
|
|
|
|
|
list_init(&hashtable->list);
|
|
|
|
hashtable->size = 0;
|
|
|
|
}
|
|
|
|
|
2009-02-07 02:26:27 +08:00
|
|
|
void *hashtable_iter(hashtable_t *hashtable)
|
|
|
|
{
|
|
|
|
return hashtable_iter_next(hashtable, &hashtable->list);
|
|
|
|
}
|
|
|
|
|
2012-01-25 02:36:31 +08:00
|
|
|
void *hashtable_iter_at(hashtable_t *hashtable, const char *key)
|
2010-02-02 03:01:24 +08:00
|
|
|
{
|
|
|
|
pair_t *pair;
|
2010-06-15 20:27:35 +08:00
|
|
|
size_t hash;
|
2010-02-02 03:01:24 +08:00
|
|
|
bucket_t *bucket;
|
|
|
|
|
2012-01-25 02:36:31 +08:00
|
|
|
hash = hash_str(key);
|
2014-01-14 17:16:39 +08:00
|
|
|
bucket = &hashtable->buckets[hash & hashmask(hashtable->order)];
|
2010-02-02 03:01:24 +08:00
|
|
|
|
|
|
|
pair = hashtable_find_pair(hashtable, bucket, key, hash);
|
|
|
|
if(!pair)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
return &pair->list;
|
|
|
|
}
|
|
|
|
|
2009-02-07 02:26:27 +08:00
|
|
|
void *hashtable_iter_next(hashtable_t *hashtable, void *iter)
|
|
|
|
{
|
|
|
|
list_t *list = (list_t *)iter;
|
|
|
|
if(list->next == &hashtable->list)
|
|
|
|
return NULL;
|
|
|
|
return list->next;
|
|
|
|
}
|
|
|
|
|
|
|
|
void *hashtable_iter_key(void *iter)
|
|
|
|
{
|
|
|
|
pair_t *pair = list_to_pair((list_t *)iter);
|
|
|
|
return pair->key;
|
|
|
|
}
|
|
|
|
|
2012-01-25 02:36:31 +08:00
|
|
|
size_t hashtable_iter_serial(void *iter)
|
|
|
|
{
|
|
|
|
pair_t *pair = list_to_pair((list_t *)iter);
|
|
|
|
return pair->serial;
|
|
|
|
}
|
|
|
|
|
2009-02-07 02:26:27 +08:00
|
|
|
void *hashtable_iter_value(void *iter)
|
|
|
|
{
|
|
|
|
pair_t *pair = list_to_pair((list_t *)iter);
|
|
|
|
return pair->value;
|
|
|
|
}
|
2010-02-02 03:01:24 +08:00
|
|
|
|
2012-01-25 02:36:31 +08:00
|
|
|
void hashtable_iter_set(void *iter, json_t *value)
|
2010-02-02 03:01:24 +08:00
|
|
|
{
|
|
|
|
pair_t *pair = list_to_pair((list_t *)iter);
|
|
|
|
|
2012-01-25 02:36:31 +08:00
|
|
|
json_decref(pair->value);
|
2010-02-02 03:01:24 +08:00
|
|
|
pair->value = value;
|
|
|
|
}
|