2016-05-26 21:23:19 +02:00
|
|
|
/*
|
2018-02-23 09:53:12 +01:00
|
|
|
* Copyright (C) Internet Systems Consortium, Inc. ("ISC")
|
2016-05-26 21:23:19 +02:00
|
|
|
*
|
2016-06-27 14:56:38 +10:00
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
2018-02-23 09:53:12 +01:00
|
|
|
*
|
|
|
|
* See the COPYRIGHT file distributed with this work for additional
|
|
|
|
* information regarding copyright ownership.
|
2016-05-26 21:23:19 +02:00
|
|
|
*/
|
|
|
|
|
2018-03-28 14:19:37 +02:00
|
|
|
#include <inttypes.h>
|
2016-05-26 21:23:19 +02:00
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include <isc/hash.h>
|
|
|
|
#include <isc/ht.h>
|
|
|
|
#include <isc/types.h>
|
|
|
|
#include <isc/magic.h>
|
|
|
|
#include <isc/mem.h>
|
|
|
|
#include <isc/result.h>
|
|
|
|
#include <isc/util.h>
|
|
|
|
|
|
|
|
|
|
|
|
typedef struct isc_ht_node isc_ht_node_t;
|
|
|
|
|
|
|
|
#define ISC_HT_MAGIC ISC_MAGIC('H', 'T', 'a', 'b')
|
|
|
|
#define ISC_HT_VALID(ht) ISC_MAGIC_VALID(ht, ISC_HT_MAGIC)
|
|
|
|
|
|
|
|
struct isc_ht_node {
|
|
|
|
void *value;
|
|
|
|
isc_ht_node_t *next;
|
|
|
|
size_t keysize;
|
2018-01-24 13:10:14 +11:00
|
|
|
unsigned char key[FLEXIBLE_ARRAY_MEMBER];
|
2016-05-26 21:23:19 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
struct isc_ht {
|
|
|
|
unsigned int magic;
|
|
|
|
isc_mem_t *mctx;
|
|
|
|
size_t size;
|
|
|
|
size_t mask;
|
|
|
|
unsigned int count;
|
|
|
|
isc_ht_node_t **table;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct isc_ht_iter {
|
|
|
|
isc_ht_t *ht;
|
2016-06-01 14:49:01 +02:00
|
|
|
size_t i;
|
2016-05-26 21:23:19 +02:00
|
|
|
isc_ht_node_t *cur;
|
|
|
|
};
|
|
|
|
|
|
|
|
isc_result_t
|
2018-03-28 14:19:37 +02:00
|
|
|
isc_ht_init(isc_ht_t **htp, isc_mem_t *mctx, uint8_t bits) {
|
2016-05-26 21:23:19 +02:00
|
|
|
isc_ht_t *ht = NULL;
|
2016-06-01 12:01:06 +02:00
|
|
|
size_t i;
|
2016-05-26 21:23:19 +02:00
|
|
|
|
|
|
|
REQUIRE(htp != NULL && *htp == NULL);
|
|
|
|
REQUIRE(mctx != NULL);
|
|
|
|
REQUIRE(bits >= 1 && bits <= (sizeof(size_t)*8 - 1));
|
|
|
|
|
|
|
|
ht = isc_mem_get(mctx, sizeof(struct isc_ht));
|
|
|
|
if (ht == NULL) {
|
|
|
|
return (ISC_R_NOMEMORY);
|
|
|
|
}
|
|
|
|
|
|
|
|
ht->mctx = NULL;
|
|
|
|
isc_mem_attach(mctx, &ht->mctx);
|
|
|
|
|
2016-06-01 13:54:09 +10:00
|
|
|
ht->size = ((size_t)1<<bits);
|
|
|
|
ht->mask = ((size_t)1<<bits)-1;
|
2016-05-26 21:23:19 +02:00
|
|
|
ht->count = 0;
|
|
|
|
|
|
|
|
ht->table = isc_mem_get(ht->mctx, ht->size * sizeof(isc_ht_node_t*));
|
|
|
|
if (ht->table == NULL) {
|
|
|
|
isc_mem_putanddetach(&ht->mctx, ht, sizeof(struct isc_ht));
|
|
|
|
return (ISC_R_NOMEMORY);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; i < ht->size; i++) {
|
|
|
|
ht->table[i] = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
ht->magic = ISC_HT_MAGIC;
|
|
|
|
|
|
|
|
*htp = ht;
|
|
|
|
return (ISC_R_SUCCESS);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
isc_ht_destroy(isc_ht_t **htp) {
|
|
|
|
isc_ht_t *ht;
|
2016-06-01 12:01:06 +02:00
|
|
|
size_t i;
|
2016-05-26 21:23:19 +02:00
|
|
|
|
|
|
|
REQUIRE(htp != NULL);
|
|
|
|
|
|
|
|
ht = *htp;
|
2019-04-30 09:44:12 +10:00
|
|
|
*htp = NULL;
|
|
|
|
|
2016-05-26 21:23:19 +02:00
|
|
|
REQUIRE(ISC_HT_VALID(ht));
|
|
|
|
|
|
|
|
ht->magic = 0;
|
|
|
|
|
|
|
|
for (i = 0; i < ht->size; i++) {
|
|
|
|
isc_ht_node_t *node = ht->table[i];
|
|
|
|
while (node != NULL) {
|
|
|
|
isc_ht_node_t *next = node->next;
|
|
|
|
ht->count--;
|
|
|
|
isc_mem_put(ht->mctx, node,
|
2018-01-24 13:10:14 +11:00
|
|
|
offsetof(isc_ht_node_t, key) +
|
|
|
|
node->keysize);
|
2016-05-26 21:23:19 +02:00
|
|
|
node = next;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
INSIST(ht->count == 0);
|
|
|
|
|
|
|
|
isc_mem_put(ht->mctx, ht->table, ht->size * sizeof(isc_ht_node_t*));
|
|
|
|
isc_mem_putanddetach(&ht->mctx, ht, sizeof(struct isc_ht));
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
isc_result_t
|
|
|
|
isc_ht_add(isc_ht_t *ht, const unsigned char *key,
|
2018-03-28 14:19:37 +02:00
|
|
|
uint32_t keysize, void *value)
|
2016-05-26 21:23:19 +02:00
|
|
|
{
|
|
|
|
isc_ht_node_t *node;
|
2018-03-28 14:19:37 +02:00
|
|
|
uint32_t hash;
|
2016-05-26 21:23:19 +02:00
|
|
|
|
|
|
|
REQUIRE(ISC_HT_VALID(ht));
|
|
|
|
REQUIRE(key != NULL && keysize > 0);
|
|
|
|
|
2019-04-04 13:51:09 +02:00
|
|
|
hash = isc_hash_function(key, keysize, true);
|
2016-05-26 21:23:19 +02:00
|
|
|
node = ht->table[hash & ht->mask];
|
|
|
|
while (node != NULL) {
|
|
|
|
if (keysize == node->keysize &&
|
2016-06-01 14:49:01 +02:00
|
|
|
memcmp(key, node->key, keysize) == 0) {
|
2016-05-26 21:23:19 +02:00
|
|
|
return (ISC_R_EXISTS);
|
|
|
|
}
|
|
|
|
node = node->next;
|
|
|
|
}
|
|
|
|
|
2018-01-24 13:10:14 +11:00
|
|
|
node = isc_mem_get(ht->mctx, offsetof(isc_ht_node_t, key) + keysize);
|
2016-06-01 14:49:01 +02:00
|
|
|
if (node == NULL)
|
2016-05-26 21:23:19 +02:00
|
|
|
return (ISC_R_NOMEMORY);
|
|
|
|
|
|
|
|
memmove(node->key, key, keysize);
|
|
|
|
node->keysize = keysize;
|
|
|
|
node->next = ht->table[hash & ht->mask];
|
|
|
|
node->value = value;
|
|
|
|
|
|
|
|
ht->count++;
|
|
|
|
ht->table[hash & ht->mask] = node;
|
|
|
|
return (ISC_R_SUCCESS);
|
|
|
|
}
|
|
|
|
|
|
|
|
isc_result_t
|
|
|
|
isc_ht_find(const isc_ht_t *ht, const unsigned char *key,
|
2018-03-28 14:19:37 +02:00
|
|
|
uint32_t keysize, void **valuep)
|
2016-05-26 21:23:19 +02:00
|
|
|
{
|
|
|
|
isc_ht_node_t *node;
|
2018-03-28 14:19:37 +02:00
|
|
|
uint32_t hash;
|
2016-05-26 21:23:19 +02:00
|
|
|
|
|
|
|
REQUIRE(ISC_HT_VALID(ht));
|
|
|
|
REQUIRE(key != NULL && keysize > 0);
|
2018-11-28 18:57:38 +11:00
|
|
|
REQUIRE(valuep == NULL || *valuep == NULL);
|
2016-05-26 21:23:19 +02:00
|
|
|
|
2019-04-04 13:51:09 +02:00
|
|
|
hash = isc_hash_function(key, keysize, true);
|
2016-05-26 21:23:19 +02:00
|
|
|
node = ht->table[hash & ht->mask];
|
|
|
|
while (node != NULL) {
|
|
|
|
if (keysize == node->keysize &&
|
2017-02-20 11:57:28 +01:00
|
|
|
memcmp(key, node->key, keysize) == 0)
|
|
|
|
{
|
|
|
|
if (valuep != NULL)
|
|
|
|
*valuep = node->value;
|
2016-05-26 21:23:19 +02:00
|
|
|
return (ISC_R_SUCCESS);
|
|
|
|
}
|
|
|
|
node = node->next;
|
|
|
|
}
|
|
|
|
|
|
|
|
return (ISC_R_NOTFOUND);
|
|
|
|
}
|
|
|
|
|
|
|
|
isc_result_t
|
2018-03-28 14:19:37 +02:00
|
|
|
isc_ht_delete(isc_ht_t *ht, const unsigned char *key, uint32_t keysize) {
|
2016-05-26 21:23:19 +02:00
|
|
|
isc_ht_node_t *node, *prev;
|
2018-03-28 14:19:37 +02:00
|
|
|
uint32_t hash;
|
2016-05-26 21:23:19 +02:00
|
|
|
|
|
|
|
REQUIRE(ISC_HT_VALID(ht));
|
|
|
|
REQUIRE(key != NULL && keysize > 0);
|
|
|
|
|
|
|
|
prev = NULL;
|
2019-04-04 13:51:09 +02:00
|
|
|
hash = isc_hash_function(key, keysize, true);
|
2016-05-26 21:23:19 +02:00
|
|
|
node = ht->table[hash & ht->mask];
|
|
|
|
while (node != NULL) {
|
|
|
|
if (keysize == node->keysize &&
|
2016-06-01 14:49:01 +02:00
|
|
|
memcmp(key, node->key, keysize) == 0) {
|
|
|
|
if (prev == NULL)
|
2016-05-26 21:23:19 +02:00
|
|
|
ht->table[hash & ht->mask] = node->next;
|
2016-06-01 14:49:01 +02:00
|
|
|
else
|
2016-05-26 21:23:19 +02:00
|
|
|
prev->next = node->next;
|
|
|
|
isc_mem_put(ht->mctx, node,
|
2018-01-24 13:10:14 +11:00
|
|
|
offsetof(isc_ht_node_t, key) +
|
|
|
|
node->keysize);
|
2016-05-26 21:23:19 +02:00
|
|
|
ht->count--;
|
|
|
|
|
|
|
|
return (ISC_R_SUCCESS);
|
|
|
|
}
|
|
|
|
|
|
|
|
prev = node;
|
|
|
|
node = node->next;
|
|
|
|
}
|
|
|
|
return (ISC_R_NOTFOUND);
|
|
|
|
}
|
|
|
|
|
|
|
|
isc_result_t
|
|
|
|
isc_ht_iter_create(isc_ht_t *ht, isc_ht_iter_t **itp) {
|
|
|
|
isc_ht_iter_t *it;
|
|
|
|
|
|
|
|
REQUIRE(ISC_HT_VALID(ht));
|
|
|
|
REQUIRE(itp != NULL && *itp == NULL);
|
|
|
|
|
|
|
|
it = isc_mem_get(ht->mctx, sizeof(isc_ht_iter_t));
|
|
|
|
if (it == NULL)
|
|
|
|
return (ISC_R_NOMEMORY);
|
|
|
|
|
|
|
|
it->ht = ht;
|
|
|
|
it->i = 0;
|
|
|
|
it->cur = NULL;
|
|
|
|
|
|
|
|
*itp = it;
|
|
|
|
|
|
|
|
return (ISC_R_SUCCESS);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
isc_ht_iter_destroy(isc_ht_iter_t **itp) {
|
|
|
|
isc_ht_iter_t *it;
|
|
|
|
isc_ht_t *ht;
|
|
|
|
|
|
|
|
REQUIRE(itp != NULL && *itp != NULL);
|
|
|
|
|
|
|
|
it = *itp;
|
|
|
|
ht = it->ht;
|
|
|
|
isc_mem_put(ht->mctx, it, sizeof(isc_ht_iter_t));
|
|
|
|
|
|
|
|
*itp = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
isc_result_t
|
|
|
|
isc_ht_iter_first(isc_ht_iter_t *it) {
|
|
|
|
REQUIRE(it != NULL);
|
|
|
|
|
|
|
|
it->i = 0;
|
|
|
|
while (it->i < it->ht->size && it->ht->table[it->i] == NULL)
|
|
|
|
it->i++;
|
|
|
|
|
2016-06-01 14:49:01 +02:00
|
|
|
if (it->i == it->ht->size)
|
2016-05-26 21:23:19 +02:00
|
|
|
return (ISC_R_NOMORE);
|
|
|
|
|
|
|
|
it->cur = it->ht->table[it->i];
|
|
|
|
|
|
|
|
return (ISC_R_SUCCESS);
|
|
|
|
}
|
|
|
|
|
|
|
|
isc_result_t
|
|
|
|
isc_ht_iter_next(isc_ht_iter_t *it) {
|
|
|
|
REQUIRE(it != NULL);
|
|
|
|
REQUIRE(it->cur != NULL);
|
|
|
|
|
|
|
|
it->cur = it->cur->next;
|
|
|
|
if (it->cur == NULL) {
|
2016-05-27 19:57:27 +02:00
|
|
|
do {
|
2016-05-26 21:23:19 +02:00
|
|
|
it->i++;
|
2016-05-27 19:57:27 +02:00
|
|
|
} while (it->i < it->ht->size && it->ht->table[it->i] == NULL);
|
|
|
|
if (it->i >= it->ht->size)
|
2016-05-26 21:23:19 +02:00
|
|
|
return (ISC_R_NOMORE);
|
|
|
|
it->cur = it->ht->table[it->i];
|
|
|
|
}
|
|
|
|
|
|
|
|
return (ISC_R_SUCCESS);
|
|
|
|
}
|
|
|
|
|
2016-05-31 23:01:53 +02:00
|
|
|
isc_result_t
|
|
|
|
isc_ht_iter_delcurrent_next(isc_ht_iter_t *it) {
|
|
|
|
isc_result_t result = ISC_R_SUCCESS;
|
|
|
|
isc_ht_node_t *to_delete = NULL;
|
|
|
|
isc_ht_node_t *prev = NULL;
|
|
|
|
isc_ht_node_t *node = NULL;
|
2018-03-28 14:19:37 +02:00
|
|
|
uint32_t hash;
|
2016-05-31 23:01:53 +02:00
|
|
|
isc_ht_t *ht;
|
|
|
|
REQUIRE(it != NULL);
|
|
|
|
REQUIRE(it->cur != NULL);
|
|
|
|
to_delete = it->cur;
|
|
|
|
ht = it->ht;
|
|
|
|
|
|
|
|
it->cur = it->cur->next;
|
|
|
|
if (it->cur == NULL) {
|
|
|
|
do {
|
2016-05-31 23:45:29 +00:00
|
|
|
it->i++;
|
2016-05-31 23:01:53 +02:00
|
|
|
} while (it->i < ht->size && ht->table[it->i] == NULL);
|
|
|
|
if (it->i >= ht->size)
|
|
|
|
result = ISC_R_NOMORE;
|
|
|
|
else
|
|
|
|
it->cur = ht->table[it->i];
|
|
|
|
}
|
|
|
|
|
2019-04-04 13:51:09 +02:00
|
|
|
hash = isc_hash_function(to_delete->key, to_delete->keysize, true);
|
2016-05-31 23:01:53 +02:00
|
|
|
node = ht->table[hash & ht->mask];
|
|
|
|
while (node != to_delete) {
|
|
|
|
prev = node;
|
|
|
|
node = node->next;
|
|
|
|
INSIST(node != NULL);
|
|
|
|
}
|
|
|
|
|
2016-06-01 14:49:01 +02:00
|
|
|
if (prev == NULL)
|
2016-05-31 23:01:53 +02:00
|
|
|
ht->table[hash & ht->mask] = node->next;
|
2016-06-01 14:49:01 +02:00
|
|
|
else
|
2016-05-31 23:01:53 +02:00
|
|
|
prev->next = node->next;
|
|
|
|
isc_mem_put(ht->mctx, node,
|
2018-01-24 13:10:14 +11:00
|
|
|
offsetof(isc_ht_node_t, key) + node->keysize);
|
2016-05-31 23:01:53 +02:00
|
|
|
ht->count--;
|
|
|
|
|
|
|
|
return (result);
|
|
|
|
}
|
|
|
|
|
2016-05-26 21:23:19 +02:00
|
|
|
void
|
|
|
|
isc_ht_iter_current(isc_ht_iter_t *it, void **valuep) {
|
|
|
|
REQUIRE(it != NULL);
|
|
|
|
REQUIRE(it->cur != NULL);
|
2018-11-28 18:57:38 +11:00
|
|
|
REQUIRE(valuep != NULL && *valuep == NULL);
|
|
|
|
|
2016-05-26 21:23:19 +02:00
|
|
|
*valuep = it->cur->value;
|
|
|
|
}
|
|
|
|
|
2016-05-31 23:01:53 +02:00
|
|
|
void
|
|
|
|
isc_ht_iter_currentkey(isc_ht_iter_t *it, unsigned char **key, size_t *keysize)
|
|
|
|
{
|
|
|
|
REQUIRE(it != NULL);
|
|
|
|
REQUIRE(it->cur != NULL);
|
2018-11-28 18:57:38 +11:00
|
|
|
REQUIRE(key != NULL && *key == NULL);
|
|
|
|
|
2016-05-31 23:01:53 +02:00
|
|
|
*key = it->cur->key;
|
|
|
|
*keysize = it->cur->keysize;
|
|
|
|
}
|
|
|
|
|
2016-05-26 21:23:19 +02:00
|
|
|
unsigned int
|
|
|
|
isc_ht_count(isc_ht_t *ht) {
|
|
|
|
REQUIRE(ISC_HT_VALID(ht));
|
|
|
|
|
|
|
|
return(ht->count);
|
|
|
|
}
|