3 * $Id: sym.h,v 1.2 2000/07/02 15:21:20 mdw Exp $
5 * Symbol table management
8 * (c) 2000 Mark Wooding
11 /*----- Licensing notice --------------------------------------------------*
13 * Copyright (c) 2000 Mark Wooding
14 * All rights reserved.
16 * Redistribution and use in source and binary forms, with or without
17 * modification, are permitted provided that the following conditions are
20 * 1. Redistributions of source code must retain the above copyright
21 * notice, this list of conditions and the following disclaimer.
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.
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
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 AUTHORS 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.
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.
50 /*----- Revision history --------------------------------------------------*
53 * Revision 1.2 2000/07/02 15:21:20 mdw
56 * Revision 1.1 2000/05/21 11:28:30 mdw
59 * --- Past lives (Become) --- *
61 * Revision 1.3 1998/01/12 16:46:30 mdw
64 * Revision 1.2 1997/08/04 10:24:25 mdw
65 * Sources placed under CVS control.
67 * Revision 1.1 1997/07/21 13:47:43 mdw
79 /*----- Required headers --------------------------------------------------*/
87 /*----- Type definitions --------------------------------------------------*/
89 /* --- Symbol table --- *
91 * A @sym_table@ contains the information needed to manage a symbol table.
92 * Users shouldn't fiddle with this information directly, but it needs to be
93 * here so that objects of the correct type can be created.
96 typedef struct sym_table
{
97 uint32 mask
; /* Bit mask for hashing purposes */
98 size_t c
; /* Down counter for growing table */
99 struct sym_base
**a
; /* Array of hash bins */
102 /* --- A symbol table entry --- *
104 * I don't care what actually gets stored in symbol entries because I don't
105 * create them: that's the responsibility of my client. All I care about
106 * here is that whatever gets passed to me is a structure whose first member
107 * is a @sym_base@. The ANSI guarantees about structure layout are
108 * sufficient to allow me to manipulate such objects.
111 typedef struct sym_base
{
112 struct sym_base
*next
; /* Next symbol in hash bin */
113 uint32 hash
; /* Hash value for symbol's name */
114 char *name
; /* Name of this symbol */
115 size_t len
; /* Length of the symbol's name */
118 /* --- An iterator block --- */
120 typedef struct sym_iter
{
121 sym_table
*t
; /* Symbol table being iterated */
122 sym_base
*n
; /* Address of next item to return */
123 size_t i
; /* Index of next hash bin to use */
126 /*----- External functions ------------------------------------------------*/
128 /* --- @sym_create@ --- *
130 * Arguments: @sym_table *t@ = symbol table to initialize
134 * Use: Initialises the given symbol table.
137 extern void sym_create(sym_table */
*t*/
);
139 /* --- @sym_destroy@ --- *
141 * Arguments: @sym_table *t@ = pointer to symbol table in question
145 * Use: Destroys a symbol table, freeing all the memory it used to
149 extern void sym_destroy(sym_table */
*t*/
);
151 /* --- @sym_find@ --- *
153 * Arguments: @sym_table *t@ = pointer to symbol table in question
154 * @const char *n@ = pointer to symbol table to look up
155 * @long l@ = length of the name string or negative to measure
156 * @size_t sz@ = size of desired symbol object, or zero
157 * @unsigned *f@ = pointer to a flag, or null.
159 * Returns: The address of a @sym_base@ structure, or null if not found
162 * Use: Looks up a symbol in a given symbol table. The name is
163 * passed by the address of its first character. The length
164 * may be given, in which case the name may contain arbitrary
165 * binary data, or it may be given as a negative number, in
166 * which case the length of the name is calculated as
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.
176 * The value of @*f@ indicates whether a new symbol entry was
177 * created: a nonzero value indicates that an old value was
181 extern void *sym_find(sym_table */
*t*/
, const char */
*n*/
, long /*l*/,
182 size_t /*sz*/, unsigned */
*f*/
);
184 /* --- @sym_remove@ --- *
186 * Arguments: @sym_table *i@ = pointer to a symbol table object
187 * @void *b@ = pointer to symbol table entry
191 * Use: Removes the object from the symbol table. The space occupied
192 * by the object and its name is freed; anything else attached
193 * to the entry should already be gone by this point.
196 extern void sym_remove(sym_table */
*t*/
, void */
*b*/
);
198 /* --- @sym_mkiter@ --- *
200 * Arguments: @sym_iter *i@ = pointer to an iterator object
201 * @sym_table *t@ = pointer to a symbol table object
205 * Use: Creates a new symbol table iterator which may be used to
206 * iterate through a symbol table.
209 extern void sym_mkiter(sym_iter */
*i*/
, sym_table */
*t*/
);
211 /* --- @sym_next@ --- *
213 * Arguments: @sym_iter *i@ = pointer to iterator object
215 * Returns: Pointer to the next symbol found, or null when finished.
217 * Use: Returns the next symbol from the table. Symbols are not
218 * returned in any particular order.
221 extern void *sym_next(sym_iter */
*i*/
);
223 /*----- That's all, folks -------------------------------------------------*/