chiark / gitweb /
Make ADNS wait for the event loop before collecting replies.
[mLib] / sym.c
CommitLineData
0875b58f 1/* -*-c-*-
2 *
6f444bda 3 * $Id: sym.c,v 1.14 2003/12/15 20:53:47 mdw Exp $
0875b58f 4 *
5 * Symbol table management
6 *
7 * (c) 1998 Straylight/Edgeware
8 */
9
c846879c 10/*----- Licensing notice --------------------------------------------------*
0875b58f 11 *
12 * This file is part of the mLib utilities library.
13 *
14 * mLib is free software; you can redistribute it and/or modify
c846879c 15 * it under the terms of the GNU Library General Public License as
16 * published by the Free Software Foundation; either version 2 of the
17 * License, or (at your option) any later version.
18 *
0875b58f 19 * mLib is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
c846879c 22 * GNU Library General Public License for more details.
23 *
24 * You should have received a copy of the GNU Library General Public
0bd98442 25 * License along with mLib; if not, write to the Free
26 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
27 * MA 02111-1307, USA.
0875b58f 28 */
29
30/*----- Revision history --------------------------------------------------*
31 *
32 * $Log: sym.c,v $
6f444bda 33 * Revision 1.14 2003/12/15 20:53:47 mdw
34 * Add global unihash table; use universal hashing instead of CRC.
35 *
ec6770dd 36 * Revision 1.13 2001/01/25 21:14:49 mdw
37 * Always add a terminating null, and don't count it in the length.
38 *
2ae5376b 39 * Revision 1.12 2001/01/20 11:49:37 mdw
40 * Export tuning parameters from header file, for the benefit of other
41 * hashtable implementations. Change the storage of symbol names: store
42 * the name after the allocated symbol block in all cases. This replaces
43 * the previous complicated and slightly wasteful arrangement.
44 *
20eb516f 45 * Revision 1.11 2000/06/17 10:37:39 mdw
46 * Add support for arena management.
47 *
c6e0eaf0 48 * Revision 1.10 1999/12/10 23:42:04 mdw
49 * Change header file guard names.
50 *
cd300147 51 * Revision 1.9 1999/10/22 22:36:37 mdw
52 * New test structure for symbol tables.
53 *
03d53b73 54 * Revision 1.8 1999/08/02 14:45:48 mdw
55 * Break low-level hashtable code out from sym.
56 *
d4d7a053 57 * Revision 1.7 1999/06/01 09:49:08 mdw
58 * Allow things to be looked up by just their caller-supplied hashes. This
59 * actually needs to be thought through better.
60 *
34b97201 61 * Revision 1.6 1999/05/26 21:08:31 mdw
62 * Rename symbols in line with newer conventions.
63 *
a7a37c4a 64 * Revision 1.5 1999/05/13 22:48:37 mdw
65 * Twiddle the extension threshold. Change `-ise' to `-ize' throughout.
66 *
0bd98442 67 * Revision 1.4 1999/05/06 19:51:35 mdw
68 * Reformatted the LGPL notice a little bit.
69 *
c846879c 70 * Revision 1.3 1999/05/05 18:50:31 mdw
71 * Change licensing conditions to LGPL.
72 *
a5d14ede 73 * Revision 1.2 1998/11/26 19:27:33 mdw
74 * Move SYM_NAME into the header file. Fix bugs.
75 *
76 * Revision 1.1.1.1 1998/06/17 23:44:42 mdw
77 * Initial version of mLib
0875b58f 78 *
79 */
80
81/*----- Header files ------------------------------------------------------*/
82
83/* --- ANSI headers --- */
84
85#include <stdio.h>
86#include <stdlib.h>
87#include <string.h>
88
89/* --- Local headers --- */
90
91#include "alloc.h"
20eb516f 92#include "arena.h"
d4d7a053 93#include "bits.h"
0875b58f 94#include "exc.h"
03d53b73 95#include "hash.h"
0875b58f 96#include "sub.h"
97#include "sym.h"
6f444bda 98#include "unihash.h"
0875b58f 99
0875b58f 100/*----- Main code ---------------------------------------------------------*/
101
34b97201 102/* --- @sym_create@ --- *
0875b58f 103 *
a7a37c4a 104 * Arguments: @sym_table *t@ = symbol table to initialize
0875b58f 105 *
106 * Returns: ---
107 *
a7a37c4a 108 * Use: Initializes the given symbol table. Raises @EXC_NOMEM@ if
0875b58f 109 * there isn't enough memory.
110 */
111
34b97201 112void sym_create(sym_table *t)
0875b58f 113{
03d53b73 114 hash_create(&t->t, SYM_INITSZ);
20eb516f 115 t->s = &sub_global;
03d53b73 116 t->load = SYM_LIMIT(SYM_INITSZ);
0875b58f 117}
118
34b97201 119/* --- @sym_destroy@ --- *
0875b58f 120 *
121 * Arguments: @sym_table *t@ = pointer to symbol table in question
122 *
123 * Returns: ---
124 *
125 * Use: Destroys a symbol table, freeing all the memory it used to
126 * occupy.
127 */
128
34b97201 129void sym_destroy(sym_table *t)
0875b58f 130{
03d53b73 131 sym_iter i;
0875b58f 132
03d53b73 133 SYM_MKITER(&i, t);
134 for (;;) {
135 sym_base *p;
136 SYM_NEXT(&i, p);
137 if (!p)
138 break;
20eb516f 139 x_free(t->t.a, p);
0875b58f 140 }
03d53b73 141 hash_destroy(&t->t);
0875b58f 142}
143
144/* --- @sym_find@ --- *
145 *
146 * Arguments: @sym_table *t@ = pointer to symbol table in question
6f444bda 147 * @const char *n@ = pointer to symbol name to look up
0875b58f 148 * @long l@ = length of the name string or negative to measure
149 * @size_t sz@ = size of desired symbol object, or zero
150 * @unsigned *f@ = pointer to a flag, or null.
151 *
152 * Returns: The address of a @sym_base@ structure, or null if not found
153 * and @sz@ is zero.
154 *
155 * Use: Looks up a symbol in a given symbol table. The name is
156 * passed by the address of its first character. The length
157 * may be given, in which case the name may contain arbitrary
158 * binary data, or it may be given as a negative number, in
159 * which case the length of the name is calculated as
03d53b73 160 * @strlen(n) + 1@.
0875b58f 161 *
162 * The return value is the address of a pointer to a @sym_base@
163 * block (which may have other things on the end, as above). If
164 * the symbol could be found, the return value points to the
165 * symbol block. If the symbol wasn't there, then if @sz@ is
166 * nonzero, a new symbol is created and its address is returned;
167 * otherwise a null pointer is returned. The exception
168 * @EXC_NOMEM@ is raised if the block can't be allocated.
169 *
170 * The value of @*f@ indicates whether a new symbol entry was
171 * created: a nonzero value indicates that an old value was
172 * found.
173 */
174
175void *sym_find(sym_table *t, const char *n, long l, size_t sz, unsigned *f)
176{
d4d7a053 177 uint32 hash;
178 size_t len = 0;
03d53b73 179 hash_base **bin, **p;
180 sym_base *q;
0875b58f 181
182 /* --- Find the correct bin --- */
183
ec6770dd 184 len = l < 0 ? strlen(n) : l;
6f444bda 185 hash = UNIHASH(&unihash_global, n, len);
03d53b73 186 bin = HASH_BIN(&t->t, hash);
0875b58f 187
188 /* --- Search the bin list --- */
189
03d53b73 190 for (p = bin; *p; p = &(*p)->next) {
191 q = (sym_base *)*p;
192 if (hash == q->b.hash && len == q->len && !memcmp(n, SYM_NAME(q), len)) {
193
0875b58f 194 /* --- Found a match --- *
195 *
196 * As a minor, and probably pointless, tweak, move the item to the
197 * front of its bin list.
198 */
199
03d53b73 200 (*p) = q->b.next;
201 q->b.next = *bin;
202 *bin = &q->b;
0875b58f 203
204 /* --- Return the block --- */
205
d4d7a053 206 if (f) *f = 1;
207 return (q);
0875b58f 208 }
0875b58f 209 }
210
211 /* --- Couldn't find the item there --- */
212
d4d7a053 213 if (f) *f = 0;
214 if (!sz) return (0);
0875b58f 215
2ae5376b 216 /* --- Create a new symbol block and initialize it --- *
217 *
218 * The name is attached to the end of the symbol block.
219 */
0875b58f 220
ec6770dd 221 q = x_alloc(t->t.a, sz + len + 1);
20eb516f 222 q->b.next = *bin;
223 q->b.hash = hash;
2ae5376b 224 q->name = (char *)q + sz;
225 memcpy(q->name, n, len);
ec6770dd 226 q->name[len] = 0;
20eb516f 227 q->len = len;
03d53b73 228 *bin = &q->b;
0875b58f 229
230 /* --- Consider growing the array --- */
231
03d53b73 232 if (t->load)
233 t->load--;
234 if (!t->load && hash_extend(&t->t))
2ae5376b 235 t->load = SYM_LIMIT(t->t.mask + 1);
0875b58f 236
237 /* --- Finished that, so return the new symbol block --- */
238
03d53b73 239 return (q);
0875b58f 240}
241
242/* --- @sym_remove@ --- *
243 *
c6e0eaf0 244 * Arguments: @sym_table *t@ = pointer to a symbol table object
03d53b73 245 * @void *p@ = pointer to symbol table entry
0875b58f 246 *
247 * Returns: ---
248 *
249 * Use: Removes the object from the symbol table. The space occupied
250 * by the object and its name is freed; anything else attached
251 * to the entry should already be gone by this point.
252 */
253
03d53b73 254void sym_remove(sym_table *t, void *p)
0875b58f 255{
03d53b73 256 sym_base *q = p;
257 hash_remove(&t->t, &q->b);
20eb516f 258 xfree(q);
03d53b73 259 t->load++;
0875b58f 260}
261
34b97201 262/* --- @sym_mkiter@ --- *
0875b58f 263 *
264 * Arguments: @sym_iter *i@ = pointer to an iterator object
265 * @sym_table *t@ = pointer to a symbol table object
266 *
267 * Returns: ---
268 *
269 * Use: Creates a new symbol table iterator which may be used to
270 * iterate through a symbol table.
271 */
272
03d53b73 273void sym_mkiter(sym_iter *i, sym_table *t) { SYM_MKITER(i, t); }
0875b58f 274
275/* --- @sym_next@ --- *
276 *
277 * Arguments: @sym_iter *i@ = pointer to iterator object
278 *
279 * Returns: Pointer to the next symbol found, or null when finished.
280 *
281 * Use: Returns the next symbol from the table. Symbols are not
282 * returned in any particular order.
283 */
284
285void *sym_next(sym_iter *i)
286{
03d53b73 287 void *p;
288 SYM_NEXT(i, p);
0875b58f 289 return (p);
290}
291
0875b58f 292/*----- That's all, folks -------------------------------------------------*/