0875b58f |
1 | /* -*-c-*- |
2 | * |
0875b58f |
3 | * Symbol table management |
4 | * |
5 | * (c) 1998 Straylight/Edgeware |
6 | */ |
7 | |
d4efbcd9 |
8 | /*----- Licensing notice --------------------------------------------------* |
0875b58f |
9 | * |
10 | * This file is part of the mLib utilities library. |
11 | * |
12 | * mLib is free software; you can redistribute it and/or modify |
c846879c |
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. |
d4efbcd9 |
16 | * |
0875b58f |
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 |
c846879c |
20 | * GNU Library General Public License for more details. |
d4efbcd9 |
21 | * |
c846879c |
22 | * You should have received a copy of the GNU Library General Public |
0bd98442 |
23 | * License along with mLib; if not, write to the Free |
24 | * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, |
25 | * MA 02111-1307, USA. |
0875b58f |
26 | */ |
27 | |
0875b58f |
28 | /*----- Header files ------------------------------------------------------*/ |
29 | |
30 | /* --- ANSI headers --- */ |
31 | |
32 | #include <stdio.h> |
33 | #include <stdlib.h> |
34 | #include <string.h> |
35 | |
36 | /* --- Local headers --- */ |
37 | |
38 | #include "alloc.h" |
20eb516f |
39 | #include "arena.h" |
d4d7a053 |
40 | #include "bits.h" |
0875b58f |
41 | #include "exc.h" |
03d53b73 |
42 | #include "hash.h" |
0875b58f |
43 | #include "sub.h" |
44 | #include "sym.h" |
6f444bda |
45 | #include "unihash.h" |
0875b58f |
46 | |
0875b58f |
47 | /*----- Main code ---------------------------------------------------------*/ |
48 | |
34b97201 |
49 | /* --- @sym_create@ --- * |
0875b58f |
50 | * |
a7a37c4a |
51 | * Arguments: @sym_table *t@ = symbol table to initialize |
0875b58f |
52 | * |
53 | * Returns: --- |
54 | * |
a7a37c4a |
55 | * Use: Initializes the given symbol table. Raises @EXC_NOMEM@ if |
0875b58f |
56 | * there isn't enough memory. |
57 | */ |
58 | |
34b97201 |
59 | void sym_create(sym_table *t) |
0875b58f |
60 | { |
03d53b73 |
61 | hash_create(&t->t, SYM_INITSZ); |
20eb516f |
62 | t->s = &sub_global; |
03d53b73 |
63 | t->load = SYM_LIMIT(SYM_INITSZ); |
0875b58f |
64 | } |
65 | |
34b97201 |
66 | /* --- @sym_destroy@ --- * |
0875b58f |
67 | * |
68 | * Arguments: @sym_table *t@ = pointer to symbol table in question |
69 | * |
70 | * Returns: --- |
71 | * |
72 | * Use: Destroys a symbol table, freeing all the memory it used to |
73 | * occupy. |
74 | */ |
75 | |
34b97201 |
76 | void sym_destroy(sym_table *t) |
0875b58f |
77 | { |
03d53b73 |
78 | sym_iter i; |
0875b58f |
79 | |
03d53b73 |
80 | SYM_MKITER(&i, t); |
81 | for (;;) { |
82 | sym_base *p; |
83 | SYM_NEXT(&i, p); |
84 | if (!p) |
85 | break; |
20eb516f |
86 | x_free(t->t.a, p); |
0875b58f |
87 | } |
03d53b73 |
88 | hash_destroy(&t->t); |
0875b58f |
89 | } |
90 | |
91 | /* --- @sym_find@ --- * |
92 | * |
93 | * Arguments: @sym_table *t@ = pointer to symbol table in question |
6f444bda |
94 | * @const char *n@ = pointer to symbol name to look up |
0875b58f |
95 | * @long l@ = length of the name string or negative to measure |
96 | * @size_t sz@ = size of desired symbol object, or zero |
97 | * @unsigned *f@ = pointer to a flag, or null. |
98 | * |
99 | * Returns: The address of a @sym_base@ structure, or null if not found |
100 | * and @sz@ is zero. |
101 | * |
102 | * Use: Looks up a symbol in a given symbol table. The name is |
103 | * passed by the address of its first character. The length |
104 | * may be given, in which case the name may contain arbitrary |
105 | * binary data, or it may be given as a negative number, in |
106 | * which case the length of the name is calculated as |
03d53b73 |
107 | * @strlen(n) + 1@. |
0875b58f |
108 | * |
109 | * The return value is the address of a pointer to a @sym_base@ |
110 | * block (which may have other things on the end, as above). If |
111 | * the symbol could be found, the return value points to the |
112 | * symbol block. If the symbol wasn't there, then if @sz@ is |
113 | * nonzero, a new symbol is created and its address is returned; |
114 | * otherwise a null pointer is returned. The exception |
115 | * @EXC_NOMEM@ is raised if the block can't be allocated. |
116 | * |
117 | * The value of @*f@ indicates whether a new symbol entry was |
118 | * created: a nonzero value indicates that an old value was |
119 | * found. |
120 | */ |
121 | |
122 | void *sym_find(sym_table *t, const char *n, long l, size_t sz, unsigned *f) |
123 | { |
d4d7a053 |
124 | uint32 hash; |
125 | size_t len = 0; |
03d53b73 |
126 | hash_base **bin, **p; |
127 | sym_base *q; |
0875b58f |
128 | |
129 | /* --- Find the correct bin --- */ |
130 | |
ec6770dd |
131 | len = l < 0 ? strlen(n) : l; |
6f444bda |
132 | hash = UNIHASH(&unihash_global, n, len); |
03d53b73 |
133 | bin = HASH_BIN(&t->t, hash); |
0875b58f |
134 | |
135 | /* --- Search the bin list --- */ |
136 | |
03d53b73 |
137 | for (p = bin; *p; p = &(*p)->next) { |
138 | q = (sym_base *)*p; |
139 | if (hash == q->b.hash && len == q->len && !memcmp(n, SYM_NAME(q), len)) { |
140 | |
0875b58f |
141 | /* --- Found a match --- * |
142 | * |
143 | * As a minor, and probably pointless, tweak, move the item to the |
144 | * front of its bin list. |
145 | */ |
146 | |
03d53b73 |
147 | (*p) = q->b.next; |
148 | q->b.next = *bin; |
149 | *bin = &q->b; |
0875b58f |
150 | |
151 | /* --- Return the block --- */ |
152 | |
d4d7a053 |
153 | if (f) *f = 1; |
154 | return (q); |
0875b58f |
155 | } |
0875b58f |
156 | } |
157 | |
158 | /* --- Couldn't find the item there --- */ |
159 | |
d4d7a053 |
160 | if (f) *f = 0; |
161 | if (!sz) return (0); |
0875b58f |
162 | |
2ae5376b |
163 | /* --- Create a new symbol block and initialize it --- * |
164 | * |
165 | * The name is attached to the end of the symbol block. |
166 | */ |
0875b58f |
167 | |
ec6770dd |
168 | q = x_alloc(t->t.a, sz + len + 1); |
20eb516f |
169 | q->b.next = *bin; |
170 | q->b.hash = hash; |
2ae5376b |
171 | q->name = (char *)q + sz; |
172 | memcpy(q->name, n, len); |
ec6770dd |
173 | q->name[len] = 0; |
20eb516f |
174 | q->len = len; |
03d53b73 |
175 | *bin = &q->b; |
0875b58f |
176 | |
177 | /* --- Consider growing the array --- */ |
178 | |
03d53b73 |
179 | if (t->load) |
180 | t->load--; |
181 | if (!t->load && hash_extend(&t->t)) |
2ae5376b |
182 | t->load = SYM_LIMIT(t->t.mask + 1); |
0875b58f |
183 | |
184 | /* --- Finished that, so return the new symbol block --- */ |
185 | |
03d53b73 |
186 | return (q); |
0875b58f |
187 | } |
188 | |
189 | /* --- @sym_remove@ --- * |
190 | * |
c6e0eaf0 |
191 | * Arguments: @sym_table *t@ = pointer to a symbol table object |
03d53b73 |
192 | * @void *p@ = pointer to symbol table entry |
0875b58f |
193 | * |
194 | * Returns: --- |
195 | * |
196 | * Use: Removes the object from the symbol table. The space occupied |
197 | * by the object and its name is freed; anything else attached |
198 | * to the entry should already be gone by this point. |
199 | */ |
200 | |
03d53b73 |
201 | void sym_remove(sym_table *t, void *p) |
0875b58f |
202 | { |
03d53b73 |
203 | sym_base *q = p; |
204 | hash_remove(&t->t, &q->b); |
20eb516f |
205 | xfree(q); |
03d53b73 |
206 | t->load++; |
0875b58f |
207 | } |
208 | |
34b97201 |
209 | /* --- @sym_mkiter@ --- * |
0875b58f |
210 | * |
211 | * Arguments: @sym_iter *i@ = pointer to an iterator object |
212 | * @sym_table *t@ = pointer to a symbol table object |
213 | * |
214 | * Returns: --- |
215 | * |
216 | * Use: Creates a new symbol table iterator which may be used to |
217 | * iterate through a symbol table. |
218 | */ |
219 | |
03d53b73 |
220 | void sym_mkiter(sym_iter *i, sym_table *t) { SYM_MKITER(i, t); } |
0875b58f |
221 | |
222 | /* --- @sym_next@ --- * |
223 | * |
224 | * Arguments: @sym_iter *i@ = pointer to iterator object |
225 | * |
226 | * Returns: Pointer to the next symbol found, or null when finished. |
227 | * |
228 | * Use: Returns the next symbol from the table. Symbols are not |
229 | * returned in any particular order. |
230 | */ |
231 | |
232 | void *sym_next(sym_iter *i) |
233 | { |
03d53b73 |
234 | void *p; |
235 | SYM_NEXT(i, p); |
0875b58f |
236 | return (p); |
237 | } |
238 | |
0875b58f |
239 | /*----- That's all, folks -------------------------------------------------*/ |