/* -*-c-*-
*
- * $Id: mparena.c,v 1.1 1999/11/17 18:02:16 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.
+ *
* Revision 1.1 1999/11/17 18:02:16 mdw
* New multiprecision integer arithmetic suite.
*
#include "mparena.h"
+/*----- 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 -------------------------------------------------*/
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 --------------------------------------------------*/
-static mparena arena = { 0, &mparena_defops };
+#ifdef MPARENA_DEBUG
+ static FILE *debugfp = 0;
+
+# define MPARENA_OPENFILE do { \
+ if (!debugfp) { \
+ if ((debugfp = fopen(MPARENA_DEBUG, "w")) == 0) { \
+ fprintf(stderr, "couldn't open debug output file\n"); \
+ exit(EXIT_FAILURE); \
+ } \
+ } \
+ } while (0)
+
+#endif
+
+static mparena arena = MPARENA_INIT;
#define MPARENA_RESOLVE(a) do { \
if ((a) == MPARENA_GLOBAL) \
* Use: Recursively dumps out the allocation tree.
*/
+#ifdef MPARENA_DEBUG
+
static void tdump(mparena_node *n)
{
if (!n)
- putchar('*');
+ putc('*', debugfp);
else {
- putchar('(');
+ putc('(', debugfp);
tdump(n->left);
- printf(", %u, ", n->v[0]);
+ fprintf(debugfp, ", %u, ", n->v[0]);
tdump(n->right);
- putchar(')');
+ putc(')', debugfp);
}
}
+#endif
+
/* --- @mparena_create@ --- *
*
* Arguments: @mparena *a@ = pointer to arena block
void mparena_create(mparena *a)
{
a->root = 0;
- a->ops = &mparena_defops;
+ a->n = 0;
+ a->ops = &mparena_defaultops;
}
/* --- @mparena_setops@ --- *
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
* digits.
*/
+#ifdef MPARENA_TRIVIAL
+
+mpw *mpalloc(mparena *a, size_t sz)
+{
+ MPARENA_RESOLVE(a);
+ return (a->ops->alloc(a, MPWS(sz)));
+}
+
+#else
+
mpw *mpalloc(mparena *a, size_t sz)
{
mparena_node **nn, *n;
MPARENA_RESOLVE(a);
nn = &a->root;
-#ifdef notdef
- printf("*** alloc %u\n", sz);
- tdump(a->root); putchar('\n');
+#ifdef MPARENA_DEBUG
+ MPARENA_OPENFILE;
+ fprintf(debugfp, "alloc %u\n before: ", sz);
+ tdump(a->root); putc('\n', debugfp);
#endif
/* --- First, find a block which is big enough --- */
again:
n = *nn;
if (!n) {
+#ifdef MPARENA_DEBUG
+ fputs(" failed\n", debugfp);
+#endif
v = a->ops->alloc(a, MPWS(sz + 1));
v[0] = sz;
+ a->n++;
return (v + 1);
}
if (n->v[0] < sz) {
/* --- If the block we've got is still too large, start digging --- */
- if (n->v[0] >= sz * 2) {
+ if (n->v[0] > sz * 2) {
nn = &n->left;
goto again;
}
p->left = left;
}
+#ifdef MPARENA_DEBUG
+ fputs(" after: ", debugfp);
+ tdump(a->root); putc('\n', debugfp);
+#endif
+
/* --- Get rid of this node now --- */
DESTROY(n);
+ a->n++;
return (v + 1);
}
+#endif
+
/* --- @mpfree@ --- *
*
* Arguments: @mparena *a@ = pointer to arena block
*
* 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);
+}
+
+#else
+
void mpfree(mparena *a, mpw *v)
{
mparena_node **nn, *n;
size_t sz = *--v;
MPARENA_RESOLVE(a);
- nn = &a->root;
+#ifdef MPARENA_DEBUG
+ MPARENA_OPENFILE;
+ fprintf(debugfp, "free %u\n before: ", sz);
+ tdump(a->root); putc('\n', debugfp);
+#endif
+
+ nn = &a->root;
while (*nn) {
n = *nn;
if (n->v[0] > sz)
n->left = n->right = 0;
n->v = v;
*nn = n;
+ a->n--;
-#ifdef notdef
- printf("*** free %u\n", sz);
- tdump(a->root); putchar('\n');
+#ifdef MPARENA_DEBUG
+ fputs(" after: ", debugfp);
+ tdump(a->root); putc('\n', debugfp);
#endif
}
+#endif
+
/*----- That's all, folks -------------------------------------------------*/