2 * Copyright (C) 2004, 2005, 2007, 2011, 2012 Internet Systems Consortium, Inc. ("ISC")
3 * Copyright (C) 1996-2001 Internet Software Consortium.
5 * Permission to use, copy, modify, and/or distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
9 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
10 * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
11 * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
12 * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
13 * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
14 * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
15 * PERFORMANCE OF THIS SOFTWARE.
26 #include <isc/magic.h>
28 #include <isc/string.h>
29 #include <isc/symtab.h>
36 LINK(struct elt) link;
39 typedef LIST(elt_t) eltlist_t;
41 #define SYMTAB_MAGIC ISC_MAGIC('S', 'y', 'm', 'T')
42 #define VALID_SYMTAB(st) ISC_MAGIC_VALID(st, SYMTAB_MAGIC)
52 isc_symtabaction_t undefine_action;
54 isc_boolean_t case_sensitive;
58 isc_symtab_create(isc_mem_t *mctx, unsigned int size,
59 isc_symtabaction_t undefine_action,
61 isc_boolean_t case_sensitive,
62 isc_symtab_t **symtabp)
67 REQUIRE(mctx != NULL);
68 REQUIRE(symtabp != NULL && *symtabp == NULL);
69 REQUIRE(size > 0); /* Should be prime. */
71 symtab = (isc_symtab_t *)isc_mem_get(mctx, sizeof(*symtab));
73 return (ISC_R_NOMEMORY);
74 symtab->table = (eltlist_t *)isc_mem_get(mctx,
75 size * sizeof(eltlist_t));
76 if (symtab->table == NULL) {
77 isc_mem_put(mctx, symtab, sizeof(*symtab));
78 return (ISC_R_NOMEMORY);
80 for (i = 0; i < size; i++)
81 INIT_LIST(symtab->table[i]);
85 symtab->maxload = size * 3 / 4;
86 symtab->undefine_action = undefine_action;
87 symtab->undefine_arg = undefine_arg;
88 symtab->case_sensitive = case_sensitive;
89 symtab->magic = SYMTAB_MAGIC;
93 return (ISC_R_SUCCESS);
97 isc_symtab_destroy(isc_symtab_t **symtabp) {
102 REQUIRE(symtabp != NULL);
104 REQUIRE(VALID_SYMTAB(symtab));
106 for (i = 0; i < symtab->size; i++) {
107 for (elt = HEAD(symtab->table[i]); elt != NULL; elt = nelt) {
108 nelt = NEXT(elt, link);
109 if (symtab->undefine_action != NULL)
110 (symtab->undefine_action)(elt->key,
113 symtab->undefine_arg);
114 isc_mem_put(symtab->mctx, elt, sizeof(*elt));
117 isc_mem_put(symtab->mctx, symtab->table,
118 symtab->size * sizeof(eltlist_t));
120 isc_mem_put(symtab->mctx, symtab, sizeof(*symtab));
125 static inline unsigned int
126 hash(const char *key, isc_boolean_t case_sensitive) {
132 * This hash function is similar to the one Ousterhout
136 if (case_sensitive) {
137 for (s = key; *s != '\0'; s++) {
141 for (s = key; *s != '\0'; s++) {
143 c = tolower((unsigned char)c);
151 #define FIND(s, k, t, b, e) \
152 b = hash((k), (s)->case_sensitive) % (s)->size; \
153 if ((s)->case_sensitive) { \
154 for (e = HEAD((s)->table[b]); e != NULL; e = NEXT(e, link)) { \
155 if (((t) == 0 || e->type == (t)) && \
156 strcmp(e->key, (k)) == 0) \
160 for (e = HEAD((s)->table[b]); e != NULL; e = NEXT(e, link)) { \
161 if (((t) == 0 || e->type == (t)) && \
162 strcasecmp(e->key, (k)) == 0) \
168 isc_symtab_lookup(isc_symtab_t *symtab, const char *key, unsigned int type,
169 isc_symvalue_t *value)
174 REQUIRE(VALID_SYMTAB(symtab));
175 REQUIRE(key != NULL);
177 FIND(symtab, key, type, bucket, elt);
180 return (ISC_R_NOTFOUND);
185 return (ISC_R_SUCCESS);
189 grow_table(isc_symtab_t *symtab) {
191 unsigned int i, newsize, newmax;
193 REQUIRE(symtab != NULL);
195 newsize = symtab->size * 2;
196 newmax = newsize * 3 / 4;
197 INSIST(newsize > 0U && newmax > 0U);
199 newtable = isc_mem_get(symtab->mctx, newsize * sizeof(eltlist_t));
200 if (newtable == NULL)
203 for (i = 0; i < newsize; i++)
204 INIT_LIST(newtable[i]);
206 for (i = 0; i < symtab->size; i++) {
209 for (elt = HEAD(symtab->table[i]); elt != NULL; elt = nelt) {
212 nelt = NEXT(elt, link);
214 UNLINK(symtab->table[i], elt, link);
215 hv = hash(elt->key, symtab->case_sensitive);
216 APPEND(newtable[hv % newsize], elt, link);
220 isc_mem_put(symtab->mctx, symtab->table,
221 symtab->size * sizeof(eltlist_t));
223 symtab->table = newtable;
224 symtab->size = newsize;
225 symtab->maxload = newmax;
229 isc_symtab_define(isc_symtab_t *symtab, const char *key, unsigned int type,
230 isc_symvalue_t value, isc_symexists_t exists_policy)
235 REQUIRE(VALID_SYMTAB(symtab));
236 REQUIRE(key != NULL);
239 FIND(symtab, key, type, bucket, elt);
241 if (exists_policy != isc_symexists_add && elt != NULL) {
242 if (exists_policy == isc_symexists_reject)
243 return (ISC_R_EXISTS);
244 INSIST(exists_policy == isc_symexists_replace);
245 UNLINK(symtab->table[bucket], elt, link);
246 if (symtab->undefine_action != NULL)
247 (symtab->undefine_action)(elt->key, elt->type,
249 symtab->undefine_arg);
251 elt = (elt_t *)isc_mem_get(symtab->mctx, sizeof(*elt));
253 return (ISC_R_NOMEMORY);
254 ISC_LINK_INIT(elt, link);
259 * Though the "key" can be const coming in, it is not stored as const
260 * so that the calling program can easily have writable access to
261 * it in its undefine_action function. In the event that it *was*
262 * truly const coming in and then the caller modified it anyway ...
263 * well, don't do that!
265 DE_CONST(key, elt->key);
270 * We prepend so that the most recent definition will be found.
272 PREPEND(symtab->table[bucket], elt, link);
274 if (symtab->count > symtab->maxload)
277 return (ISC_R_SUCCESS);
281 isc_symtab_undefine(isc_symtab_t *symtab, const char *key, unsigned int type) {
285 REQUIRE(VALID_SYMTAB(symtab));
286 REQUIRE(key != NULL);
288 FIND(symtab, key, type, bucket, elt);
291 return (ISC_R_NOTFOUND);
293 if (symtab->undefine_action != NULL)
294 (symtab->undefine_action)(elt->key, elt->type,
295 elt->value, symtab->undefine_arg);
296 UNLINK(symtab->table[bucket], elt, link);
297 isc_mem_put(symtab->mctx, elt, sizeof(*elt));
300 return (ISC_R_SUCCESS);