Major memory management overhaul. Added arena support. Use the secure
[u/mdw/catacomb] / mparena.c
index e40a6a6..95f88d5 100644 (file)
--- a/mparena.c
+++ b/mparena.c
@@ -1,6 +1,6 @@
 /* -*-c-*-
  *
- * $Id: mparena.c,v 1.2 1999/11/21 22:14:19 mdw Exp $
+ * $Id: mparena.c,v 1.5 2000/06/17 11:35:48 mdw Exp $
  *
  * Allocation and freeing of MP buffers
  *
 /*----- Revision history --------------------------------------------------* 
  *
  * $Log: mparena.c,v $
+ * Revision 1.5  2000/06/17 11:35:48  mdw
+ * Overhaul to use mLib's arena system underneath.
+ *
+ * Revision 1.4  1999/12/10 23:28:52  mdw
+ * Memory allocation counting.
+ *
+ * Revision 1.3  1999/11/22 13:58:00  mdw
+ * Document the tweakables.
+ *
  * Revision 1.2  1999/11/21 22:14:19  mdw
  * Fix bug.  Improve diagnostic capabilities.
  *
 #include <stdlib.h>
 #include <string.h>
 
-#include <mLib/alloc.h>
+#include <mLib/arena.h>
+#include <mLib/exc.h>
 #include <mLib/sub.h>
 
 #include "mparena.h"
 
 /*----- Tweakables --------------------------------------------------------*/
 
-/* #define MPARENA_TRIVIAL */
-
-/* #define MPARENA_DEBUG "mparena.out" */
+/* --- @MPARENA_TRIVIAL@ --- *
+ *
+ * Make the allocator a passthrough.  It immediately calls the underlying
+ * allocation functions rather than attempting to keep track of blocks
+ * itself.
+ */
 
-/*----- Default allocator -------------------------------------------------*/
+/* #define MPARENA_TRIVIAL */
 
-static void *defalloc(mparena *a, size_t sz) { return xmalloc(sz); }
-static void deffree(mparena *a, void *p) { free(p); }
+/* --- @MPARENA_DEBUG@ --- *
+ *
+ * The name of an output trace file to which logging information about the
+ * state of arena trees should be written.  If unset, no logging is done.
+ */
 
-mparena_ops mparena_defops = { defalloc, deffree };
+/* #define MPARENA_DEBUG "mparena.out" */
 
 /*----- Static variables --------------------------------------------------*/
 
@@ -78,12 +94,10 @@ mparena_ops mparena_defops = { defalloc, deffree };
 
 #endif
 
-static mparena arena = { 0, &mparena_defops };
+/*----- Standard arenas ---------------------------------------------------*/
 
-#define MPARENA_RESOLVE(a) do {                                                \
-  if ((a) == MPARENA_GLOBAL)                                           \
-    (a) = &arena;                                                      \
-} while (0)
+mparena mparena_global = MPARENA_INIT;
+mparena mparena_secure = MPARENA_INIT;
 
 /*----- Main code ---------------------------------------------------------*/
 
@@ -126,28 +140,21 @@ static void tdump(mparena_node *n)
 void mparena_create(mparena *a)
 {
   a->root = 0;
-  a->ops = &mparena_defops;
+  a->n = 0;
+  a->a = &arena_stdlib;
 }
 
-/* --- @mparena_setops@ --- *
+/* --- @mparena_setarena@ --- *
  *
- * Arguments:  @mparena *a@ = pointer to arena block
- *             @mparena_ops *ops@ = pointer to operations block or null
+ * Arguments:  @mparena *a@ = pointer to MP arena block
+ *             @arena *aa@ = pointer to arena
  *
- * Returns:    The previous operations block.
+ * Returns:    ---
  *
- * Use:                Sets or queries the operations attached to an arena.
+ * Use:                Sets the underlying arena for an MP arena.
  */
 
