chiark / gitweb /
testrig: Provide useful interface for more complicated test rigs.
[mLib] / sym.c
1 /* -*-c-*-
2  *
3  * $Id: sym.c,v 1.15 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 /*----- Header files ------------------------------------------------------*/
31
32 /* --- ANSI headers --- */
33
34 #include <stdio.h>
35 #include <stdlib.h>
36 #include <string.h>
37
38 /* --- Local headers --- */
39
40 #include "alloc.h"
41 #include "arena.h"
42 #include "bits.h"
43 #include "exc.h"
44 #include "hash.h"
45 #include "sub.h"
46 #include "sym.h"
47 #include "unihash.h"
48
49 /*----- Main code ---------------------------------------------------------*/
50
51 /* --- @sym_create@ --- *
52  *
53  * Arguments:   @sym_table *t@ = symbol table to initialize
54  *
55  * Returns:     ---
56  *
57  * Use:         Initializes the given symbol table.  Raises @EXC_NOMEM@ if
58  *              there isn't enough memory.
59  */
60
61 void sym_create(sym_table *t)
62 {
63   hash_create(&t->t, SYM_INITSZ);
64   t->s = &sub_global;
65   t->load = SYM_LIMIT(SYM_INITSZ);
66 }
67
68 /* --- @sym_destroy@ --- *
69  *
70  * Arguments:   @sym_table *t@ = pointer to symbol table in question
71  *
72  * Returns:     ---
73  *
74  * Use:         Destroys a symbol table, freeing all the memory it used to
75  *              occupy.
76  */
77
78 void sym_destroy(sym_table *t)
79 {
80   sym_iter i;
81
82   SYM_MKITER(&i, t);
83   for (;;) {
84     sym_base *p;
85     SYM_NEXT(&i, p);
86     if (!p)
87       break;
88     x_free(t->t.a, p);
89   }
90   hash_destroy(&t->t);
91 }
92
93 /* --- @sym_find@ --- *
94  *
95  * Arguments:   @sym_table *t@ = pointer to symbol table in question
96  *              @const char *n@ = pointer to symbol name to look up
97  *              @long l@ = length of the name string or negative to measure
98  *              @size_t sz@ = size of desired symbol object, or zero
99  *              @unsigned *f@ = pointer to a flag, or null.
100  *
101  * Returns:     The address of a @sym_base@ structure, or null if not found
102  *              and @sz@ is zero.
103  *
104  * Use:         Looks up a symbol in a given symbol table.  The name is
105  *              passed by the address of its first character.  The length
106  *              may be given, in which case the name may contain arbitrary
107  *              binary data, or it may be given as a negative number, in
108  *              which case the length of the name is calculated as
109  *              @strlen(n) + 1@.
110  *
111  *              The return value is the address of a pointer to a @sym_base@
112  *              block (which may have other things on the end, as above).  If
113  *              the symbol could be found, the return value points to the
114  *              symbol block.  If the symbol wasn't there, then if @sz@ is
115  *              nonzero, a new symbol is created and its address is returned;
116  *              otherwise a null pointer is returned.  The exception
117  *              @EXC_NOMEM@ is raised if the block can't be allocated.
118  *
119  *              The value of @*f@ indicates whether a new symbol entry was
120  *              created: a nonzero value indicates that an old value was
121  *              found.
122  */
123
124 void *sym_find(sym_table *t, const char *n, long l, size_t sz, unsigned *f)
125 {
126   uint32 hash;
127   size_t len = 0;
128   hash_base **bin, **p;
129   sym_base *q;
130
131   /* --- Find the correct bin --- */
132
133   len = l < 0 ? strlen(n) : l;
134   hash = UNIHASH(&unihash_global, n, len);
135   bin = HASH_BIN(&t->t, hash);
136
137   /* --- Search the bin list --- */
138
139   for (p = bin; *p; p = &(*p)->next) {
140     q = (sym_base *)*p;
141     if (hash == q->b.hash && len == q->len && !memcmp(n, SYM_NAME(q), len)) {
142
143       /* --- Found a match --- *
144        *
145        * As a minor, and probably pointless, tweak, move the item to the
146        * front of its bin list.
147        */
148
149       (*p) = q->b.next;
150       q->b.next = *bin;
151       *bin = &q->b;
152
153       /* --- Return the block --- */
154
155       if (f) *f = 1;
156       return (q);
157     }
158   }
159
160   /* --- Couldn't find the item there --- */
161
162   if (f) *f = 0;
163   if (!sz) return (0);
164
165   /* --- Create a new symbol block and initialize it --- *
166    *
167    * The name is attached to the end of the symbol block.
168    */
169
170   q = x_alloc(t->t.a, sz + len + 1);
171   q->b.next = *bin;
172   q->b.hash = hash;
173   q->name = (char *)q + sz;
174   memcpy(q->name, n, len);
175   q->name[len] = 0;
176   q->len = len;
177   *bin = &q->b;
178
179   /* --- Consider growing the array --- */
180
181   if (t->load)
182     t->load--;
183   if (!t->load && hash_extend(&t->t))
184     t->load = SYM_LIMIT(t->t.mask + 1);
185
186   /* --- Finished that, so return the new symbol block --- */
187
188   return (q);
189 }
190
191 /* --- @sym_remove@ --- *
192  *
193  * Arguments:   @sym_table *t@ = pointer to a symbol table object
194  *              @void *p@ = pointer to symbol table entry
195  *
196  * Returns:     ---
197  *
198  * Use:         Removes the object from the symbol table.  The space occupied
199  *              by the object and its name is freed; anything else attached
200  *              to the entry should already be gone by this point.
201  */
202
203 void sym_remove(sym_table *t, void *p)
204 {
205   sym_base *q = p;
206   hash_remove(&t->t, &q->b);
207   xfree(q);
208   t->load++;
209 }
210
211 /* --- @sym_mkiter@ --- *
212  *
213  * Arguments:   @sym_iter *i@ = pointer to an iterator object
214  *              @sym_table *t@ = pointer to a symbol table object
215  *
216  * Returns:     ---
217  *
218  * Use:         Creates a new symbol table iterator which may be used to
219  *              iterate through a symbol table.
220  */
221
222 void sym_mkiter(sym_iter *i, sym_table *t) { SYM_MKITER(i, t); }
223
224 /* --- @sym_next@ --- *
225  *
226  * Arguments:   @sym_iter *i@ = pointer to iterator object
227  *
228  * Returns:     Pointer to the next symbol found, or null when finished.
229  *
230  * Use:         Returns the next symbol from the table.  Symbols are not
231  *              returned in any particular order.
232  */
233
234 void *sym_next(sym_iter *i)
235 {
236   void *p;
237   SYM_NEXT(i, p);
238   return (p);
239 }
240
241 /*----- That's all, folks -------------------------------------------------*/