chiark / gitweb /
Change header file guard names.
[mLib] / sym.c
1 /* -*-c-*-
2  *
3  * $Id: sym.c,v 1.10 1999/12/10 23:42:04 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 /*----- Revision history --------------------------------------------------*
31  *
32  * $Log: sym.c,v $
33  * Revision 1.10  1999/12/10 23:42:04  mdw
34  * Change header file guard names.
35  *
36  * Revision 1.9  1999/10/22 22:36:37  mdw
37  * New test structure for symbol tables.
38  *
39  * Revision 1.8  1999/08/02 14:45:48  mdw
40  * Break low-level hashtable code out from sym.
41  *
42  * Revision 1.7  1999/06/01 09:49:08  mdw
43  * Allow things to be looked up by just their caller-supplied hashes.  This
44  * actually needs to be thought through better.
45  *
46  * Revision 1.6  1999/05/26 21:08:31  mdw
47  * Rename symbols in line with newer conventions.
48  *
49  * Revision 1.5  1999/05/13 22:48:37  mdw
50  * Twiddle the extension threshold.  Change `-ise' to `-ize' throughout.
51  *
52  * Revision 1.4  1999/05/06 19:51:35  mdw
53  * Reformatted the LGPL notice a little bit.
54  *
55  * Revision 1.3  1999/05/05 18:50:31  mdw
56  * Change licensing conditions to LGPL.
57  *
58  * Revision 1.2  1998/11/26 19:27:33  mdw
59  * Move SYM_NAME into the header file.  Fix bugs.
60  *
61  * Revision 1.1.1.1  1998/06/17 23:44:42  mdw
62  * Initial version of mLib
63  *
64  */
65
66 /*----- Header files ------------------------------------------------------*/
67
68 /* --- ANSI headers --- */
69
70 #include <stdio.h>
71 #include <stdlib.h>
72 #include <string.h>
73
74 /* --- Local headers --- */
75
76 #include "alloc.h"
77 #include "bits.h"
78 #include "crc32.h"
79 #include "exc.h"
80 #include "hash.h"
81 #include "sub.h"
82 #include "sym.h"
83 #include "track.h"
84
85 /*----- Tuning parameters -------------------------------------------------*/
86
87 /* --- Initial hash table size --- *
88  *
89  * This is the initial @mask@ value.  It must be of the form %$2^n - 1$%,
90  * so that it can be used to mask of the bottom bits of a hash value.
91  */
92
93 #define SYM_INITSZ 64                   /* Size of a new hash table */
94
95 /* --- Maximum load factor --- *
96  *
97  * This parameter controls how much the table has to be loaded before the
98  * table is extended.  The number of elements %$n$%, the number of bins %$b$%
99  * and the limit %$l$% satisfy the relation %$n < bl$%; if a new item is
100  * added to the table and this relation is found to be false, the table is
101  * doubled in size.
102  */
103
104 #define SYM_LIMIT(n) ((n) * 2)          /* Load factor for growing table */
105
106 /*----- Main code ---------------------------------------------------------*/
107
108 /* --- @sym_create@ --- *
109  *
110  * Arguments:   @sym_table *t@ = symbol table to initialize
111  *
112  * Returns:     ---
113  *
114  * Use:         Initializes the given symbol table.  Raises @EXC_NOMEM@ if
115  *              there isn't enough memory.
116  */
117
118 void sym_create(sym_table *t)
119 {
120   TRACK_CTX("symbol table creation");
121   TRACK_PUSH;
122   hash_create(&t->t, SYM_INITSZ);
123   t->load = SYM_LIMIT(SYM_INITSZ);
124   TRACK_POP;
125 }
126
127 /* --- @sym_destroy@ --- *
128  *
129  * Arguments:   @sym_table *t@ = pointer to symbol table in question
130  *
131  * Returns:     ---
132  *
133  * Use:         Destroys a symbol table, freeing all the memory it used to
134  *              occupy.
135  */
136
137 void sym_destroy(sym_table *t)
138 {
139   sym_iter i;
140
141   TRACK_CTX("symbol table destruction");
142   TRACK_PUSH;
143
144   SYM_MKITER(&i, t);
145   for (;;) {
146     sym_base *p;
147     SYM_NEXT(&i, p);
148     if (!p)
149       break;
150     if (p->len > SYM_BUFSZ)
151       sub_free(p->name.p, p->len);
152     free(p);
153   }
154   hash_destroy(&t->t);
155
156   TRACK_POP;
157 }
158
159 /* --- @sym_find@ --- *
160  *
161  * Arguments:   @sym_table *t@ = pointer to symbol table in question
162  *              @const char *n@ = pointer to symbol table to look up
163  *              @long l@ = length of the name string or negative to measure
164  *              @size_t sz@ = size of desired symbol object, or zero
165  *              @unsigned *f@ = pointer to a flag, or null.
166  *
167  * Returns:     The address of a @sym_base@ structure, or null if not found
168  *              and @sz@ is zero.
169  *
170  * Use:         Looks up a symbol in a given symbol table.  The name is
171  *              passed by the address of its first character.  The length
172  *              may be given, in which case the name may contain arbitrary
173  *              binary data, or it may be given as a negative number, in
174  *              which case the length of the name is calculated as
175  *              @strlen(n) + 1@.
176  *
177  *              The return value is the address of a pointer to a @sym_base@
178  *              block (which may have other things on the end, as above).  If
179  *              the symbol could be found, the return value points to the
180  *              symbol block.  If the symbol wasn't there, then if @sz@ is
181  *              nonzero, a new symbol is created and its address is returned;
182  *              otherwise a null pointer is returned.  The exception
183  *              @EXC_NOMEM@ is raised if the block can't be allocated.
184  *
185  *              The value of @*f@ indicates whether a new symbol entry was
186  *              created: a nonzero value indicates that an old value was
187  *              found.
188  */
189
190 void *sym_find(sym_table *t, const char *n, long l, size_t sz, unsigned *f)
191 {
192   uint32 hash;
193   size_t len = 0;
194   hash_base **bin, **p;
195   sym_base *q;
196
197   /* --- Find the correct bin --- */
198
199   len = l < 0 ? strlen(n) + 1 : l;
200   CRC32(hash, 0, n, len);
201   bin = HASH_BIN(&t->t, hash);
202
203   /* --- Search the bin list --- */
204
205   for (p = bin; *p; p = &(*p)->next) {
206     q = (sym_base *)*p;
207     if (hash == q->b.hash && len == q->len && !memcmp(n, SYM_NAME(q), len)) {
208
209       /* --- Found a match --- *
210        *
211        * As a minor, and probably pointless, tweak, move the item to the
212        * front of its bin list.
213        */
214
215       (*p) = q->b.next;
216       q->b.next = *bin;
217       *bin = &q->b;
218
219       /* --- Return the block --- */
220
221       if (f) *f = 1;
222       return (q);
223     }
224   }
225
226   /* --- Couldn't find the item there --- */
227
228   if (f) *f = 0;
229   if (!sz) return (0);
230
231   /* --- Create a new symbol block and initialize it --- */
232
233   {
234     TRACK_CTX("new symbol creation");
235     TRACK_PUSH;
236
237     q = xmalloc(sz);
238     q->b.next = *bin;
239     q->b.hash = hash;
240     q->len = len;
241     if (n) {
242       if (len <= SYM_BUFSZ)
243         memcpy(q->name.b, n, len);
244       else {
245         TRY {
246           q->name.p = sub_alloc(len);
247           memcpy(q->name.p, n, len);
248         } CATCH {
249           free(q);
250           TRACK_POP;
251           RETHROW;
252         } END_TRY;
253       }
254     }
255
256     TRACK_POP;
257   }
258
259   *bin = &q->b;
260
261   /* --- Consider growing the array --- */
262
263   if (t->load)
264     t->load--;
265   if (!t->load && hash_extend(&t->t))
266     t->load = SYM_LIMIT(t->t.mask / 2 + 1);
267
268   /* --- Finished that, so return the new symbol block --- */
269
270   return (q);
271 }
272
273 /* --- @sym_remove@ --- *
274  *
275  * Arguments:   @sym_table *t@ = pointer to a symbol table object
276  *              @void *p@ = pointer to symbol table entry
277  *
278  * Returns:     ---
279  *
280  * Use:         Removes the object from the symbol table.  The space occupied
281  *              by the object and its name is freed; anything else attached
282  *              to the entry should already be gone by this point.
283  */
284
285 void sym_remove(sym_table *t, void *p)
286 {
287   sym_base *q = p;
288   hash_remove(&t->t, &q->b);
289   if (q->len > SYM_BUFSZ)
290     sub_free(q->name.p, q->len);
291   free(q);
292   t->load++;
293 }
294
295 /* --- @sym_mkiter@ --- *
296  *
297  * Arguments:   @sym_iter *i@ = pointer to an iterator object
298  *              @sym_table *t@ = pointer to a symbol table object
299  *
300  * Returns:     ---
301  *
302  * Use:         Creates a new symbol table iterator which may be used to
303  *              iterate through a symbol table.
304  */
305
306 void sym_mkiter(sym_iter *i, sym_table *t) { SYM_MKITER(i, t); }
307
308 /* --- @sym_next@ --- *
309  *
310  * Arguments:   @sym_iter *i@ = pointer to iterator object
311  *
312  * Returns:     Pointer to the next symbol found, or null when finished.
313  *
314  * Use:         Returns the next symbol from the table.  Symbols are not
315  *              returned in any particular order.
316  */
317
318 void *sym_next(sym_iter *i)
319 {
320   void *p;
321   SYM_NEXT(i, p);
322   return (p);
323 }
324
325 /*----- That's all, folks -------------------------------------------------*/