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