3 * $Id: mp-mem.c,v 1.7 2004/04/08 01:36:15 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 /*----- Header files ------------------------------------------------------*/
37 /*----- Main code ---------------------------------------------------------*/
41 * Arguments: @size_t sz@ = size of vector required
42 * @unsigned f@ = flags to set
44 * Returns: Pointer to a new MP structure.
46 * Use: Allocates a new multiprecision integer. The data space is
47 * allocated from either the standard global or secret arena,
48 * depending on the initial flags requested.
51 mp *mp_new(size_t sz, unsigned f)
54 m->a = (f & MP_BURN) ? MPARENA_SECURE : MPARENA_GLOBAL;
55 m->v = mpalloc(m->a, sz);
58 m->f = f & ~(MP_CONST | MP_DESTROYED);
63 /* --- @mp_create@ --- *
65 * Arguments: @size_t sz@ = size of vector required
67 * Returns: Pointer to pristine new MP structure with enough memory
70 * Use: Creates a new multiprecision integer, initially zero. The
71 * integer has a single reference.
74 mp *mp_create(size_t sz)
77 m->v = mpalloc(MPARENA_GLOBAL, sz);
80 m->a = MPARENA_GLOBAL;
86 /* --- @mp_createsecure@ --- *
88 * Arguments: @size_t sz@ = size of vector required
90 * Returns: Pointer to pristine new MP structure with enough memory
93 * Use: Creates a new multiprecision integer with indeterminate
94 * contents. The integer has a single reference. The integer's
95 * data space is allocated from the secure arena. Its burn flag
99 mp *mp_createsecure(size_t sz)
102 m->v = mpalloc(MPARENA_SECURE, sz);
105 m->a = MPARENA_SECURE;
106 m->f = MP_UNDEF | MP_BURN;
111 /* --- @mp_build@ --- *
113 * Arguments: @mp *m@ = pointer to an MP block to fill in
114 * @mpw *v@ = pointer to a word array
115 * @mpw *vl@ = pointer just past end of array
119 * Use: Creates a multiprecision integer representing some smallish
120 * number. You must provide storage for the number and dispose
121 * of it when you've finished with it. The number is marked as
122 * constant while it exists.
125 void mp_build(mp *m, mpw *v, mpw *vl)
134 /* --- @mp_destroy@ --- *
136 * Arguments: @mp *m@ = pointer to a multiprecision integer
140 * Use: Destroys a multiprecision integer. The reference count isn't
141 * checked. Don't use this function if you don't know what
142 * you're doing: use @mp_drop@ instead.
145 void mp_destroy(mp *m)
147 assert(((void)"Destroying a free integer", !(m->f & MP_DESTROYED)));
148 assert(((void)"Attempted to destroy a constant", !(m->f & MP_CONST)));
150 memset(m->v, 0, MPWS(m->sz));
152 m->f |= MP_DESTROYED;
156 /* --- @mp_copy@ --- *
158 * Arguments: @mp *m@ = pointer to a multiprecision integer
160 * Returns: A copy of the given multiprecision integer.
162 * Use: Copies the given integer. In fact you just get another
163 * reference to the same old one again.
166 mp *mp_copy(mp *m) { return MP_COPY(m); }
168 /* --- @mp_drop@ --- *
170 * Arguments: @mp *m@ = pointer to a multiprecision integer
174 * Use: Drops a reference to an integer which isn't wanted any more.
175 * If there are no more references, the integer is destroyed.
178 void mp_drop(mp *m) { if (m) MP_DROP(m); }
180 /* --- @mp_split@ --- *
182 * Arguments: @mp *m@ = pointer to a multiprecision integer
184 * Returns: A reference to the same integer, possibly with a different
187 * Use: Splits off a modifiable version of the integer referred to.
190 mp *mp_split(mp *m) { MP_SPLIT(m); return (m); }
192 /* --- @mp_resize@ --- *
194 * Arguments: @mp *m@ = pointer to a multiprecision integer
195 * @size_t sz@ = new size
199 * Use: Changes an integer's size. The length and value are not
200 * changed. It is an error to
203 void mp_resize(mp *m, size_t sz) { MP_RESIZE(m, sz); }
205 /* --- @mp_ensure@ --- *
207 * Arguments: @mp *m@ = pointer to a multiprecision integer
208 * @size_t sz@ = required length
212 * Use: Changes an integer's length. If there is not enough space
213 * allocated for the new length then the size is increased. It
216 void mp_ensure(mp *m, size_t sz) { MP_ENSURE(m, sz); }
218 /* --- @mp_dest@ --- *
220 * Arguments: @mp *m@ = a suggested destination integer
221 * @size_t sz@ = size required for result, in digits
222 * @unsigned f@ = various flags
224 * Returns: A pointer to an appropriate destination.
226 * Use: Converts a suggested destination into a real destination with
227 * the required properties. If the real destination is @d@,
228 * then the following properties will hold:
230 * * @d@ will have exactly one reference.
232 * * If @m@ is not @MP_NEW@, then the contents of @m@ will not
233 * change, unless @f@ has the @MP_UNDEF@ flag set.
235 * * If @m@ is not @MP_NEW@, then he reference count of @m@ on
236 * entry is equal to the sum of the counts of @d@ and @m@ on
239 * * The size of @d@ will be at least @sz@.
241 * * If @f@ has the @MP_BURN@ flag set, then @d@ will be
242 * allocated from @MPARENA_SECURE@.
244 * Understanding this function is crucial to using Catacomb's
245 * multiprecision integer library effectively.
248 mp *mp_dest(mp *m, size_t sz, unsigned f)
250 /* --- If no destination, make one --- */
253 m = mp_new(sz, f | MP_UNDEF | MP_BURN);
254 else if (m == MP_NEW)
255 m = mp_new(sz, f | MP_UNDEF);
257 size_t len = MP_LEN(m);
258 unsigned undef = (m->f | f) & MP_UNDEF;
260 /* --- If the value must be preserved, the block can't shrink --- */
262 if (!undef && sz < len)
265 /* --- Otherwise check whether the destination is suitable --- */
267 if (m->ref > 1 || (m->f & MP_CONST) ||
268 sz > m->sz || !((f & ~m->f) & MP_BURN)) {
270 /* --- No -- allocate a new buffer --- *
272 * The buffer must be secure if (a) the caller requested a secure
273 * buffer, or (b) the old buffer is secure and I'm not allowed to
274 * discard the old contents.
280 if ((f & MP_BURN) || (!undef && (m->f & MP_BURN)))
286 /* --- Copy the data over --- */
289 memcpy(v, m->v, MPWS(len));
291 memset(v + len, 0, MPWS(sz - len));
294 /* --- If @m@ has other references, make a new node --- *
296 * Otherwise dispose of the old buffer.
299 if (!(m->f & MP_CONST) && m->ref == 1) {
301 memset(m->v, 0, MPWS(m->sz));
311 /* --- Fix up the node --- */
316 m->f = ((m->f & ~(MP_CONST | MP_BURN)) |
317 (f & (MP_BURN | MP_UNDEF)));
321 /* --- If the number is growing in its buffer, fix it up --- */
325 memset(m->vl, 0, MPWS(sz - len));
335 /*----- That's all, folks -------------------------------------------------*/