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