chiark / gitweb /
Use new @SYM_LEN@ macro for name lengths.
[mLib] / sym.h
CommitLineData
0875b58f 1/* -*-c-*-
2 *
20eb516f 3 * $Id: sym.h,v 1.11 2000/06/17 10:37:39 mdw Exp $
0875b58f 4 *
5 * Symbol table management
6 *
7 * (c) 1998 Straylight/Edgeware
8 */
9
c846879c 10/*----- Licensing notice --------------------------------------------------*
0875b58f 11 *
12 * This file is part of the mLib utilities library.
13 *
14 * mLib is free software; you can redistribute it and/or modify
c846879c 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.
18 *
0875b58f 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
c846879c 22 * GNU Library General Public License for more details.
23 *
24 * You should have received a copy of the GNU Library General Public
0bd98442 25 * License along with mLib; if not, write to the Free
26 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
27 * MA 02111-1307, USA.
0875b58f 28 */
29
30/*----- Revision history --------------------------------------------------*
31 *
32 * $Log: sym.h,v $
20eb516f 33 * Revision 1.11 2000/06/17 10:37:39 mdw
34 * Add support for arena management.
35 *
c6e0eaf0 36 * Revision 1.10 1999/12/10 23:42:04 mdw
37 * Change header file guard names.
38 *
dc2feaa9 39 * Revision 1.9 1999/08/02 16:53:48 mdw
40 * Improve type safety for sym_iter objects.
41 *
03d53b73 42 * Revision 1.8 1999/08/02 14:45:48 mdw
43 * Break low-level hashtable code out from sym.
44 *
d4d7a053 45 * Revision 1.7 1999/06/01 09:49:33 mdw
46 * Allow things to be looked up by just their caller-supplied hashes. This
47 * actually needs to be thought through better.
48 *
34b97201 49 * Revision 1.6 1999/05/26 21:08:31 mdw
50 * Rename symbols in line with newer conventions.
51 *
8c6d948b 52 * Revision 1.5 1999/05/13 22:48:37 mdw
53 * Change `-ise' to `-ize' throughout.
54 *
0bd98442 55 * Revision 1.4 1999/05/06 19:51:35 mdw
56 * Reformatted the LGPL notice a little bit.
57 *
c846879c 58 * Revision 1.3 1999/05/05 18:50:31 mdw
59 * Change licensing conditions to LGPL.
60 *
a5d14ede 61 * Revision 1.2 1998/11/26 19:27:34 mdw
62 * Move SYM_NAME into the header file. Fix bugs.
63 *
64 * Revision 1.1.1.1 1998/06/17 23:44:42 mdw
65 * Initial version of mLib
0875b58f 66 *
67 */
68
c6e0eaf0 69#ifndef MLIB_SYM_H
70#define MLIB_SYM_H
0875b58f 71
72#ifdef __cplusplus
73 extern "C" {
74#endif
75
76/*----- Required headers --------------------------------------------------*/
77
78#include <stddef.h>
79
c6e0eaf0 80#ifndef MLIB_BITS_H
d4d7a053 81# include "bits.h"
82#endif
83
c6e0eaf0 84#ifndef MLIB_HASH_H
03d53b73 85# include "hash.h"
86#endif
87
20eb516f 88#ifndef MLIB_SUB_H
89# include "sub.h"
90#endif
91
0875b58f 92/*----- Type definitions --------------------------------------------------*/
93
94/* --- Symbol table --- *
95 *
96 * A @sym_table@ contains the information needed to manage a symbol table.
97 * Users shouldn't fiddle with this information directly, but it needs to be
98 * here so that objects of the correct type can be created.
99 */
100
101typedef struct sym_table {
03d53b73 102 hash_table t;
20eb516f 103 subarena *s;
03d53b73 104 size_t load;
0875b58f 105} sym_table;
106
107/* --- A symbol table entry --- *
108 *
109 * I don't care what actually gets stored in symbol entries because I don't
110 * create them: that's the responsibility of my client. All I care about
111 * here is that whatever gets passed to me is a structure whose first member
112 * is a @sym_base@. The ANSI guarantees about structure layout are
113 * sufficient to allow me to manipulate such objects.
114 */
115
116#define SYM_BUFSZ 16 /* Size of local string buffer */
117
118typedef struct sym_base {
03d53b73 119 hash_base b;
0875b58f 120 union {
121 char *p; /* Pointer to name string */
122 char b[SYM_BUFSZ]; /* Buffer containing a short name */
123 } name; /* Name of this symbol */
124 size_t len; /* Length of the symbol's name */
125} sym_base;
126
a5d14ede 127/* --- A macro to pick a symbol's name out from the mess --- */
128
129#define SYM_NAME(sy) \
130 (((sym_base *)(sy))->len > SYM_BUFSZ ? \
131 ((sym_base *)(sy))->name.p : \
132 ((sym_base *)(sy))->name.b)
133
0875b58f 134/* --- An iterator block --- */
135
dc2feaa9 136typedef struct { hash_iter i; } sym_iter;
0875b58f 137
138/*----- External functions ------------------------------------------------*/
139
34b97201 140/* --- @sym_create@ --- *
0875b58f 141 *
8c6d948b 142 * Arguments: @sym_table *t@ = symbol table to initialize
0875b58f 143 *
144 * Returns: ---
145 *
8c6d948b 146 * Use: Initializes the given symbol table. Raises @EXC_NOMEM@ if
0875b58f 147 * there isn't enough memory.
148 */
149
34b97201 150extern void sym_create(sym_table */*t*/);
0875b58f 151
34b97201 152/* --- @sym_destroy@ --- *
0875b58f 153 *
154 * Arguments: @sym_table *t@ = pointer to symbol table in question
155 *
156 * Returns: ---
157 *
158 * Use: Destroys a symbol table, freeing all the memory it used to
159 * occupy.
160 */
161
34b97201 162extern void sym_destroy(sym_table */*t*/);
0875b58f 163
164/* --- @sym_find@ --- *
165 *
166 * Arguments: @sym_table *t@ = pointer to symbol table in question
167 * @const char *n@ = pointer to symbol table to look up
168 * @long l@ = length of the name string or negative to measure
169 * @size_t sz@ = size of desired symbol object, or zero
170 * @unsigned *f@ = pointer to a flag, or null.
171 *
172 * Returns: The address of a @sym_base@ structure, or null if not found
173 * and @sz@ is zero.
174 *
175 * Use: Looks up a symbol in a given symbol table. The name is
176 * passed by the address of its first character. The length
177 * may be given, in which case the name may contain arbitrary
178 * binary data, or it may be given as a negative number, in
179 * which case the length of the name is calculated as
03d53b73 180 * @strlen(n) + 1@.
0875b58f 181 *
182 * The return value is the address of a pointer to a @sym_base@
183 * block (which may have other things on the end, as above). If
184 * the symbol could be found, the return value points to the
185 * symbol block. If the symbol wasn't there, then if @sz@ is
186 * nonzero, a new symbol is created and its address is returned;
187 * otherwise a null pointer is returned. The exception
188 * @EXC_NOMEM@ is raised if the block can't be allocated.
189 *
190 * The value of @*f@ indicates whether a new symbol entry was
191 * created: a nonzero value indicates that an old value was
192 * found.
193 */
194
195extern void *sym_find(sym_table */*t*/, const char */*n*/, long /*l*/,
196 size_t /*sz*/, unsigned */*f*/);
197
198/* --- @sym_remove@ --- *
199 *
c6e0eaf0 200 * Arguments: @sym_table *t@ = pointer to a symbol table object
0875b58f 201 * @void *b@ = pointer to symbol table entry
202 *
203 * Returns: ---
204 *
205 * Use: Removes the object from the symbol table. The space occupied
206 * by the object and its name is freed; anything else attached
207 * to the entry should already be gone by this point.
208 */
209
210extern void sym_remove(sym_table */*t*/, void */*b*/);
211
34b97201 212/* --- @sym_mkiter@ --- *
0875b58f 213 *
214 * Arguments: @sym_iter *i@ = pointer to an iterator object
215 * @sym_table *t@ = pointer to a symbol table object
216 *
217 * Returns: ---
218 *
219 * Use: Creates a new symbol table iterator which may be used to
220 * iterate through a symbol table.
221 */
222
dc2feaa9 223#define SYM_MKITER(i_, t_) HASH_MKITER(&(i_)->i, &(t_)->t)
03d53b73 224
34b97201 225extern void sym_mkiter(sym_iter */*i*/, sym_table */*t*/);
0875b58f 226
227/* --- @sym_next@ --- *
228 *
229 * Arguments: @sym_iter *i@ = pointer to iterator object
230 *
231 * Returns: Pointer to the next symbol found, or null when finished.
232 *
233 * Use: Returns the next symbol from the table. Symbols are not
234 * returned in any particular order.
235 */
236
dc2feaa9 237#define SYM_NEXT(i_, p) do { \
03d53b73 238 hash_base *_q; \
dc2feaa9 239 HASH_NEXT(&(i_)->i, _q); \
03d53b73 240 (p) = (void *)_q; \
241} while (0)
242
0875b58f 243extern void *sym_next(sym_iter */*i*/);
244
245/*----- That's all, folks -------------------------------------------------*/
246
247#ifdef __cplusplus
248 }
249#endif
250
251#endif