X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~mdw/git/mLib/blobdiff_plain/c6e0eaf00cd09a4e5237e70d8351049ec91d7653..7292c4c6e2b50cc43f17507745ff6c3e2d0057f3:/sym.c diff --git a/sym.c b/sym.c index a4445de..3aea8c7 100644 --- a/sym.c +++ b/sym.c @@ -1,6 +1,6 @@ /* -*-c-*- * - * $Id: sym.c,v 1.10 1999/12/10 23:42:04 mdw Exp $ + * $Id: sym.c,v 1.13 2001/01/25 21:14:49 mdw Exp $ * * Symbol table management * @@ -30,6 +30,18 @@ /*----- Revision history --------------------------------------------------* * * $Log: sym.c,v $ + * Revision 1.13 2001/01/25 21:14:49 mdw + * Always add a terminating null, and don't count it in the length. + * + * Revision 1.12 2001/01/20 11:49:37 mdw + * Export tuning parameters from header file, for the benefit of other + * hashtable implementations. Change the storage of symbol names: store + * the name after the allocated symbol block in all cases. This replaces + * the previous complicated and slightly wasteful arrangement. + * + * 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. * @@ -74,34 +86,13 @@ /* --- 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 -------------------------------------------------*/ - -/* --- Initial hash table size --- * - * - * This is the initial @mask@ value. It must be of the form %$2^n - 1$%, - * so that it can be used to mask of the bottom bits of a hash value. - */ - -#define SYM_INITSZ 64 /* Size of a new hash table */ - -/* --- Maximum load factor --- * - * - * This parameter controls how much the table has to be loaded before the - * table is extended. The number of elements %$n$%, the number of bins %$b$% - * and the limit %$l$% satisfy the relation %$n < bl$%; if a new item is - * added to the table and this relation is found to be false, the table is - * doubled in size. - */ - -#define SYM_LIMIT(n) ((n) * 2) /* Load factor for growing table */ /*----- Main code ---------------------------------------------------------*/ @@ -117,11 +108,9 @@ 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@ --- * @@ -138,22 +127,15 @@ void sym_destroy(sym_table *t) { sym_iter i; - TRACK_CTX("symbol table destruction"); - TRACK_PUSH; - SYM_MKITER(&i, t); for (;;) { sym_base *p; SYM_NEXT(&i, p); if (!p) break; - if (p->len > SYM_BUFSZ) - sub_free(p->name.p, p->len); - free(p); + x_free(t->t.a, p); } hash_destroy(&t->t); - - TRACK_POP; } /* --- @sym_find@ --- * @@ -196,7 +178,7 @@ void *sym_find(sym_table *t, const char *n, long l, size_t sz, unsigned *f) /* --- Find the correct bin --- */ - len = l < 0 ? strlen(n) + 1 : l; + len = l < 0 ? strlen(n) : l; CRC32(hash, 0, n, len); bin = HASH_BIN(&t->t, hash); @@ -228,34 +210,18 @@ void *sym_find(sym_table *t, const char *n, long l, size_t sz, unsigned *f) if (f) *f = 0; if (!sz) return (0); - /* --- 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; - } - } - - TRACK_POP; - } - + /* --- Create a new symbol block and initialize it --- * + * + * The name is attached to the end of the symbol block. + */ + + q = x_alloc(t->t.a, sz + len + 1); + q->b.next = *bin; + q->b.hash = hash; + q->name = (char *)q + sz; + memcpy(q->name, n, len); + q->name[len] = 0; + q->len = len; *bin = &q->b; /* --- Consider growing the array --- */ @@ -263,7 +229,7 @@ void *sym_find(sym_table *t, const char *n, long l, size_t sz, unsigned *f) if (t->load) t->load--; if (!t->load && hash_extend(&t->t)) - t->load = SYM_LIMIT(t->t.mask / 2 + 1); + t->load = SYM_LIMIT(t->t.mask + 1); /* --- Finished that, so return the new symbol block --- */ @@ -286,9 +252,7 @@ void sym_remove(sym_table *t, void *p) { sym_base *q = p; hash_remove(&t->t, &q->b); - if (q->len > SYM_BUFSZ) - sub_free(q->name.p, q->len); - free(q); + xfree(q); t->load++; }