X-Git-Url: https://git.distorted.org.uk/u/mdw/catacomb/blobdiff_plain/97d21728c4cfdfc94e3744c2eb5a323e06635124..6ec3a4cf4aaa7cd375e1aa18f85861986259b8e5:/mparena.c diff --git a/mparena.c b/mparena.c index e7c12cd..c726912 100644 --- a/mparena.c +++ b/mparena.c @@ -1,13 +1,13 @@ /* -*-c-*- * - * $Id: mparena.c,v 1.4 1999/12/10 23:28:52 mdw Exp $ + * $Id$ * * Allocation and freeing of MP buffers * * (c) 1999 Straylight/Edgeware */ -/*----- Licensing notice --------------------------------------------------* +/*----- Licensing notice --------------------------------------------------* * * This file is part of Catacomb. * @@ -15,42 +15,26 @@ * 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.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. - * - */ - /*----- Header files ------------------------------------------------------*/ #include #include #include -#include +#include +#include #include #include "mparena.h" @@ -64,7 +48,7 @@ * itself. */ -/* #define MPARENA_TRIVIAL */ +#define MPARENA_TRIVIAL /* --- @MPARENA_DEBUG@ --- * * @@ -74,13 +58,6 @@ /* #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 +74,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 +121,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 +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) @@ -208,9 +175,8 @@ void mparena_destroy(mparena *a) unsigned mparena_count(mparena *a) { - MPARENA_RESOLVE(a); return (a->n); -} +} /* --- @mpalloc@ --- * * @@ -227,8 +193,13 @@ unsigned mparena_count(mparena *a) mpw *mpalloc(mparena *a, size_t sz) { - MPARENA_RESOLVE(a); - return (a->ops->alloc(a, MPWS(sz))); + mpw *v; + if (!sz) return (0); + a->n++; + v = A_ALLOC(a->a, MPWS(sz)); + if (!v) + THROW(EXC_NOMEM); + return (v); } #else @@ -238,12 +209,11 @@ mpw *mpalloc(mparena *a, size_t sz) mparena_node **nn, *n; mpw *v; - MPARENA_RESOLVE(a); nn = &a->root; #ifdef MPARENA_DEBUG MPARENA_OPENFILE; - fprintf(debugfp, "alloc %u\n before: ", sz); + fprintf(debugfp, "alloc %u\n before: ", sz); tdump(a->root); putc('\n', debugfp); #endif @@ -255,7 +225,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 +296,9 @@ again: void mpfree(mparena *a, mpw *v) { - MPARENA_RESOLVE(a); - a->ops->free(a, v); + if (!v) return; + a->n--; + A_FREE(a->a, v); } #else @@ -336,8 +308,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);