-mparena_ops *mparena_setops(mparena *a, mparena_ops *ops)
-{
-  mparena_ops *o;
-  MPARENA_RESOLVE(a);
-  o = a->ops;
-  if (ops)
-    a->ops = ops;
-  return (0);
-}
+extern void mparena_setarena(mparena *a, arena *aa) { a->a = aa; }
 
 /* --- @mparena_destroy@ --- *
  *
@@ -162,7 +169,7 @@ mparena_ops *mparena_setops(mparena *a, mparena_ops *ops)
 
 static void tfree(mparena *a, mparena_node *n)
 {
-  a->ops->free(a, n->v);
+  A_FREE(a->a, n->v);
   if (n->left)
     tfree(a, n->left);
   if (n->right)
@@ -176,6 +183,21 @@ void mparena_destroy(mparena *a)
   a->root = 0;
 }
 
+/* --- @mparena_count@ --- *
+ *
+ * Arguments:  @mparena *a@ = pointer to arena block
+ *
+ * Returns:    Number of allocated blocks from this arena.
+ *
+ * Use:                Reports the number of blocks allocated from the arena and not
+ *             yet freed.
+ */
+
+unsigned mparena_count(mparena *a)
+{
+  return (a->n);
+} 
+
 /* --- @mpalloc@ --- *
  *
  * Arguments:  @mparena *a@ = pointer to arena block
@@ -191,8 +213,11 @@ void mparena_destroy(mparena *a)
 
 mpw *mpalloc(mparena *a, size_t sz)
 {
-  MPARENA_RESOLVE(a);
-  return (a->ops->alloc(a, MPWS(sz)));
+  mpw *v;
+  v = A_ALLOC(a->a, MPWS(sz));
+  if (!v)
+    THROW(EXC_NOMEM);
+  return (v);
 }
 
 #else
@@ -202,15 +227,12 @@ mpw *mpalloc(mparena *a, size_t sz)
   mparena_node **nn, *n;
   mpw *v;
 
-  MPARENA_RESOLVE(a);
   nn = &a->root;
 
 #ifdef MPARENA_DEBUG
   MPARENA_OPENFILE;
   fprintf(debugfp, "alloc %u\n  before: ", sz);
   tdump(a->root); putc('\n', debugfp);
-  if (sz == 0)
-    asm("nop");
 #endif
 
   /* --- First, find a block which is big enough --- */
@@ -221,8 +243,10 @@ again:
 #ifdef MPARENA_DEBUG
     fputs("  failed\n", debugfp);
 #endif
-    v = a->ops->alloc(a, MPWS(sz + 1));
+    if ((v = A_ALLOC(a->a, MPWS(sz + 1))) == 0)
+      THROW(EXC_NOMEM);
     v[0] = sz;
+    a->n++;
     return (v + 1);
   }
   if (n->v[0] < sz) {
@@ -270,6 +294,7 @@ again:
   /* --- Get rid of this node now --- */
 
   DESTROY(n);
+  a->n++;
   return (v + 1);
 }
 
@@ -282,16 +307,14 @@ again:
  *
  * Returns:    ---
  *
- * Use:                Returns an MP vector to an arena.  It doesn't have to be
- *             returned to the arena from which it was allocated.
+ * Use:                Returns an MP vector to an arena.
  */
 
 #ifdef MPARENA_TRIVIAL
 
 void mpfree(mparena *a, mpw *v)
 {
-  MPARENA_RESOLVE(a);
-  a->ops->free(a, v);
+  A_FREE(a->a, v);
 }
 
 #else
@@ -301,8 +324,6 @@ void mpfree(mparena *a, mpw *v)
   mparena_node **nn, *n;
   size_t sz = *--v;
 
-  MPARENA_RESOLVE(a);
-
 #ifdef MPARENA_DEBUG
   MPARENA_OPENFILE;
   fprintf(debugfp, "free %u\n  before: ", sz);
@@ -322,6 +343,7 @@ void mpfree(mparena *a, mpw *v)
   n->left = n->right = 0;
   n->v = v;
   *nn = n;
+  a->n--;
 
 #ifdef MPARENA_DEBUG
   fputs("  after: ", debugfp);