chiark / gitweb /
Add support for arena management.
authormdw <mdw>
Sat, 17 Jun 2000 10:39:00 +0000 (10:39 +0000)
committermdw <mdw>
Sat, 17 Jun 2000 10:39:00 +0000 (10:39 +0000)
darray.c
darray.h
dstr.c
dstr.h
env.c
hash.c
hash.h
sym.c
sym.h

index 83150f7c12ec52088213f1c1a4245f41b7b0d9c2..9e3eb905160bb3aa66f60798c76257d37aeebbcc 100644 (file)
--- a/darray.c
+++ b/darray.c
@@ -1,6 +1,6 @@
 /* -*-c-*-
  *
- * $Id: darray.c,v 1.4 1999/11/06 12:40:45 mdw Exp $
+ * $Id: darray.c,v 1.5 2000/06/17 10:37:39 mdw Exp $
  *
  * Dynamically growing dense arrays
  *
@@ -30,6 +30,9 @@
 /*----- Revision history --------------------------------------------------* 
  *
  * $Log: darray.c,v $
+ * Revision 1.5  2000/06/17 10:37:39  mdw
+ * Add support for arena management.
+ *
  * Revision 1.4  1999/11/06 12:40:45  mdw
  * Minor changes to allocation strategy.
  *
@@ -51,6 +54,7 @@
 #include <stdlib.h>
 
 #include "alloc.h"
+#include "arena.h"
 #include "darray.h"
 
 /*----- Magic numbers -----------------------------------------------------*/
@@ -140,14 +144,14 @@ void *da_ensure(da_base *b, void *v, size_t sz, size_t n)
    */
 
   if (p && slots == b->off) {
-    q = xrealloc(p - b->off * sz, nsz * sz);
+    q = x_realloc(b->a, p - b->off * sz, nsz * sz);
     q += slots * sz;
   } else {
-    q = xmalloc(nsz * sz);
+    q = x_alloc(b->a, nsz * sz);
     q += slots * sz;
     if (p) {
       memcpy(q, p, b->len * sz);
-      free(p - b->off * sz);
+      x_free(b->a, p - b->off * sz);
     }
   }
 
@@ -246,11 +250,11 @@ void *da_shunt(da_base *b, void *v, size_t sz, size_t n)
    * almost all the time -- that's the whole point of this routine!
    */
 
-  q = xmalloc(nsz * sz);
+  q = x_alloc(b->a, nsz * sz);
   q += (nsz - slots) * sz;
   if (p) {
     memcpy(q, p, b->len * sz);
-    free(p - b->off * sz);
+    x_free(b->a, p - b->off * sz);
   }
 
   /* --- Fill in the other parts of the base structure --- */
@@ -286,13 +290,13 @@ void *da_tidy(da_base *b, void *v, size_t sz)
     return (p);
 
   if (!b->len) {
-    free(p - b->off * sz);
+    xfree(p - b->off * sz);
     return (0);
   }
 
-  q = xmalloc(b->len * sz);
+  q = x_alloc(b->a, b->len * sz);
   memcpy(q, p, b->len * sz);
-  free(p - b->off * sz);
+  x_free(b->a, p - b->off * sz);
   b->sz = b->len;
   b->off = 0;
   return (q);
index 065c5071edced675cf76332c9b8eed9e98e089b6..b8f17eb0499bf5418b23346b46c828abf00242d2 100644 (file)
--- a/darray.h
+++ b/darray.h
@@ -1,6 +1,6 @@
 /* -*-c-*-
  *
- * $Id: darray.h,v 1.4 1999/12/10 23:42:04 mdw Exp $
+ * $Id: darray.h,v 1.5 2000/06/17 10:37:39 mdw Exp $
  *
  * Dynamically growing dense arrays
  *
@@ -30,6 +30,9 @@
 /*----- Revision history --------------------------------------------------* 
  *
  * $Log: darray.h,v $
+ * Revision 1.5  2000/06/17 10:37:39  mdw
+ * Add support for arena management.
+ *
  * Revision 1.4  1999/12/10 23:42:04  mdw
  * Change header file guard names.
  *
@@ -104,6 +107,7 @@ typedef struct da_base {
   size_t len;                          /* Length of useful portion */
   size_t off;                          /* Offset of @v@ into space */
   unsigned push, unshift;              /* Pushes/unshifts since growth */
