3 * $Id: sym.h,v 1.8 1999/08/02 14:45:48 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.8 1999/08/02 14:45:48 mdw
34 * Break low-level hashtable code out from sym.
36 * Revision 1.7 1999/06/01 09:49:33 mdw
37 * Allow things to be looked up by just their caller-supplied hashes. This
38 * actually needs to be thought through better.
40 * Revision 1.6 1999/05/26 21:08:31 mdw
41 * Rename symbols in line with newer conventions.
43 * Revision 1.5 1999/05/13 22:48:37 mdw
44 * Change `-ise' to `-ize' throughout.
46 * Revision 1.4 1999/05/06 19:51:35 mdw
47 * Reformatted the LGPL notice a little bit.
49 * Revision 1.3 1999/05/05 18:50:31 mdw
50 * Change licensing conditions to LGPL.
52 * Revision 1.2 1998/11/26 19:27:34 mdw
53 * Move SYM_NAME into the header file. Fix bugs.
55 * Revision 1.1.1.1 1998/06/17 23:44:42 mdw
56 * Initial version of mLib
67 /*----- Required headers --------------------------------------------------*/
79 /*----- Type definitions --------------------------------------------------*/
81 /* --- Symbol table --- *
83 * A @sym_table@ contains the information needed to manage a symbol table.
84 * Users shouldn't fiddle with this information directly, but it needs to be
85 * here so that objects of the correct type can be created.
88 typedef struct sym_table
{
93 /* --- A symbol table entry --- *
95 * I don't care what actually gets stored in symbol entries because I don't
96 * create them: that's the responsibility of my client. All I care about
97 * here is that whatever gets passed to me is a structure whose first member
98 * is a @sym_base@. The ANSI guarantees about structure layout are
99 * sufficient to allow me to manipulate such objects.
102 #define SYM_BUFSZ 16 /* Size of local string buffer */
104 typedef struct sym_base
{
107 char *p
; /* Pointer to name string */
108 char b
[SYM_BUFSZ
]; /* Buffer containing a short name */
109 } name
; /* Name of this symbol */
110 size_t len
; /* Length of the symbol's name */
113 /* --- A macro to pick a symbol's name out from the mess --- */
115 #define SYM_NAME(sy) \
116 (((sym_base *)(sy))->len > SYM_BUFSZ ? \
117 ((sym_base *)(sy))->name.p : \
118 ((sym_base *)(sy))->name.b)
120 /* --- An iterator block --- */
122 typedef hash_iter sym_iter
;
124 /*----- External functions ------------------------------------------------*/
126 /* --- @sym_create@ --- *
128 * Arguments: @sym_table *t@ = symbol table to initialize
132 * Use: Initializes the given symbol table. Raises @EXC_NOMEM@ if
133 * there isn't enough memory.
136 extern void sym_create(sym_table */
*t*/
);
138 /* --- @sym_destroy@ --- *
140 * Arguments: @sym_table *t@ = pointer to symbol table in question
144 * Use: Destroys a symbol table, freeing all the memory it used to
148 extern void sym_destroy(sym_table */
*t*/
);
150 /* --- @sym_find@ --- *
152 * Arguments: @sym_table *t@ = pointer to symbol table in question
153 * @const char *n@ = pointer to symbol table to look up
154 * @long l@ = length of the name string or negative to measure
155 * @size_t sz@ = size of desired symbol object, or zero
156 * @unsigned *f@ = pointer to a flag, or null.
158 * Returns: The address of a @sym_base@ structure, or null if not found
161 * Use: Looks up a symbol in a given symbol table. The name is
162 * passed by the address of its first character. The length
163 * may be given, in which case the name may contain arbitrary
164 * binary data, or it may be given as a negative number, in
165 * which case the length of the name is calculated as
168 * The return value is the address of a pointer to a @sym_base@
169 * block (which may have other things on the end, as above). If
170 * the symbol could be found, the return value points to the
171 * symbol block. If the symbol wasn't there, then if @sz@ is
172 * nonzero, a new symbol is created and its address is returned;
173 * otherwise a null pointer is returned. The exception
174 * @EXC_NOMEM@ is raised if the block can't be allocated.
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 #define SYM_MKITER(i, t) HASH_MKITER((i), &(t)->t)
211 extern void sym_mkiter(sym_iter */
*i*/
, sym_table */
*t*/
);
213 /* --- @sym_next@ --- *
215 * Arguments: @sym_iter *i@ = pointer to iterator object
217 * Returns: Pointer to the next symbol found, or null when finished.
219 * Use: Returns the next symbol from the table. Symbols are not
220 * returned in any particular order.
223 #define SYM_NEXT(i, p) do { \
225 HASH_NEXT((i), _q); \
229 extern void *sym_next(sym_iter */
*i*/
);
231 /*----- That's all, folks -------------------------------------------------*/