1998-12-18 19:06:16 +00:00
|
|
|
/*
|
2018-02-23 09:53:12 +01:00
|
|
|
* Copyright (C) Internet Systems Consortium, Inc. ("ISC")
|
2000-08-01 01:33:37 +00: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.
|
1998-12-18 19:06:16 +00:00
|
|
|
*/
|
|
|
|
|
2005-04-27 04:57:32 +00:00
|
|
|
/*! \file */
|
2000-06-22 22:00:42 +00:00
|
|
|
|
1998-12-18 19:06:16 +00:00
|
|
|
#include <ctype.h>
|
2018-04-17 08:29:14 -07:00
|
|
|
#include <stdbool.h>
|
1998-12-18 19:06:16 +00:00
|
|
|
|
2001-06-04 19:33:39 +00:00
|
|
|
#include <isc/magic.h>
|
2000-04-28 04:16:34 +00:00
|
|
|
#include <isc/mem.h>
|
2000-05-08 14:38:29 +00:00
|
|
|
#include <isc/string.h>
|
1998-12-18 19:06:16 +00:00
|
|
|
#include <isc/symtab.h>
|
1999-12-16 22:24:22 +00:00
|
|
|
#include <isc/util.h>
|
1998-12-18 21:17:37 +00:00
|
|
|
|
1998-12-18 19:06:16 +00:00
|
|
|
typedef struct elt {
|
2020-02-12 13:59:18 +01:00
|
|
|
char * key;
|
|
|
|
unsigned int type;
|
|
|
|
isc_symvalue_t value;
|
|
|
|
LINK(struct elt) link;
|
1998-12-18 19:06:16 +00:00
|
|
|
} elt_t;
|
|
|
|
|
2020-02-12 13:59:18 +01:00
|
|
|
typedef LIST(elt_t) eltlist_t;
|
1998-12-18 19:06:16 +00:00
|
|
|
|
2020-02-12 13:59:18 +01:00
|
|
|
#define SYMTAB_MAGIC ISC_MAGIC('S', 'y', 'm', 'T')
|
|
|
|
#define VALID_SYMTAB(st) ISC_MAGIC_VALID(st, SYMTAB_MAGIC)
|
1998-12-18 21:17:37 +00:00
|
|
|
|
1998-12-18 19:06:16 +00:00
|
|
|
struct isc_symtab {
|
1998-12-18 21:17:37 +00:00
|
|
|
/* Unlocked. */
|
2020-02-12 13:59:18 +01:00
|
|
|
unsigned int magic;
|
|
|
|
isc_mem_t * mctx;
|
|
|
|
unsigned int size;
|
|
|
|
unsigned int count;
|
|
|
|
unsigned int maxload;
|
|
|
|
eltlist_t * table;
|
|
|
|
isc_symtabaction_t undefine_action;
|
|
|
|
void * undefine_arg;
|
|
|
|
bool case_sensitive;
|
1998-12-18 19:06:16 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
isc_result_t
|
|
|
|
isc_symtab_create(isc_mem_t *mctx, unsigned int size,
|
2020-02-12 13:59:18 +01:00
|
|
|
isc_symtabaction_t undefine_action, void *undefine_arg,
|
|
|
|
bool case_sensitive, isc_symtab_t **symtabp)
|
1998-12-18 19:06:16 +00:00
|
|
|
{
|
|
|
|
isc_symtab_t *symtab;
|
2020-02-12 13:59:18 +01:00
|
|
|
unsigned int i;
|
1998-12-18 19:06:16 +00:00
|
|
|
|
|
|
|
REQUIRE(mctx != NULL);
|
|
|
|
REQUIRE(symtabp != NULL && *symtabp == NULL);
|
2020-02-12 13:59:18 +01:00
|
|
|
REQUIRE(size > 0); /* Should be prime. */
|
1998-12-18 19:06:16 +00:00
|
|
|
|
2019-07-16 15:52:14 +02:00
|
|
|
symtab = isc_mem_get(mctx, sizeof(*symtab));
|
2013-03-08 14:38:03 +11:00
|
|
|
|
|
|
|
symtab->mctx = NULL;
|
|
|
|
isc_mem_attach(mctx, &symtab->mctx);
|
2019-07-16 15:52:14 +02:00
|
|
|
symtab->table = isc_mem_get(mctx, size * sizeof(eltlist_t));
|
1998-12-18 19:06:16 +00:00
|
|
|
for (i = 0; i < size; i++)
|
|
|
|
INIT_LIST(symtab->table[i]);
|
|
|
|
symtab->size = size;
|
2011-11-30 04:27:17 +00:00
|
|
|
symtab->count = 0;
|
|
|
|
symtab->maxload = size * 3 / 4;
|
1998-12-18 21:17:37 +00:00
|
|
|
symtab->undefine_action = undefine_action;
|
1999-06-08 13:02:10 +00:00
|
|
|
symtab->undefine_arg = undefine_arg;
|
1999-01-04 22:30:28 +00:00
|
|
|
symtab->case_sensitive = case_sensitive;
|
1998-12-18 21:17:37 +00:00
|
|
|
symtab->magic = SYMTAB_MAGIC;
|
1998-12-18 19:06:16 +00:00
|
|
|
|
|
|
|
*symtabp = symtab;
|
|
|
|
|
|
|
|
return (ISC_R_SUCCESS);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2020-02-12 13:59:18 +01:00
|
|
|
isc_symtab_destroy(isc_symtab_t **symtabp)
|
|
|
|
{
|
1998-12-18 19:06:16 +00:00
|
|
|
isc_symtab_t *symtab;
|
2020-02-12 13:59:18 +01:00
|
|
|
unsigned int i;
|
|
|
|
elt_t * elt, *nelt;
|
1998-12-18 19:06:16 +00:00
|
|
|
|
1998-12-18 21:17:37 +00:00
|
|
|
REQUIRE(symtabp != NULL);
|
1998-12-18 19:06:16 +00:00
|
|
|
symtab = *symtabp;
|
2020-02-08 04:37:54 -08:00
|
|
|
*symtabp = NULL;
|
1998-12-18 21:17:37 +00:00
|
|
|
REQUIRE(VALID_SYMTAB(symtab));
|
1998-12-18 19:06:16 +00:00
|
|
|
|
|
|
|
for (i = 0; i < symtab->size; i++) {
|
|
|
|
for (elt = HEAD(symtab->table[i]); elt != NULL; elt = nelt) {
|
|
|
|
nelt = NEXT(elt, link);
|
1998-12-18 21:17:37 +00:00
|
|
|
if (symtab->undefine_action != NULL)
|
2020-02-12 13:59:18 +01:00
|
|
|
(symtab->undefine_action)(elt->key, elt->type,
|
|
|
|
elt->value,
|
|
|
|
symtab->undefine_arg);
|
2001-11-27 01:56:32 +00:00
|
|
|
isc_mem_put(symtab->mctx, elt, sizeof(*elt));
|
1998-12-18 19:06:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
isc_mem_put(symtab->mctx, symtab->table,
|
2001-11-27 01:56:32 +00:00
|
|
|
symtab->size * sizeof(eltlist_t));
|
1998-12-18 21:17:37 +00:00
|
|
|
symtab->magic = 0;
|
2013-03-08 14:38:03 +11:00
|
|
|
isc_mem_putanddetach(&symtab->mctx, symtab, sizeof(*symtab));
|
1998-12-18 19:06:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static inline unsigned int
|
2020-02-12 13:59:18 +01:00
|
|
|
hash(const char *key, bool case_sensitive)
|
|
|
|
{
|
|
|
|
const char * s;
|
1998-12-18 19:06:16 +00:00
|
|
|
unsigned int h = 0;
|
2020-02-12 13:59:18 +01:00
|
|
|
int c;
|
1998-12-18 19:06:16 +00:00
|
|
|
|
|
|
|
/*
|
2001-03-07 23:52:16 +00:00
|
|
|
* This hash function is similar to the one Ousterhout
|
|
|
|
* uses in Tcl.
|
1998-12-18 19:06:16 +00:00
|
|
|
*/
|
|
|
|
|
1999-01-04 22:30:28 +00:00
|
|
|
if (case_sensitive) {
|
|
|
|
for (s = key; *s != '\0'; s++) {
|
2001-03-07 23:52:16 +00:00
|
|
|
h += (h << 3) + *s;
|
1999-01-04 22:30:28 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
for (s = key; *s != '\0'; s++) {
|
|
|
|
c = *s;
|
2000-05-09 22:22:25 +00:00
|
|
|
c = tolower((unsigned char)c);
|
2001-03-07 23:52:16 +00:00
|
|
|
h += (h << 3) + c;
|
1998-12-18 19:06:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return (h);
|
|
|
|
}
|
|
|
|
|
2020-02-12 13:59:18 +01:00
|
|
|
#define FIND(s, k, t, b, e) \
|
|
|
|
b = hash((k), (s)->case_sensitive) % (s)->size; \
|
|
|
|
if ((s)->case_sensitive) { \
|
1999-01-04 22:30:28 +00:00
|
|
|
for (e = HEAD((s)->table[b]); e != NULL; e = NEXT(e, link)) { \
|
2020-02-12 13:59:18 +01:00
|
|
|
if (((t) == 0 || e->type == (t)) && \
|
|
|
|
strcmp(e->key, (k)) == 0) \
|
|
|
|
break; \
|
|
|
|
} \
|
|
|
|
} else { \
|
1999-01-04 22:30:28 +00:00
|
|
|
for (e = HEAD((s)->table[b]); e != NULL; e = NEXT(e, link)) { \
|
2020-02-12 13:59:18 +01:00
|
|
|
if (((t) == 0 || e->type == (t)) && \
|
|
|
|
strcasecmp(e->key, (k)) == 0) \
|
|
|
|
break; \
|
|
|
|
} \
|
1998-12-18 19:06:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
isc_result_t
|
|
|
|
isc_symtab_lookup(isc_symtab_t *symtab, const char *key, unsigned int type,
|
|
|
|
isc_symvalue_t *value)
|
|
|
|
{
|
|
|
|
unsigned int bucket;
|
2020-02-12 13:59:18 +01:00
|
|
|
elt_t * elt;
|
1998-12-18 19:06:16 +00:00
|
|
|
|
1998-12-18 21:17:37 +00:00
|
|
|
REQUIRE(VALID_SYMTAB(symtab));
|
|
|
|
REQUIRE(key != NULL);
|
|
|
|
|
1998-12-18 19:06:16 +00:00
|
|
|
FIND(symtab, key, type, bucket, elt);
|
|
|
|
|
|
|
|
if (elt == NULL)
|
|
|
|
return (ISC_R_NOTFOUND);
|
|
|
|
|
|
|
|
if (value != NULL)
|
|
|
|
*value = elt->value;
|
|
|
|
|
|
|
|
return (ISC_R_SUCCESS);
|
|
|
|
}
|
|
|
|
|
2011-11-30 04:27:17 +00:00
|
|
|
static void
|
2020-02-12 13:59:18 +01:00
|
|
|
grow_table(isc_symtab_t *symtab)
|
|
|
|
{
|
|
|
|
eltlist_t * newtable;
|
2011-11-30 04:27:17 +00:00
|
|
|
unsigned int i, newsize, newmax;
|
|
|
|
|
|
|
|
REQUIRE(symtab != NULL);
|
|
|
|
|
|
|
|
newsize = symtab->size * 2;
|
|
|
|
newmax = newsize * 3 / 4;
|
2011-12-01 01:33:27 +00:00
|
|
|
INSIST(newsize > 0U && newmax > 0U);
|
2011-11-30 04:27:17 +00:00
|
|
|
|
|
|
|
newtable = isc_mem_get(symtab->mctx, newsize * sizeof(eltlist_t));
|
|
|
|
|
|
|
|
for (i = 0; i < newsize; i++)
|
|
|
|
INIT_LIST(newtable[i]);
|
|
|
|
|
|
|
|
for (i = 0; i < symtab->size; i++) {
|
|
|
|
elt_t *elt, *nelt;
|
|
|
|
|
|
|
|
for (elt = HEAD(symtab->table[i]); elt != NULL; elt = nelt) {
|
|
|
|
unsigned int hv;
|
|
|
|
|
2011-11-30 06:06:33 +00:00
|
|
|
nelt = NEXT(elt, link);
|
|
|
|
|
2011-11-30 04:27:17 +00:00
|
|
|
UNLINK(symtab->table[i], elt, link);
|
|
|
|
hv = hash(elt->key, symtab->case_sensitive);
|
|
|
|
APPEND(newtable[hv % newsize], elt, link);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
isc_mem_put(symtab->mctx, symtab->table,
|
|
|
|
symtab->size * sizeof(eltlist_t));
|
|
|
|
|
|
|
|
symtab->table = newtable;
|
|
|
|
symtab->size = newsize;
|
|
|
|
symtab->maxload = newmax;
|
|
|
|
}
|
|
|
|
|
1998-12-18 19:06:16 +00:00
|
|
|
isc_result_t
|
2000-06-01 17:20:56 +00:00
|
|
|
isc_symtab_define(isc_symtab_t *symtab, const char *key, unsigned int type,
|
1998-12-19 00:13:59 +00:00
|
|
|
isc_symvalue_t value, isc_symexists_t exists_policy)
|
1998-12-18 19:06:16 +00:00
|
|
|
{
|
|
|
|
unsigned int bucket;
|
2020-02-12 13:59:18 +01:00
|
|
|
elt_t * elt;
|
1998-12-18 19:06:16 +00:00
|
|
|
|
1998-12-18 21:17:37 +00:00
|
|
|
REQUIRE(VALID_SYMTAB(symtab));
|
|
|
|
REQUIRE(key != NULL);
|
1998-12-19 00:15:19 +00:00
|
|
|
REQUIRE(type != 0);
|
1998-12-18 21:17:37 +00:00
|
|
|
|
1998-12-18 19:06:16 +00:00
|
|
|
FIND(symtab, key, type, bucket, elt);
|
|
|
|
|
1998-12-19 00:13:59 +00:00
|
|
|
if (exists_policy != isc_symexists_add && elt != NULL) {
|
|
|
|
if (exists_policy == isc_symexists_reject)
|
|
|
|
return (ISC_R_EXISTS);
|
|
|
|
INSIST(exists_policy == isc_symexists_replace);
|
|
|
|
UNLINK(symtab->table[bucket], elt, link);
|
|
|
|
if (symtab->undefine_action != NULL)
|
|
|
|
(symtab->undefine_action)(elt->key, elt->type,
|
1999-06-08 13:02:10 +00:00
|
|
|
elt->value,
|
|
|
|
symtab->undefine_arg);
|
1998-12-19 00:13:59 +00:00
|
|
|
} else {
|
2019-07-16 15:52:14 +02:00
|
|
|
elt = isc_mem_get(symtab->mctx, sizeof(*elt));
|
2000-10-20 02:21:58 +00:00
|
|
|
ISC_LINK_INIT(elt, link);
|
2011-11-30 04:27:17 +00:00
|
|
|
symtab->count++;
|
1998-12-19 00:13:59 +00:00
|
|
|
}
|
2000-06-01 17:20:56 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Though the "key" can be const coming in, it is not stored as const
|
|
|
|
* so that the calling program can easily have writable access to
|
|
|
|
* it in its undefine_action function. In the event that it *was*
|
|
|
|
* truly const coming in and then the caller modified it anyway ...
|
|
|
|
* well, don't do that!
|
|
|
|
*/
|
|
|
|
DE_CONST(key, elt->key);
|
1998-12-18 19:06:16 +00:00
|
|
|
elt->type = type;
|
|
|
|
elt->value = value;
|
1998-12-18 21:17:37 +00:00
|
|
|
|
1998-12-19 00:13:59 +00:00
|
|
|
/*
|
1998-12-19 00:15:50 +00:00
|
|
|
* We prepend so that the most recent definition will be found.
|
1998-12-19 00:13:59 +00:00
|
|
|
*/
|
|
|
|
PREPEND(symtab->table[bucket], elt, link);
|
1998-12-18 19:06:16 +00:00
|
|
|
|
2011-11-30 04:27:17 +00:00
|
|
|
if (symtab->count > symtab->maxload)
|
|
|
|
grow_table(symtab);
|
|
|
|
|
1998-12-18 19:06:16 +00:00
|
|
|
return (ISC_R_SUCCESS);
|
|
|
|
}
|
|
|
|
|
|
|
|
isc_result_t
|
2020-02-12 13:59:18 +01:00
|
|
|
isc_symtab_undefine(isc_symtab_t *symtab, const char *key, unsigned int type)
|
|
|
|
{
|
1998-12-18 19:06:16 +00:00
|
|
|
unsigned int bucket;
|
2020-02-12 13:59:18 +01:00
|
|
|
elt_t * elt;
|
1998-12-18 19:06:16 +00:00
|
|
|
|
1998-12-18 21:17:37 +00:00
|
|
|
REQUIRE(VALID_SYMTAB(symtab));
|
|
|
|
REQUIRE(key != NULL);
|
|
|
|
|
1998-12-18 19:06:16 +00:00
|
|
|
FIND(symtab, key, type, bucket, elt);
|
|
|
|
|
|
|
|
if (elt == NULL)
|
|
|
|
return (ISC_R_NOTFOUND);
|
|
|
|
|
1998-12-18 21:17:37 +00:00
|
|
|
if (symtab->undefine_action != NULL)
|
2020-02-12 13:59:18 +01:00
|
|
|
(symtab->undefine_action)(elt->key, elt->type, elt->value,
|
|
|
|
symtab->undefine_arg);
|
1998-12-18 19:06:16 +00:00
|
|
|
UNLINK(symtab->table[bucket], elt, link);
|
2001-11-27 01:56:32 +00:00
|
|
|
isc_mem_put(symtab->mctx, elt, sizeof(*elt));
|
2011-11-30 04:27:17 +00:00
|
|
|
symtab->count--;
|
1998-12-18 19:06:16 +00:00
|
|
|
|
|
|
|
return (ISC_R_SUCCESS);
|
|
|
|
}
|
2013-11-13 20:35:40 -08:00
|
|
|
|
|
|
|
unsigned int
|
2020-02-12 13:59:18 +01:00
|
|
|
isc_symtab_count(isc_symtab_t *symtab)
|
|
|
|
{
|
2013-11-13 20:35:40 -08:00
|
|
|
REQUIRE(VALID_SYMTAB(symtab));
|
|
|
|
return (symtab->count);
|
|
|
|
}
|