+  arena *a;                            /* Pointer to allocation arena */
 } da_base;
 
 /* --- @DA_DECL@ --- *
@@ -119,7 +123,7 @@ typedef struct da_base {
 
 /*----- Initialization, creation and destruction --------------------------*/
 
-#define DA_INIT { { 0, 0, 0, 0, 0 }, 0 } /* Standard initializer */
+#define DA_INIT { { 0, 0, 0, 0, 0, &arena_stdlib }, 0 }
 
 /* --- @DA_CREATE@ --- *
  *
@@ -128,11 +132,12 @@ typedef struct da_base {
  * Use:                Initializes an array block.
  */
 
-#define DA_CREATE(a) do {                                              \
-  (a)->b.sz = (a)->b.len = 0;                                          \
-  (a)->b.off = 0;                                                      \
-  (a)->b.push = (a)->b.unshift = 0;                                    \
-  (a)->v = 0;                                                          \
+#define DA_CREATE(aa) do {                                             \
+  (aa)->b.sz = (aa)->b.len = 0;                                                \
+  (aa)->b.off = 0;                                                     \
+  (aa)->b.push = (aa)->b.unshift = 0;                                  \
+  (aa)->b.a = &arena_stdlib;                                           \
+  (aa)->v = 0;                                                         \
 } while (0)
 
 /* --- @DA_DESTROY@ --- *
@@ -142,10 +147,10 @@ typedef struct da_base {
  * Use:                Destroys an array.  The array is left valid but empty.
  */
 
-#define DA_DESTROY(a) do {                                             \
-  if ((a)->v)                                                          \
-    free((a)->v - (a)->b.off);                                         \
-  DA_CREATE(a);                                                                \
+#define DA_DESTROY(aa) do {                                            \
+  if ((aa)->v)                                                         \
+    x_free((aa)->b.a, (aa)->v - (aa)->b.off);                          \
+  DA_CREATE(aa);                                                               \
 } while (0)
 
 /*----- Storage reservation -----------------------------------------------*/
