3 * $Id: sym.h,v 1.14 2004/04/08 01:36:13 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,
37 /*----- Required headers --------------------------------------------------*/
53 /*----- Tuning parameters -------------------------------------------------*/
55 /* --- Initial hash table size --- *
57 * This is the initial @mask@ value. It must be of the form %$2^n - 1$%,
58 * so that it can be used to mask of the bottom bits of a hash value.
61 #define SYM_INITSZ 32 /* Size of a new hash table */
63 /* --- Maximum load factor --- *
65 * This parameter controls how much the table has to be loaded before the
66 * table is extended. The number of elements %$n$%, the number of bins %$b$%
67 * and the limit %$l$% satisfy the relation %$n < bl$%; if a new item is
68 * added to the table and this relation is found to be false, the table is
72 #define SYM_LIMIT(n) ((n) * 2) /* Load factor for growing table */
74 /*----- Type definitions --------------------------------------------------*/
76 /* --- Symbol table --- *
78 * A @sym_table@ contains the information needed to manage a symbol table.
79 * Users shouldn't fiddle with this information directly, but it needs to be
80 * here so that objects of the correct type can be created.
83 typedef struct sym_table
{
89 /* --- A symbol table entry --- *
91 * I don't care what actually gets stored in symbol entries because I don't
92 * create them: that's the responsibility of my client. All I care about
93 * here is that whatever gets passed to me is a structure whose first member
94 * is a @sym_base@. The ANSI guarantees about structure layout are
95 * sufficient to allow me to manipulate such objects.
98 typedef struct sym_base
{
99 hash_base b
; /* Base structure */
100 char *name
; /* Pointer to name string */
101 size_t len
; /* Length of the symbol's name */
104 /* --- Macros for picking out useful information --- *
106 * Note that @SYM_LEN@ returns the size of the symbol key. For textual keys,
107 * this will include the terminating null.
110 #define SYM_NAME(sy) ((const char *)(((sym_base *)(sy))->name))
111 #define SYM_LEN(sy) (((sym_base *)(sy))->len + 0)
112 #define SYM_HASH(sy) (((sym_base *)(sy))->b.hash + 0)
114 /* --- An iterator block --- */
116 typedef struct { hash_iter i
; } sym_iter
;
118 /*----- External functions ------------------------------------------------*/
120 /* --- @sym_create@ --- *
122 * Arguments: @sym_table *t@ = symbol table to initialize
126 * Use: Initializes the given symbol table. Raises @EXC_NOMEM@ if
127 * there isn't enough memory.
130 extern void sym_create(sym_table */
*t*/
);
132 /* --- @sym_destroy@ --- *
134 * Arguments: @sym_table *t@ = pointer to symbol table in question
138 * Use: Destroys a symbol table, freeing all the memory it used to
142 extern void sym_destroy(sym_table */
*t*/
);
144 /* --- @sym_find@ --- *
146 * Arguments: @sym_table *t@ = pointer to symbol table in question
147 * @const char *n@ = pointer to symbol name to look up
148 * @long l@ = length of the name string or negative to measure
149 * @size_t sz@ = size of desired symbol object, or zero
150 * @unsigned *f@ = pointer to a flag, or null.
152 * Returns: The address of a @sym_base@ structure, or null if not found
155 * Use: Looks up a symbol in a given symbol table. The name is
156 * passed by the address of its first character. The length
157 * may be given, in which case the name may contain arbitrary
158 * binary data, or it may be given as a negative number, in
159 * which case the length of the name is calculated as
162 * The return value is the address of a pointer to a @sym_base@
163 * block (which may have other things on the end, as above). If
164 * the symbol could be found, the return value points to the
165 * symbol block. If the symbol wasn't there, then if @sz@ is
166 * nonzero, a new symbol is created and its address is returned;
167 * otherwise a null pointer is returned. The exception
168 * @EXC_NOMEM@ is raised if the block can't be allocated.
170 * The value of @*f@ indicates whether a new symbol entry was
171 * created: a nonzero value indicates that an old value was
175 extern void *sym_find(sym_table */
*t*/
, const char */
*n*/
, long /*l*/,
176 size_t /*sz*/, unsigned */
*f*/
);
178 /* --- @sym_remove@ --- *
180 * Arguments: @sym_table *t@ = pointer to a symbol table object
181 * @void *b@ = pointer to symbol table entry
185 * Use: Removes the object from the symbol table. The space occupied
186 * by the object and its name is freed; anything else attached
187 * to the entry should already be gone by this point.
190 extern void sym_remove(sym_table */
*t*/
, void */
*b*/
);
192 /* --- @sym_mkiter@ --- *
194 * Arguments: @sym_iter *i@ = pointer to an iterator object
195 * @sym_table *t@ = pointer to a symbol table object
199 * Use: Creates a new symbol table iterator which may be used to
200 * iterate through a symbol table.
203 #define SYM_MKITER(i_, t_) HASH_MKITER(&(i_)->i, &(t_)->t)
205 extern void sym_mkiter(sym_iter */
*i*/
, sym_table */
*t*/
);
207 /* --- @sym_next@ --- *
209 * Arguments: @sym_iter *i@ = pointer to iterator object
211 * Returns: Pointer to the next symbol found, or null when finished.
213 * Use: Returns the next symbol from the table. Symbols are not
214 * returned in any particular order.
217 #define SYM_NEXT(i_, p) do { \
219 HASH_NEXT(&(i_)->i, _q); \
223 extern void *sym_next(sym_iter */
*i*/
);
225 /*----- That's all, folks -------------------------------------------------*/