0875b58f |
1 | /* -*-c-*- |
2 | * |
d4d7a053 |
3 | * $Id: sym.h,v 1.7 1999/06/01 09:49:33 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 $ |
d4d7a053 |
33 | * Revision 1.7 1999/06/01 09:49:33 mdw |
34 | * Allow things to be looked up by just their caller-supplied hashes. This |
35 | * actually needs to be thought through better. |
36 | * |
34b97201 |
37 | * Revision 1.6 1999/05/26 21:08:31 mdw |
38 | * Rename symbols in line with newer conventions. |
39 | * |
8c6d948b |
40 | * Revision 1.5 1999/05/13 22:48:37 mdw |
41 | * Change `-ise' to `-ize' throughout. |
42 | * |
0bd98442 |
43 | * Revision 1.4 1999/05/06 19:51:35 mdw |
44 | * Reformatted the LGPL notice a little bit. |
45 | * |
c846879c |
46 | * Revision 1.3 1999/05/05 18:50:31 mdw |
47 | * Change licensing conditions to LGPL. |
48 | * |
a5d14ede |
49 | * Revision 1.2 1998/11/26 19:27:34 mdw |
50 | * Move SYM_NAME into the header file. Fix bugs. |
51 | * |
52 | * Revision 1.1.1.1 1998/06/17 23:44:42 mdw |
53 | * Initial version of mLib |
0875b58f |
54 | * |
55 | */ |
56 | |
57 | #ifndef SYM_H |
58 | #define SYM_H |
59 | |
60 | #ifdef __cplusplus |
61 | extern "C" { |
62 | #endif |
63 | |
64 | /*----- Required headers --------------------------------------------------*/ |
65 | |
66 | #include <stddef.h> |
67 | |
d4d7a053 |
68 | #ifndef BITS_H |
69 | # include "bits.h" |
70 | #endif |
71 | |
0875b58f |
72 | /*----- Type definitions --------------------------------------------------*/ |
73 | |
74 | /* --- Symbol table --- * |
75 | * |
76 | * A @sym_table@ contains the information needed to manage a symbol table. |
77 | * Users shouldn't fiddle with this information directly, but it needs to be |
78 | * here so that objects of the correct type can be created. |
79 | */ |
80 | |
81 | typedef struct sym_table { |
82 | unsigned long mask; /* Bit mask for hashing purposes */ |
83 | size_t c; /* Down counter for growing table */ |
84 | struct sym_base **a; /* Array of hash bins */ |
85 | } sym_table; |
86 | |
87 | /* --- A symbol table entry --- * |
88 | * |
89 | * I don't care what actually gets stored in symbol entries because I don't |
90 | * create them: that's the responsibility of my client. All I care about |
91 | * here is that whatever gets passed to me is a structure whose first member |
92 | * is a @sym_base@. The ANSI guarantees about structure layout are |
93 | * sufficient to allow me to manipulate such objects. |
94 | */ |
95 | |
96 | #define SYM_BUFSZ 16 /* Size of local string buffer */ |
97 | |
98 | typedef struct sym_base { |
99 | struct sym_base *next; /* Next symbol in hash bin */ |
d4d7a053 |
100 | uint32 hash; /* Hash value for symbol's name */ |
0875b58f |
101 | union { |
102 | char *p; /* Pointer to name string */ |
103 | char b[SYM_BUFSZ]; /* Buffer containing a short name */ |
104 | } name; /* Name of this symbol */ |
105 | size_t len; /* Length of the symbol's name */ |
106 | } sym_base; |
107 | |
a5d14ede |
108 | /* --- A macro to pick a symbol's name out from the mess --- */ |
109 | |
110 | #define SYM_NAME(sy) \ |
111 | (((sym_base *)(sy))->len > SYM_BUFSZ ? \ |
112 | ((sym_base *)(sy))->name.p : \ |
113 | ((sym_base *)(sy))->name.b) |
114 | |
0875b58f |
115 | /* --- An iterator block --- */ |
116 | |
117 | typedef struct sym_iter { |
118 | sym_table *t; /* Symbol table being iterated */ |
119 | sym_base *n; /* Address of next item to return */ |
120 | size_t i; /* Index of next hash bin to use */ |
121 | } sym_iter; |
122 | |
123 | /*----- External functions ------------------------------------------------*/ |
124 | |
34b97201 |
125 | /* --- @sym_create@ --- * |
0875b58f |
126 | * |
8c6d948b |
127 | * Arguments: @sym_table *t@ = symbol table to initialize |
0875b58f |
128 | * |
129 | * Returns: --- |
130 | * |
8c6d948b |
131 | * Use: Initializes the given symbol table. Raises @EXC_NOMEM@ if |
0875b58f |
132 | * there isn't enough memory. |
133 | */ |
134 | |
34b97201 |
135 | extern void sym_create(sym_table */*t*/); |
0875b58f |
136 | |
34b97201 |
137 | /* --- @sym_destroy@ --- * |
0875b58f |
138 | * |
139 | * Arguments: @sym_table *t@ = pointer to symbol table in question |
140 | * |
141 | * Returns: --- |
142 | * |
143 | * Use: Destroys a symbol table, freeing all the memory it used to |
144 | * occupy. |
145 | */ |
146 | |
34b97201 |
147 | extern void sym_destroy(sym_table */*t*/); |
0875b58f |
148 | |
149 | /* --- @sym_find@ --- * |
150 | * |
151 | * Arguments: @sym_table *t@ = pointer to symbol table in question |
152 | * @const char *n@ = pointer to symbol table to look up |
153 | * @long l@ = length of the name string or negative to measure |
154 | * @size_t sz@ = size of desired symbol object, or zero |
155 | * @unsigned *f@ = pointer to a flag, or null. |
156 | * |
157 | * Returns: The address of a @sym_base@ structure, or null if not found |
158 | * and @sz@ is zero. |
159 | * |
160 | * Use: Looks up a symbol in a given symbol table. The name is |
161 | * passed by the address of its first character. The length |
162 | * may be given, in which case the name may contain arbitrary |
163 | * binary data, or it may be given as a negative number, in |
164 | * which case the length of the name is calculated as |
d4d7a053 |
165 | * @strlen(n) + 1@. The name pointer @n@ may also be zero; in |
166 | * this case, @l@ is taken to be a raw hash, and any element |
167 | * with a matching hash is taken to be the one wanted. |
0875b58f |
168 | * |
169 | * The return value is the address of a pointer to a @sym_base@ |
170 | * block (which may have other things on the end, as above). If |
171 | * the symbol could be found, the return value points to the |
172 | * symbol block. If the symbol wasn't there, then if @sz@ is |
173 | * nonzero, a new symbol is created and its address is returned; |
174 | * otherwise a null pointer is returned. The exception |
175 | * @EXC_NOMEM@ is raised if the block can't be allocated. |
176 | * |
177 | * The value of @*f@ indicates whether a new symbol entry was |
178 | * created: a nonzero value indicates that an old value was |
179 | * found. |
180 | */ |
181 | |
182 | extern void *sym_find(sym_table */*t*/, const char */*n*/, long /*l*/, |
183 | size_t /*sz*/, unsigned */*f*/); |
184 | |
185 | /* --- @sym_remove@ --- * |
186 | * |
187 | * Arguments: @sym_table *i@ = pointer to a symbol table object |
188 | * @void *b@ = pointer to symbol table entry |
189 | * |
190 | * Returns: --- |
191 | * |
192 | * Use: Removes the object from the symbol table. The space occupied |
193 | * by the object and its name is freed; anything else attached |
194 | * to the entry should already be gone by this point. |
195 | */ |
196 | |
197 | extern void sym_remove(sym_table */*t*/, void */*b*/); |
198 | |
34b97201 |
199 | /* --- @sym_mkiter@ --- * |
0875b58f |
200 | * |
201 | * Arguments: @sym_iter *i@ = pointer to an iterator object |
202 | * @sym_table *t@ = pointer to a symbol table object |
203 | * |
204 | * Returns: --- |
205 | * |
206 | * Use: Creates a new symbol table iterator which may be used to |
207 | * iterate through a symbol table. |
208 | */ |
209 | |
34b97201 |
210 | extern void sym_mkiter(sym_iter */*i*/, sym_table */*t*/); |
0875b58f |
211 | |
212 | /* --- @sym_next@ --- * |
213 | * |
214 | * Arguments: @sym_iter *i@ = pointer to iterator object |
215 | * |
216 | * Returns: Pointer to the next symbol found, or null when finished. |
217 | * |
218 | * Use: Returns the next symbol from the table. Symbols are not |
219 | * returned in any particular order. |
220 | */ |
221 | |
222 | extern void *sym_next(sym_iter */*i*/); |
223 | |
224 | /*----- That's all, folks -------------------------------------------------*/ |
225 | |
226 | #ifdef __cplusplus |
227 | } |
228 | #endif |
229 | |
230 | #endif |