0875b58f |
1 | /* -*-c-*- |
2 | * |
20eb516f |
3 | * $Id: sym.c,v 1.11 2000/06/17 10:37:39 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 $ |
20eb516f |
33 | * Revision 1.11 2000/06/17 10:37:39 mdw |
34 | * Add support for arena management. |
35 | * |
c6e0eaf0 |
36 | * Revision 1.10 1999/12/10 23:42:04 mdw |
37 | * Change header file guard names. |
38 | * |
cd300147 |
39 | * Revision 1.9 1999/10/22 22:36:37 mdw |
40 | * New test structure for symbol tables. |
41 | * |
03d53b73 |
42 | * Revision 1.8 1999/08/02 14:45:48 mdw |
43 | * Break low-level hashtable code out from sym. |
44 | * |
d4d7a053 |
45 | * Revision 1.7 1999/06/01 09:49:08 mdw |
46 | * Allow things to be looked up by just their caller-supplied hashes. This |
47 | * actually needs to be thought through better. |
48 | * |
34b97201 |
49 | * Revision 1.6 1999/05/26 21:08:31 mdw |
50 | * Rename symbols in line with newer conventions. |
51 | * |
a7a37c4a |
52 | * Revision 1.5 1999/05/13 22:48:37 mdw |
53 | * Twiddle the extension threshold. Change `-ise' to `-ize' throughout. |
54 | * |
0bd98442 |
55 | * Revision 1.4 1999/05/06 19:51:35 mdw |
56 | * Reformatted the LGPL notice a little bit. |
57 | * |
c846879c |
58 | * Revision 1.3 1999/05/05 18:50:31 mdw |
59 | * Change licensing conditions to LGPL. |
60 | * |
a5d14ede |
61 | * Revision 1.2 1998/11/26 19:27:33 mdw |
62 | * Move SYM_NAME into the header file. Fix bugs. |
63 | * |
64 | * Revision 1.1.1.1 1998/06/17 23:44:42 mdw |
65 | * Initial version of mLib |
0875b58f |
66 | * |
67 | */ |
68 | |
69 | /*----- Header files ------------------------------------------------------*/ |
70 | |
71 | /* --- ANSI headers --- */ |
72 | |
73 | #include <stdio.h> |
74 | #include <stdlib.h> |
75 | #include <string.h> |
76 | |
77 | /* --- Local headers --- */ |
78 | |
79 | #include "alloc.h" |
20eb516f |
80 | #include "arena.h" |
d4d7a053 |
81 | #include "bits.h" |
0875b58f |
82 | #include "crc32.h" |
83 | #include "exc.h" |
03d53b73 |
84 | #include "hash.h" |
0875b58f |
85 | #include "sub.h" |
86 | #include "sym.h" |
0875b58f |
87 | |
88 | /*----- Tuning parameters -------------------------------------------------*/ |
89 | |
90 | /* --- Initial hash table size --- * |
91 | * |
92 | * This is the initial @mask@ value. It must be of the form %$2^n - 1$%, |
93 | * so that it can be used to mask of the bottom bits of a hash value. |
94 | */ |
95 | |
03d53b73 |
96 | #define SYM_INITSZ 64 /* Size of a new hash table */ |
0875b58f |
97 | |
98 | /* --- Maximum load factor --- * |
99 | * |
100 | * This parameter controls how much the table has to be loaded before the |
101 | * table is extended. The number of elements %$n$%, the number of bins %$b$% |
102 | * and the limit %$l$% satisfy the relation %$n < bl$%; if a new item is |
103 | * added to the table and this relation is found to be false, the table is |
104 | * doubled in size. |
0875b58f |
105 | */ |
106 | |
03d53b73 |
107 | #define SYM_LIMIT(n) ((n) * 2) /* Load factor for growing table */ |
0875b58f |
108 | |
0875b58f |
109 | /*----- Main code ---------------------------------------------------------*/ |
110 | |
34b97201 |
111 | /* --- @sym_create@ --- * |
0875b58f |
112 | * |
a7a37c4a |
113 | * Arguments: @sym_table *t@ = symbol table to initialize |
0875b58f |
114 | * |
115 | * Returns: --- |
116 | * |
a7a37c4a |
117 | * Use: Initializes the given symbol table. Raises @EXC_NOMEM@ if |
0875b58f |
118 | * there isn't enough memory. |
119 | */ |
120 | |
34b97201 |
121 | void sym_create(sym_table *t) |
0875b58f |
122 | { |
03d53b73 |
123 | hash_create(&t->t, SYM_INITSZ); |
20eb516f |
124 | t->s = &sub_global; |
03d53b73 |
125 | t->load = SYM_LIMIT(SYM_INITSZ); |
0875b58f |
126 | } |
127 | |
34b97201 |
128 | /* --- @sym_destroy@ --- * |
0875b58f |
129 | * |
130 | * Arguments: @sym_table *t@ = pointer to symbol table in question |
131 | * |
132 | * Returns: --- |
133 | * |
134 | * Use: Destroys a symbol table, freeing all the memory it used to |
135 | * occupy. |
136 | */ |
137 | |
34b97201 |
138 | void sym_destroy(sym_table *t) |
0875b58f |
139 | { |
03d53b73 |
140 | sym_iter i; |
0875b58f |
141 | |
03d53b73 |
142 | SYM_MKITER(&i, t); |
143 | for (;;) { |
144 | sym_base *p; |
145 | SYM_NEXT(&i, p); |
146 | if (!p) |
147 | break; |
148 | if (p->len > SYM_BUFSZ) |
20eb516f |
149 | subarena_free(t->s, p->name.p, p->len); |
150 | x_free(t->t.a, p); |
0875b58f |
151 | } |
03d53b73 |
152 | hash_destroy(&t->t); |
0875b58f |
153 | } |
154 | |
155 | /* --- @sym_find@ --- * |
156 | * |
157 | * Arguments: @sym_table *t@ = pointer to symbol table in question |
158 | * @const char *n@ = pointer to symbol table to look up |
159 | * @long l@ = length of the name string or negative to measure |
160 | * @size_t sz@ = size of desired symbol object, or zero |
161 | * @unsigned *f@ = pointer to a flag, or null. |
162 | * |
163 | * Returns: The address of a @sym_base@ structure, or null if not found |
164 | * and @sz@ is zero. |
165 | * |
166 | * Use: Looks up a symbol in a given symbol table. The name is |
167 | * passed by the address of its first character. The length |
168 | * may be given, in which case the name may contain arbitrary |
169 | * binary data, or it may be given as a negative number, in |
170 | * which case the length of the name is calculated as |
03d53b73 |
171 | * @strlen(n) + 1@. |
0875b58f |
172 | * |
173 | * The return value is the address of a pointer to a @sym_base@ |
174 | * block (which may have other things on the end, as above). If |
175 | * the symbol could be found, the return value points to the |
176 | * symbol block. If the symbol wasn't there, then if @sz@ is |
177 | * nonzero, a new symbol is created and its address is returned; |
178 | * otherwise a null pointer is returned. The exception |
179 | * @EXC_NOMEM@ is raised if the block can't be allocated. |
180 | * |
181 | * The value of @*f@ indicates whether a new symbol entry was |
182 | * created: a nonzero value indicates that an old value was |
183 | * found. |
184 | */ |
185 | |
186 | void *sym_find(sym_table *t, const char *n, long l, size_t sz, unsigned *f) |
187 | { |
d4d7a053 |
188 | uint32 hash; |
189 | size_t len = 0; |
03d53b73 |
190 | hash_base **bin, **p; |
191 | sym_base *q; |
0875b58f |
192 | |
193 | /* --- Find the correct bin --- */ |
194 | |
03d53b73 |
195 | len = l < 0 ? strlen(n) + 1 : l; |
196 | CRC32(hash, 0, n, len); |
197 | bin = HASH_BIN(&t->t, hash); |
0875b58f |
198 | |
199 | /* --- Search the bin list --- */ |
200 | |
03d53b73 |
201 | for (p = bin; *p; p = &(*p)->next) { |
202 | q = (sym_base *)*p; |
203 | if (hash == q->b.hash && len == q->len && !memcmp(n, SYM_NAME(q), len)) { |
204 | |
0875b58f |
205 | /* --- Found a match --- * |
206 | * |
207 | * As a minor, and probably pointless, tweak, move the item to the |
208 | * front of its bin list. |
209 | */ |
210 | |
03d53b73 |
211 | (*p) = q->b.next; |
212 | q->b.next = *bin; |
213 | *bin = &q->b; |
0875b58f |
214 | |
215 | /* --- Return the block --- */ |
216 | |
d4d7a053 |
217 | if (f) *f = 1; |
218 | return (q); |
0875b58f |
219 | } |
0875b58f |
220 | } |
221 | |
222 | /* --- Couldn't find the item there --- */ |
223 | |
d4d7a053 |
224 | if (f) *f = 0; |
225 | if (!sz) return (0); |
0875b58f |
226 | |
a7a37c4a |
227 | /* --- Create a new symbol block and initialize it --- */ |
0875b58f |
228 | |
20eb516f |
229 | q = x_alloc(t->t.a, sz); |
230 | q->b.next = *bin; |
231 | q->b.hash = hash; |
232 | q->len = len; |
233 | if (n) { |
234 | if (len <= SYM_BUFSZ) |
235 | memcpy(q->name.b, n, len); |
236 | else { |
237 | q->name.p = subarena_alloc(t->s, len); |
238 | memcpy(q->name.p, n, len); |
0875b58f |
239 | } |
0875b58f |
240 | } |
241 | |
03d53b73 |
242 | *bin = &q->b; |
0875b58f |
243 | |
244 | /* --- Consider growing the array --- */ |
245 | |
03d53b73 |
246 | if (t->load) |
247 | t->load--; |
248 | if (!t->load && hash_extend(&t->t)) |
249 | t->load = SYM_LIMIT(t->t.mask / 2 + 1); |
0875b58f |
250 | |
251 | /* --- Finished that, so return the new symbol block --- */ |
252 | |
03d53b73 |
253 | return (q); |
0875b58f |
254 | } |
255 | |
256 | /* --- @sym_remove@ --- * |
257 | * |
c6e0eaf0 |
258 | * Arguments: @sym_table *t@ = pointer to a symbol table object |
03d53b73 |
259 | * @void *p@ = pointer to symbol table entry |
0875b58f |
260 | * |
261 | * Returns: --- |
262 | * |
263 | * Use: Removes the object from the symbol table. The space occupied |
264 | * by the object and its name is freed; anything else attached |
265 | * to the entry should already be gone by this point. |
266 | */ |
267 | |
03d53b73 |
268 | void sym_remove(sym_table *t, void *p) |
0875b58f |
269 | { |
03d53b73 |
270 | sym_base *q = p; |
271 | hash_remove(&t->t, &q->b); |
272 | if (q->len > SYM_BUFSZ) |
20eb516f |
273 | subarena_free(t->s, q->name.p, q->len); |
274 | xfree(q); |
03d53b73 |
275 | t->load++; |
0875b58f |
276 | } |
277 | |
34b97201 |
278 | /* --- @sym_mkiter@ --- * |
0875b58f |
279 | * |
280 | * Arguments: @sym_iter *i@ = pointer to an iterator object |
281 | * @sym_table *t@ = pointer to a symbol table object |
282 | * |
283 | * Returns: --- |
284 | * |
285 | * Use: Creates a new symbol table iterator which may be used to |
286 | * iterate through a symbol table. |
287 | */ |
288 | |
03d53b73 |
289 | void sym_mkiter(sym_iter *i, sym_table *t) { SYM_MKITER(i, t); } |
0875b58f |
290 | |
291 | /* --- @sym_next@ --- * |
292 | * |
293 | * Arguments: @sym_iter *i@ = pointer to iterator object |
294 | * |
295 | * Returns: Pointer to the next symbol found, or null when finished. |
296 | * |
297 | * Use: Returns the next symbol from the table. Symbols are not |
298 | * returned in any particular order. |
299 | */ |
300 | |
301 | void *sym_next(sym_iter *i) |
302 | { |
03d53b73 |
303 | void *p; |
304 | SYM_NEXT(i, p); |
0875b58f |
305 | return (p); |
306 | } |
307 | |
0875b58f |
308 | /*----- That's all, folks -------------------------------------------------*/ |