X-Git-Url: https://git.distorted.org.uk/u/mdw/catacomb/blobdiff_plain/f1140c41e9297d14386ee18fbed7a9686d223024..813390c45f438f411662b1a55678e63f11681eb4:/mp-mem.c diff --git a/mp-mem.c b/mp-mem.c index f35fd58..fcb6c4d 100644 --- a/mp-mem.c +++ b/mp-mem.c @@ -1,13 +1,13 @@ /* -*-c-*- * - * $Id: mp-mem.c,v 1.4 2001/02/03 12:00:29 mdw Exp $ + * $Id: mp-mem.c,v 1.8 2004/04/08 16:17:32 mdw Exp $ * * Memory management for multiprecision numbers * * (c) 1999 Straylight/Edgeware */ -/*----- Licensing notice --------------------------------------------------* +/*----- Licensing notice --------------------------------------------------* * * This file is part of Catacomb. * @@ -15,38 +15,18 @@ * 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: mp-mem.c,v $ - * Revision 1.4 2001/02/03 12:00:29 mdw - * Now @mp_drop@ checks its argument is non-NULL before attempting to free - * it. Note that the macro version @MP_DROP@ doesn't do this. - * - * Revision 1.3 2000/06/17 11:45:09 mdw - * Major memory management overhaul. Added arena support. Use the secure - * arena for secret integers. Replace and improve the MP management macros - * (e.g., replace MP_MODIFY by MP_DEST). - * - * Revision 1.2 1999/12/10 23:19:02 mdw - * Improve error-checking. - * - * Revision 1.1 1999/11/17 18:02:16 mdw - * New multiprecision integer arithmetic suite. - * - */ - /*----- Header files ------------------------------------------------------*/ #include @@ -118,7 +98,7 @@ mp *mp_create(size_t sz) mp *mp_createsecure(size_t sz) { - mp *m = CREATE(mp); + mp *m = CREATE(mp); m->v = mpalloc(MPARENA_SECURE, sz); m->vl = m->v + sz; m->sz = sz; @@ -147,6 +127,7 @@ void mp_build(mp *m, mpw *v, mpw *vl) m->v = v; m->vl = vl; m->sz = vl - v; + m->a = MPARENA_GLOBAL; m->f = MP_CONST; m->ref = 1; } @@ -207,19 +188,7 @@ void mp_drop(mp *m) { if (m) MP_DROP(m); } * Use: Splits off a modifiable version of the integer referred to. */ -mp *mp_split(mp *m) -{ -/* if ((m->f & MP_CONST) || m->ref > 1) { */ -/* size_t len = MP_LEN(m); */ -/* mp *mm = mp_new(len, m->f); */ -/* if (!(m->f & MP_UNDEF)) */ -/* memcpy(mm->v, m->v, MPWS(len)); */ -/* m->ref--; */ -/* m = mm; */ -/* } */ - MP_SPLIT(m); - return (m); -} +mp *mp_split(mp *m) { MP_SPLIT(m); return (m); } /* --- @mp_resize@ --- * * @@ -229,25 +198,10 @@ mp *mp_split(mp *m) * Returns: --- * * Use: Changes an integer's size. The length and value are not - * changed. It is an error to + * changed. It is an error to */ -void mp_resize(mp *m, size_t sz) -{ -/* mparena *a = (m->f & MP_BURN) ? MPARENA_SECURE : MPARENA_GLOBAL; */ -/* mpw *v; */ -/* size_t len = MP_LEN(m); */ -/* assert(((void)"can't make size less than length", sz >= len)); */ -/* v = mpalloc(a, sz); */ -/* if (!(m->f & MP_UNDEF)) */ -/* memcpy(v, m->v, MPWS(len)); */ -/* if (m->f & MP_BURN) */ -/* memset(m->v, 0, MPWS(m->sz)); */ -/* mpfree(m->a, m->v); */ -/* m->a = a; */ -/* m->v = v; */ -/* m->vl = v + len; */ -} +void mp_resize(mp *m, size_t sz) { MP_RESIZE(m, sz); } /* --- @mp_ensure@ --- * * @@ -260,17 +214,7 @@ void mp_resize(mp *m, size_t sz) * allocated for the new length then the size is increased. It */ -void mp_ensure(mp *m, size_t sz) -{ -/* size_t len = MP_LEN(m); */ -/* if (sz >= len) { */ -/* if (sz > m->sz) */ -/* mp_resize(m, sz); */ -/* if (!(m->f & MP_UNDEF) && sz > len) */ -/* memset(m->vl, 0, MPWS(sz - len)); */ -/* m->vl = m->v + sz; */ -/* } */ -} +void mp_ensure(mp *m, size_t sz) { MP_ENSURE(m, sz); } /* --- @mp_dest@ --- * * @@ -322,7 +266,7 @@ mp *mp_dest(mp *m, size_t sz, unsigned f) /* --- Otherwise check whether the destination is suitable --- */ if (m->ref > 1 || (m->f & MP_CONST) || - m->sz > len || !((f & ~m->f) & MP_BURN)) { + sz > m->sz || ((f & ~m->f) & MP_BURN)) { /* --- No -- allocate a new buffer --- * * @@ -330,7 +274,7 @@ mp *mp_dest(mp *m, size_t sz, unsigned f) * buffer, or (b) the old buffer is secure and I'm not allowed to * discard the old contents. */ - + mparena *a; mpw *v;