3 * $Id: mp-mem.c,v 1.3 2000/06/17 11:45:09 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.3 2000/06/17 11:45:09 mdw
34 * Major memory management overhaul. Added arena support. Use the secure
35 * arena for secret integers. Replace and improve the MP management macros
36 * (e.g., replace MP_MODIFY by MP_DEST).
38 * Revision 1.2 1999/12/10 23:19:02 mdw
39 * Improve error-checking.
41 * Revision 1.1 1999/11/17 18:02:16 mdw
42 * New multiprecision integer arithmetic suite.
46 /*----- Header files ------------------------------------------------------*/
53 /*----- Main code ---------------------------------------------------------*/
57 * Arguments: @size_t sz@ = size of vector required
58 * @unsigned f@ = flags to set
60 * Returns: Pointer to a new MP structure.
62 * Use: Allocates a new multiprecision integer. The data space is
63 * allocated from either the standard global or secret arena,
64 * depending on the initial flags requested.
67 mp
*mp_new(size_t sz
, unsigned f
)
70 m
->a
= (f
& MP_BURN
) ? MPARENA_SECURE
: MPARENA_GLOBAL
;
71 m
->v
= mpalloc(m
->a
, sz
);
74 m
->f
= f
& ~(MP_CONST
| MP_DESTROYED
);
79 /* --- @mp_create@ --- *
81 * Arguments: @size_t sz@ = size of vector required
83 * Returns: Pointer to pristine new MP structure with enough memory
86 * Use: Creates a new multiprecision integer, initially zero. The
87 * integer has a single reference.
90 mp
*mp_create(size_t sz
)
93 m
->v
= mpalloc(MPARENA_GLOBAL
, sz
);
96 m
->a
= MPARENA_GLOBAL
;
102 /* --- @mp_createsecure@ --- *
104 * Arguments: @size_t sz@ = size of vector required
106 * Returns: Pointer to pristine new MP structure with enough memory
109 * Use: Creates a new multiprecision integer with indeterminate
110 * contents. The integer has a single reference. The integer's
111 * data space is allocated from the secure arena. Its burn flag
115 mp
*mp_createsecure(size_t sz
)
118 m
->v
= mpalloc(MPARENA_SECURE
, sz
);
121 m
->a
= MPARENA_SECURE
;
122 m
->f
= MP_UNDEF
| MP_BURN
;
127 /* --- @mp_build@ --- *
129 * Arguments: @mp *m@ = pointer to an MP block to fill in
130 * @mpw *v@ = pointer to a word array
131 * @mpw *vl@ = pointer just past end of array
135 * Use: Creates a multiprecision integer representing some smallish
136 * number. You must provide storage for the number and dispose
137 * of it when you've finished with it. The number is marked as
138 * constant while it exists.
141 void mp_build(mp
*m
, mpw
*v
, mpw
*vl
)
150 /* --- @mp_destroy@ --- *
152 * Arguments: @mp *m@ = pointer to a multiprecision integer
156 * Use: Destroys a multiprecision integer. The reference count isn't
157 * checked. Don't use this function if you don't know what
158 * you're doing: use @mp_drop@ instead.
161 void mp_destroy(mp
*m
)
163 assert(((void)"Destroying a free integer", !(m
->f
& MP_DESTROYED
)));
164 assert(((void)"Attempted to destroy a constant", !(m
->f
& MP_CONST
)));
166 memset(m
->v
, 0, MPWS(m
->sz
));
168 m
->f
|= MP_DESTROYED
;
172 /* --- @mp_copy@ --- *
174 * Arguments: @mp *m@ = pointer to a multiprecision integer
176 * Returns: A copy of the given multiprecision integer.
178 * Use: Copies the given integer. In fact you just get another
179 * reference to the same old one again.
182 mp
*mp_copy(mp
*m
) { return MP_COPY(m
); }
184 /* --- @mp_drop@ --- *
186 * Arguments: @mp *m@ = pointer to a multiprecision integer
190 * Use: Drops a reference to an integer which isn't wanted any more.
191 * If there are no more references, the integer is destroyed.
194 void mp_drop(mp
*m
) { MP_DROP(m
); }
196 /* --- @mp_split@ --- *
198 * Arguments: @mp *m@ = pointer to a multiprecision integer
200 * Returns: A reference to the same integer, possibly with a different
203 * Use: Splits off a modifiable version of the integer referred to.
208 /* if ((m->f & MP_CONST) || m->ref > 1) { */
209 /* size_t len = MP_LEN(m); */
210 /* mp *mm = mp_new(len, m->f); */
211 /* if (!(m->f & MP_UNDEF)) */
212 /* memcpy(mm->v, m->v, MPWS(len)); */
220 /* --- @mp_resize@ --- *
222 * Arguments: @mp *m@ = pointer to a multiprecision integer
223 * @size_t sz@ = new size
227 * Use: Changes an integer's size. The length and value are not
228 * changed. It is an error to
231 void mp_resize(mp
*m
, size_t sz
)
233 /* mparena *a = (m->f & MP_BURN) ? MPARENA_SECURE : MPARENA_GLOBAL; */
235 /* size_t len = MP_LEN(m); */
236 /* assert(((void)"can't make size less than length", sz >= len)); */
237 /* v = mpalloc(a, sz); */
238 /* if (!(m->f & MP_UNDEF)) */
239 /* memcpy(v, m->v, MPWS(len)); */
240 /* if (m->f & MP_BURN) */
241 /* memset(m->v, 0, MPWS(m->sz)); */
242 /* mpfree(m->a, m->v); */
245 /* m->vl = v + len; */
248 /* --- @mp_ensure@ --- *
250 * Arguments: @mp *m@ = pointer to a multiprecision integer
251 * @size_t sz@ = required length
255 * Use: Changes an integer's length. If there is not enough space
256 * allocated for the new length then the size is increased. It
259 void mp_ensure(mp
*m
, size_t sz
)
261 /* size_t len = MP_LEN(m); */
262 /* if (sz >= len) { */
263 /* if (sz > m->sz) */
264 /* mp_resize(m, sz); */
265 /* if (!(m->f & MP_UNDEF) && sz > len) */
266 /* memset(m->vl, 0, MPWS(sz - len)); */
267 /* m->vl = m->v + sz; */
271 /* --- @mp_dest@ --- *
273 * Arguments: @mp *m@ = a suggested destination integer
274 * @size_t sz@ = size required for result, in digits
275 * @unsigned f@ = various flags
277 * Returns: A pointer to an appropriate destination.
279 * Use: Converts a suggested destination into a real destination with
280 * the required properties. If the real destination is @d@,
281 * then the following properties will hold:
283 * * @d@ will have exactly one reference.
285 * * If @m@ is not @MP_NEW@, then the contents of @m@ will not
286 * change, unless @f@ has the @MP_UNDEF@ flag set.
288 * * If @m@ is not @MP_NEW@, then he reference count of @m@ on
289 * entry is equal to the sum of the counts of @d@ and @m@ on
292 * * The size of @d@ will be at least @sz@.
294 * * If @f@ has the @MP_BURN@ flag set, then @d@ will be
295 * allocated from @MPARENA_SECURE@.
297 * Understanding this function is crucial to using Catacomb's
298 * multiprecision integer library effectively.
301 mp
*mp_dest(mp
*m
, size_t sz
, unsigned f
)
303 /* --- If no destination, make one --- */
306 m
= mp_new(sz
, f
| MP_UNDEF
| MP_BURN
);
307 else if (m
== MP_NEW
)
308 m
= mp_new(sz
, f
| MP_UNDEF
);
310 size_t len
= MP_LEN(m
);
311 unsigned undef
= (m
->f
| f
) & MP_UNDEF
;
313 /* --- If the value must be preserved, the block can't shrink --- */
315 if (!undef
&& sz
< len
)
318 /* --- Otherwise check whether the destination is suitable --- */
320 if (m
->ref
> 1 || (m
->f
& MP_CONST
) ||
321 m
->sz
> len
|| !((f
& ~m
->f
) & MP_BURN
)) {
323 /* --- No -- allocate a new buffer --- *
325 * The buffer must be secure if (a) the caller requested a secure
326 * buffer, or (b) the old buffer is secure and I'm not allowed to
327 * discard the old contents.
333 if ((f
& MP_BURN
) || (!undef
&& (m
->f
& MP_BURN
)))
339 /* --- Copy the data over --- */
342 memcpy(v
, m
->v
, MPWS(len
));
344 memset(v
+ len
, 0, MPWS(sz
- len
));
347 /* --- If @m@ has other references, make a new node --- *
349 * Otherwise dispose of the old buffer.
352 if (!(m
->f
& MP_CONST
) && m
->ref
== 1) {
354 memset(m
->v
, 0, MPWS(m
->sz
));
364 /* --- Fix up the node --- */
369 m
->f
= ((m
->f
& ~(MP_CONST
| MP_BURN
)) |
370 (f
& (MP_BURN
| MP_UNDEF
)));
374 /* --- If the number is growing in its buffer, fix it up --- */
378 memset(m
->vl
, 0, MPWS(sz
- len
));
388 /*----- That's all, folks -------------------------------------------------*/