diff --git a/dstr.c b/dstr.c
index 4df8f7238a4d8ea3e2c127e00787dca78280d946..d76ccf51187b6d282d28250433f2d96e5151c41e 100644 (file)
--- a/dstr.c
+++ b/dstr.c
@@ -1,6 +1,6 @@
 /* -*-c-*-
  *
- * $Id: dstr.c,v 1.13 1999/12/22 15:39:28 mdw Exp $
+ * $Id: dstr.c,v 1.14 2000/06/17 10:37:39 mdw Exp $
  *
  * Handle dynamically growing strings
  *
@@ -30,6 +30,9 @@
 /*----- Revision history --------------------------------------------------*
  *
  * $Log: dstr.c,v $
+ * Revision 1.14  2000/06/17 10:37:39  mdw
+ * Add support for arena management.
+ *
  * Revision 1.13  1999/12/22 15:39:28  mdw
  * Fix overflow in dstr_putline.
  *
@@ -156,9 +159,9 @@ void dstr_ensure(dstr *d, size_t sz)
   do nsz <<= 1; while (nsz < rq);
 
   if (d->buf)
-    d->buf = xrealloc(d->buf, nsz);
+    d->buf = x_realloc(d->a, d->buf, nsz);
   else
-    d->buf = xmalloc(nsz);
+    d->buf = x_alloc(d->a, nsz);
   d->sz = nsz;
 }
 
@@ -237,7 +240,7 @@ void dstr_putm(dstr *d, const void *p, size_t sz) { DPUTM(d, p, sz); }
 
 void dstr_tidy(dstr *d)
 {
-  d->buf = xrealloc(d->buf, d->len + 1);
+  d->buf = x_realloc(d->a, d->buf, d->len + 1);
   d->buf[d->len] = 0;
   d->sz = d->len + 1;
 }
diff --git a/dstr.h b/dstr.h
index 6bc33f89dcea6fec00a938c3aa78192c7ef7ddaa..538dd33a0f8653d48d85b9b7d9d278317551212d 100644 (file)
--- a/dstr.h
+++ b/dstr.h
@@ -1,6 +1,6 @@
 /* -*-c-*-
  *
- * $Id: dstr.h,v 1.10 1999/12/22 15:39:51 mdw Exp $
+ * $Id: dstr.h,v 1.11 2000/06/17 10:37:39 mdw Exp $
  *
  * Handle dynamically growing strings
  *
@@ -30,6 +30,9 @@
 /*----- Revision history --------------------------------------------------*
  *
  * $Log: dstr.h,v $
+ * Revision 1.11  2000/06/17 10:37:39  mdw
+ * Add support for arena management.
+ *
  * Revision 1.10  1999/12/22 15:39:51  mdw
  * Fix argument reuse in DPUTS.
  *
 #include <stdio.h>
 #include <stdlib.h>
 
+#ifndef MLIB_ALLOC_H
+#  include "alloc.h"
+#endif
+
+#ifndef MLIB_ARENA_H
+#  include "arena.h"
+#endif
+
 /*----- Data structures ---------------------------------------------------*/
 
 typedef struct dstr {
   char *buf;                           /* Pointer to string buffer */
   size_t sz;                           /* Size of the buffer */
   size_t len;                          /* Length of the string */
+  arena *a;                            /* Pointer to arena */
 } dstr;
 
-#define DSTR_INIT { 0, 0, 0 }          /* How to initialize one */
+#define DSTR_INIT { 0, 0, 0, &arena_stdlib } /* How to initialize one */
 
 /*----- Functions provided ------------------------------------------------*/
 
@@ -114,6 +126,7 @@ extern void dstr_create(dstr */*d*/);
   _dd->buf = 0;                                                                \
   _dd->sz = 0;                                                         \
   _dd->len = 0;                                                                \
+  _dd->a = &arena_stdlib;                                              \
 } while (0)
 
 /* --- @dstr_destroy@ --- *
@@ -130,7 +143,7 @@ extern void dstr_destroy(dstr */*d*/);
 #define DDESTROY(d) do {                                               \
   dstr *_d = (d);                                                      \
   if (_d->buf)                                                         \
-    free(_d->buf);                                                     \
+    x_free(_d->a, _d->buf);                                            \
   DCREATE(_d);                                                         \
 } while (0)
 
diff --git a/env.c b/env.c
index 4a4fbe5494ae4d1d7a37a4b2eabfe7b5b263bfb4..d648b79b153c61727c3c3c6abb0e5ca93dfe9d94 100644 (file)
--- a/env.c
+++ b/env.c
@@ -1,6 +1,6 @@
 /* -*-c-*-
  *
- * $Id: env.c,v 1.1 1999/07/26 23:15:57 mdw Exp $
+ * $Id: env.c,v 1.2 2000/06/17 10:39:00 mdw Exp $
  *
  * Fiddling with environment variables
  *
@@ -30,6 +30,9 @@
 /*----- Revision history --------------------------------------------------* 
  *
  * $Log: env.c,v $
+ * Revision 1.2  2000/06/17 10:39:00  mdw
+ * Add support for arena management.
+ *
  * Revision 1.1  1999/07/26 23:15:57  mdw
  * Fiddling with environment variables.
  *
@@ -97,7 +100,7 @@ void env_put(sym_table *t, const char *name, const char *value)
   {
     size_t eq = strcspn(name, "=");
     if (name[eq] == '=') {
-      q = xmalloc(eq + 1);
+      q = x_alloc(t->t.a, eq + 1);
       memcpy(q, name, eq);
       q[eq] = 0;
       value = name + eq + 1;
@@ -110,21 +113,21 @@ void env_put(sym_table *t, const char *name, const char *value)
   if (!value) {
     var *v;
     if ((v = sym_find(t, name, -1, 0, 0)) != 0) {
-      free(v->v);
+      x_free(t->t.a, v->v);
       sym_remove(t, v);
     }
   } else {
     unsigned found;
     var *v = sym_find(t, name, -1, sizeof(*v), &found);
     if (found)
-      free(v->v);
-    v->v = xstrdup(value);
+      x_free(t->t.a, v->v);
+    v->v = x_strdup(t->t.a, value);
   }
 
   /* --- Tidying --- */
 
   if (q)
