/* -*-c-*-
*
- * $Id: sym.c,v 1.8 1999/08/02 14:45:48 mdw Exp $
+ * $Id: sym.c,v 1.11 2000/06/17 10:37:39 mdw Exp $
*
* Symbol table management
*
/*----- Revision history --------------------------------------------------*
*
* $Log: sym.c,v $
+ * Revision 1.11 2000/06/17 10:37:39 mdw
+ * Add support for arena management.
+ *
+ * Revision 1.10 1999/12/10 23:42:04 mdw
+ * Change header file guard names.
+ *
+ * Revision 1.9 1999/10/22 22:36:37 mdw
+ * New test structure for symbol tables.
+ *
* Revision 1.8 1999/08/02 14:45:48 mdw
* Break low-level hashtable code out from sym.
*
/* --- Local headers --- */
#include "alloc.h"
+#include "arena.h"
#include "bits.h"
#include "crc32.h"
#include "exc.h"
#include "hash.h"
#include "sub.h"
#include "sym.h"
-#include "track.h"
/*----- Tuning parameters -------------------------------------------------*/
void sym_create(sym_table *t)
{
- TRACK_CTX("symbol table creation");
- TRACK_PUSH;
hash_create(&t->t, SYM_INITSZ);
+ t->s = &sub_global;
t->load = SYM_LIMIT(SYM_INITSZ);
- TRACK_POP;
}
/* --- @sym_destroy@ --- *
{
sym_iter i;
- TRACK_CTX("symbol table destruction");
- TRACK_PUSH;
-
SYM_MKITER(&i, t);
for (;;) {
sym_base *p;
if (!p)
break;
if (p->len > SYM_BUFSZ)
- sub_free(p->name.p, p->len);
- free(p);
+ subarena_free(t->s, p->name.p, p->len);
+ x_free(t->t.a, p);
}
hash_destroy(&t->t);
-
- TRACK_POP;
}
/* --- @sym_find@ --- *
/* --- Create a new symbol block and initialize it --- */
- {
- TRACK_CTX("new symbol creation");
- TRACK_PUSH;
-
- q = xmalloc(sz);
- q->b.next = *bin;
- q->b.hash = hash;
- q->len = len;
- if (n) {
- if (len <= SYM_BUFSZ)
- memcpy(q->name.b, n, len);
- else {
- TRY {
- q->name.p = sub_alloc(len);
- memcpy(q->name.p, n, len);
- } CATCH {
- free(q);
- TRACK_POP;
- RETHROW;
- } END_TRY;
- }
+ q = x_alloc(t->t.a, sz);
+ q->b.next = *bin;
+ q->b.hash = hash;
+ q->len = len;
+ if (n) {
+ if (len <= SYM_BUFSZ)
+ memcpy(q->name.b, n, len);
+ else {
+ q->name.p = subarena_alloc(t->s, len);
+ memcpy(q->name.p, n, len);
}
-
- TRACK_POP;
}
*bin = &q->b;
/* --- @sym_remove@ --- *
*
- * Arguments: @sym_table *i@ = pointer to a symbol table object
+ * Arguments: @sym_table *t@ = pointer to a symbol table object
* @void *p@ = pointer to symbol table entry
*
* Returns: ---
sym_base *q = p;
hash_remove(&t->t, &q->b);
if (q->len > SYM_BUFSZ)
- sub_free(q->name.p, q->len);
- free(q);
+ subarena_free(t->s, q->name.p, q->len);
+ xfree(q);
t->load++;
}
return (p);
}
-/*----- Symbol table test code --------------------------------------------*/
-
-#ifdef TEST_RIG
-
-#include <errno.h>
-#include <time.h>
-
-typedef struct sym_word {
- sym_base base;
- size_t i;
-} sym_word;
-
-
-/* --- What it does --- *
- *
- * Reads the file /usr/dict/words (change to some other file full of
- * interesting and appropriate bits of text to taste) into a big buffer and
- * picks apart into lines. Then picks lines at random and enters them into
- * the symbol table.
- */
-
-int main(void)
-{
- char *buff, *p, *lim;
- size_t sz, done;
- FILE *fp;
- int i;
- char **line;
- sym_word **flag;
- sym_table tbl;
- int entries;
-
- /* --- Initialize for reading the file --- */
-
- sz = BUFSIZ;
- buff = xmalloc(sz + 1);
- done = 0;
- sub_init();
-
- if ((fp = fopen("/usr/dict/words", "r")) == 0)
- fprintf(stderr, "buggered ;-( (%s)\n", strerror(errno));
-
- /* --- Read buffers of text --- *
- *
- * Read a buffer. If more to come, double the buffer size and try again.
- * This is the method I recommended to comp.lang.c, so I may as well try
- * it.
- */
-
- for (;;) {
- i = fread(buff + done, 1, sz - done, fp);
- done += i;
- if (done != sz)
- break;
- sz <<= 1;
- buff = xrealloc(buff, sz + 1);
- }
-
- /* --- Count the lines --- */
-
- lim = buff + done;
-
- sz = 1;
- for (p = buff; p < lim; p++)
- if (*p == '\n') sz++;
-
- /* --- Build a table of line starts --- */
-
- line = xmalloc(sz * sizeof(char *));
- i = 0;
- line[i++] = buff;
- for (p = buff; p < lim; p++)
- if (*p == '\n') *p = 0, line[i++] = p + 1;
- *lim = 0;
-
- /* --- Build a table of lines --- *
- *
- * This reverses the mapping which the symbol table performs, so that its
- * accuracy can be tested.
- */
-
- flag = xmalloc(sz * sizeof(sym_word *));
- for (i = 0; i < sz; i++)
- flag[i] = 0;
- entries = 0;
-
- sym_create(&tbl);
-
- for (;;) {
- i = (unsigned)rand() % sz;
-
- switch (rand() % 5)
- {
- case 0: {
- sym_word *w;
-
- printf("? %s\n", line[i]);
-
- w = sym_find(&tbl, line[i], -1, 0, 0);
- if (w != flag[i])
- printf("*** error: find `%s' gave %p not %p\n",
- line[i], (void *)w, (void *)flag[i]);
- else if (w && w->i != i)
- printf("*** error: find sym for `%s' gives index %i not %i\n",
- line[i], w->i, i);
- } break;
-
- case 1: {
- unsigned f;
- sym_word *w;
-
- printf("+ %s\n", line[i]);
-
- w = sym_find(&tbl, line[i], -1, sizeof(sym_word), &f);
- if (f)
- {
- if (w != flag[i])
- printf("*** error: create `%s' gave %p not %p\n",
- line[i], (void *)w, (void *)flag[i]);
- else if (w && w->i != i)
- printf("*** error: create sym for `%s' gives index %i not %i\n",
- line[i], w->i, i);
- }
- else
- {
- if (flag[i])
- printf("*** error: create `%s' gave new block, should be %p\n",
- line[i], (void *)flag[i]);
- else {
- flag[i] = w;
- w->i = i;
- entries++;
- }
- }
- } break;
-
- case 2: {
- sym_iter it;
- sym_word *w, **ntbl;
- int v;
-
- if (!entries)
- break;
- v = (rand() % entries) == 0;
- if (!v)
- break;
-
- printf(".\n");
-
- ntbl = xmalloc(sz * sizeof(sym_word *));
- memcpy(ntbl, flag, sz * sizeof(sym_word *));
- sym_mkiter(&it, &tbl);
-
- while ((w = sym_next(&it)) != 0) {
- if (ntbl[w->i] == 0)
- printf("*** error: iterate returned duff item %s\n", SYM_NAME(w));
- else {
- printf(": %s\n", SYM_NAME(w));
- ntbl[w->i] = 0;
- }
- }
-
- for (i = 0; i < sz; i++)
- if (ntbl[i]) printf("*** error: iterate didn't return item %s\n",
- SYM_NAME(ntbl[i]));
- free(ntbl);
- } break;
-
- case 3: {
- sym_base *b;
- int v = rand() & 255 ? 0 : 1;
- break;
-
- printf("dump\n");
-
- for (i = 0; i <= tbl.b.mask; i++) {
- if (!tbl.b.v[i]) continue;
- if (v) printf(" %i: ", i);
- b = (sym_base *)tbl.b.v[i];
- while (b) {
- if ((b->b.hash & tbl.b.mask) != i)
- printf("*** error: bad hash value found");
- if (v) printf("`%s'(%08lx:%lu) ",
- line[((sym_word *)b)->i],
- b->b.hash,
- b->b.hash & tbl.b.mask);
- b = (sym_base *)b->b.next;
- }
- if (v) putchar('\n');
- }
- } break;
-
- case 4: {
- if (flag[i]) {
- printf("- %s\n", SYM_NAME(&flag[i]->base));
- if ((rand() & 1023) == 0) {
- putchar('-'); fflush(stdout);
- }
- sym_remove(&tbl, flag[i]);
- flag[i] = 0;
- entries--;
- }
- } break;
- }
-
- }
-
- return (0);
-}
-
-#endif
-
/*----- That's all, folks -------------------------------------------------*/