Commit | Line | Data |
---|---|---|
e6e0e332 MW |
1 | /* -*-c-*- |
2 | * | |
3 | * $Id: sym.h,v 1.1 2000/05/21 11:28:30 mdw Exp $ | |
4 | * | |
5 | * Symbol table management | |
6 | * | |
7 | * (c) 1998 Straylight | |
8 | * (c) 2000 Mark Wooding | |
9 | */ | |
10 | ||
11 | /*----- Licensing notice --------------------------------------------------* | |
12 | * | |
13 | * Copyright (c) 2000 Mark Wooding | |
14 | * All rights reserved. | |
15 | * | |
16 | * Redistribution and use in source and binary forms, with or without | |
17 | * modification, are permitted provided that the following conditions are | |
18 | * met: | |
19 | * | |
20 | * 1. Redistributions of source code must retain the above copyright | |
21 | * notice, this list of conditions and the following disclaimer. | |
22 | * | |
23 | * 2, Redistributions in binary form must reproduce the above copyright | |
24 | * notice, this list of conditions and the following disclaimer in the | |
25 | * documentation and/or other materials provided with the distribution. | |
26 | * | |
27 | * 3. The name of the authors may not be used to endorse or promote | |
28 | * products derived from this software without specific prior written | |
29 | * permission. | |
30 | * | |
31 | * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED | |
32 | * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF | |
33 | * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN | |
34 | * NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, | |
35 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES | |
36 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR | |
37 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
38 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, | |
39 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN | |
40 | * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | |
41 | * POSSIBILITY OF SUCH DAMAGE. | |
42 | * | |
43 | * Instead of accepting the above terms, you may redistribute and/or modify | |
44 | * this software under the terms of either the GNU General Public License, | |
45 | * or the GNU Library General Public License, published by the Free | |
46 | * Software Foundation; either version 2 of the License, or (at your | |
47 | * option) any later version. | |
48 | */ | |
49 | ||
50 | /*----- Revision history --------------------------------------------------* | |
51 | * | |
52 | * $Log: sym.h,v $ | |
53 | * Revision 1.1 2000/05/21 11:28:30 mdw | |
54 | * Initial check-in. | |
55 | * | |
56 | * --- Past lives (Become) --- * | |
57 | * | |
58 | * Revision 1.3 1998/01/12 16:46:30 mdw | |
59 | * Fix copyright date. | |
60 | * | |
61 | * Revision 1.2 1997/08/04 10:24:25 mdw | |
62 | * Sources placed under CVS control. | |
63 | * | |
64 | * Revision 1.1 1997/07/21 13:47:43 mdw | |
65 | * Initial revision | |
66 | * | |
67 | */ | |
68 | ||
69 | #ifndef SYM_H | |
70 | #define SYM_H | |
71 | ||
72 | #ifdef __cplusplus | |
73 | extern "C" { | |
74 | #endif | |
75 | ||
76 | /*----- Required headers --------------------------------------------------*/ | |
77 | ||
78 | #include <stddef.h> | |
79 | ||
80 | #ifndef BITS_H | |
81 | # include "bits.h" | |
82 | #endif | |
83 | ||
84 | /*----- Type definitions --------------------------------------------------*/ | |
85 | ||
86 | /* --- Symbol table --- * | |
87 | * | |
88 | * A @sym_table@ contains the information needed to manage a symbol table. | |
89 | * Users shouldn't fiddle with this information directly, but it needs to be | |
90 | * here so that objects of the correct type can be created. | |
91 | */ | |
92 | ||
93 | typedef struct sym_table { | |
94 | uint32 mask; /* Bit mask for hashing purposes */ | |
95 | size_t c; /* Down counter for growing table */ | |
96 | struct sym_base **a; /* Array of hash bins */ | |
97 | } sym_table; | |
98 | ||
99 | /* --- A symbol table entry --- * | |
100 | * | |
101 | * I don't care what actually gets stored in symbol entries because I don't | |
102 | * create them: that's the responsibility of my client. All I care about | |
103 | * here is that whatever gets passed to me is a structure whose first member | |
104 | * is a @sym_base@. The ANSI guarantees about structure layout are | |
105 | * sufficient to allow me to manipulate such objects. | |
106 | */ | |
107 | ||
108 | typedef struct sym_base { | |
109 | struct sym_base *next; /* Next symbol in hash bin */ | |
110 | uint32 hash; /* Hash value for symbol's name */ | |
111 | char *name; /* Name of this symbol */ | |
112 | size_t len; /* Length of the symbol's name */ | |
113 | } sym_base; | |
114 | ||
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 | ||
125 | /* --- @sym_create@ --- * | |
126 | * | |
127 | * Arguments: @sym_table *t@ = symbol table to initialize | |
128 | * | |
129 | * Returns: --- | |
130 | * | |
131 | * Use: Initialises the given symbol table. | |
132 | */ | |
133 | ||
134 | extern void sym_create(sym_table */*t*/); | |
135 | ||
136 | /* --- @sym_destroy@ --- * | |
137 | * | |
138 | * Arguments: @sym_table *t@ = pointer to symbol table in question | |
139 | * | |
140 | * Returns: --- | |
141 | * | |
142 | * Use: Destroys a symbol table, freeing all the memory it used to | |
143 | * occupy. | |
144 | */ | |
145 | ||
146 | extern void sym_destroy(sym_table */*t*/); | |
147 | ||
148 | /* --- @sym_find@ --- * | |
149 | * | |
150 | * Arguments: @sym_table *t@ = pointer to symbol table in question | |
151 | * @const char *n@ = pointer to symbol table to look up | |
152 | * @long l@ = length of the name string or negative to measure | |
153 | * @size_t sz@ = size of desired symbol object, or zero | |
154 | * @unsigned *f@ = pointer to a flag, or null. | |
155 | * | |
156 | * Returns: The address of a @sym_base@ structure, or null if not found | |
157 | * and @sz@ is zero. | |
158 | * | |
159 | * Use: Looks up a symbol in a given symbol table. The name is | |
160 | * passed by the address of its first character. The length | |
161 | * may be given, in which case the name may contain arbitrary | |
162 | * binary data, or it may be given as a negative number, in | |
163 | * which case the length of the name is calculated as | |
164 | * @strlen(n)@. | |
165 | * | |
166 | * The return value is the address of a pointer to a @sym_base@ | |
167 | * block (which may have other things on the end, as above). If | |
168 | * the symbol could be found, the return value points to the | |
169 | * symbol block. If the symbol wasn't there, then if @sz@ is | |
170 | * nonzero, a new symbol is created and its address is returned; | |
171 | * otherwise a null pointer is returned. | |
172 | * | |
173 | * The value of @*f@ indicates whether a new symbol entry was | |
174 | * created: a nonzero value indicates that an old value was | |
175 | * found. | |
176 | */ | |
177 | ||
178 | extern void *sym_find(sym_table */*t*/, const char */*n*/, long /*l*/, | |
179 | size_t /*sz*/, unsigned */*f*/); | |
180 | ||
181 | /* --- @sym_remove@ --- * | |
182 | * | |
183 | * Arguments: @sym_table *i@ = pointer to a symbol table object | |
184 | * @void *b@ = pointer to symbol table entry | |
185 | * | |
186 | * Returns: --- | |
187 | * | |
188 | * Use: Removes the object from the symbol table. The space occupied | |
189 | * by the object and its name is freed; anything else attached | |
190 | * to the entry should already be gone by this point. | |
191 | */ | |
192 | ||
193 | extern void sym_remove(sym_table */*t*/, void */*b*/); | |
194 | ||
195 | /* --- @sym_mkiter@ --- * | |
196 | * | |
197 | * Arguments: @sym_iter *i@ = pointer to an iterator object | |
198 | * @sym_table *t@ = pointer to a symbol table object | |
199 | * | |
200 | * Returns: --- | |
201 | * | |
202 | * Use: Creates a new symbol table iterator which may be used to | |
203 | * iterate through a symbol table. | |
204 | */ | |
205 | ||
206 | extern void sym_mkiter(sym_iter */*i*/, sym_table */*t*/); | |
207 | ||
208 | /* --- @sym_next@ --- * | |
209 | * | |
210 | * Arguments: @sym_iter *i@ = pointer to iterator object | |
211 | * | |
212 | * Returns: Pointer to the next symbol found, or null when finished. | |
213 | * | |
214 | * Use: Returns the next symbol from the table. Symbols are not | |
215 | * returned in any particular order. | |
216 | */ | |
217 | ||
218 | extern void *sym_next(sym_iter */*i*/); | |
219 | ||
220 | /*----- That's all, folks -------------------------------------------------*/ | |
221 | ||
222 | #ifdef __cplusplus | |
223 | } | |
224 | #endif | |
225 | ||
226 | #endif |