3 * $Id: sym.h,v 1.3 1999/05/05 18:50:31 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 Software
26 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
29 /*----- Revision history --------------------------------------------------*
32 * Revision 1.3 1999/05/05 18:50:31 mdw
33 * Change licensing conditions to LGPL.
35 * Revision 1.2 1998/11/26 19:27:34 mdw
36 * Move SYM_NAME into the header file. Fix bugs.
38 * Revision 1.1.1.1 1998/06/17 23:44:42 mdw
39 * Initial version of mLib
50 /*----- Required headers --------------------------------------------------*/
54 /*----- Type definitions --------------------------------------------------*/
56 /* --- Symbol table --- *
58 * A @sym_table@ contains the information needed to manage a symbol table.
59 * Users shouldn't fiddle with this information directly, but it needs to be
60 * here so that objects of the correct type can be created.
63 typedef struct sym_table {
64 unsigned long mask; /* Bit mask for hashing purposes */
65 size_t c; /* Down counter for growing table */
66 struct sym_base **a; /* Array of hash bins */
69 /* --- A symbol table entry --- *
71 * I don't care what actually gets stored in symbol entries because I don't
72 * create them: that's the responsibility of my client. All I care about
73 * here is that whatever gets passed to me is a structure whose first member
74 * is a @sym_base@. The ANSI guarantees about structure layout are
75 * sufficient to allow me to manipulate such objects.
78 #define SYM_BUFSZ 16 /* Size of local string buffer */
80 typedef struct sym_base {
81 struct sym_base *next; /* Next symbol in hash bin */
82 unsigned long hash; /* Hash value for symbol's name */
84 char *p; /* Pointer to name string */
85 char b[SYM_BUFSZ]; /* Buffer containing a short name */
86 } name; /* Name of this symbol */
87 size_t len; /* Length of the symbol's name */
90 /* --- A macro to pick a symbol's name out from the mess --- */
92 #define SYM_NAME(sy) \
93 (((sym_base *)(sy))->len > SYM_BUFSZ ? \
94 ((sym_base *)(sy))->name.p : \
95 ((sym_base *)(sy))->name.b)
97 /* --- An iterator block --- */
99 typedef struct sym_iter {
100 sym_table *t; /* Symbol table being iterated */
101 sym_base *n; /* Address of next item to return */
102 size_t i; /* Index of next hash bin to use */
105 /*----- External functions ------------------------------------------------*/
107 /* --- @sym_createTable@ --- *
109 * Arguments: @sym_table *t@ = symbol table to initialise
113 * Use: Initialises the given symbol table. Raises @EXC_NOMEM@ if
114 * there isn't enough memory.
117 extern void sym_createTable(sym_table */*t*/);
119 /* --- @sym_destroyTable@ --- *
121 * Arguments: @sym_table *t@ = pointer to symbol table in question
125 * Use: Destroys a symbol table, freeing all the memory it used to
129 extern void sym_destroyTable(sym_table */*t*/);
131 /* --- @sym_find@ --- *
133 * Arguments: @sym_table *t@ = pointer to symbol table in question
134 * @const char *n@ = pointer to symbol table to look up
135 * @long l@ = length of the name string or negative to measure
136 * @size_t sz@ = size of desired symbol object, or zero
137 * @unsigned *f@ = pointer to a flag, or null.
139 * Returns: The address of a @sym_base@ structure, or null if not found
142 * Use: Looks up a symbol in a given symbol table. The name is
143 * passed by the address of its first character. The length
144 * may be given, in which case the name may contain arbitrary
145 * binary data, or it may be given as a negative number, in
146 * which case the length of the name is calculated as
149 * The return value is the address of a pointer to a @sym_base@
150 * block (which may have other things on the end, as above). If
151 * the symbol could be found, the return value points to the
152 * symbol block. If the symbol wasn't there, then if @sz@ is
153 * nonzero, a new symbol is created and its address is returned;
154 * otherwise a null pointer is returned. The exception
155 * @EXC_NOMEM@ is raised if the block can't be allocated.
157 * The value of @*f@ indicates whether a new symbol entry was
158 * created: a nonzero value indicates that an old value was
162 extern void *sym_find(sym_table */*t*/, const char */*n*/, long /*l*/,
163 size_t /*sz*/, unsigned */*f*/);
165 /* --- @sym_remove@ --- *
167 * Arguments: @sym_table *i@ = pointer to a symbol table object
168 * @void *b@ = pointer to symbol table entry
172 * Use: Removes the object from the symbol table. The space occupied
173 * by the object and its name is freed; anything else attached
174 * to the entry should already be gone by this point.
177 extern void sym_remove(sym_table */*t*/, void */*b*/);
179 /* --- @sym_createIter@ --- *
181 * Arguments: @sym_iter *i@ = pointer to an iterator object
182 * @sym_table *t@ = pointer to a symbol table object
186 * Use: Creates a new symbol table iterator which may be used to
187 * iterate through a symbol table.
190 extern void sym_createIter(sym_iter */*i*/, sym_table */*t*/);
192 /* --- @sym_next@ --- *
194 * Arguments: @sym_iter *i@ = pointer to iterator object
196 * Returns: Pointer to the next symbol found, or null when finished.
198 * Use: Returns the next symbol from the table. Symbols are not
199 * returned in any particular order.
202 extern void *sym_next(sym_iter */*i*/);
204 /*----- That's all, folks -------------------------------------------------*/