3 * $Id: mp-mem.c,v 1.6 2004/04/03 03:30:22 mdw Exp $
5 * Memory management for multiprecision numbers
7 * (c) 1999 Straylight/Edgeware
10 /*----- Licensing notice --------------------------------------------------*
12 * This file is part of Catacomb.
14 * Catacomb is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU Library General Public License as
16 * published by the Free Software Foundation; either version 2 of the
17 * License, or (at your option) any later version.
19 * Catacomb is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU Library General Public License for more details.
24 * You should have received a copy of the GNU Library General Public
25 * License along with Catacomb; if not, write to the Free
26 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
30 /*----- Revision history --------------------------------------------------*
33 * Revision 1.6 2004/04/03 03:30:22 mdw
34 * Fix long-standing stupidity in @mp_dest@.
36 * Revision 1.5 2001/06/16 12:57:00 mdw
37 * Implement some missing functions.
39 * Revision 1.4 2001/02/03 12:00:29 mdw
40 * Now @mp_drop@ checks its argument is non-NULL before attempting to free
41 * it. Note that the macro version @MP_DROP@ doesn't do this.
43 * Revision 1.3 2000/06/17 11:45:09 mdw
44 * Major memory management overhaul. Added arena support. Use the secure
45 * arena for secret integers. Replace and improve the MP management macros
46 * (e.g., replace MP_MODIFY by MP_DEST).
48 * Revision 1.2 1999/12/10 23:19:02 mdw
49 * Improve error-checking.
51 * Revision 1.1 1999/11/17 18:02:16 mdw
52 * New multiprecision integer arithmetic suite.
56 /*----- Header files ------------------------------------------------------*/
63 /*----- Main code ---------------------------------------------------------*/
67 * Arguments: @size_t sz@ = size of vector required
68 * @unsigned f@ = flags to set
70 * Returns: Pointer to a new MP structure.
72 * Use: Allocates a new multiprecision integer. The data space is
73 * allocated from either the standard global or secret arena,
74 * depending on the initial flags requested.
77 mp *mp_new(size_t sz, unsigned f)
80 m->a = (f & MP_BURN) ? MPARENA_SECURE : MPARENA_GLOBAL;
81 m->v = mpalloc(m->a, sz);
84 m->f = f & ~(MP_CONST | MP_DESTROYED);
89 /* --- @mp_create@ --- *
91 * Arguments: @size_t sz@ = size of vector required
93 * Returns: Pointer to pristine new MP structure with enough memory
96 * Use: Creates a new multiprecision integer, initially zero. The
97 * integer has a single reference.
100 mp *mp_create(size_t sz)
103 m->v = mpalloc(MPARENA_GLOBAL, sz);
106 m->a = MPARENA_GLOBAL;
112 /* --- @mp_createsecure@ --- *
114 * Arguments: @size_t sz@ = size of vector required
116 * Returns: Pointer to pristine new MP structure with enough memory
119 * Use: Creates a new multiprecision integer with indeterminate
120 * contents. The integer has a single reference. The integer's
121 * data space is allocated from the secure arena. Its burn flag
125 mp *mp_createsecure(size_t sz)
128 m->v = mpalloc(MPARENA_SECURE, sz);
131 m->a = MPARENA_SECURE;
132 m->f = MP_UNDEF | MP_BURN;
137 /* --- @mp_build@ --- *
139 * Arguments: @mp *m@ = pointer to an MP block to fill in
140 * @mpw *v@ = pointer to a word array
141 * @mpw *vl@ = pointer just past end of array
145 * Use: Creates a multiprecision integer representing some smallish
146 * number. You must provide storage for the number and dispose
147 * of it when you've finished with it. The number is marked as
148 * constant while it exists.
151 void mp_build(mp *m, mpw *v, mpw *vl)
160 /* --- @mp_destroy@ --- *
162 * Arguments: @mp *m@ = pointer to a multiprecision integer
166 * Use: Destroys a multiprecision integer. The reference count isn't
167 * checked. Don't use this function if you don't know what
168 * you're doing: use @mp_drop@ instead.
171 void mp_destroy(mp *m)
173 assert(((void)"Destroying a free integer", !(m->f & MP_DESTROYED)));
174 assert(((void)"Attempted to destroy a constant", !(m->f & MP_CONST)));
176 memset(m->v, 0, MPWS(m->sz));
178 m->f |= MP_DESTROYED;
182 /* --- @mp_copy@ --- *
184 * Arguments: @mp *m@ = pointer to a multiprecision integer
186 * Returns: A copy of the given multiprecision integer.
188 * Use: Copies the given integer. In fact you just get another
189 * reference to the same old one again.
192 mp *mp_copy(mp *m) { return MP_COPY(m); }
194 /* --- @mp_drop@ --- *
196 * Arguments: @mp *m@ = pointer to a multiprecision integer
200 * Use: Drops a reference to an integer which isn't wanted any more.
201 * If there are no more references, the integer is destroyed.
204 void mp_drop(mp *m) { if (m) MP_DROP(m); }
206 /* --- @mp_split@ --- *
208 * Arguments: @mp *m@ = pointer to a multiprecision integer
210 * Returns: A reference to the same integer, possibly with a different
213 * Use: Splits off a modifiable version of the integer referred to.
216 mp *mp_split(mp *m) { MP_SPLIT(m); return (m); }
218 /* --- @mp_resize@ --- *
220 * Arguments: @mp *m@ = pointer to a multiprecision integer
221 * @size_t sz@ = new size
225 * Use: Changes an integer's size. The length and value are not
226 * changed. It is an error to
229 void mp_resize(mp *m, size_t sz) { MP_RESIZE(m, sz); }
231 /* --- @mp_ensure@ --- *
233 * Arguments: @mp *m@ = pointer to a multiprecision integer
234 * @size_t sz@ = required length
238 * Use: Changes an integer's length. If there is not enough space
239 * allocated for the new length then the size is increased. It
242 void mp_ensure(mp *m, size_t sz) { MP_ENSURE(m, sz); }
244 /* --- @mp_dest@ --- *
246 * Arguments: @mp *m@ = a suggested destination integer
247 * @size_t sz@ = size required for result, in digits
248 * @unsigned f@ = various flags
250 * Returns: A pointer to an appropriate destination.
252 * Use: Converts a suggested destination into a real destination with
253 * the required properties. If the real destination is @d@,
254 * then the following properties will hold:
256 * * @d@ will have exactly one reference.
258 * * If @m@ is not @MP_NEW@, then the contents of @m@ will not
259 * change, unless @f@ has the @MP_UNDEF@ flag set.
261 * * If @m@ is not @MP_NEW@, then he reference count of @m@ on
262 * entry is equal to the sum of the counts of @d@ and @m@ on
265 * * The size of @d@ will be at least @sz@.
267 * * If @f@ has the @MP_BURN@ flag set, then @d@ will be
268 * allocated from @MPARENA_SECURE@.
270 * Understanding this function is crucial to using Catacomb's
271 * multiprecision integer library effectively.
274 mp *mp_dest(mp *m, size_t sz, unsigned f)
276 /* --- If no destination, make one --- */
279 m = mp_new(sz, f | MP_UNDEF | MP_BURN);
280 else if (m == MP_NEW)
281 m = mp_new(sz, f | MP_UNDEF);
283 size_t len = MP_LEN(m);
284 unsigned undef = (m->f | f) & MP_UNDEF;
286 /* --- If the value must be preserved, the block can't shrink --- */
288 if (!undef && sz < len)
291 /* --- Otherwise check whether the destination is suitable --- */
293 if (m->ref > 1 || (m->f & MP_CONST) ||
294 sz > m->sz || !((f & ~m->f) & MP_BURN)) {
296 /* --- No -- allocate a new buffer --- *
298 * The buffer must be secure if (a) the caller requested a secure
299 * buffer, or (b) the old buffer is secure and I'm not allowed to
300 * discard the old contents.
306 if ((f & MP_BURN) || (!undef && (m->f & MP_BURN)))
312 /* --- Copy the data over --- */
315 memcpy(v, m->v, MPWS(len));
317 memset(v + len, 0, MPWS(sz - len));
320 /* --- If @m@ has other references, make a new node --- *
322 * Otherwise dispose of the old buffer.
325 if (!(m->f & MP_CONST) && m->ref == 1) {
327 memset(m->v, 0, MPWS(m->sz));
337 /* --- Fix up the node --- */
342 m->f = ((m->f & ~(MP_CONST | MP_BURN)) |
343 (f & (MP_BURN | MP_UNDEF)));
347 /* --- If the number is growing in its buffer, fix it up --- */
351 memset(m->vl, 0, MPWS(sz - len));
361 /*----- That's all, folks -------------------------------------------------*/