3 * $Id: mp-mem.c,v 1.4 2001/02/03 12:00:29 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.4 2001/02/03 12:00:29 mdw
34 * Now @mp_drop@ checks its argument is non-NULL before attempting to free
35 * it. Note that the macro version @MP_DROP@ doesn't do this.
37 * Revision 1.3 2000/06/17 11:45:09 mdw
38 * Major memory management overhaul. Added arena support. Use the secure
39 * arena for secret integers. Replace and improve the MP management macros
40 * (e.g., replace MP_MODIFY by MP_DEST).
42 * Revision 1.2 1999/12/10 23:19:02 mdw
43 * Improve error-checking.
45 * Revision 1.1 1999/11/17 18:02:16 mdw
46 * New multiprecision integer arithmetic suite.
50 /*----- Header files ------------------------------------------------------*/
57 /*----- Main code ---------------------------------------------------------*/
61 * Arguments: @size_t sz@ = size of vector required
62 * @unsigned f@ = flags to set
64 * Returns: Pointer to a new MP structure.
66 * Use: Allocates a new multiprecision integer. The data space is
67 * allocated from either the standard global or secret arena,
68 * depending on the initial flags requested.
71 mp
*mp_new(size_t sz
, unsigned f
)
74 m
->a
= (f
& MP_BURN
) ? MPARENA_SECURE
: MPARENA_GLOBAL
;
75 m
->v
= mpalloc(m
->a
, sz
);
78 m
->f
= f
& ~(MP_CONST
| MP_DESTROYED
);
83 /* --- @mp_create@ --- *
85 * Arguments: @size_t sz@ = size of vector required
87 * Returns: Pointer to pristine new MP structure with enough memory
90 * Use: Creates a new multiprecision integer, initially zero. The
91 * integer has a single reference.
94 mp
*mp_create(size_t sz
)
97 m
->v
= mpalloc(MPARENA_GLOBAL
, sz
);
100 m
->a
= MPARENA_GLOBAL
;
106 /* --- @mp_createsecure@ --- *
108 * Arguments: @size_t sz@ = size of vector required
110 * Returns: Pointer to pristine new MP structure with enough memory
113 * Use: Creates a new multiprecision integer with indeterminate
114 * contents. The integer has a single reference. The integer's
115 * data space is allocated from the secure arena. Its burn flag
119 mp
*mp_createsecure(size_t sz
)
122 m
->v
= mpalloc(MPARENA_SECURE
, sz
);
125 m
->a
= MPARENA_SECURE
;
126 m
->f
= MP_UNDEF
| MP_BURN
;
131 /* --- @mp_build@ --- *
133 * Arguments: @mp *m@ = pointer to an MP block to fill in
134 * @mpw *v@ = pointer to a word array
135 * @mpw *vl@ = pointer just past end of array
139 * Use: Creates a multiprecision integer representing some smallish
140 * number. You must provide storage for the number and dispose
141 * of it when you've finished with it. The number is marked as
142 * constant while it exists.
145 void mp_build(mp
*m
, mpw
*v
, mpw
*vl
)
154 /* --- @mp_destroy@ --- *
156 * Arguments: @mp *m@ = pointer to a multiprecision integer
160 * Use: Destroys a multiprecision integer. The reference count isn't
161 * checked. Don't use this function if you don't know what
162 * you're doing: use @mp_drop@ instead.
165 void mp_destroy(mp
*m
)
167 assert(((void)"Destroying a free integer", !(m
->f
& MP_DESTROYED
)));
168 assert(((void)"Attempted to destroy a constant", !(m
->f
& MP_CONST
)));
170 memset(m
->v
, 0, MPWS(m
->sz
));
172 m
->f
|= MP_DESTROYED
;
176 /* --- @mp_copy@ --- *
178 * Arguments: @mp *m@ = pointer to a multiprecision integer
180 * Returns: A copy of the given multiprecision integer.
182 * Use: Copies the given integer. In fact you just get another
183 * reference to the same old one again.
186 mp
*mp_copy(mp
*m
) { return MP_COPY(m
); }
188 /* --- @mp_drop@ --- *
190 * Arguments: @mp *m@ = pointer to a multiprecision integer
194 * Use: Drops a reference to an integer which isn't wanted any more.
195 * If there are no more references, the integer is destroyed.
198 void mp_drop(mp
*m
) { if (m
) MP_DROP(m
); }
200 /* --- @mp_split@ --- *
202 * Arguments: @mp *m@ = pointer to a multiprecision integer
204 * Returns: A reference to the same integer, possibly with a different
207 * Use: Splits off a modifiable version of the integer referred to.
212 /* if ((m->f & MP_CONST) || m->ref > 1) { */
213 /* size_t len = MP_LEN(m); */
214 /* mp *mm = mp_new(len, m->f); */
215 /* if (!(m->f & MP_UNDEF)) */
216 /* memcpy(mm->v, m->v, MPWS(len)); */
224 /* --- @mp_resize@ --- *
226 * Arguments: @mp *m@ = pointer to a multiprecision integer
227 * @size_t sz@ = new size
231 * Use: Changes an integer's size. The length and value are not
232 * changed. It is an error to
235 void mp_resize(mp
*m
, size_t sz
)
237 /* mparena *a = (m->f & MP_BURN) ? MPARENA_SECURE : MPARENA_GLOBAL; */
239 /* size_t len = MP_LEN(m); */
240 /* assert(((void)"can't make size less than length", sz >= len)); */
241 /* v = mpalloc(a, sz); */
242 /* if (!(m->f & MP_UNDEF)) */
243 /* memcpy(v, m->v, MPWS(len)); */
244 /* if (m->f & MP_BURN) */
245 /* memset(m->v, 0, MPWS(m->sz)); */
246 /* mpfree(m->a, m->v); */
249 /* m->vl = v + len; */
252 /* --- @mp_ensure@ --- *
254 * Arguments: @mp *m@ = pointer to a multiprecision integer
255 * @size_t sz@ = required length
259 * Use: Changes an integer's length. If there is not enough space
260 * allocated for the new length then the size is increased. It
263 void mp_ensure(mp
*m
, size_t sz
)
265 /* size_t len = MP_LEN(m); */
266 /* if (sz >= len) { */
267 /* if (sz > m->sz) */
268 /* mp_resize(m, sz); */
269 /* if (!(m->f & MP_UNDEF) && sz > len) */
270 /* memset(m->vl, 0, MPWS(sz - len)); */
271 /* m->vl = m->v + sz; */
275 /* --- @mp_dest@ --- *
277 * Arguments: @mp *m@ = a suggested destination integer
278 * @size_t sz@ = size required for result, in digits
279 * @unsigned f@ = various flags
281 * Returns: A pointer to an appropriate destination.
283 * Use: Converts a suggested destination into a real destination with
284 * the required properties. If the real destination is @d@,
285 * then the following properties will hold:
287 * * @d@ will have exactly one reference.
289 * * If @m@ is not @MP_NEW@, then the contents of @m@ will not
290 * change, unless @f@ has the @MP_UNDEF@ flag set.
292 * * If @m@ is not @MP_NEW@, then he reference count of @m@ on
293 * entry is equal to the sum of the counts of @d@ and @m@ on
296 * * The size of @d@ will be at least @sz@.
298 * * If @f@ has the @MP_BURN@ flag set, then @d@ will be
299 * allocated from @MPARENA_SECURE@.
301 * Understanding this function is crucial to using Catacomb's
302 * multiprecision integer library effectively.
305 mp
*mp_dest(mp
*m
, size_t sz
, unsigned f
)
307 /* --- If no destination, make one --- */
310 m
= mp_new(sz
, f
| MP_UNDEF
| MP_BURN
);
311 else if (m
== MP_NEW
)
312 m
= mp_new(sz
, f
| MP_UNDEF
);
314 size_t len
= MP_LEN(m
);
315 unsigned undef
= (m
->f
| f
) & MP_UNDEF
;
317 /* --- If the value must be preserved, the block can't shrink --- */
319 if (!undef
&& sz
< len
)
322 /* --- Otherwise check whether the destination is suitable --- */
324 if (m
->ref
> 1 || (m
->f
& MP_CONST
) ||
325 m
->sz
> len
|| !((f
& ~m
->f
) & MP_BURN
)) {
327 /* --- No -- allocate a new buffer --- *
329 * The buffer must be secure if (a) the caller requested a secure
330 * buffer, or (b) the old buffer is secure and I'm not allowed to
331 * discard the old contents.
337 if ((f
& MP_BURN
) || (!undef
&& (m
->f
& MP_BURN
)))
343 /* --- Copy the data over --- */
346 memcpy(v
, m
->v
, MPWS(len
));
348 memset(v
+ len
, 0, MPWS(sz
- len
));
351 /* --- If @m@ has other references, make a new node --- *
353 * Otherwise dispose of the old buffer.
356 if (!(m
->f
& MP_CONST
) && m
->ref
== 1) {
358 memset(m
->v
, 0, MPWS(m
->sz
));
368 /* --- Fix up the node --- */
373 m
->f
= ((m
->f
& ~(MP_CONST
| MP_BURN
)) |
374 (f
& (MP_BURN
| MP_UNDEF
)));
378 /* --- If the number is growing in its buffer, fix it up --- */
382 memset(m
->vl
, 0, MPWS(sz
- len
));
392 /*----- That's all, folks -------------------------------------------------*/