proxmark3/client/deps/jansson/hashtable.c

325 lines
8.4 KiB
C
Raw Normal View History

2018-11-12 21:00:35 +08:00
/*
* Copyright (c) 2009-2016 Petri Lehtinen <petri@digip.org>
*
* This library is free software; you can redistribute it and/or modify
* it under the terms of the MIT license. See LICENSE for details.
*/
#if HAVE_CONFIG_H
#include <jansson_private_config.h>
#endif
#include <stdlib.h>
#include <string.h>
#if HAVE_STDINT_H
#include <stdint.h>
#endif
2020-04-16 16:25:29 +08:00
#include "jansson_config.h" /* for JSON_INLINE */
2018-11-12 21:00:35 +08:00
#include "jansson_private.h" /* for container_of() */
#include "hashtable.h"
#ifndef INITIAL_HASHTABLE_ORDER
#define INITIAL_HASHTABLE_ORDER 3
#endif
typedef struct hashtable_list list_t;
typedef struct hashtable_pair pair_t;
typedef struct hashtable_bucket bucket_t;
extern volatile uint32_t hashtable_seed;
/* Implementation of the hash function */
#include "lookup3.h"
#define list_to_pair(list_) container_of(list_, pair_t, list)
#define ordered_list_to_pair(list_) container_of(list_, pair_t, ordered_list)
#define hash_str(key) ((size_t)hashlittle((key), strlen(key), hashtable_seed))
static JSON_INLINE void list_init(list_t *list) {
2018-11-12 21:00:35 +08:00
list->next = list;
list->prev = list;
}
static JSON_INLINE void list_insert(list_t *list, list_t *node) {
2018-11-12 21:00:35 +08:00
node->next = list;
node->prev = list->prev;
list->prev->next = node;
list->prev = node;
}
static JSON_INLINE void list_remove(list_t *list) {
2018-11-12 21:00:35 +08:00
list->prev->next = list->next;
list->next->prev = list->prev;
}
static JSON_INLINE int bucket_is_empty(hashtable_t *hashtable, bucket_t *bucket) {
2018-11-12 21:00:35 +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) {
2019-03-10 07:00:59 +08:00
if (bucket_is_empty(hashtable, bucket)) {
2018-11-12 21:00:35 +08:00
list_insert(&hashtable->list, list);
bucket->first = bucket->last = list;
2019-03-10 07:00:59 +08:00
} else {
2018-11-12 21:00:35 +08:00
list_insert(bucket->first, list);
bucket->first = list;
}
}
static pair_t *hashtable_find_pair(hashtable_t *hashtable, bucket_t *bucket,
const char *key, size_t hash) {
2018-11-12 21:00:35 +08:00
list_t *list;
pair_t *pair;
2019-03-10 07:00:59 +08:00
if (bucket_is_empty(hashtable, bucket))
2018-11-12 21:00:35 +08:00
return NULL;
list = bucket->first;
2019-03-10 07:00:59 +08:00
while (1) {
2018-11-12 21:00:35 +08:00
pair = list_to_pair(list);
2019-03-10 07:00:59 +08:00
if (pair->hash == hash && strcmp(pair->key, key) == 0)
2018-11-12 21:00:35 +08:00
return pair;
2019-03-10 07:00:59 +08:00
if (list == bucket->last)
2018-11-12 21:00:35 +08:00
break;
list = list->next;
}
return NULL;
}
/* returns 0 on success, -1 if key was not found */
static int hashtable_do_del(hashtable_t *hashtable,
const char *key, size_t hash) {
2018-11-12 21:00:35 +08:00
pair_t *pair;
bucket_t *bucket;
size_t index;
index = hash & hashmask(hashtable->order);
bucket = &hashtable->buckets[index];
pair = hashtable_find_pair(hashtable, bucket, key, hash);
2019-03-10 07:00:59 +08:00
if (!pair)
2018-11-12 21:00:35 +08:00
return -1;
2019-03-10 07:00:59 +08:00
if (&pair->list == bucket->first && &pair->list == bucket->last)
2018-11-12 21:00:35 +08:00
bucket->first = bucket->last = &hashtable->list;
2019-03-10 07:00:59 +08:00
else if (&pair->list == bucket->first)
2018-11-12 21:00:35 +08:00
bucket->first = pair->list.next;
2019-03-10 07:00:59 +08:00
else if (&pair->list == bucket->last)
2018-11-12 21:00:35 +08:00
bucket->last = pair->list.prev;
list_remove(&pair->list);
list_remove(&pair->ordered_list);
json_decref(pair->value);
jsonp_free(pair);
hashtable->size--;
return 0;
}
static void hashtable_do_clear(hashtable_t *hashtable) {
2018-11-12 21:00:35 +08:00
list_t *list, *next;
pair_t *pair;
2019-03-10 07:00:59 +08:00
for (list = hashtable->list.next; list != &hashtable->list; list = next) {
2018-11-12 21:00:35 +08:00
next = list->next;
pair = list_to_pair(list);
json_decref(pair->value);
jsonp_free(pair);
}
}
static int hashtable_do_rehash(hashtable_t *hashtable) {
2018-11-12 21:00:35 +08:00
list_t *list, *next;
pair_t *pair;
size_t i, index, new_size, new_order;
struct hashtable_bucket *new_buckets;
new_order = hashtable->order + 1;
new_size = hashsize(new_order);
new_buckets = jsonp_malloc(new_size * sizeof(bucket_t));
2019-03-10 07:00:59 +08:00
if (!new_buckets)
2018-11-12 21:00:35 +08:00
return -1;
jsonp_free(hashtable->buckets);
hashtable->buckets = new_buckets;
hashtable->order = new_order;
2019-03-10 07:00:59 +08:00
for (i = 0; i < hashsize(hashtable->order); i++) {
2018-11-12 21:00:35 +08:00
hashtable->buckets[i].first = hashtable->buckets[i].last =
2019-03-10 07:00:59 +08:00
&hashtable->list;
2018-11-12 21:00:35 +08:00
}
list = hashtable->list.next;
list_init(&hashtable->list);
2019-03-10 07:00:59 +08:00
for (; list != &hashtable->list; list = next) {
2018-11-12 21:00:35 +08:00
next = list->next;
pair = list_to_pair(list);
index = pair->hash % new_size;
insert_to_bucket(hashtable, &hashtable->buckets[index], &pair->list);
}
return 0;
}
int hashtable_init(hashtable_t *hashtable) {
2018-11-12 21:00:35 +08:00
size_t i;
hashtable->size = 0;
hashtable->order = INITIAL_HASHTABLE_ORDER;
hashtable->buckets = jsonp_malloc(hashsize(hashtable->order) * sizeof(bucket_t));
2019-03-10 07:00:59 +08:00
if (!hashtable->buckets)
2018-11-12 21:00:35 +08:00
return -1;
list_init(&hashtable->list);
list_init(&hashtable->ordered_list);
2019-03-10 07:00:59 +08:00
for (i = 0; i < hashsize(hashtable->order); i++) {
2018-11-12 21:00:35 +08:00
hashtable->buckets[i].first = hashtable->buckets[i].last =
2019-03-10 07:00:59 +08:00
&hashtable->list;
2018-11-12 21:00:35 +08:00
}
return 0;
}
void hashtable_close(hashtable_t *hashtable) {
2018-11-12 21:00:35 +08:00
hashtable_do_clear(hashtable);
jsonp_free(hashtable->buckets);
}
int hashtable_set(hashtable_t *hashtable, const char *key, json_t *value) {
2018-11-12 21:00:35 +08:00
pair_t *pair;
bucket_t *bucket;
size_t hash, index;
/* rehash if the load ratio exceeds 1 */
2019-03-10 07:00:59 +08:00
if (hashtable->size >= hashsize(hashtable->order))
if (hashtable_do_rehash(hashtable))
2018-11-12 21:00:35 +08:00
return -1;
hash = hash_str(key);
index = hash & hashmask(hashtable->order);
bucket = &hashtable->buckets[index];
pair = hashtable_find_pair(hashtable, bucket, key, hash);
2019-03-10 07:00:59 +08:00
if (pair) {
2018-11-12 21:00:35 +08:00
json_decref(pair->value);
pair->value = value;
2019-03-10 07:00:59 +08:00
} else {
2018-11-12 21:00:35 +08:00
/* offsetof(...) returns the size of pair_t without the last,
flexible member. This way, the correct amount is
allocated. */
size_t len = strlen(key);
2020-01-01 04:39:37 +08:00
if (len >= (size_t) - 1 - offsetof(pair_t, key)) {
2018-11-12 21:00:35 +08:00
/* Avoid an overflow if the key is very long */
return -1;
}
pair = jsonp_malloc(offsetof(pair_t, key) + len + 1);
2019-03-10 07:00:59 +08:00
if (!pair)
2018-11-12 21:00:35 +08:00
return -1;
pair->hash = hash;
strncpy(pair->key, key, len + 1);
pair->value = value;
list_init(&pair->list);
list_init(&pair->ordered_list);
insert_to_bucket(hashtable, bucket, &pair->list);
list_insert(&hashtable->ordered_list, &pair->ordered_list);
hashtable->size++;
}
return 0;
}
void *hashtable_get(hashtable_t *hashtable, const char *key) {
2018-11-12 21:00:35 +08:00
pair_t *pair;
size_t hash;
bucket_t *bucket;
hash = hash_str(key);
bucket = &hashtable->buckets[hash & hashmask(hashtable->order)];
pair = hashtable_find_pair(hashtable, bucket, key, hash);
2019-03-10 07:00:59 +08:00
if (!pair)
2018-11-12 21:00:35 +08:00
return NULL;
return pair->value;
}
int hashtable_del(hashtable_t *hashtable, const char *key) {
2018-11-12 21:00:35 +08:00
size_t hash = hash_str(key);
return hashtable_do_del(hashtable, key, hash);
}
void hashtable_clear(hashtable_t *hashtable) {
2018-11-12 21:00:35 +08:00
size_t i;
hashtable_do_clear(hashtable);
2019-03-10 07:00:59 +08:00
for (i = 0; i < hashsize(hashtable->order); i++) {
2018-11-12 21:00:35 +08:00
hashtable->buckets[i].first = hashtable->buckets[i].last =
2019-03-10 07:00:59 +08:00
&hashtable->list;
2018-11-12 21:00:35 +08:00
}
list_init(&hashtable->list);
list_init(&hashtable->ordered_list);
hashtable->size = 0;
}
void *hashtable_iter(hashtable_t *hashtable) {
2018-11-12 21:00:35 +08:00
return hashtable_iter_next(hashtable, &hashtable->ordered_list);
}
void *hashtable_iter_at(hashtable_t *hashtable, const char *key) {
2018-11-12 21:00:35 +08:00
pair_t *pair;
size_t hash;
bucket_t *bucket;
hash = hash_str(key);
bucket = &hashtable->buckets[hash & hashmask(hashtable->order)];
pair = hashtable_find_pair(hashtable, bucket, key, hash);
2019-03-10 07:00:59 +08:00
if (!pair)
2018-11-12 21:00:35 +08:00
return NULL;
return &pair->ordered_list;
}
void *hashtable_iter_next(hashtable_t *hashtable, void *iter) {
2018-11-12 21:00:35 +08:00
list_t *list = (list_t *)iter;
2019-03-10 07:00:59 +08:00
if (list->next == &hashtable->ordered_list)
2018-11-12 21:00:35 +08:00
return NULL;
return list->next;
}
void *hashtable_iter_key(void *iter) {
2018-11-12 21:00:35 +08:00
pair_t *pair = ordered_list_to_pair((list_t *)iter);
return pair->key;
}
void *hashtable_iter_value(void *iter) {
2018-11-12 21:00:35 +08:00
pair_t *pair = ordered_list_to_pair((list_t *)iter);
return pair->value;
}
void hashtable_iter_set(void *iter, json_t *value) {
2018-11-12 21:00:35 +08:00
pair_t *pair = ordered_list_to_pair((list_t *)iter);
json_decref(pair->value);
pair->value = value;
}