More changes. Still embryonic.
[u/mdw/catacomb] / mparena.c
index e40a6a6..e7c12cd 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.4 1999/12/10 23:28:52 mdw Exp $
  *
  * Allocation and freeing of MP buffers
  *
 /*----- Revision history --------------------------------------------------* 
  *
  * $Log: mparena.c,v $
+ * 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.
  *
 
 /*----- Tweakables --------------------------------------------------------*/
 
+/* --- @MPARENA_TRIVIAL@ --- *
+ *
+ * Make the allocator a passthrough.  It immediately calls the underlying
+ * allocation functions rather than attempting to keep track of blocks
+ * itself.
+ */
+
 /* #define MPARENA_TRIVIAL */
 
+/* --- @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.
+ */
+
 /* #define MPARENA_DEBUG "mparena.out" */
 
 /*----- Default allocator -------------------------------------------------*/
@@ -60,7 +79,7 @@
 static void *defalloc(mparena *a, size_t sz) { return xmalloc(sz); }
 static void deffree(mparena *a, void *p) { free(p); }
 
-mparena_ops mparena_defops = { defalloc, deffree };
+mparena_ops mparena_defaultops = { defalloc, deffree };
 
 /*----- Static variables --------------------------------------------------*/
 
@@ -78,7 +97,7 @@ mparena_ops mparena_defops = { defalloc, deffree };
 
 #endif
 
-static mparena arena = { 0, &mparena_defops };
+static mparena arena = MPARENA_INIT;
 
 #define MPARENA_RESOLVE(a) do {                                                \
   if ((a) == MPARENA_GLOBAL)                                           \
@@ -126,7 +145,8 @@ static void tdump(mparena_node *n)
 void mparena_create(mparena *a)
 {
   a->root = 0;
-  a->ops = &mparena_defops;
+  a->n = 0;
+  a->ops = &mparena_defaultops;
 }
 
 /* --- @mparena_setops@ --- *
@@ -176,6 +196,22 @@ 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)
+{
+  MPARENA_RESOLVE(a);
+  return (a->n);
+} 
+
 /* --- @mpalloc@ --- *
  *
  * Arguments:  @mparena *a@ = pointer to arena block
@@ -209,8 +245,6 @@ mpw *mpalloc(mparena *a, size_t sz)
   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 --- */
@@ -223,6 +257,7 @@ again:
 #endif
     v = a->ops->alloc(a, MPWS(sz + 1));
     v[0] = sz;
+    a->n++;
     return (v + 1);
   }
   if (n->v[0] < sz) {
@@ -270,6 +305,7 @@ again:
   /* --- Get rid of this node now --- */
 
   DESTROY(n);
+  a->n++;
   return (v + 1);
 }
 
@@ -282,8 +318,7 @@ 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
@@ -322,6 +357,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);