1 /* $NetBSD: symtab.c,v 1.6 2014/12/10 04:37:59 christos Exp $ */
4 * Copyright (C) 2004, 2005, 2007, 2011-2013 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.
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)
54 isc_symtabaction_t undefine_action
;
56 isc_boolean_t case_sensitive
;
60 isc_symtab_create(isc_mem_t
*mctx
, unsigned int size
,
61 isc_symtabaction_t undefine_action
,
63 isc_boolean_t case_sensitive
,
64 isc_symtab_t
**symtabp
)
69 REQUIRE(mctx
!= NULL
);
70 REQUIRE(symtabp
!= NULL
&& *symtabp
== NULL
);
71 REQUIRE(size
> 0); /* Should be prime. */
73 symtab
= (isc_symtab_t
*)isc_mem_get(mctx
, sizeof(*symtab
));
75 return (ISC_R_NOMEMORY
);
78 isc_mem_attach(mctx
, &symtab
->mctx
);
79 symtab
->table
= (eltlist_t
*)isc_mem_get(mctx
,
80 size
* sizeof(eltlist_t
));
81 if (symtab
->table
== NULL
) {
82 isc_mem_putanddetach(&symtab
->mctx
, symtab
, sizeof(*symtab
));
83 return (ISC_R_NOMEMORY
);
85 for (i
= 0; i
< size
; i
++)
86 INIT_LIST(symtab
->table
[i
]);
89 symtab
->maxload
= size
* 3 / 4;
90 symtab
->undefine_action
= undefine_action
;
91 symtab
->undefine_arg
= undefine_arg
;
92 symtab
->case_sensitive
= case_sensitive
;
93 symtab
->magic
= SYMTAB_MAGIC
;
97 return (ISC_R_SUCCESS
);
101 isc_symtab_destroy(isc_symtab_t
**symtabp
) {
102 isc_symtab_t
*symtab
;
106 REQUIRE(symtabp
!= NULL
);
108 REQUIRE(VALID_SYMTAB(symtab
));
110 for (i
= 0; i
< symtab
->size
; i
++) {
111 for (elt
= HEAD(symtab
->table
[i
]); elt
!= NULL
; elt
= nelt
) {
112 nelt
= NEXT(elt
, link
);
113 if (symtab
->undefine_action
!= NULL
)
114 (symtab
->undefine_action
)(elt
->key
,
117 symtab
->undefine_arg
);
118 isc_mem_put(symtab
->mctx
, elt
, sizeof(*elt
));
121 isc_mem_put(symtab
->mctx
, symtab
->table
,
122 symtab
->size
* sizeof(eltlist_t
));
124 isc_mem_putanddetach(&symtab
->mctx
, symtab
, sizeof(*symtab
));
129 static inline unsigned int
130 hash(const char *key
, isc_boolean_t case_sensitive
) {
136 * This hash function is similar to the one Ousterhout
140 if (case_sensitive
) {
141 for (s
= key
; *s
!= '\0'; s
++) {
145 for (s
= key
; *s
!= '\0'; s
++) {
147 c
= tolower((unsigned char)c
);
155 #define FIND(s, k, t, b, e) \
156 b = hash((k), (s)->case_sensitive) % (s)->size; \
157 if ((s)->case_sensitive) { \
158 for (e = HEAD((s)->table[b]); e != NULL; e = NEXT(e, link)) { \
159 if (((t) == 0 || e->type == (t)) && \
160 strcmp(e->key, (k)) == 0) \
164 for (e = HEAD((s)->table[b]); e != NULL; e = NEXT(e, link)) { \
165 if (((t) == 0 || e->type == (t)) && \
166 strcasecmp(e->key, (k)) == 0) \
172 isc_symtab_lookup(isc_symtab_t
*symtab
, const char *key
, unsigned int type
,
173 isc_symvalue_t
*value
)
178 REQUIRE(VALID_SYMTAB(symtab
));
179 REQUIRE(key
!= NULL
);
181 FIND(symtab
, key
, type
, bucket
, elt
);
184 return (ISC_R_NOTFOUND
);
189 return (ISC_R_SUCCESS
);
193 grow_table(isc_symtab_t
*symtab
) {
195 unsigned int i
, newsize
, newmax
;
197 REQUIRE(symtab
!= NULL
);
199 newsize
= symtab
->size
* 2;
200 newmax
= newsize
* 3 / 4;
201 INSIST(newsize
> 0U && newmax
> 0U);
203 newtable
= isc_mem_get(symtab
->mctx
, newsize
* sizeof(eltlist_t
));
204 if (newtable
== NULL
)
207 for (i
= 0; i
< newsize
; i
++)
208 INIT_LIST(newtable
[i
]);
210 for (i
= 0; i
< symtab
->size
; i
++) {
213 for (elt
= HEAD(symtab
->table
[i
]); elt
!= NULL
; elt
= nelt
) {
216 nelt
= NEXT(elt
, link
);
218 UNLINK(symtab
->table
[i
], elt
, link
);
219 hv
= hash(elt
->key
, symtab
->case_sensitive
);
220 APPEND(newtable
[hv
% newsize
], elt
, link
);
224 isc_mem_put(symtab
->mctx
, symtab
->table
,
225 symtab
->size
* sizeof(eltlist_t
));
227 symtab
->table
= newtable
;
228 symtab
->size
= newsize
;
229 symtab
->maxload
= newmax
;
233 isc_symtab_define(isc_symtab_t
*symtab
, const char *key
, unsigned int type
,
234 isc_symvalue_t value
, isc_symexists_t exists_policy
)
239 REQUIRE(VALID_SYMTAB(symtab
));
240 REQUIRE(key
!= NULL
);
243 FIND(symtab
, key
, type
, bucket
, elt
);
245 if (exists_policy
!= isc_symexists_add
&& elt
!= NULL
) {
246 if (exists_policy
== isc_symexists_reject
)
247 return (ISC_R_EXISTS
);
248 INSIST(exists_policy
== isc_symexists_replace
);
249 UNLINK(symtab
->table
[bucket
], elt
, link
);
250 if (symtab
->undefine_action
!= NULL
)
251 (symtab
->undefine_action
)(elt
->key
, elt
->type
,
253 symtab
->undefine_arg
);
255 elt
= (elt_t
*)isc_mem_get(symtab
->mctx
, sizeof(*elt
));
257 return (ISC_R_NOMEMORY
);
258 ISC_LINK_INIT(elt
, link
);
263 * Though the "key" can be const coming in, it is not stored as const
264 * so that the calling program can easily have writable access to
265 * it in its undefine_action function. In the event that it *was*
266 * truly const coming in and then the caller modified it anyway ...
267 * well, don't do that!
269 DE_CONST(key
, elt
->key
);
274 * We prepend so that the most recent definition will be found.
276 PREPEND(symtab
->table
[bucket
], elt
, link
);
278 if (symtab
->count
> symtab
->maxload
)
281 return (ISC_R_SUCCESS
);
285 isc_symtab_undefine(isc_symtab_t
*symtab
, const char *key
, unsigned int type
) {
289 REQUIRE(VALID_SYMTAB(symtab
));
290 REQUIRE(key
!= NULL
);
292 FIND(symtab
, key
, type
, bucket
, elt
);
295 return (ISC_R_NOTFOUND
);
297 if (symtab
->undefine_action
!= NULL
)
298 (symtab
->undefine_action
)(elt
->key
, elt
->type
,
299 elt
->value
, symtab
->undefine_arg
);
300 UNLINK(symtab
->table
[bucket
], elt
, link
);
301 isc_mem_put(symtab
->mctx
, elt
, sizeof(*elt
));
304 return (ISC_R_SUCCESS
);
308 isc_symtab_count(isc_symtab_t
*symtab
) {
309 REQUIRE(VALID_SYMTAB(symtab
));
310 return (symtab
->count
);