0875b58f |
1 | /* -*-c-*- |
2 | * |
6f444bda |
3 | * $Id: sym.h,v 1.13 2003/12/15 20:53:47 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.h,v $ |
6f444bda |
33 | * Revision 1.13 2003/12/15 20:53:47 mdw |
34 | * Add global unihash table; use universal hashing instead of CRC. |
35 | * |
2ae5376b |
36 | * Revision 1.12 2001/01/20 11:49:37 mdw |
37 | * Export tuning parameters from header file, for the benefit of other |
38 | * hashtable implementations. Change the storage of symbol names: store |
39 | * the name after the allocated symbol block in all cases. This replaces |
40 | * the previous complicated and slightly wasteful arrangement. |
41 | * |
20eb516f |
42 | * Revision 1.11 2000/06/17 10:37:39 mdw |
43 | * Add support for arena management. |
44 | * |
c6e0eaf0 |
45 | * Revision 1.10 1999/12/10 23:42:04 mdw |
46 | * Change header file guard names. |
47 | * |
dc2feaa9 |
48 | * Revision 1.9 1999/08/02 16:53:48 mdw |
49 | * Improve type safety for sym_iter objects. |
50 | * |
03d53b73 |
51 | * Revision 1.8 1999/08/02 14:45:48 mdw |
52 | * Break low-level hashtable code out from sym. |
53 | * |
d4d7a053 |
54 | * Revision 1.7 1999/06/01 09:49:33 mdw |
55 | * Allow things to be looked up by just their caller-supplied hashes. This |
56 | * actually needs to be thought through better. |
57 | * |
34b97201 |
58 | * Revision 1.6 1999/05/26 21:08:31 mdw |
59 | * Rename symbols in line with newer conventions. |
60 | * |
8c6d948b |
61 | * Revision 1.5 1999/05/13 22:48:37 mdw |
62 | * Change `-ise' to `-ize' throughout. |
63 | * |
0bd98442 |
64 | * Revision 1.4 1999/05/06 19:51:35 mdw |
65 | * Reformatted the LGPL notice a little bit. |
66 | * |
c846879c |
67 | * Revision 1.3 1999/05/05 18:50:31 mdw |
68 | * Change licensing conditions to LGPL. |
69 | * |
a5d14ede |
70 | * Revision 1.2 1998/11/26 19:27:34 mdw |
71 | * Move SYM_NAME into the header file. Fix bugs. |
72 | * |
73 | * Revision 1.1.1.1 1998/06/17 23:44:42 mdw |
74 | * Initial version of mLib |
0875b58f |
75 | * |
76 | */ |
77 | |
c6e0eaf0 |
78 | #ifndef MLIB_SYM_H |
79 | #define MLIB_SYM_H |
0875b58f |
80 | |
81 | #ifdef __cplusplus |
82 | extern "C" { |
83 | #endif |
84 | |
85 | /*----- Required headers --------------------------------------------------*/ |
86 | |
87 | #include <stddef.h> |
88 | |
c6e0eaf0 |
89 | #ifndef MLIB_BITS_H |
d4d7a053 |
90 | # include "bits.h" |
91 | #endif |
92 | |
c6e0eaf0 |
93 | #ifndef MLIB_HASH_H |
03d53b73 |
94 | # include "hash.h" |
95 | #endif |
96 | |
20eb516f |
97 | #ifndef MLIB_SUB_H |
98 | # include "sub.h" |
99 | #endif |
100 | |
2ae5376b |
101 | /*----- Tuning parameters -------------------------------------------------*/ |
102 | |
103 | /* --- Initial hash table size --- * |
104 | * |
105 | * This is the initial @mask@ value. It must be of the form %$2^n - 1$%, |
106 | * so that it can be used to mask of the bottom bits of a hash value. |
107 | */ |
108 | |
109 | #define SYM_INITSZ 32 /* Size of a new hash table */ |
110 | |
111 | /* --- Maximum load factor --- * |
112 | * |
113 | * This parameter controls how much the table has to be loaded before the |
114 | * table is extended. The number of elements %$n$%, the number of bins %$b$% |
115 | * and the limit %$l$% satisfy the relation %$n < bl$%; if a new item is |
116 | * added to the table and this relation is found to be false, the table is |
117 | * doubled in size. |
118 | */ |
119 | |
120 | #define SYM_LIMIT(n) ((n) * 2) /* Load factor for growing table */ |
121 | |
0875b58f |
122 | /*----- Type definitions --------------------------------------------------*/ |
123 | |
124 | /* --- Symbol table --- * |
125 | * |
126 | * A @sym_table@ contains the information needed to manage a symbol table. |
127 | * Users shouldn't fiddle with this information directly, but it needs to be |
128 | * here so that objects of the correct type can be created. |
129 | */ |
130 | |
131 | typedef struct sym_table { |
03d53b73 |
132 | hash_table t; |
20eb516f |
133 | subarena *s; |
03d53b73 |
134 | size_t load; |
0875b58f |
135 | } sym_table; |
136 | |
137 | /* --- A symbol table entry --- * |
138 | * |
139 | * I don't care what actually gets stored in symbol entries because I don't |
140 | * create them: that's the responsibility of my client. All I care about |
141 | * here is that whatever gets passed to me is a structure whose first member |
142 | * is a @sym_base@. The ANSI guarantees about structure layout are |
143 | * sufficient to allow me to manipulate such objects. |
144 | */ |
145 | |
0875b58f |
146 | typedef struct sym_base { |
2ae5376b |
147 | hash_base b; /* Base structure */ |
148 | char *name; /* Pointer to name string */ |
0875b58f |
149 | size_t len; /* Length of the symbol's name */ |
150 | } sym_base; |
151 | |
2ae5376b |
152 | /* --- Macros for picking out useful information --- * |
153 | * |
154 | * Note that @SYM_LEN@ returns the size of the symbol key. For textual keys, |
155 | * this will include the terminating null. |
156 | */ |
a5d14ede |
157 | |
2ae5376b |
158 | #define SYM_NAME(sy) ((const char *)(((sym_base *)(sy))->name)) |
159 | #define SYM_LEN(sy) (((sym_base *)(sy))->len + 0) |
160 | #define SYM_HASH(sy) (((sym_base *)(sy))->b.hash + 0) |
a5d14ede |
161 | |
0875b58f |
162 | /* --- An iterator block --- */ |
163 | |
dc2feaa9 |
164 | typedef struct { hash_iter i; } sym_iter; |
0875b58f |
165 | |
166 | /*----- External functions ------------------------------------------------*/ |
167 | |
34b97201 |
168 | /* --- @sym_create@ --- * |
0875b58f |
169 | * |
8c6d948b |
170 | * Arguments: @sym_table *t@ = symbol table to initialize |
0875b58f |
171 | * |
172 | * Returns: --- |
173 | * |
8c6d948b |
174 | * Use: Initializes the given symbol table. Raises @EXC_NOMEM@ if |
0875b58f |
175 | * there isn't enough memory. |
176 | */ |
177 | |
34b97201 |
178 | extern void sym_create(sym_table */*t*/); |
0875b58f |
179 | |
34b97201 |
180 | /* --- @sym_destroy@ --- * |
0875b58f |
181 | * |
182 | * Arguments: @sym_table *t@ = pointer to symbol table in question |
183 | * |
184 | * Returns: --- |
185 | * |
186 | * Use: Destroys a symbol table, freeing all the memory it used to |
187 | * occupy. |
188 | */ |
189 | |
34b97201 |
190 | extern void sym_destroy(sym_table */*t*/); |
0875b58f |
191 | |
192 | /* --- @sym_find@ --- * |
193 | * |
194 | * Arguments: @sym_table *t@ = pointer to symbol table in question |
6f444bda |
195 | * @const char *n@ = pointer to symbol name to look up |
0875b58f |
196 | * @long l@ = length of the name string or negative to measure |
197 | * @size_t sz@ = size of desired symbol object, or zero |
198 | * @unsigned *f@ = pointer to a flag, or null. |
199 | * |
200 | * Returns: The address of a @sym_base@ structure, or null if not found |
201 | * and @sz@ is zero. |
202 | * |
203 | * Use: Looks up a symbol in a given symbol table. The name is |
204 | * passed by the address of its first character. The length |
205 | * may be given, in which case the name may contain arbitrary |
206 | * binary data, or it may be given as a negative number, in |
207 | * which case the length of the name is calculated as |
03d53b73 |
208 | * @strlen(n) + 1@. |
0875b58f |
209 | * |
210 | * The return value is the address of a pointer to a @sym_base@ |
211 | * block (which may have other things on the end, as above). If |
212 | * the symbol could be found, the return value points to the |
213 | * symbol block. If the symbol wasn't there, then if @sz@ is |
214 | * nonzero, a new symbol is created and its address is returned; |
215 | * otherwise a null pointer is returned. The exception |
216 | * @EXC_NOMEM@ is raised if the block can't be allocated. |
217 | * |
218 | * The value of @*f@ indicates whether a new symbol entry was |
219 | * created: a nonzero value indicates that an old value was |
220 | * found. |
221 | */ |
222 | |
223 | extern void *sym_find(sym_table */*t*/, const char */*n*/, long /*l*/, |
224 | size_t /*sz*/, unsigned */*f*/); |
225 | |
226 | /* --- @sym_remove@ --- * |
227 | * |
c6e0eaf0 |
228 | * Arguments: @sym_table *t@ = pointer to a symbol table object |
0875b58f |
229 | * @void *b@ = pointer to symbol table entry |
230 | * |
231 | * Returns: --- |
232 | * |
233 | * Use: Removes the object from the symbol table. The space occupied |
234 | * by the object and its name is freed; anything else attached |
235 | * to the entry should already be gone by this point. |
236 | */ |
237 | |
238 | extern void sym_remove(sym_table */*t*/, void */*b*/); |
239 | |
34b97201 |
240 | /* --- @sym_mkiter@ --- * |
0875b58f |
241 | * |
242 | * Arguments: @sym_iter *i@ = pointer to an iterator object |
243 | * @sym_table *t@ = pointer to a symbol table object |
244 | * |
245 | * Returns: --- |
246 | * |
247 | * Use: Creates a new symbol table iterator which may be used to |
248 | * iterate through a symbol table. |
249 | */ |
250 | |
dc2feaa9 |
251 | #define SYM_MKITER(i_, t_) HASH_MKITER(&(i_)->i, &(t_)->t) |
03d53b73 |
252 | |
34b97201 |
253 | extern void sym_mkiter(sym_iter */*i*/, sym_table */*t*/); |
0875b58f |
254 | |
255 | /* --- @sym_next@ --- * |
256 | * |
257 | * Arguments: @sym_iter *i@ = pointer to iterator object |
258 | * |
259 | * Returns: Pointer to the next symbol found, or null when finished. |
260 | * |
261 | * Use: Returns the next symbol from the table. Symbols are not |
262 | * returned in any particular order. |
263 | */ |
264 | |
dc2feaa9 |
265 | #define SYM_NEXT(i_, p) do { \ |
03d53b73 |
266 | hash_base *_q; \ |
dc2feaa9 |
267 | HASH_NEXT(&(i_)->i, _q); \ |
03d53b73 |
268 | (p) = (void *)_q; \ |
269 | } while (0) |
270 | |
0875b58f |
271 | extern void *sym_next(sym_iter */*i*/); |
272 | |
273 | /*----- That's all, folks -------------------------------------------------*/ |
274 | |
275 | #ifdef __cplusplus |
276 | } |
277 | #endif |
278 | |
279 | #endif |