3 * $Id: sym.c,v 1.8 1999/08/02 14:45:48 mdw Exp $
5 * Symbol table management
7 * (c) 1998 Straylight/Edgeware
10 /*----- Licensing notice --------------------------------------------------*
12 * This file is part of the mLib utilities library.
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.
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.
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,
30 /*----- Revision history --------------------------------------------------*
33 * Revision 1.8 1999/08/02 14:45:48 mdw
34 * Break low-level hashtable code out from sym.
36 * Revision 1.7 1999/06/01 09:49:08 mdw
37 * Allow things to be looked up by just their caller-supplied hashes. This
38 * actually needs to be thought through better.
40 * Revision 1.6 1999/05/26 21:08:31 mdw
41 * Rename symbols in line with newer conventions.
43 * Revision 1.5 1999/05/13 22:48:37 mdw
44 * Twiddle the extension threshold. Change `-ise' to `-ize' throughout.
46 * Revision 1.4 1999/05/06 19:51:35 mdw
47 * Reformatted the LGPL notice a little bit.
49 * Revision 1.3 1999/05/05 18:50:31 mdw
50 * Change licensing conditions to LGPL.
52 * Revision 1.2 1998/11/26 19:27:33 mdw
53 * Move SYM_NAME into the header file. Fix bugs.
55 * Revision 1.1.1.1 1998/06/17 23:44:42 mdw
56 * Initial version of mLib
60 /*----- Header files ------------------------------------------------------*/
62 /* --- ANSI headers --- */
68 /* --- Local headers --- */
79 /*----- Tuning parameters -------------------------------------------------*/
81 /* --- Initial hash table size --- *
83 * This is the initial @mask@ value. It must be of the form %$2^n - 1$%,
84 * so that it can be used to mask of the bottom bits of a hash value.
87 #define SYM_INITSZ 64 /* Size of a new hash table */
89 /* --- Maximum load factor --- *
91 * This parameter controls how much the table has to be loaded before the
92 * table is extended. The number of elements %$n$%, the number of bins %$b$%
93 * and the limit %$l$% satisfy the relation %$n < bl$%; if a new item is
94 * added to the table and this relation is found to be false, the table is
98 #define SYM_LIMIT(n) ((n) * 2) /* Load factor for growing table */
100 /*----- Main code ---------------------------------------------------------*/
102 /* --- @sym_create@ --- *
104 * Arguments: @sym_table *t@ = symbol table to initialize
108 * Use: Initializes the given symbol table. Raises @EXC_NOMEM@ if
109 * there isn't enough memory.
112 void sym_create(sym_table *t)
114 TRACK_CTX("symbol table creation");
116 hash_create(&t->t, SYM_INITSZ);
117 t->load = SYM_LIMIT(SYM_INITSZ);
121 /* --- @sym_destroy@ --- *
123 * Arguments: @sym_table *t@ = pointer to symbol table in question
127 * Use: Destroys a symbol table, freeing all the memory it used to
131 void sym_destroy(sym_table *t)
135 TRACK_CTX("symbol table destruction");
144 if (p->len > SYM_BUFSZ)
145 sub_free(p->name.p, p->len);
153 /* --- @sym_find@ --- *
155 * Arguments: @sym_table *t@ = pointer to symbol table in question
156 * @const char *n@ = pointer to symbol table to look up
157 * @long l@ = length of the name string or negative to measure
158 * @size_t sz@ = size of desired symbol object, or zero
159 * @unsigned *f@ = pointer to a flag, or null.
161 * Returns: The address of a @sym_base@ structure, or null if not found
164 * Use: Looks up a symbol in a given symbol table. The name is
165 * passed by the address of its first character. The length
166 * may be given, in which case the name may contain arbitrary
167 * binary data, or it may be given as a negative number, in
168 * which case the length of the name is calculated as
171 * The return value is the address of a pointer to a @sym_base@
172 * block (which may have other things on the end, as above). If
173 * the symbol could be found, the return value points to the
174 * symbol block. If the symbol wasn't there, then if @sz@ is
175 * nonzero, a new symbol is created and its address is returned;
176 * otherwise a null pointer is returned. The exception
177 * @EXC_NOMEM@ is raised if the block can't be allocated.
179 * The value of @*f@ indicates whether a new symbol entry was
180 * created: a nonzero value indicates that an old value was
184 void *sym_find(sym_table *t, const char *n, long l, size_t sz, unsigned *f)
188 hash_base **bin, **p;
191 /* --- Find the correct bin --- */
193 len = l < 0 ? strlen(n) + 1 : l;
194 CRC32(hash, 0, n, len);
195 bin = HASH_BIN(&t->t, hash);
197 /* --- Search the bin list --- */
199 for (p = bin; *p; p = &(*p)->next) {
201 if (hash == q->b.hash && len == q->len && !memcmp(n, SYM_NAME(q), len)) {
203 /* --- Found a match --- *
205 * As a minor, and probably pointless, tweak, move the item to the
206 * front of its bin list.
213 /* --- Return the block --- */
220 /* --- Couldn't find the item there --- */
225 /* --- Create a new symbol block and initialize it --- */
228 TRACK_CTX("new symbol creation");
236 if (len <= SYM_BUFSZ)
237 memcpy(q->name.b, n, len);
240 q->name.p = sub_alloc(len);
241 memcpy(q->name.p, n, len);
255 /* --- Consider growing the array --- */
259 if (!t->load && hash_extend(&t->t))
260 t->load = SYM_LIMIT(t->t.mask / 2 + 1);
262 /* --- Finished that, so return the new symbol block --- */
267 /* --- @sym_remove@ --- *
269 * Arguments: @sym_table *i@ = pointer to a symbol table object
270 * @void *p@ = pointer to symbol table entry
274 * Use: Removes the object from the symbol table. The space occupied
275 * by the object and its name is freed; anything else attached
276 * to the entry should already be gone by this point.
279 void sym_remove(sym_table *t, void *p)
282 hash_remove(&t->t, &q->b);
283 if (q->len > SYM_BUFSZ)
284 sub_free(q->name.p, q->len);
289 /* --- @sym_mkiter@ --- *
291 * Arguments: @sym_iter *i@ = pointer to an iterator object
292 * @sym_table *t@ = pointer to a symbol table object
296 * Use: Creates a new symbol table iterator which may be used to
297 * iterate through a symbol table.
300 void sym_mkiter(sym_iter *i, sym_table *t) { SYM_MKITER(i, t); }
302 /* --- @sym_next@ --- *
304 * Arguments: @sym_iter *i@ = pointer to iterator object
306 * Returns: Pointer to the next symbol found, or null when finished.
308 * Use: Returns the next symbol from the table. Symbols are not
309 * returned in any particular order.
312 void *sym_next(sym_iter *i)
319 /*----- Symbol table test code --------------------------------------------*/
326 typedef struct sym_word {
332 /* --- What it does --- *
334 * Reads the file /usr/dict/words (change to some other file full of
335 * interesting and appropriate bits of text to taste) into a big buffer and
336 * picks apart into lines. Then picks lines at random and enters them into
342 char *buff, *p, *lim;
351 /* --- Initialize for reading the file --- */
354 buff = xmalloc(sz + 1);
358 if ((fp = fopen("/usr/dict/words", "r")) == 0)
359 fprintf(stderr, "buggered ;-( (%s)\n", strerror(errno));
361 /* --- Read buffers of text --- *
363 * Read a buffer. If more to come, double the buffer size and try again.
364 * This is the method I recommended to comp.lang.c, so I may as well try
369 i = fread(buff + done, 1, sz - done, fp);
374 buff = xrealloc(buff, sz + 1);
377 /* --- Count the lines --- */
382 for (p = buff; p < lim; p++)
383 if (*p == '\n') sz++;
385 /* --- Build a table of line starts --- */
387 line = xmalloc(sz * sizeof(char *));
390 for (p = buff; p < lim; p++)
391 if (*p == '\n') *p = 0, line[i++] = p + 1;
394 /* --- Build a table of lines --- *
396 * This reverses the mapping which the symbol table performs, so that its
397 * accuracy can be tested.
400 flag = xmalloc(sz * sizeof(sym_word *));
401 for (i = 0; i < sz; i++)
408 i = (unsigned)rand() % sz;
415 printf("? %s\n", line[i]);
417 w = sym_find(&tbl, line[i], -1, 0, 0);
419 printf("*** error: find `%s' gave %p not %p\n",
420 line[i], (void *)w, (void *)flag[i]);
421 else if (w && w->i != i)
422 printf("*** error: find sym for `%s' gives index %i not %i\n",
430 printf("+ %s\n", line[i]);
432 w = sym_find(&tbl, line[i], -1, sizeof(sym_word), &f);
436 printf("*** error: create `%s' gave %p not %p\n",
437 line[i], (void *)w, (void *)flag[i]);
438 else if (w && w->i != i)
439 printf("*** error: create sym for `%s' gives index %i not %i\n",
445 printf("*** error: create `%s' gave new block, should be %p\n",
446 line[i], (void *)flag[i]);
462 v = (rand() % entries) == 0;
468 ntbl = xmalloc(sz * sizeof(sym_word *));
469 memcpy(ntbl, flag, sz * sizeof(sym_word *));
470 sym_mkiter(&it, &tbl);
472 while ((w = sym_next(&it)) != 0) {
474 printf("*** error: iterate returned duff item %s\n", SYM_NAME(w));
476 printf(": %s\n", SYM_NAME(w));
481 for (i = 0; i < sz; i++)
482 if (ntbl[i]) printf("*** error: iterate didn't return item %s\n",
489 int v = rand() & 255 ? 0 : 1;
494 for (i = 0; i <= tbl.b.mask; i++) {
495 if (!tbl.b.v[i]) continue;
496 if (v) printf(" %i: ", i);
497 b = (sym_base *)tbl.b.v[i];
499 if ((b->b.hash & tbl.b.mask) != i)
500 printf("*** error: bad hash value found");
501 if (v) printf("`%s'(%08lx:%lu) ",
502 line[((sym_word *)b)->i],
504 b->b.hash & tbl.b.mask);
505 b = (sym_base *)b->b.next;
507 if (v) putchar('\n');
513 printf("- %s\n", SYM_NAME(&flag[i]->base));
514 if ((rand() & 1023) == 0) {
515 putchar('-'); fflush(stdout);
517 sym_remove(&tbl, flag[i]);
531 /*----- That's all, folks -------------------------------------------------*/