3 * Memory management for multiprecision numbers
5 * (c) 1999 Straylight/Edgeware
8 /*----- Licensing notice --------------------------------------------------*
10 * This file is part of Catacomb.
12 * Catacomb is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU Library General Public License as
14 * published by the Free Software Foundation; either version 2 of the
15 * License, or (at your option) any later version.
17 * Catacomb is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU Library General Public License for more details.
22 * You should have received a copy of the GNU Library General Public
23 * License along with Catacomb; if not, write to the Free
24 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
28 /*----- Header files ------------------------------------------------------*/
35 /*----- Main code ---------------------------------------------------------*/
39 * Arguments: @size_t sz@ = size of vector required
40 * @unsigned f@ = flags to set
42 * Returns: Pointer to a new MP structure.
44 * Use: Allocates a new multiprecision integer. The data space is
45 * allocated from either the standard global or secret arena,
46 * depending on the initial flags requested.
49 mp *mp_new(size_t sz, unsigned f)
52 m->a = (f & MP_BURN) ? MPARENA_SECURE : MPARENA_GLOBAL;
53 m->v = mpalloc(m->a, sz);
56 m->f = f & ~(MP_CONST | MP_DESTROYED);
61 /* --- @mp_create@ --- *
63 * Arguments: @size_t sz@ = size of vector required
65 * Returns: Pointer to pristine new MP structure with enough memory
68 * Use: Creates a new multiprecision integer, initially zero. The
69 * integer has a single reference.
72 mp *mp_create(size_t sz)
75 m->v = mpalloc(MPARENA_GLOBAL, sz);
78 m->a = MPARENA_GLOBAL;
84 /* --- @mp_createsecure@ --- *
86 * Arguments: @size_t sz@ = size of vector required
88 * Returns: Pointer to pristine new MP structure with enough memory
91 * Use: Creates a new multiprecision integer with indeterminate
92 * contents. The integer has a single reference. The integer's
93 * data space is allocated from the secure arena. Its burn flag
97 mp *mp_createsecure(size_t sz)
100 m->v = mpalloc(MPARENA_SECURE, sz);
103 m->a = MPARENA_SECURE;
104 m->f = MP_UNDEF | MP_BURN;
109 /* --- @mp_build@ --- *
111 * Arguments: @mp *m@ = pointer to an MP block to fill in
112 * @mpw *v@ = pointer to a word array
113 * @mpw *vl@ = pointer just past end of array
117 * Use: Creates a multiprecision integer representing some smallish
118 * number. You must provide storage for the number and dispose
119 * of it when you've finished with it. The number is marked as
120 * constant while it exists.
123 void mp_build(mp *m, mpw *v, mpw *vl)
128 m->a = MPARENA_GLOBAL;
133 /* --- @mp_destroy@ --- *
135 * Arguments: @mp *m@ = pointer to a multiprecision integer
139 * Use: Destroys a multiprecision integer. The reference count isn't
140 * checked. Don't use this function if you don't know what
141 * you're doing: use @mp_drop@ instead.
144 void mp_destroy(mp *m)
146 assert(((void)"Destroying a free integer", !(m->f & MP_DESTROYED)));
147 assert(((void)"Attempted to destroy a constant", !(m->f & MP_CONST)));
149 memset(m->v, 0, MPWS(m->sz));
151 m->f |= MP_DESTROYED;
155 /* --- @mp_copy@ --- *
157 * Arguments: @mp *m@ = pointer to a multiprecision integer
159 * Returns: A copy of the given multiprecision integer.
161 * Use: Copies the given integer. In fact you just get another
162 * reference to the same old one again.
165 mp *mp_copy(mp *m) { return MP_COPY(m); }
167 /* --- @mp_drop@ --- *
169 * Arguments: @mp *m@ = pointer to a multiprecision integer
173 * Use: Drops a reference to an integer which isn't wanted any more.
174 * If there are no more references, the integer is destroyed.
177 void mp_drop(mp *m) { if (m) MP_DROP(m); }
179 /* --- @mp_split@ --- *
181 * Arguments: @mp *m@ = pointer to a multiprecision integer
183 * Returns: A reference to the same integer, possibly with a different
186 * Use: Splits off a modifiable version of the integer referred to.
189 mp *mp_split(mp *m) { MP_SPLIT(m); return (m); }
191 /* --- @mp_resize@ --- *
193 * Arguments: @mp *m@ = pointer to a multiprecision integer
194 * @size_t sz@ = new size
198 * Use: Changes an integer's size. The length and value are not
199 * changed. It is an error to
202 void mp_resize(mp *m, size_t sz) { MP_RESIZE(m, sz); }
204 /* --- @mp_ensure@ --- *
206 * Arguments: @mp *m@ = pointer to a multiprecision integer
207 * @size_t sz@ = required length
211 * Use: Changes an integer's length. If there is not enough space
212 * allocated for the new length then the size is increased. It
215 void mp_ensure(mp *m, size_t sz) { MP_ENSURE(m, sz); }
217 /* --- @mp_dest@ --- *
219 * Arguments: @mp *m@ = a suggested destination integer
220 * @size_t sz@ = size required for result, in digits
221 * @unsigned f@ = various flags
223 * Returns: A pointer to an appropriate destination.
225 * Use: Converts a suggested destination into a real destination with
226 * the required properties. If the real destination is @d@,
227 * then the following properties will hold:
229 * * @d@ will have exactly one reference.
231 * * If @m@ is not @MP_NEW@, then the contents of @m@ will not
232 * change, unless @f@ has the @MP_UNDEF@ flag set.
234 * * If @m@ is not @MP_NEW@, then he reference count of @m@ on
235 * entry is equal to the sum of the counts of @d@ and @m@ on
238 * * The size of @d@ will be at least @sz@.
240 * * If @f@ has the @MP_BURN@ flag set, then @d@ will be
241 * allocated from @MPARENA_SECURE@.
243 * Understanding this function is crucial to using Catacomb's
244 * multiprecision integer library effectively.
247 mp *mp_dest(mp *m, size_t sz, unsigned f)
249 /* --- If no destination, make one --- */
252 m = mp_new(sz, f | MP_UNDEF | MP_BURN);
253 else if (m == MP_NEW)
254 m = mp_new(sz, f | MP_UNDEF);
256 size_t len = MP_LEN(m);
257 unsigned undef = (m->f | f) & MP_UNDEF;
259 /* --- If the value must be preserved, the block can't shrink --- */
261 if (!undef && sz < len)
264 /* --- Otherwise check whether the destination is suitable --- */
266 if (m->ref > 1 || (m->f & MP_CONST) ||
267 sz > m->sz || ((f & ~m->f) & MP_BURN)) {
269 /* --- No -- allocate a new buffer --- *
271 * The buffer must be secure if (a) the caller requested a secure
272 * buffer, or (b) the old buffer is secure and I'm not allowed to
273 * discard the old contents.
279 if ((f & MP_BURN) || (!undef && (m->f & MP_BURN)))
285 /* --- Copy the data over --- */
288 memcpy(v, m->v, MPWS(len));
290 memset(v + len, 0, MPWS(sz - len));
293 /* --- If @m@ has other references, make a new node --- *
295 * Otherwise dispose of the old buffer.
298 if (!(m->f & MP_CONST) && m->ref == 1) {
300 memset(m->v, 0, MPWS(m->sz));
310 /* --- Fix up the node --- */
315 m->f = ((m->f & ~(MP_CONST | MP_BURN)) |
316 (f & (MP_BURN | MP_UNDEF)));
320 /* --- If the number is growing in its buffer, fix it up --- */
324 memset(m->vl, 0, MPWS(sz - len));
334 /*----- That's all, folks -------------------------------------------------*/