3 * $Id: mp-mem.c,v 1.5 2001/06/16 12:57:00 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.5 2001/06/16 12:57:00 mdw
34 * Implement some missing functions.
36 * Revision 1.4 2001/02/03 12:00:29 mdw
37 * Now @mp_drop@ checks its argument is non-NULL before attempting to free
38 * it. Note that the macro version @MP_DROP@ doesn't do this.
40 * Revision 1.3 2000/06/17 11:45:09 mdw
41 * Major memory management overhaul. Added arena support. Use the secure
42 * arena for secret integers. Replace and improve the MP management macros
43 * (e.g., replace MP_MODIFY by MP_DEST).
45 * Revision 1.2 1999/12/10 23:19:02 mdw
46 * Improve error-checking.
48 * Revision 1.1 1999/11/17 18:02:16 mdw
49 * New multiprecision integer arithmetic suite.
53 /*----- Header files ------------------------------------------------------*/
60 /*----- Main code ---------------------------------------------------------*/
64 * Arguments: @size_t sz@ = size of vector required
65 * @unsigned f@ = flags to set
67 * Returns: Pointer to a new MP structure.
69 * Use: Allocates a new multiprecision integer. The data space is
70 * allocated from either the standard global or secret arena,
71 * depending on the initial flags requested.
74 mp
*mp_new(size_t sz
, unsigned f
)
77 m
->a
= (f
& MP_BURN
) ? MPARENA_SECURE
: MPARENA_GLOBAL
;
78 m
->v
= mpalloc(m
->a
, sz
);
81 m
->f
= f
& ~(MP_CONST
| MP_DESTROYED
);
86 /* --- @mp_create@ --- *
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, initially zero. The
94 * integer has a single reference.
97 mp
*mp_create(size_t sz
)
100 m
->v
= mpalloc(MPARENA_GLOBAL
, sz
);
103 m
->a
= MPARENA_GLOBAL
;
109 /* --- @mp_createsecure@ --- *
111 * Arguments: @size_t sz@ = size of vector required
113 * Returns: Pointer to pristine new MP structure with enough memory
116 * Use: Creates a new multiprecision integer with indeterminate
117 * contents. The integer has a single reference. The integer's
118 * data space is allocated from the secure arena. Its burn flag
122 mp
*mp_createsecure(size_t sz
)
125 m
->v
= mpalloc(MPARENA_SECURE
, sz
);
128 m
->a
= MPARENA_SECURE
;
129 m
->f
= MP_UNDEF
| MP_BURN
;
134 /* --- @mp_build@ --- *
136 * Arguments: @mp *m@ = pointer to an MP block to fill in
137 * @mpw *v@ = pointer to a word array
138 * @mpw *vl@ = pointer just past end of array
142 * Use: Creates a multiprecision integer representing some smallish
143 * number. You must provide storage for the number and dispose
144 * of it when you've finished with it. The number is marked as
145 * constant while it exists.
148 void mp_build(mp
*m
, mpw
*v
, mpw
*vl
)
157 /* --- @mp_destroy@ --- *
159 * Arguments: @mp *m@ = pointer to a multiprecision integer
163 * Use: Destroys a multiprecision integer. The reference count isn't
164 * checked. Don't use this function if you don't know what
165 * you're doing: use @mp_drop@ instead.
168 void mp_destroy(mp
*m
)
170 assert(((void)"Destroying a free integer", !(m
->f
& MP_DESTROYED
)));
171 assert(((void)"Attempted to destroy a constant", !(m
->f
& MP_CONST
)));
173 memset(m
->v
, 0, MPWS(m
->sz
));
175 m
->f
|= MP_DESTROYED
;
179 /* --- @mp_copy@ --- *
181 * Arguments: @mp *m@ = pointer to a multiprecision integer
183 * Returns: A copy of the given multiprecision integer.
185 * Use: Copies the given integer. In fact you just get another
186 * reference to the same old one again.
189 mp
*mp_copy(mp
*m
) { return MP_COPY(m
); }
191 /* --- @mp_drop@ --- *
193 * Arguments: @mp *m@ = pointer to a multiprecision integer
197 * Use: Drops a reference to an integer which isn't wanted any more.
198 * If there are no more references, the integer is destroyed.
201 void mp_drop(mp
*m
) { if (m
) MP_DROP(m
); }
203 /* --- @mp_split@ --- *
205 * Arguments: @mp *m@ = pointer to a multiprecision integer
207 * Returns: A reference to the same integer, possibly with a different
210 * Use: Splits off a modifiable version of the integer referred to.
213 mp
*mp_split(mp
*m
) { MP_SPLIT(m
); return (m
); }
215 /* --- @mp_resize@ --- *
217 * Arguments: @mp *m@ = pointer to a multiprecision integer
218 * @size_t sz@ = new size
222 * Use: Changes an integer's size. The length and value are not
223 * changed. It is an error to
226 void mp_resize(mp
*m
, size_t sz
) { MP_RESIZE(m
, sz
); }
228 /* --- @mp_ensure@ --- *
230 * Arguments: @mp *m@ = pointer to a multiprecision integer
231 * @size_t sz@ = required length
235 * Use: Changes an integer's length. If there is not enough space
236 * allocated for the new length then the size is increased. It
239 void mp_ensure(mp
*m
, size_t sz
) { MP_ENSURE(m
, sz
); }
241 /* --- @mp_dest@ --- *
243 * Arguments: @mp *m@ = a suggested destination integer
244 * @size_t sz@ = size required for result, in digits
245 * @unsigned f@ = various flags
247 * Returns: A pointer to an appropriate destination.
249 * Use: Converts a suggested destination into a real destination with
250 * the required properties. If the real destination is @d@,
251 * then the following properties will hold:
253 * * @d@ will have exactly one reference.
255 * * If @m@ is not @MP_NEW@, then the contents of @m@ will not
256 * change, unless @f@ has the @MP_UNDEF@ flag set.
258 * * If @m@ is not @MP_NEW@, then he reference count of @m@ on
259 * entry is equal to the sum of the counts of @d@ and @m@ on
262 * * The size of @d@ will be at least @sz@.
264 * * If @f@ has the @MP_BURN@ flag set, then @d@ will be
265 * allocated from @MPARENA_SECURE@.
267 * Understanding this function is crucial to using Catacomb's
268 * multiprecision integer library effectively.
271 mp
*mp_dest(mp
*m
, size_t sz
, unsigned f
)
273 /* --- If no destination, make one --- */
276 m
= mp_new(sz
, f
| MP_UNDEF
| MP_BURN
);
277 else if (m
== MP_NEW
)
278 m
= mp_new(sz
, f
| MP_UNDEF
);
280 size_t len
= MP_LEN(m
);
281 unsigned undef
= (m
->f
| f
) & MP_UNDEF
;
283 /* --- If the value must be preserved, the block can't shrink --- */
285 if (!undef
&& sz
< len
)
288 /* --- Otherwise check whether the destination is suitable --- */
290 if (m
->ref
> 1 || (m
->f
& MP_CONST
) ||
291 m
->sz
> len
|| !((f
& ~m
->f
) & MP_BURN
)) {
293 /* --- No -- allocate a new buffer --- *
295 * The buffer must be secure if (a) the caller requested a secure
296 * buffer, or (b) the old buffer is secure and I'm not allowed to
297 * discard the old contents.
303 if ((f
& MP_BURN
) || (!undef
&& (m
->f
& MP_BURN
)))
309 /* --- Copy the data over --- */
312 memcpy(v
, m
->v
, MPWS(len
));
314 memset(v
+ len
, 0, MPWS(sz
- len
));
317 /* --- If @m@ has other references, make a new node --- *
319 * Otherwise dispose of the old buffer.
322 if (!(m
->f
& MP_CONST
) && m
->ref
== 1) {
324 memset(m
->v
, 0, MPWS(m
->sz
));
334 /* --- Fix up the node --- */
339 m
->f
= ((m
->f
& ~(MP_CONST
| MP_BURN
)) |
340 (f
& (MP_BURN
| MP_UNDEF
)));
344 /* --- If the number is growing in its buffer, fix it up --- */
348 memset(m
->vl
, 0, MPWS(sz
- len
));
358 /*----- That's all, folks -------------------------------------------------*/