-    free(q);
+    xfree(q);
 }
 
 /* --- @env_import@ --- *
@@ -209,7 +212,7 @@ void env_destroy(sym_table *t)
   var *v;
 
   for (sym_mkiter(&i, t); (v = sym_next(&i)) != 0; )
-    free(v->v);
+    x_free(t->t.a, v->v);
   sym_destroy(t);
 }
 
diff --git a/hash.c b/hash.c
index 96c150bf7c9ca09a9c20eea77f5f1e68e4657979..3945755004ca82fa7dc3907ce1fb9fd0961d874e 100644 (file)
--- a/hash.c
+++ b/hash.c
@@ -1,6 +1,6 @@
 /* -*-c-*-
  *
- * $Id: hash.c,v 1.1 1999/08/02 14:45:48 mdw Exp $
+ * $Id: hash.c,v 1.2 2000/06/17 10:37:39 mdw Exp $
  *
  * General hashtable infrastructure
  *
@@ -30,6 +30,9 @@
 /*----- Revision history --------------------------------------------------* 
  *
  * $Log: hash.c,v $
+ * Revision 1.2  2000/06/17 10:37:39  mdw
+ * Add support for arena management.
+ *
  * Revision 1.1  1999/08/02 14:45:48  mdw
  * Break low-level hashtable code out from sym.
  *
 #include <string.h>
 
 #include "alloc.h"
+#include "arena.h"
 #include "bits.h"
 #include "exc.h"
 #include "hash.h"
-#include "track.h"
 
 /*----- Main code ---------------------------------------------------------*/
 
 void hash_create(hash_table *t, size_t n)
 {
   hash_base **v;
-  TRACK_CTX("hashtable creation");
-  TRACK_PUSH;
-  t->v = xmalloc(n * sizeof(hash_base *));
+
+  t->a = arena_global;
+  t->v = x_alloc(t->a, n * sizeof(hash_base *));
   t->mask = n - 1;
   for (v = t->v; n; v++, n--)
     *v = 0;
-  TRACK_POP;
 }
 
 /* --- @hash_destroy@ --- *
@@ -85,10 +87,7 @@ void hash_create(hash_table *t, size_t n)
 
 void hash_destroy(hash_table *t)
 {
-  TRACK_CTX("hashtable destruction");
-  TRACK_PUSH;
-  free(t->v);
-  TRACK_POP;
+  x_free(t->a, t->v);
 }
 
 /* --- @hash_bin@ --- *
@@ -123,15 +122,9 @@ int hash_extend(hash_table *t)
   uint32 m = t->mask + 1;
   size_t i;
 
-  /* --- Push in a tracking context --- */
-
-  TRACK_CTX("hashtable extension");
-  TRACK_PUSH;
-
   /* --- Allocate a new hash bin vector --- */
 
