chiark / gitweb /
Use mdwopt from common files distrib.
[mLib] / sym.h
... / ...
CommitLineData
1/* -*-c-*-
2 *
3 * $Id: sym.h,v 1.2 1998/11/26 19:27:34 mdw Exp $
4 *
5 * Symbol table management
6 *
7 * (c) 1998 Straylight/Edgeware
8 */
9
10/*----- Licensing notice --------------------------------------------------*
11 *
12 * This file is part of the mLib utilities library.
13 *
14 * mLib is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation; either version 2 of the License, or
17 * (at your option) any later version.
18 *
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 General Public License for more details.
23 *
24 * You should have received a copy of the GNU General Public License
25 * along with mLib; if not, write to the Free Software Foundation,
26 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
27 */
28
29/*----- Revision history --------------------------------------------------*
30 *
31 * $Log: sym.h,v $
32 * Revision 1.2 1998/11/26 19:27:34 mdw
33 * Move SYM_NAME into the header file. Fix bugs.
34 *
35 * Revision 1.1.1.1 1998/06/17 23:44:42 mdw
36 * Initial version of mLib
37 *
38 */
39
40#ifndef SYM_H
41#define SYM_H
42
43#ifdef __cplusplus
44 extern "C" {
45#endif
46
47/*----- Required headers --------------------------------------------------*/
48
49#include <stddef.h>
50
51/*----- Type definitions --------------------------------------------------*/
52
53/* --- Symbol table --- *
54 *
55 * A @sym_table@ contains the information needed to manage a symbol table.
56 * Users shouldn't fiddle with this information directly, but it needs to be
57 * here so that objects of the correct type can be created.
58 */
59
60typedef struct sym_table {
61 unsigned long mask; /* Bit mask for hashing purposes */
62 size_t c; /* Down counter for growing table */
63 struct sym_base **a; /* Array of hash bins */
64} sym_table;
65
66/* --- A symbol table entry --- *
67 *
68 * I don't care what actually gets stored in symbol entries because I don't
69 * create them: that's the responsibility of my client. All I care about
70 * here is that whatever gets passed to me is a structure whose first member
71 * is a @sym_base@. The ANSI guarantees about structure layout are
72 * sufficient to allow me to manipulate such objects.
73 */
74
75#define SYM_BUFSZ 16 /* Size of local string buffer */
76
77typedef struct sym_base {
78 struct sym_base *next; /* Next symbol in hash bin */
79 unsigned long hash; /* Hash value for symbol's name */
80 union {
81 char *p; /* Pointer to name string */
82 char b[SYM_BUFSZ]; /* Buffer containing a short name */
83 } name; /* Name of this symbol */
84 size_t len; /* Length of the symbol's name */
85} sym_base;
86
87/* --- A macro to pick a symbol's name out from the mess --- */
88
89#define SYM_NAME(sy) \
90 (((sym_base *)(sy))->len > SYM_BUFSZ ? \
91 ((sym_base *)(sy))->name.p : \
92 ((sym_base *)(sy))->name.b)
93
94/* --- An iterator block --- */
95
96typedef struct sym_iter {
97 sym_table *t; /* Symbol table being iterated */
98 sym_base *n; /* Address of next item to return */
99 size_t i; /* Index of next hash bin to use */
100} sym_iter;
101
102/*----- External functions ------------------------------------------------*/
103
104/* --- @sym_createTable@ --- *
105 *
106 * Arguments: @sym_table *t@ = symbol table to initialise
107 *
108 * Returns: ---
109 *
110 * Use: Initialises the given symbol table. Raises @EXC_NOMEM@ if
111 * there isn't enough memory.
112 */
113
114extern void sym_createTable(sym_table */*t*/);
115
116/* --- @sym_destroyTable@ --- *
117 *
118 * Arguments: @sym_table *t@ = pointer to symbol table in question
119 *
120 * Returns: ---
121 *
122 * Use: Destroys a symbol table, freeing all the memory it used to
123 * occupy.
124 */
125
126extern void sym_destroyTable(sym_table */*t*/);
127
128/* --- @sym_find@ --- *
129 *
130 * Arguments: @sym_table *t@ = pointer to symbol table in question
131 * @const char *n@ = pointer to symbol table to look up
132 * @long l@ = length of the name string or negative to measure
133 * @size_t sz@ = size of desired symbol object, or zero
134 * @unsigned *f@ = pointer to a flag, or null.
135 *
136 * Returns: The address of a @sym_base@ structure, or null if not found
137 * and @sz@ is zero.
138 *
139 * Use: Looks up a symbol in a given symbol table. The name is
140 * passed by the address of its first character. The length
141 * may be given, in which case the name may contain arbitrary
142 * binary data, or it may be given as a negative number, in
143 * which case the length of the name is calculated as
144 * @strlen(n)@.
145 *
146 * The return value is the address of a pointer to a @sym_base@
147 * block (which may have other things on the end, as above). If
148 * the symbol could be found, the return value points to the
149 * symbol block. If the symbol wasn't there, then if @sz@ is
150 * nonzero, a new symbol is created and its address is returned;
151 * otherwise a null pointer is returned. The exception
152 * @EXC_NOMEM@ is raised if the block can't be allocated.
153 *
154 * The value of @*f@ indicates whether a new symbol entry was
155 * created: a nonzero value indicates that an old value was
156 * found.
157 */
158
159extern void *sym_find(sym_table */*t*/, const char */*n*/, long /*l*/,
160 size_t /*sz*/, unsigned */*f*/);
161
162/* --- @sym_remove@ --- *
163 *
164 * Arguments: @sym_table *i@ = pointer to a symbol table object
165 * @void *b@ = pointer to symbol table entry
166 *
167 * Returns: ---
168 *
169 * Use: Removes the object from the symbol table. The space occupied
170 * by the object and its name is freed; anything else attached
171 * to the entry should already be gone by this point.
172 */
173
174extern void sym_remove(sym_table */*t*/, void */*b*/);
175
176/* --- @sym_createIter@ --- *
177 *
178 * Arguments: @sym_iter *i@ = pointer to an iterator object
179 * @sym_table *t@ = pointer to a symbol table object
180 *
181 * Returns: ---
182 *
183 * Use: Creates a new symbol table iterator which may be used to
184 * iterate through a symbol table.
185 */
186
187extern void sym_createIter(sym_iter */*i*/, sym_table */*t*/);
188
189/* --- @sym_next@ --- *
190 *
191 * Arguments: @sym_iter *i@ = pointer to iterator object
192 *
193 * Returns: Pointer to the next symbol found, or null when finished.
194 *
195 * Use: Returns the next symbol from the table. Symbols are not
196 * returned in any particular order.
197 */
198
199extern void *sym_next(sym_iter */*i*/);
200
201/*----- That's all, folks -------------------------------------------------*/
202
203#ifdef __cplusplus
204 }
205#endif
206
207#endif