/* -*-c-*-
*
- * $Id: sym.c,v 1.8 1999/08/02 14:45:48 mdw Exp $
+ * $Id: sym.c,v 1.15 2004/04/08 01:36:13 mdw Exp $
*
* Symbol table management
*
* (c) 1998 Straylight/Edgeware
*/
-/*----- Licensing notice --------------------------------------------------*
+/*----- Licensing notice --------------------------------------------------*
*
* This file is part of the mLib utilities library.
*
* it under the terms of the GNU Library General Public License as
* published by the Free Software Foundation; either version 2 of the
* License, or (at your option) any later version.
- *
+ *
* mLib is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Library General Public License for more details.
- *
+ *
* You should have received a copy of the GNU Library General Public
* License along with mLib; if not, write to the Free
* Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
* MA 02111-1307, USA.
*/
-/*----- Revision history --------------------------------------------------*
- *
- * $Log: sym.c,v $
- * Revision 1.8 1999/08/02 14:45:48 mdw
- * Break low-level hashtable code out from sym.
- *
- * Revision 1.7 1999/06/01 09:49:08 mdw
- * Allow things to be looked up by just their caller-supplied hashes. This
- * actually needs to be thought through better.
- *
- * Revision 1.6 1999/05/26 21:08:31 mdw
- * Rename symbols in line with newer conventions.
- *
- * Revision 1.5 1999/05/13 22:48:37 mdw
- * Twiddle the extension threshold. Change `-ise' to `-ize' throughout.
- *
- * Revision 1.4 1999/05/06 19:51:35 mdw
- * Reformatted the LGPL notice a little bit.
- *
- * Revision 1.3 1999/05/05 18:50:31 mdw
- * Change licensing conditions to LGPL.
- *
- * Revision 1.2 1998/11/26 19:27:33 mdw
- * Move SYM_NAME into the header file. Fix bugs.
- *
- * Revision 1.1.1.1 1998/06/17 23:44:42 mdw
- * Initial version of mLib
- *
- */
-
/*----- Header files ------------------------------------------------------*/
/* --- ANSI headers --- */
/* --- 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 */
+#include "unihash.h"
/*----- Main code ---------------------------------------------------------*/
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;
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@ --- *
*
* Arguments: @sym_table *t@ = pointer to symbol table in question
- * @const char *n@ = pointer to symbol table to look up
+ * @const char *n@ = pointer to symbol name to look up
* @long l@ = length of the name string or negative to measure
* @size_t sz@ = size of desired symbol object, or zero
* @unsigned *f@ = pointer to a flag, or null.
/* --- Find the correct bin --- */
- len = l < 0 ? strlen(n) + 1 : l;
- CRC32(hash, 0, n, len);
+ len = l < 0 ? strlen(n) : l;
+ hash = UNIHASH(&unihash_global, n, len);
bin = HASH_BIN(&t->t, hash);
/* --- Search the bin list --- */
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 --- */
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 --- */
/* --- @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);
+ 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 -------------------------------------------------*/