Overhaul to use mLib's arena system underneath.
authormdw <mdw>
Sat, 17 Jun 2000 11:35:48 +0000 (11:35 +0000)
committermdw <mdw>
Sat, 17 Jun 2000 11:35:48 +0000 (11:35 +0000)
mparena.c
mparena.h

index e7c12cd..95f88d5 100644 (file)
--- a/mparena.c
+++ b/mparena.c
@@ -1,6 +1,6 @@
 /* -*-c-*-
  *
- * $Id: mparena.c,v 1.4 1999/12/10 23:28:52 mdw Exp $
+ * $Id: mparena.c,v 1.5 2000/06/17 11:35:48 mdw Exp $
  *
  * Allocation and freeing of MP buffers
  *
@@ -30,6 +30,9 @@
 /*----- 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.
  *
@@ -50,7 +53,8 @@
 #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"
 
 /* #define MPARENA_DEBUG "mparena.out" */
 
-/*----- Default allocator -------------------------------------------------*/
-
-static void *defalloc(mparena *a, size_t sz) { return xmalloc(sz); }
-static void deffree(mparena *a, void *p) { free(p); }
-
-mparena_ops mparena_defaultops = { defalloc, deffree };
-
 /*----- Static variables --------------------------------------------------*/
 
 #ifdef MPARENA_DEBUG
@@ -97,12 +94,10 @@ mparena_ops mparena_defaultops = { defalloc, deffree };
 
 #endif
 
-static mparena arena = MPARENA_INIT;
+/*----- 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 ---------------------------------------------------------*/
 
@@ -146,28 +141,20 @@ void mparena_create(mparena *a)
 {
   a->root = 0;
   a->n = 0;
-  a->ops = &mparena_defaultops;
+  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@ --- *
  *
@@ -182,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)
@@ -208,7 +195,6 @@ void mparena_destroy(mparena *a)
 
 unsigned mparena_count(mparena *a)
 {
-  MPARENA_RESOLVE(a);
   return (a->n);
 } 
 
@@ -227,8 +213,11 @@ unsigned mparena_count(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
@@ -238,7 +227,6 @@ mpw *mpalloc(mparena *a, size_t sz)
   mparena_node **nn, *n;
   mpw *v;
 
-  MPARENA_RESOLVE(a);
   nn = &a->root;
 
 #ifdef MPARENA_DEBUG
@@ -255,7 +243,8 @@ 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);
@@ -325,8 +314,7 @@ again:
 
 void mpfree(mparena *a, mpw *v)
 {
-  MPARENA_RESOLVE(a);
-  a->ops->free(a, v);
+  A_FREE(a->a, v);
 }
 
 #else
@@ -336,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);
index b541b25..92bf4b6 100644 (file)
--- a/mparena.h
+++ b/mparena.h
@@ -1,6 +1,6 @@
 /* -*-c-*-
  *
- * $Id: mparena.h,v 1.2 1999/12/10 23:28:59 mdw Exp $
+ * $Id: mparena.h,v 1.3 2000/06/17 11:35:48 mdw Exp $
  *
  * Allocation and freeing of MP buffers
  *
@@ -30,6 +30,9 @@
 /*----- Revision history --------------------------------------------------* 
  *
  * $Log: mparena.h,v $
+ * Revision 1.3  2000/06/17 11:35:48  mdw
+ * Overhaul to use mLib's arena system underneath.
+ *
  * Revision 1.2  1999/12/10 23:28:59  mdw
  * Memory allocation counting.
  *
@@ -47,6 +50,8 @@
 
 /*----- Header files ------------------------------------------------------*/
 
+#include <mLib/arena.h>
+
 #ifndef CATACOMB_MPW_H
 #  include "mpw.h"
 #endif
@@ -72,28 +77,16 @@ typedef struct mparena_node {
 typedef struct mparena {
   mparena_node *root;
   unsigned n;
-  struct mparena_ops *ops;
+  arena *a;
 } mparena;
 
-/* --- @mparena_ops@ --- *
- *
- * Operations required for an arena memory manager.  The default manager just
- * calls @xmalloc@ and @free@, although it's possible to envisage a more
- * paranoid implementation which allocates locked memory pages.  Switch them
- * over with @mparena_setops@.  It's usual to only do this when you've
- * attached your extra state to the end of the @mparena@ structure.
- */
-
-typedef struct mparena_ops {
-  void *(*alloc)(mparena */*a*/, size_t /*sz*/);
-  void (*free)(mparena */*a*/, void */*p*/);
-} mparena_ops;
-
-/*----- Magical constants -------------------------------------------------*/
+/*----- Standard arenas ---------------------------------------------------*/
 
-#define MPARENA_GLOBAL ((mparena *)0)
+extern mparena mparena_global;
+#define MPARENA_GLOBAL (&mparena_global)
 
-extern mparena_ops mparena_defaultops;
+extern mparena mparena_secure;
+#define MPARENA_SECURE (&mparena_secure)
 
 /*----- Functions provided ------------------------------------------------*/
 
@@ -109,19 +102,19 @@ extern mparena_ops mparena_defaultops;
 
 extern void mparena_create(mparena */*a*/);
 
-#define MPARENA_INIT { 0, 0, &mparena_defaultops }
+#define MPARENA_INIT { 0, 0, &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.
  */
 
-extern mparena_ops *mparena_setops(mparena */*a*/, mparena_ops */*ops*/);
+extern void mparena_setarena(mparena */*a*/, arena */*aa*/);
 
 /* --- @mparena_destroy@ --- *
  *
@@ -131,7 +124,8 @@ extern mparena_ops *mparena_setops(mparena */*a*/, mparena_ops */*ops*/);
  *
  * Use:                Frees an MP arena, and all the vectors held within it.  The
  *             blocks which are currently allocated can be freed into some
- *             other arena.
+ *             other MP arena, as long as the underlying arenas are the
+ *             same.
  */
 
 extern void mparena_destroy(mparena */*a*/);