3 * $Id: sym.h,v 1.7 1999/06/01 09:49:33 mdw Exp $
5 * Symbol table management
7 * (c) 1998 Straylight/Edgeware
10 /*----- Licensing notice --------------------------------------------------*
12 * This file is part of the mLib utilities library.
14 * mLib is free software; you can redistribute it and/or modify
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.
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
22 * GNU Library General Public License for more details.
24 * You should have received a copy of the GNU Library General Public
25 * License along with mLib; if not, write to the Free
26 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
30 /*----- Revision history --------------------------------------------------*
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.
37 * Revision 1.6 1999/05/26 21:08:31 mdw
38 * Rename symbols in line with newer conventions.
40 * Revision 1.5 1999/05/13 22:48:37 mdw
41 * Change `-ise' to `-ize' throughout.
43 * Revision 1.4 1999/05/06 19:51:35 mdw
44 * Reformatted the LGPL notice a little bit.
46 * Revision 1.3 1999/05/05 18:50:31 mdw
47 * Change licensing conditions to LGPL.
49 * Revision 1.2 1998/11/26 19:27:34 mdw
50 * Move SYM_NAME into the header file. Fix bugs.
52 * Revision 1.1.1.1 1998/06/17 23:44:42 mdw
53 * Initial version of mLib
64 /*----- Required headers --------------------------------------------------*/
72 /*----- Type definitions --------------------------------------------------*/
74 /* --- Symbol table --- *
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.
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 */
87 /* --- A symbol table entry --- *
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.
96 #define SYM_BUFSZ 16 /* Size of local string buffer */
98 typedef struct sym_base
{
99 struct sym_base
*next
; /* Next symbol in hash bin */
100 uint32 hash
; /* Hash value for symbol's name */
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 */
108 /* --- A macro to pick a symbol's name out from the mess --- */
110 #define SYM_NAME(sy) \
111 (((sym_base *)(sy))->len > SYM_BUFSZ ? \
112 ((sym_base *)(sy))->name.p : \
113 ((sym_base *)(sy))->name.b)
115 /* --- An iterator block --- */
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 */
123 /*----- External functions ------------------------------------------------*/
125 /* --- @sym_create@ --- *
127 * Arguments: @sym_table *t@ = symbol table to initialize
131 * Use: Initializes the given symbol table. Raises @EXC_NOMEM@ if
132 * there isn't enough memory.
135 extern void sym_create(sym_table */
*t*/
);
137 /* --- @sym_destroy@ --- *
139 * Arguments: @sym_table *t@ = pointer to symbol table in question
143 * Use: Destroys a symbol table, freeing all the memory it used to
147 extern void sym_destroy(sym_table */
*t*/
);
149 /* --- @sym_find@ --- *
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.
157 * Returns: The address of a @sym_base@ structure, or null if not found
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
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.
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.
177 * The value of @*f@ indicates whether a new symbol entry was
178 * created: a nonzero value indicates that an old value was
182 extern void *sym_find(sym_table */
*t*/
, const char */
*n*/
, long /*l*/,
183 size_t /*sz*/, unsigned */
*f*/
);
185 /* --- @sym_remove@ --- *
187 * Arguments: @sym_table *i@ = pointer to a symbol table object
188 * @void *b@ = pointer to symbol table entry
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.
197 extern void sym_remove(sym_table */
*t*/
, void */
*b*/
);
199 /* --- @sym_mkiter@ --- *
201 * Arguments: @sym_iter *i@ = pointer to an iterator object
202 * @sym_table *t@ = pointer to a symbol table object
206 * Use: Creates a new symbol table iterator which may be used to
207 * iterate through a symbol table.
210 extern void sym_mkiter(sym_iter */
*i*/
, sym_table */
*t*/
);
212 /* --- @sym_next@ --- *
214 * Arguments: @sym_iter *i@ = pointer to iterator object
216 * Returns: Pointer to the next symbol found, or null when finished.
218 * Use: Returns the next symbol from the table. Symbols are not
219 * returned in any particular order.
222 extern void *sym_next(sym_iter */
*i*/
);
224 /*----- That's all, folks -------------------------------------------------*/