chiark / gitweb /
mdup: New unit for juggling file descriptors.
[mLib] / sym.h
... / ...
CommitLineData
1/* -*-c-*-
2 *
3 * $Id: sym.h,v 1.14 2004/04/08 01:36:13 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 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 *
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.
23 *
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,
27 * MA 02111-1307, USA.
28 */
29
30#ifndef MLIB_SYM_H
31#define MLIB_SYM_H
32
33#ifdef __cplusplus
34 extern "C" {
35#endif
36
37/*----- Required headers --------------------------------------------------*/
38
39#include <stddef.h>
40
41#ifndef MLIB_BITS_H
42# include "bits.h"
43#endif
44
45#ifndef MLIB_HASH_H
46# include "hash.h"
47#endif
48
49#ifndef MLIB_SUB_H
50# include "sub.h"
51#endif
52
53/*----- Tuning parameters -------------------------------------------------*/
54
55/* --- Initial hash table size --- *
56 *
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.
59 */
60
61#define SYM_INITSZ 32 /* Size of a new hash table */
62
63/* --- Maximum load factor --- *
64 *
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
69 * doubled in size.
70 */
71
72#define SYM_LIMIT(n) ((n) * 2) /* Load factor for growing table */
73
74/*----- Type definitions --------------------------------------------------*/
75
76/* --- Symbol table --- *
77 *
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.
81 */
82
83typedef struct sym_table {
84 hash_table t;
85 subarena *s;
86 size_t load;
87} sym_table;
88
89/* --- A symbol table entry --- *
90 *
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.
96 */
97
98typedef 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 */
102} sym_base;
103
104/* --- Macros for picking out useful information --- *
105 *
106 * Note that @SYM_LEN@ returns the size of the symbol key. For textual keys,
107 * this will include the terminating null.
108 */
109
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)
113
114/* --- An iterator block --- */
115
116typedef struct { hash_iter i; } sym_iter;
117
118/*----- External functions ------------------------------------------------*/
119
120/* --- @sym_create@ --- *
121 *
122 * Arguments: @sym_table *t@ = symbol table to initialize
123 *
124 * Returns: ---
125 *
126 * Use: Initializes the given symbol table. Raises @EXC_NOMEM@ if
127 * there isn't enough memory.
128 */
129
130extern void sym_create(sym_table */*t*/);
131
132/* --- @sym_destroy@ --- *
133 *
134 * Arguments: @sym_table *t@ = pointer to symbol table in question
135 *
136 * Returns: ---
137 *
138 * Use: Destroys a symbol table, freeing all the memory it used to
139 * occupy.
140 */
141
142extern void sym_destroy(sym_table */*t*/);
143
144/* --- @sym_find@ --- *
145 *
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.
151 *
152 * Returns: The address of a @sym_base@ structure, or null if not found
153 * and @sz@ is zero.
154 *
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
160 * @strlen(n) + 1@.
161 *
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.
169 *
170 * The value of @*f@ indicates whether a new symbol entry was
171 * created: a nonzero value indicates that an old value was
172 * found.
173 */
174
175extern void *sym_find(sym_table */*t*/, const char */*n*/, long /*l*/,
176 size_t /*sz*/, unsigned */*f*/);
177
178/* --- @sym_remove@ --- *
179 *
180 * Arguments: @sym_table *t@ = pointer to a symbol table object
181 * @void *b@ = pointer to symbol table entry
182 *
183 * Returns: ---
184 *
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.
188 */
189
190extern void sym_remove(sym_table */*t*/, void */*b*/);
191
192/* --- @sym_mkiter@ --- *
193 *
194 * Arguments: @sym_iter *i@ = pointer to an iterator object
195 * @sym_table *t@ = pointer to a symbol table object
196 *
197 * Returns: ---
198 *
199 * Use: Creates a new symbol table iterator which may be used to
200 * iterate through a symbol table.
201 */
202
203#define SYM_MKITER(i_, t_) HASH_MKITER(&(i_)->i, &(t_)->t)
204
205extern void sym_mkiter(sym_iter */*i*/, sym_table */*t*/);
206
207/* --- @sym_next@ --- *
208 *
209 * Arguments: @sym_iter *i@ = pointer to iterator object
210 *
211 * Returns: Pointer to the next symbol found, or null when finished.
212 *
213 * Use: Returns the next symbol from the table. Symbols are not
214 * returned in any particular order.
215 */
216
217#define SYM_NEXT(i_, p) do { \
218 hash_base *_q; \
219 HASH_NEXT(&(i_)->i, _q); \
220 (p) = (void *)_q; \
221} while (0)
222
223extern void *sym_next(sym_iter */*i*/);
224
225/*----- That's all, folks -------------------------------------------------*/
226
227#ifdef __cplusplus
228 }
229#endif
230
231#endif