chiark / gitweb /
url: Allow `;' to separate key/value pairs in URL-encoded strings.
[mLib] / sym.c
diff --git a/sym.c b/sym.c
index 4e11fba5d1c378eb4e313b3915018a72ab0abab9..a5cec57b576a602e4917adfe5170322f8e294323 100644 (file)
--- a/sym.c
+++ b/sym.c
@@ -1,6 +1,6 @@
 /* -*-c-*-
  *
- * $Id: sym.c,v 1.11 2000/06/17 10:37:39 mdw Exp $
+ * $Id: sym.c,v 1.15 2004/04/08 01:36:13 mdw Exp $
  *
  * Symbol table management
  *
  * MA 02111-1307, USA.
  */
 
-/*----- 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.
- *
- * 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 --- */
 #include "alloc.h"
 #include "arena.h"
 #include "bits.h"
-#include "crc32.h"
 #include "exc.h"
 #include "hash.h"
 #include "sub.h"
 #include "sym.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 ---------------------------------------------------------*/
 
@@ -145,8 +85,6 @@ void sym_destroy(sym_table *t)
     SYM_NEXT(&i, p);
     if (!p)
       break;
-    if (p->len > SYM_BUFSZ)
-      subarena_free(t->s, p->name.p, p->len);
     x_free(t->t.a, p);
   }
   hash_destroy(&t->t);
@@ -155,7 +93,7 @@ void sym_destroy(sym_table *t)
 /* --- @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.
@@ -192,8 +130,8 @@ 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;
-  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 --- */
@@ -224,21 +162,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 --- */
+  /* --- 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);
+  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;
-  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);
-    }
-  }
-
   *bin = &q->b;
 
   /* --- Consider growing the array --- */
@@ -246,7 +181,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 --- */
 
@@ -269,8 +204,6 @@ void sym_remove(sym_table *t, void *p)
 {
   sym_base *q = p;
   hash_remove(&t->t, &q->b);
-  if (q->len > SYM_BUFSZ)
-    subarena_free(t->s, q->name.p, q->len);
   xfree(q);
   t->load++;
 }