4 * Copyright (C) 2004, 2005, 2007 Internet Systems Consortium, Inc. ("ISC")
5 * Copyright (C) 1996-2001 Internet Software Consortium.
7 * Permission to use, copy, modify, and/or distribute this software for any
8 * purpose with or without fee is hereby granted, provided that the above
9 * copyright notice and this permission notice appear in all copies.
11 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
12 * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
13 * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
14 * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
15 * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
16 * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
17 * PERFORMANCE OF THIS SOFTWARE.
20 /* Id: symtab.c,v 1.30 2007/06/19 23:47:17 tbox Exp */
28 #include <isc/magic.h>
30 #include <isc/string.h>
31 #include <isc/symtab.h>
38 LINK(struct elt
) link
;
41 typedef 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)
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
]);
84 symtab
->undefine_action
= undefine_action
;
85 symtab
->undefine_arg
= undefine_arg
;
86 symtab
->case_sensitive
= case_sensitive
;
87 symtab
->magic
= SYMTAB_MAGIC
;
91 return (ISC_R_SUCCESS
);
95 isc_symtab_destroy(isc_symtab_t
**symtabp
) {
100 REQUIRE(symtabp
!= NULL
);
102 REQUIRE(VALID_SYMTAB(symtab
));
104 for (i
= 0; i
< symtab
->size
; i
++) {
105 for (elt
= HEAD(symtab
->table
[i
]); elt
!= NULL
; elt
= nelt
) {
106 nelt
= NEXT(elt
, link
);
107 if (symtab
->undefine_action
!= NULL
)
108 (symtab
->undefine_action
)(elt
->key
,
111 symtab
->undefine_arg
);
112 isc_mem_put(symtab
->mctx
, elt
, sizeof(*elt
));
115 isc_mem_put(symtab
->mctx
, symtab
->table
,
116 symtab
->size
* sizeof(eltlist_t
));
118 isc_mem_put(symtab
->mctx
, symtab
, sizeof(*symtab
));
123 static inline unsigned int
124 hash(const char *key
, isc_boolean_t case_sensitive
) {
130 * This hash function is similar to the one Ousterhout
134 if (case_sensitive
) {
135 for (s
= key
; *s
!= '\0'; s
++) {
139 for (s
= key
; *s
!= '\0'; s
++) {
141 c
= tolower((unsigned char)c
);
149 #define FIND(s, k, t, b, e) \
150 b = hash((k), (s)->case_sensitive) % (s)->size; \
151 if ((s)->case_sensitive) { \
152 for (e = HEAD((s)->table[b]); e != NULL; e = NEXT(e, link)) { \
153 if (((t) == 0 || e->type == (t)) && \
154 strcmp(e->key, (k)) == 0) \
158 for (e = HEAD((s)->table[b]); e != NULL; e = NEXT(e, link)) { \
159 if (((t) == 0 || e->type == (t)) && \
160 strcasecmp(e->key, (k)) == 0) \
166 isc_symtab_lookup(isc_symtab_t
*symtab
, const char *key
, unsigned int type
,
167 isc_symvalue_t
*value
)
172 REQUIRE(VALID_SYMTAB(symtab
));
173 REQUIRE(key
!= NULL
);
175 FIND(symtab
, key
, type
, bucket
, elt
);
178 return (ISC_R_NOTFOUND
);
183 return (ISC_R_SUCCESS
);
187 isc_symtab_define(isc_symtab_t
*symtab
, const char *key
, unsigned int type
,
188 isc_symvalue_t value
, isc_symexists_t exists_policy
)
193 REQUIRE(VALID_SYMTAB(symtab
));
194 REQUIRE(key
!= NULL
);
197 FIND(symtab
, key
, type
, bucket
, elt
);
199 if (exists_policy
!= isc_symexists_add
&& elt
!= NULL
) {
200 if (exists_policy
== isc_symexists_reject
)
201 return (ISC_R_EXISTS
);
202 INSIST(exists_policy
== isc_symexists_replace
);
203 UNLINK(symtab
->table
[bucket
], elt
, link
);
204 if (symtab
->undefine_action
!= NULL
)
205 (symtab
->undefine_action
)(elt
->key
, elt
->type
,
207 symtab
->undefine_arg
);
209 elt
= (elt_t
*)isc_mem_get(symtab
->mctx
, sizeof(*elt
));
211 return (ISC_R_NOMEMORY
);
212 ISC_LINK_INIT(elt
, link
);
216 * Though the "key" can be const coming in, it is not stored as const
217 * so that the calling program can easily have writable access to
218 * it in its undefine_action function. In the event that it *was*
219 * truly const coming in and then the caller modified it anyway ...
220 * well, don't do that!
222 DE_CONST(key
, elt
->key
);
227 * We prepend so that the most recent definition will be found.
229 PREPEND(symtab
->table
[bucket
], elt
, link
);
231 return (ISC_R_SUCCESS
);
235 isc_symtab_undefine(isc_symtab_t
*symtab
, const char *key
, unsigned int type
) {
239 REQUIRE(VALID_SYMTAB(symtab
));
240 REQUIRE(key
!= NULL
);
242 FIND(symtab
, key
, type
, bucket
, elt
);
245 return (ISC_R_NOTFOUND
);
247 if (symtab
->undefine_action
!= NULL
)
248 (symtab
->undefine_action
)(elt
->key
, elt
->type
,
249 elt
->value
, symtab
->undefine_arg
);
250 UNLINK(symtab
->table
[bucket
], elt
, link
);
251 isc_mem_put(symtab
->mctx
, elt
, sizeof(*elt
));
253 return (ISC_R_SUCCESS
);