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