-  if ((v = realloc(t->v, m * 2 * sizeof(hash_base *))) == 0) {
-    TRACK_POP;
+  if ((v = A_REALLOC(t->a, t->v, m * 2 * sizeof(hash_base *))) == 0) {
     return (0);
   }
   t->v = v;
@@ -156,7 +149,6 @@ int hash_extend(hash_table *t)
     *q = 0;
   }
 
-  TRACK_POP;
   return (1);
 }
 
diff --git a/hash.h b/hash.h
index a7dd70542d7c10ca08acf12d00314ea81c8eefc4..c76c6a186b1cbd887c279d45f56d2839bc89efc7 100644 (file)
--- a/hash.h
+++ b/hash.h
@@ -1,6 +1,6 @@
 /* -*-c-*-
  *
- * $Id: hash.h,v 1.2 1999/12/10 23:42:04 mdw Exp $
+ * $Id: hash.h,v 1.3 2000/06/17 10:37:39 mdw Exp $
  *
  * General hashtable infrastructure
  *
@@ -30,6 +30,9 @@
 /*----- Revision history --------------------------------------------------* 
  *
  * $Log: hash.h,v $
+ * Revision 1.3  2000/06/17 10:37:39  mdw
+ * Add support for arena management.
+ *
  * Revision 1.2  1999/12/10 23:42:04  mdw
  * Change header file guard names.
  *
 
 #include <stddef.h>
 
+#ifndef MLIB_ARENA_H
+#  include "arena.h"
+#endif
+
 #ifndef MLIB_BITS_H
 #  include "bits.h"
 #endif
@@ -72,6 +79,7 @@
 typedef struct hash_table {
   uint32 mask;                         /* Bit mask of hash bits */
   struct hash_base **v;                        /* Vector of hash bins */
+  arena *a;                            /* Allocation arena */
 } hash_table;
 
 /* --- A hashtable entry --- *
diff --git a/sym.c b/sym.c
index a4445de528cd0947824e7180e9f968e4b73b40a8..4e11fba5d1c378eb4e313b3915018a72ab0abab9 100644 (file)
--- 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.11 2000/06/17 10:37:39 mdw Exp $
  *
  * Symbol table management
  *
@@ -30,6 +30,9 @@
 /*----- 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.
  *
 /* --- 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@ --- *
@@ -138,9 +139,6 @@ void sym_destroy(sym_table *t)
 {
   sym_iter i;
 
-  TRACK_CTX("symbol table destruction");
-  TRACK_PUSH;
-
   SYM_MKITER(&i, t);
   for (;;) {
     sym_base *p;
@@ -148,12 +146,10 @@ void sym_destroy(sym_table *t)
     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@ --- *
@@ -230,30 +226,17 @@ void *sym_find(sym_table *t, const char *n, long l, size_t sz, unsigned *f)
 
   /* --- 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;
@@ -287,8 +270,8 @@ 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);
+    subarena_free(t->s, q->name.p, q->len);
+  xfree(q);
   t->load++;
 }
 
diff --git a/sym.h b/sym.h
index 7dd9217c38e532cd06a2f9da8a336a3f20beda11..d9d270c054a1de88c035e1d46d0c1d30f52e79ee 100644 (file)
--- a/sym.h
+++ b/sym.h
@@ -1,6 +1,6 @@
 /* -*-c-*-
  *
- * $Id: sym.h,v 1.10 1999/12/10 23:42:04 mdw Exp $
+ * $Id: sym.h,v 1.11 2000/06/17 10:37:39 mdw Exp $
  *
  * Symbol table management
  *
@@ -30,6 +30,9 @@
 /*----- Revision history --------------------------------------------------*
  *
  * $Log: sym.h,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.
  *
 #  include "hash.h"
 #endif
 
+#ifndef MLIB_SUB_H
+#  include "sub.h"
+#endif
+
 /*----- Type definitions --------------------------------------------------*/
 
 /* --- Symbol table --- *
 
 typedef struct sym_table {
   hash_table t;
+  subarena *s;
   size_t load;
 } sym_table;