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