X-Git-Url: https://git.distorted.org.uk/u/mdw/catacomb/blobdiff_plain/d3409d5ecf2492cff862616de72a580d1a8e8dc0..813390c45f438f411662b1a55678e63f11681eb4:/mparena.c diff --git a/mparena.c b/mparena.c index 4e02454..c726912 100644 --- a/mparena.c +++ b/mparena.c @@ -1,13 +1,13 @@ /* -*-c-*- * - * $Id: mparena.c,v 1.1 1999/11/17 18:02:16 mdw Exp $ + * $Id$ * * Allocation and freeing of MP buffers * * (c) 1999 Straylight/Edgeware */ -/*----- Licensing notice --------------------------------------------------* +/*----- Licensing notice --------------------------------------------------* * * This file is part of Catacomb. * @@ -15,52 +15,69 @@ * it under the terms of the GNU Library General Public License as * published by the Free Software Foundation; either version 2 of the * License, or (at your option) any later version. - * + * * Catacomb is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU Library General Public License for more details. - * + * * You should have received a copy of the GNU Library General Public * License along with Catacomb; if not, write to the Free * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, * MA 02111-1307, USA. */ -/*----- Revision history --------------------------------------------------* - * - * $Log: mparena.c,v $ - * Revision 1.1 1999/11/17 18:02:16 mdw - * New multiprecision integer arithmetic suite. - * - */ - /*----- Header files ------------------------------------------------------*/ #include #include #include -#include +#include +#include #include #include "mparena.h" -/*----- Default allocator -------------------------------------------------*/ +/*----- Tweakables --------------------------------------------------------*/ + +/* --- @MPARENA_TRIVIAL@ --- * + * + * Make the allocator a passthrough. It immediately calls the underlying + * allocation functions rather than attempting to keep track of blocks + * itself. + */ -static void *defalloc(mparena *a, size_t sz) { return xmalloc(sz); } -static void deffree(mparena *a, void *p) { free(p); } +#define MPARENA_TRIVIAL -mparena_ops mparena_defops = { defalloc, deffree }; +/* --- @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" */ /*----- 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 + +/*----- 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 ---------------------------------------------------------*/ @@ -73,19 +90,23 @@ static mparena arena = { 0, &mparena_defops }; * 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 @@ -99,28 +120,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@ --- * * @@ -135,7 +149,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) @@ -149,6 +163,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 @@ -160,17 +189,32 @@ void mparena_destroy(mparena *a) * digits. */ +#ifdef MPARENA_TRIVIAL + +mpw *mpalloc(mparena *a, size_t sz) +{ + mpw *v; + if (!sz) return (0); + a->n++; + v = A_ALLOC(a->a, MPWS(sz)); + if (!v) + THROW(EXC_NOMEM); + return (v); +} + +#else + mpw *mpalloc(mparena *a, size_t sz) { mparena_node **nn, *n; mpw *v; - 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 --- */ @@ -178,8 +222,13 @@ mpw *mpalloc(mparena *a, size_t sz) again: n = *nn; if (!n) { - v = a->ops->alloc(a, MPWS(sz + 1)); +#ifdef MPARENA_DEBUG + fputs(" failed\n", debugfp); +#endif + 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) { @@ -196,7 +245,7 @@ again: /* --- 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; } @@ -219,12 +268,20 @@ 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 @@ -232,18 +289,32 @@ 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) +{ + if (!v) return; + a->n--; + A_FREE(a->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) @@ -256,11 +327,14 @@ void mpfree(mparena *a, mpw *v) 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 -------------------------------------------------*/