0875b58f |
1 | /* -*-c-*- |
2 | * |
8656dc50 |
3 | * $Id: sym.h,v 1.14 2004/04/08 01:36:13 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 | |
c6e0eaf0 |
30 | #ifndef MLIB_SYM_H |
31 | #define MLIB_SYM_H |
0875b58f |
32 | |
33 | #ifdef __cplusplus |
34 | extern "C" { |
35 | #endif |
36 | |
37 | /*----- Required headers --------------------------------------------------*/ |
38 | |
39 | #include <stddef.h> |
40 | |
c6e0eaf0 |
41 | #ifndef MLIB_BITS_H |
d4d7a053 |
42 | # include "bits.h" |
43 | #endif |
44 | |
c6e0eaf0 |
45 | #ifndef MLIB_HASH_H |
03d53b73 |
46 | # include "hash.h" |
47 | #endif |
48 | |
20eb516f |
49 | #ifndef MLIB_SUB_H |
50 | # include "sub.h" |
51 | #endif |
52 | |
2ae5376b |
53 | /*----- Tuning parameters -------------------------------------------------*/ |
54 | |
55 | /* --- Initial hash table size --- * |
56 | * |
57 | * This is the initial @mask@ value. It must be of the form %$2^n - 1$%, |
58 | * so that it can be used to mask of the bottom bits of a hash value. |
59 | */ |
60 | |
61 | #define SYM_INITSZ 32 /* Size of a new hash table */ |
62 | |
63 | /* --- Maximum load factor --- * |
64 | * |
65 | * This parameter controls how much the table has to be loaded before the |
66 | * table is extended. The number of elements %$n$%, the number of bins %$b$% |
67 | * and the limit %$l$% satisfy the relation %$n < bl$%; if a new item is |
68 | * added to the table and this relation is found to be false, the table is |
69 | * doubled in size. |
70 | */ |
71 | |
72 | #define SYM_LIMIT(n) ((n) * 2) /* Load factor for growing table */ |
73 | |
0875b58f |
74 | /*----- Type definitions --------------------------------------------------*/ |
75 | |
76 | /* --- Symbol table --- * |
77 | * |
78 | * A @sym_table@ contains the information needed to manage a symbol table. |
79 | * Users shouldn't fiddle with this information directly, but it needs to be |
80 | * here so that objects of the correct type can be created. |
81 | */ |
82 | |
83 | typedef struct sym_table { |
03d53b73 |
84 | hash_table t; |
20eb516f |
85 | subarena *s; |
03d53b73 |
86 | size_t load; |
0875b58f |
87 | } sym_table; |
88 | |
89 | /* --- A symbol table entry --- * |
90 | * |
91 | * I don't care what actually gets stored in symbol entries because I don't |
92 | * create them: that's the responsibility of my client. All I care about |
93 | * here is that whatever gets passed to me is a structure whose first member |
94 | * is a @sym_base@. The ANSI guarantees about structure layout are |
95 | * sufficient to allow me to manipulate such objects. |
96 | */ |
97 | |
0875b58f |
98 | typedef struct sym_base { |
2ae5376b |
99 | hash_base b; /* Base structure */ |
100 | char *name; /* Pointer to name string */ |
0875b58f |
101 | size_t len; /* Length of the symbol's name */ |
102 | } sym_base; |
103 | |
2ae5376b |
104 | /* --- Macros for picking out useful information --- * |
105 | * |
106 | * Note that @SYM_LEN@ returns the size of the symbol key. For textual keys, |
107 | * this will include the terminating null. |
108 | */ |
a5d14ede |
109 | |
2ae5376b |
110 | #define SYM_NAME(sy) ((const char *)(((sym_base *)(sy))->name)) |
111 | #define SYM_LEN(sy) (((sym_base *)(sy))->len + 0) |
112 | #define SYM_HASH(sy) (((sym_base *)(sy))->b.hash + 0) |
a5d14ede |
113 | |
0875b58f |
114 | /* --- An iterator block --- */ |
115 | |
dc2feaa9 |
116 | typedef struct { hash_iter i; } sym_iter; |
0875b58f |
117 | |
118 | /*----- External functions ------------------------------------------------*/ |
119 | |
34b97201 |
120 | /* --- @sym_create@ --- * |
0875b58f |
121 | * |
8c6d948b |
122 | * Arguments: @sym_table *t@ = symbol table to initialize |
0875b58f |
123 | * |
124 | * Returns: --- |
125 | * |
8c6d948b |
126 | * Use: Initializes the given symbol table. Raises @EXC_NOMEM@ if |
0875b58f |
127 | * there isn't enough memory. |
128 | */ |
129 | |
34b97201 |
130 | extern void sym_create(sym_table */*t*/); |
0875b58f |
131 | |
34b97201 |
132 | /* --- @sym_destroy@ --- * |
0875b58f |
133 | * |
134 | * Arguments: @sym_table *t@ = pointer to symbol table in question |
135 | * |
136 | * Returns: --- |
137 | * |
138 | * Use: Destroys a symbol table, freeing all the memory it used to |
139 | * occupy. |
140 | */ |
141 | |
34b97201 |
142 | extern void sym_destroy(sym_table */*t*/); |
0875b58f |
143 | |
144 | /* --- @sym_find@ --- * |
145 | * |
146 | * Arguments: @sym_table *t@ = pointer to symbol table in question |
6f444bda |
147 | * @const char *n@ = pointer to symbol name to look up |
0875b58f |
148 | * @long l@ = length of the name string or negative to measure |
149 | * @size_t sz@ = size of desired symbol object, or zero |
150 | * @unsigned *f@ = pointer to a flag, or null. |
151 | * |
152 | * Returns: The address of a @sym_base@ structure, or null if not found |
153 | * and @sz@ is zero. |
154 | * |
155 | * Use: Looks up a symbol in a given symbol table. The name is |
156 | * passed by the address of its first character. The length |
157 | * may be given, in which case the name may contain arbitrary |
158 | * binary data, or it may be given as a negative number, in |
159 | * which case the length of the name is calculated as |
03d53b73 |
160 | * @strlen(n) + 1@. |
0875b58f |
161 | * |
162 | * The return value is the address of a pointer to a @sym_base@ |
163 | * block (which may have other things on the end, as above). If |
164 | * the symbol could be found, the return value points to the |
165 | * symbol block. If the symbol wasn't there, then if @sz@ is |
166 | * nonzero, a new symbol is created and its address is returned; |
167 | * otherwise a null pointer is returned. The exception |
168 | * @EXC_NOMEM@ is raised if the block can't be allocated. |
169 | * |
170 | * The value of @*f@ indicates whether a new symbol entry was |
171 | * created: a nonzero value indicates that an old value was |
172 | * found. |
173 | */ |
174 | |
175 | extern void *sym_find(sym_table */*t*/, const char */*n*/, long /*l*/, |
176 | size_t /*sz*/, unsigned */*f*/); |
177 | |
178 | /* --- @sym_remove@ --- * |
179 | * |
c6e0eaf0 |
180 | * Arguments: @sym_table *t@ = pointer to a symbol table object |
0875b58f |
181 | * @void *b@ = pointer to symbol table entry |
182 | * |
183 | * Returns: --- |
184 | * |
185 | * Use: Removes the object from the symbol table. The space occupied |
186 | * by the object and its name is freed; anything else attached |
187 | * to the entry should already be gone by this point. |
188 | */ |
189 | |
190 | extern void sym_remove(sym_table */*t*/, void */*b*/); |
191 | |
34b97201 |
192 | /* --- @sym_mkiter@ --- * |
0875b58f |
193 | * |
194 | * Arguments: @sym_iter *i@ = pointer to an iterator object |
195 | * @sym_table *t@ = pointer to a symbol table object |
196 | * |
197 | * Returns: --- |
198 | * |
199 | * Use: Creates a new symbol table iterator which may be used to |
200 | * iterate through a symbol table. |
201 | */ |
202 | |
dc2feaa9 |
203 | #define SYM_MKITER(i_, t_) HASH_MKITER(&(i_)->i, &(t_)->t) |
03d53b73 |
204 | |
34b97201 |
205 | extern void sym_mkiter(sym_iter */*i*/, sym_table */*t*/); |
0875b58f |
206 | |
207 | /* --- @sym_next@ --- * |
208 | * |
209 | * Arguments: @sym_iter *i@ = pointer to iterator object |
210 | * |
211 | * Returns: Pointer to the next symbol found, or null when finished. |
212 | * |
213 | * Use: Returns the next symbol from the table. Symbols are not |
214 | * returned in any particular order. |
215 | */ |
216 | |
dc2feaa9 |
217 | #define SYM_NEXT(i_, p) do { \ |
03d53b73 |
218 | hash_base *_q; \ |
dc2feaa9 |
219 | HASH_NEXT(&(i_)->i, _q); \ |
03d53b73 |
220 | (p) = (void *)_q; \ |
221 | } while (0) |
222 | |
0875b58f |
223 | extern void *sym_next(sym_iter */*i*/); |
224 | |
225 | /*----- That's all, folks -------------------------------------------------*/ |
226 | |
227 | #ifdef __cplusplus |
228 | } |
229 | #endif |
230 | |
231 | #endif |