3 * Symbol table management
5 * (c) 1998 Straylight/Edgeware
8 /*----- Licensing notice --------------------------------------------------*
10 * This file is part of the mLib utilities library.
12 * mLib is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU Library General Public License as
14 * published by the Free Software Foundation; either version 2 of the
15 * License, or (at your option) any later version.
17 * mLib is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU Library General Public License for more details.
22 * You should have received a copy of the GNU Library General Public
23 * License along with mLib; if not, write to the Free
24 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
28 /*----- Header files ------------------------------------------------------*/
30 /* --- ANSI headers --- */
36 /* --- Local headers --- */
48 /*----- Main code ---------------------------------------------------------*/
50 /* --- @sym_create@ --- *
52 * Arguments: @sym_table *t@ = symbol table to initialize
56 * Use: Initializes the given symbol table. Raises @EXC_NOMEM@ if
57 * there isn't enough memory.
60 void sym_create(sym_table *t)
62 hash_create(&t->t, SYM_INITSZ);
64 t->load = SYM_LIMIT(SYM_INITSZ);
67 /* --- @sym_destroy@ --- *
69 * Arguments: @sym_table *t@ = pointer to symbol table in question
73 * Use: Destroys a symbol table, freeing all the memory it used to
77 void sym_destroy(sym_table *t)
92 /* --- @sym_find@ --- *
94 * Arguments: @sym_table *t@ = pointer to symbol table in question
95 * @const char *n@ = pointer to symbol name to look up
96 * @long l@ = length of the name string or negative to measure
97 * @size_t sz@ = size of desired symbol object, or zero
98 * @unsigned *f@ = pointer to a flag, or null.
100 * Returns: The address of a @sym_base@ structure, or null if not found
103 * Use: Looks up a symbol in a given symbol table. The name is
104 * passed by the address of its first character. The length
105 * may be given, in which case the name may contain arbitrary
106 * binary data, or it may be given as a negative number, in
107 * which case the length of the name is calculated as
110 * The return value is the address of a pointer to a @sym_base@
111 * block (which may have other things on the end, as above). If
112 * the symbol could be found, the return value points to the
113 * symbol block. If the symbol wasn't there, then if @sz@ is
114 * nonzero, a new symbol is created and its address is returned;
115 * otherwise a null pointer is returned. The exception
116 * @EXC_NOMEM@ is raised if the block can't be allocated.
118 * The value of @*f@ indicates whether a new symbol entry was
119 * created: a nonzero value indicates that an old value was
123 void *sym_find(sym_table *t, const char *n, long l, size_t sz, unsigned *f)
127 hash_base **bin, **p;
130 /* --- Find the correct bin --- */
132 len = l < 0 ? strlen(n) : l;
133 hash = UNIHASH(&unihash_global, n, len);
134 bin = HASH_BIN(&t->t, hash);
136 /* --- Search the bin list --- */
138 for (p = bin; *p; p = &(*p)->next) {
140 if (hash == q->b.hash && len == q->len &&
141 MEMCMP(n, ==, SYM_NAME(q), len)) {
143 /* --- Found a match --- *
145 * As a minor, and probably pointless, tweak, move the item to the
146 * front of its bin list.
153 /* --- Return the block --- */
160 /* --- Couldn't find the item there --- */
165 /* --- Create a new symbol block and initialize it --- *
167 * The name is attached to the end of the symbol block.
170 q = x_alloc(t->t.a, sz + len + 1);
173 q->name = (char *)q + sz;
174 memcpy(q->name, n, len);
179 /* --- Consider growing the array --- */
183 if (!t->load && hash_extend(&t->t))
184 t->load = SYM_LIMIT(t->t.mask + 1);
186 /* --- Finished that, so return the new symbol block --- */
191 /* --- @sym_remove@ --- *
193 * Arguments: @sym_table *t@ = pointer to a symbol table object
194 * @void *p@ = pointer to symbol table entry
198 * Use: Removes the object from the symbol table. The space occupied
199 * by the object and its name is freed; anything else attached
200 * to the entry should already be gone by this point.
203 void sym_remove(sym_table *t, void *p)
206 hash_remove(&t->t, &q->b);
211 /* --- @sym_mkiter@ --- *
213 * Arguments: @sym_iter *i@ = pointer to an iterator object
214 * @sym_table *t@ = pointer to a symbol table object
218 * Use: Creates a new symbol table iterator which may be used to
219 * iterate through a symbol table.
222 void sym_mkiter(sym_iter *i, sym_table *t) { SYM_MKITER(i, t); }
224 /* --- @sym_next@ --- *
226 * Arguments: @sym_iter *i@ = pointer to iterator object
228 * Returns: Pointer to the next symbol found, or null when finished.
230 * Use: Returns the next symbol from the table. Symbols are not
231 * returned in any particular order.
234 void *sym_next(sym_iter *i)
241 /*----- That's all, folks -------------------------------------------------*/