2 * Copyright (C) 2004, 2005 Internet Systems Consortium, Inc. ("ISC")
3 * Copyright (C) 1996-2001 Internet Software Consortium.
5 * Permission to use, copy, modify, and 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.
18 /* $Id: symtab.c,v 1.26.18.2 2005/04/29 00:16:50 marka Exp $ */
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)
50 isc_symtabaction_t undefine_action;
52 isc_boolean_t case_sensitive;
56 isc_symtab_create(isc_mem_t *mctx, unsigned int size,
57 isc_symtabaction_t undefine_action,
59 isc_boolean_t case_sensitive,
60 isc_symtab_t **symtabp)
65 REQUIRE(mctx != NULL);
66 REQUIRE(symtabp != NULL && *symtabp == NULL);
67 REQUIRE(size > 0); /* Should be prime. */
69 symtab = (isc_symtab_t *)isc_mem_get(mctx, sizeof(*symtab));
71 return (ISC_R_NOMEMORY);
72 symtab->table = (eltlist_t *)isc_mem_get(mctx,
73 size * sizeof(eltlist_t));
74 if (symtab->table == NULL) {
75 isc_mem_put(mctx, symtab, sizeof(*symtab));
76 return (ISC_R_NOMEMORY);
78 for (i = 0; i < size; i++)
79 INIT_LIST(symtab->table[i]);
82 symtab->undefine_action = undefine_action;
83 symtab->undefine_arg = undefine_arg;
84 symtab->case_sensitive = case_sensitive;
85 symtab->magic = SYMTAB_MAGIC;
89 return (ISC_R_SUCCESS);
93 isc_symtab_destroy(isc_symtab_t **symtabp) {
98 REQUIRE(symtabp != NULL);
100 REQUIRE(VALID_SYMTAB(symtab));
102 for (i = 0; i < symtab->size; i++) {
103 for (elt = HEAD(symtab->table[i]); elt != NULL; elt = nelt) {
104 nelt = NEXT(elt, link);
105 if (symtab->undefine_action != NULL)
106 (symtab->undefine_action)(elt->key,
109 symtab->undefine_arg);
110 isc_mem_put(symtab->mctx, elt, sizeof(*elt));
113 isc_mem_put(symtab->mctx, symtab->table,
114 symtab->size * sizeof(eltlist_t));
116 isc_mem_put(symtab->mctx, symtab, sizeof(*symtab));
121 static inline unsigned int
122 hash(const char *key, isc_boolean_t case_sensitive) {
128 * This hash function is similar to the one Ousterhout
132 if (case_sensitive) {
133 for (s = key; *s != '\0'; s++) {
137 for (s = key; *s != '\0'; s++) {
139 c = tolower((unsigned char)c);
147 #define FIND(s, k, t, b, e) \
148 b = hash((k), (s)->case_sensitive) % (s)->size; \
149 if ((s)->case_sensitive) { \
150 for (e = HEAD((s)->table[b]); e != NULL; e = NEXT(e, link)) { \
151 if (((t) == 0 || e->type == (t)) && \
152 strcmp(e->key, (k)) == 0) \
156 for (e = HEAD((s)->table[b]); e != NULL; e = NEXT(e, link)) { \
157 if (((t) == 0 || e->type == (t)) && \
158 strcasecmp(e->key, (k)) == 0) \
164 isc_symtab_lookup(isc_symtab_t *symtab, const char *key, unsigned int type,
165 isc_symvalue_t *value)
170 REQUIRE(VALID_SYMTAB(symtab));
171 REQUIRE(key != NULL);
173 FIND(symtab, key, type, bucket, elt);
176 return (ISC_R_NOTFOUND);
181 return (ISC_R_SUCCESS);
185 isc_symtab_define(isc_symtab_t *symtab, const char *key, unsigned int type,
186 isc_symvalue_t value, isc_symexists_t exists_policy)
191 REQUIRE(VALID_SYMTAB(symtab));
192 REQUIRE(key != NULL);
195 FIND(symtab, key, type, bucket, elt);
197 if (exists_policy != isc_symexists_add && elt != NULL) {
198 if (exists_policy == isc_symexists_reject)
199 return (ISC_R_EXISTS);
200 INSIST(exists_policy == isc_symexists_replace);
201 UNLINK(symtab->table[bucket], elt, link);
202 if (symtab->undefine_action != NULL)
203 (symtab->undefine_action)(elt->key, elt->type,
205 symtab->undefine_arg);
207 elt = (elt_t *)isc_mem_get(symtab->mctx, sizeof(*elt));
209 return (ISC_R_NOMEMORY);
210 ISC_LINK_INIT(elt, link);
214 * Though the "key" can be const coming in, it is not stored as const
215 * so that the calling program can easily have writable access to
216 * it in its undefine_action function. In the event that it *was*
217 * truly const coming in and then the caller modified it anyway ...
218 * well, don't do that!
220 DE_CONST(key, elt->key);
225 * We prepend so that the most recent definition will be found.
227 PREPEND(symtab->table[bucket], elt, link);
229 return (ISC_R_SUCCESS);
233 isc_symtab_undefine(isc_symtab_t *symtab, const char *key, unsigned int type) {
237 REQUIRE(VALID_SYMTAB(symtab));
238 REQUIRE(key != NULL);
240 FIND(symtab, key, type, bucket, elt);
243 return (ISC_R_NOTFOUND);
245 if (symtab->undefine_action != NULL)
246 (symtab->undefine_action)(elt->key, elt->type,
247 elt->value, symtab->undefine_arg);
248 UNLINK(symtab->table[bucket], elt, link);
249 isc_mem_put(symtab->mctx, elt, sizeof(*elt));
251 return (ISC_R_SUCCESS);