2 * Portions Copyright (C) 2004, 2005, 2007 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/or 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.5.18.4 2007/09/13 23:46:26 tbox Exp $ */
28 #include <isc/assertions.h>
29 #include <isc/magic.h>
30 #include <isc/string.h>
32 #include <isccc/result.h>
33 #include <isccc/symtab.h>
34 #include <isccc/util.h>
39 isccc_symvalue_t value;
40 ISC_LINK(struct elt) link;
43 typedef ISC_LIST(elt_t) eltlist_t;
45 #define SYMTAB_MAGIC ISC_MAGIC('S', 'y', 'm', 'T')
46 #define VALID_SYMTAB(st) ISC_MAGIC_VALID(st, SYMTAB_MAGIC)
52 isccc_symtabundefaction_t undefine_action;
54 isc_boolean_t case_sensitive;
58 isccc_symtab_create(unsigned int size,
59 isccc_symtabundefaction_t undefine_action,
61 isc_boolean_t case_sensitive,
62 isccc_symtab_t **symtabp)
64 isccc_symtab_t *symtab;
67 REQUIRE(symtabp != NULL && *symtabp == NULL);
68 REQUIRE(size > 0); /* Should be prime. */
70 symtab = malloc(sizeof(*symtab));
72 return (ISC_R_NOMEMORY);
73 symtab->table = malloc(size * sizeof(eltlist_t));
74 if (symtab->table == NULL) {
76 return (ISC_R_NOMEMORY);
78 for (i = 0; i < size; i++)
79 ISC_LIST_INIT(symtab->table[i]);
81 symtab->undefine_action = undefine_action;
82 symtab->undefine_arg = undefine_arg;
83 symtab->case_sensitive = case_sensitive;
84 symtab->magic = SYMTAB_MAGIC;
88 return (ISC_R_SUCCESS);
92 free_elt(isccc_symtab_t *symtab, unsigned int bucket, elt_t *elt) {
93 ISC_LIST_UNLINK(symtab->table[bucket], elt, link);
94 if (symtab->undefine_action != NULL)
95 (symtab->undefine_action)(elt->key, elt->type, elt->value,
96 symtab->undefine_arg);
101 isccc_symtab_destroy(isccc_symtab_t **symtabp) {
102 isccc_symtab_t *symtab;
106 REQUIRE(symtabp != NULL);
108 REQUIRE(VALID_SYMTAB(symtab));
110 for (i = 0; i < symtab->size; i++) {
111 for (elt = ISC_LIST_HEAD(symtab->table[i]);
114 nelt = ISC_LIST_NEXT(elt, link);
115 free_elt(symtab, i, elt);
125 static inline unsigned int
126 hash(const char *key, isc_boolean_t case_sensitive) {
133 * P. J. Weinberger's hash function, adapted from p. 436 of
134 * _Compilers: Principles, Techniques, and Tools_, Aho, Sethi
135 * and Ullman, Addison-Wesley, 1986, ISBN 0-201-10088-6.
138 if (case_sensitive) {
139 for (s = key; *s != '\0'; s++) {
141 if ((g = ( h & 0xf0000000 )) != 0) {
147 for (s = key; *s != '\0'; s++) {
149 c = tolower((unsigned char)c);
151 if ((g = ( h & 0xf0000000 )) != 0) {
161 #define FIND(s, k, t, b, e) \
162 b = hash((k), (s)->case_sensitive) % (s)->size; \
163 if ((s)->case_sensitive) { \
164 for (e = ISC_LIST_HEAD((s)->table[b]); \
166 e = ISC_LIST_NEXT(e, link)) { \
167 if (((t) == 0 || e->type == (t)) && \
168 strcmp(e->key, (k)) == 0) \
172 for (e = ISC_LIST_HEAD((s)->table[b]); \
174 e = ISC_LIST_NEXT(e, link)) { \
175 if (((t) == 0 || e->type == (t)) && \
176 strcasecmp(e->key, (k)) == 0) \
182 isccc_symtab_lookup(isccc_symtab_t *symtab, const char *key, unsigned int type,
183 isccc_symvalue_t *value)
188 REQUIRE(VALID_SYMTAB(symtab));
189 REQUIRE(key != NULL);
191 FIND(symtab, key, type, bucket, elt);
194 return (ISC_R_NOTFOUND);
199 return (ISC_R_SUCCESS);
203 isccc_symtab_define(isccc_symtab_t *symtab, char *key, unsigned int type,
204 isccc_symvalue_t value, isccc_symexists_t exists_policy)
209 REQUIRE(VALID_SYMTAB(symtab));
210 REQUIRE(key != NULL);
213 FIND(symtab, key, type, bucket, elt);
215 if (exists_policy != isccc_symexists_add && elt != NULL) {
216 if (exists_policy == isccc_symexists_reject)
217 return (ISC_R_EXISTS);
218 INSIST(exists_policy == isccc_symexists_replace);
219 ISC_LIST_UNLINK(symtab->table[bucket], elt, link);
220 if (symtab->undefine_action != NULL)
221 (symtab->undefine_action)(elt->key, elt->type,
223 symtab->undefine_arg);
225 elt = malloc(sizeof(*elt));
227 return (ISC_R_NOMEMORY);
228 ISC_LINK_INIT(elt, link);
236 * We prepend so that the most recent definition will be found.
238 ISC_LIST_PREPEND(symtab->table[bucket], elt, link);
240 return (ISC_R_SUCCESS);
244 isccc_symtab_undefine(isccc_symtab_t *symtab, const char *key, unsigned int type) {
248 REQUIRE(VALID_SYMTAB(symtab));
249 REQUIRE(key != NULL);
251 FIND(symtab, key, type, bucket, elt);
254 return (ISC_R_NOTFOUND);
256 free_elt(symtab, bucket, elt);
258 return (ISC_R_SUCCESS);
262 isccc_symtab_foreach(isccc_symtab_t *symtab, isccc_symtabforeachaction_t action,
268 REQUIRE(VALID_SYMTAB(symtab));
269 REQUIRE(action != NULL);
271 for (i = 0; i < symtab->size; i++) {
272 for (elt = ISC_LIST_HEAD(symtab->table[i]);
275 nelt = ISC_LIST_NEXT(elt, link);
276 if ((action)(elt->key, elt->type, elt->value, arg))
277 free_elt(symtab, i, elt);