/* -*-c-*-
*
- * $Id: mparena.c,v 1.3 1999/11/22 13:58:00 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.
*
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 --------------------------------------------------*/
#endif
-static mparena arena = { 0, &mparena_defops };
+static mparena arena = MPARENA_INIT;
#define MPARENA_RESOLVE(a) do { \
if ((a) == MPARENA_GLOBAL) \
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
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 --- */
#endif
v = a->ops->alloc(a, MPWS(sz + 1));
v[0] = sz;
+ a->n++;
return (v + 1);
}
if (n->v[0] < sz) {
/* --- Get rid of this node now --- */
DESTROY(n);
+ a->n++;
return (v + 1);
}
*
* 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
n->left = n->right = 0;
n->v = v;
*nn = n;
+ a->n--;
#ifdef MPARENA_DEBUG
fputs(" after: ", debugfp);
/* -*-c-*-
*
- * $Id: mparena.h,v 1.1 1999/11/17 18:02:16 mdw Exp $
+ * $Id: mparena.h,v 1.2 1999/12/10 23:28:59 mdw Exp $
*
* Allocation and freeing of MP buffers
*
/*----- Revision history --------------------------------------------------*
*
* $Log: mparena.h,v $
+ * Revision 1.2 1999/12/10 23:28:59 mdw
+ * Memory allocation counting.
+ *
* Revision 1.1 1999/11/17 18:02:16 mdw
* New multiprecision integer arithmetic suite.
*
*/
-#ifndef MPARENA_H
-#define MPARENA_H
+#ifndef CATACOMB_MPARENA_H
+#define CATACOMB_MPARENA_H
#ifdef __cplusplus
extern "C" {
/*----- Header files ------------------------------------------------------*/
-#ifndef MPW_H
+#ifndef CATACOMB_MPW_H
# include "mpw.h"
#endif
typedef struct mparena {
mparena_node *root;
+ unsigned n;
struct mparena_ops *ops;
} mparena;
extern void mparena_create(mparena */*a*/);
-#define MPARENA_INIT { 0, &mparena_defaultops }
+#define MPARENA_INIT { 0, 0, &mparena_defaultops }
/* --- @mparena_setops@ --- *
*
extern void mparena_destroy(mparena */*a*/);
+/* --- @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.
+ */
+
+extern unsigned mparena_count(mparena */*a*/);
+
/* --- @mpalloc@ --- *
*
* 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.
*/
extern void mpfree(mparena */*a*/, mpw */*v*/);