2 * Portions Copyright (C) 2004 Internet Systems Consortium, Inc. ("ISC")
3 * Portions Copyright (C) 2001 Internet Software Consortium.
4 * Portions Copyright (C) 2001 Nominum, Inc.
6 * Permission to use, copy, modify, and distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
10 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC AND NOMINUM DISCLAIMS ALL
11 * WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
12 * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY
13 * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19 /* $Id: symtab.c,v 1.3.12.3 2004/03/08 09:05:04 marka Exp $ */
27 #include <isc/assertions.h>
28 #include <isc/magic.h>
30 #include <isccc/result.h>
31 #include <isccc/symtab.h>
32 #include <isccc/util.h>
37 isccc_symvalue_t value;
38 ISC_LINK(struct elt) link;
41 typedef ISC_LIST(elt_t) eltlist_t;
43 #define SYMTAB_MAGIC ISC_MAGIC('S', 'y', 'm', 'T')
44 #define VALID_SYMTAB(st) ISC_MAGIC_VALID(st, SYMTAB_MAGIC)
50 isccc_symtabundefaction_t undefine_action;
52 isc_boolean_t case_sensitive;
56 isccc_symtab_create(unsigned int size,
57 isccc_symtabundefaction_t undefine_action,
59 isc_boolean_t case_sensitive,
60 isccc_symtab_t **symtabp)
62 isccc_symtab_t *symtab;
65 REQUIRE(symtabp != NULL && *symtabp == NULL);
66 REQUIRE(size > 0); /* Should be prime. */
68 symtab = malloc(sizeof(*symtab));
70 return (ISC_R_NOMEMORY);
71 symtab->table = malloc(size * sizeof(eltlist_t));
72 if (symtab->table == NULL) {
74 return (ISC_R_NOMEMORY);
76 for (i = 0; i < size; i++)
77 ISC_LIST_INIT(symtab->table[i]);
79 symtab->undefine_action = undefine_action;
80 symtab->undefine_arg = undefine_arg;
81 symtab->case_sensitive = case_sensitive;
82 symtab->magic = SYMTAB_MAGIC;
86 return (ISC_R_SUCCESS);
90 free_elt(isccc_symtab_t *symtab, unsigned int bucket, elt_t *elt) {
91 ISC_LIST_UNLINK(symtab->table[bucket], elt, link);
92 if (symtab->undefine_action != NULL)
93 (symtab->undefine_action)(elt->key, elt->type, elt->value,
94 symtab->undefine_arg);
99 isccc_symtab_destroy(isccc_symtab_t **symtabp) {
100 isccc_symtab_t *symtab;
104 REQUIRE(symtabp != NULL);
106 REQUIRE(VALID_SYMTAB(symtab));
108 for (i = 0; i < symtab->size; i++) {
109 for (elt = ISC_LIST_HEAD(symtab->table[i]);
112 nelt = ISC_LIST_NEXT(elt, link);
113 free_elt(symtab, i, elt);
123 static inline unsigned int
124 hash(const char *key, isc_boolean_t case_sensitive) {
131 * P. J. Weinberger's hash function, adapted from p. 436 of
132 * _Compilers: Principles, Techniques, and Tools_, Aho, Sethi
133 * and Ullman, Addison-Wesley, 1986, ISBN 0-201-10088-6.
136 if (case_sensitive) {
137 for (s = key; *s != '\0'; s++) {
139 if ((g = ( h & 0xf0000000 )) != 0) {
145 for (s = key; *s != '\0'; s++) {
147 c = tolower((unsigned char)c);
149 if ((g = ( h & 0xf0000000 )) != 0) {
159 #define FIND(s, k, t, b, e) \
160 b = hash((k), (s)->case_sensitive) % (s)->size; \
161 if ((s)->case_sensitive) { \
162 for (e = ISC_LIST_HEAD((s)->table[b]); \
164 e = ISC_LIST_NEXT(e, link)) { \
165 if (((t) == 0 || e->type == (t)) && \
166 strcmp(e->key, (k)) == 0) \
170 for (e = ISC_LIST_HEAD((s)->table[b]); \
172 e = ISC_LIST_NEXT(e, link)) { \
173 if (((t) == 0 || e->type == (t)) && \
174 strcasecmp(e->key, (k)) == 0) \
180 isccc_symtab_lookup(isccc_symtab_t *symtab, const char *key, unsigned int type,
181 isccc_symvalue_t *value)
186 REQUIRE(VALID_SYMTAB(symtab));
187 REQUIRE(key != NULL);
189 FIND(symtab, key, type, bucket, elt);
192 return (ISC_R_NOTFOUND);
197 return (ISC_R_SUCCESS);
201 isccc_symtab_define(isccc_symtab_t *symtab, char *key, unsigned int type,
202 isccc_symvalue_t value, isccc_symexists_t exists_policy)
207 REQUIRE(VALID_SYMTAB(symtab));
208 REQUIRE(key != NULL);
211 FIND(symtab, key, type, bucket, elt);
213 if (exists_policy != isccc_symexists_add && elt != NULL) {
214 if (exists_policy == isccc_symexists_reject)
215 return (ISC_R_EXISTS);
216 INSIST(exists_policy == isccc_symexists_replace);
217 ISC_LIST_UNLINK(symtab->table[bucket], elt, link);
218 if (symtab->undefine_action != NULL)
219 (symtab->undefine_action)(elt->key, elt->type,
221 symtab->undefine_arg);
223 elt = malloc(sizeof(*elt));
225 return (ISC_R_NOMEMORY);
226 ISC_LINK_INIT(elt, link);
234 * We prepend so that the most recent definition will be found.
236 ISC_LIST_PREPEND(symtab->table[bucket], elt, link);
238 return (ISC_R_SUCCESS);
242 isccc_symtab_undefine(isccc_symtab_t *symtab, const char *key, unsigned int type) {
246 REQUIRE(VALID_SYMTAB(symtab));
247 REQUIRE(key != NULL);
249 FIND(symtab, key, type, bucket, elt);
252 return (ISC_R_NOTFOUND);
254 free_elt(symtab, bucket, elt);
256 return (ISC_R_SUCCESS);
260 isccc_symtab_foreach(isccc_symtab_t *symtab, isccc_symtabforeachaction_t action,
266 REQUIRE(VALID_SYMTAB(symtab));
267 REQUIRE(action != NULL);
269 for (i = 0; i < symtab->size; i++) {
270 for (elt = ISC_LIST_HEAD(symtab->table[i]);
273 nelt = ISC_LIST_NEXT(elt, link);
274 if ((action)(elt->key, elt->type, elt->value, arg))
275 free_elt(symtab, i, elt);