3 * $Id: mp-mem.c,v 1.8 2004/04/08 16:17:32 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
)
130 m
->a
= MPARENA_GLOBAL
;
135 /* --- @mp_destroy@ --- *
137 * Arguments: @mp *m@ = pointer to a multiprecision integer
141 * Use: Destroys a multiprecision integer. The reference count isn't
142 * checked. Don't use this function if you don't know what
143 * you're doing: use @mp_drop@ instead.
146 void mp_destroy(mp
*m
)
148 assert(((void)"Destroying a free integer", !(m
->f
& MP_DESTROYED
)));
149 assert(((void)"Attempted to destroy a constant", !(m
->f
& MP_CONST
)));
151 memset(m
->v
, 0, MPWS(m
->sz
));
153 m
->f
|= MP_DESTROYED
;
157 /* --- @mp_copy@ --- *
159 * Arguments: @mp *m@ = pointer to a multiprecision integer
161 * Returns: A copy of the given multiprecision integer.
163 * Use: Copies the given integer. In fact you just get another
164 * reference to the same old one again.
167 mp
*mp_copy(mp
*m
) { return MP_COPY(m
); }
169 /* --- @mp_drop@ --- *
171 * Arguments: @mp *m@ = pointer to a multiprecision integer
175 * Use: Drops a reference to an integer which isn't wanted any more.
176 * If there are no more references, the integer is destroyed.
179 void mp_drop(mp
*m
) { if (m
) MP_DROP(m
); }
181 /* --- @mp_split@ --- *
183 * Arguments: @mp *m@ = pointer to a multiprecision integer
185 * Returns: A reference to the same integer, possibly with a different
188 * Use: Splits off a modifiable version of the integer referred to.
191 mp
*mp_split(mp
*m
) { MP_SPLIT(m
); return (m
); }
193 /* --- @mp_resize@ --- *
195 * Arguments: @mp *m@ = pointer to a multiprecision integer
196 * @size_t sz@ = new size
200 * Use: Changes an integer's size. The length and value are not
201 * changed. It is an error to
204 void mp_resize(mp
*m
, size_t sz
) { MP_RESIZE(m
, sz
); }
206 /* --- @mp_ensure@ --- *
208 * Arguments: @mp *m@ = pointer to a multiprecision integer
209 * @size_t sz@ = required length
213 * Use: Changes an integer's length. If there is not enough space
214 * allocated for the new length then the size is increased. It
217 void mp_ensure(mp
*m
, size_t sz
) { MP_ENSURE(m
, sz
); }
219 /* --- @mp_dest@ --- *
221 * Arguments: @mp *m@ = a suggested destination integer
222 * @size_t sz@ = size required for result, in digits
223 * @unsigned f@ = various flags
225 * Returns: A pointer to an appropriate destination.
227 * Use: Converts a suggested destination into a real destination with
228 * the required properties. If the real destination is @d@,
229 * then the following properties will hold:
231 * * @d@ will have exactly one reference.
233 * * If @m@ is not @MP_NEW@, then the contents of @m@ will not
234 * change, unless @f@ has the @MP_UNDEF@ flag set.
236 * * If @m@ is not @MP_NEW@, then he reference count of @m@ on
237 * entry is equal to the sum of the counts of @d@ and @m@ on
240 * * The size of @d@ will be at least @sz@.
242 * * If @f@ has the @MP_BURN@ flag set, then @d@ will be
243 * allocated from @MPARENA_SECURE@.
245 * Understanding this function is crucial to using Catacomb's
246 * multiprecision integer library effectively.
249 mp
*mp_dest(mp
*m
, size_t sz
, unsigned f
)
251 /* --- If no destination, make one --- */
254 m
= mp_new(sz
, f
| MP_UNDEF
| MP_BURN
);
255 else if (m
== MP_NEW
)
256 m
= mp_new(sz
, f
| MP_UNDEF
);
258 size_t len
= MP_LEN(m
);
259 unsigned undef
= (m
->f
| f
) & MP_UNDEF
;
261 /* --- If the value must be preserved, the block can't shrink --- */
263 if (!undef
&& sz
< len
)
266 /* --- Otherwise check whether the destination is suitable --- */
268 if (m
->ref
> 1 || (m
->f
& MP_CONST
) ||
269 sz
> m
->sz
|| ((f
& ~m
->f
) & MP_BURN
)) {
271 /* --- No -- allocate a new buffer --- *
273 * The buffer must be secure if (a) the caller requested a secure
274 * buffer, or (b) the old buffer is secure and I'm not allowed to
275 * discard the old contents.
281 if ((f
& MP_BURN
) || (!undef
&& (m
->f
& MP_BURN
)))
287 /* --- Copy the data over --- */
290 memcpy(v
, m
->v
, MPWS(len
));
292 memset(v
+ len
, 0, MPWS(sz
- len
));
295 /* --- If @m@ has other references, make a new node --- *
297 * Otherwise dispose of the old buffer.
300 if (!(m
->f
& MP_CONST
) && m
->ref
== 1) {
302 memset(m
->v
, 0, MPWS(m
->sz
));
312 /* --- Fix up the node --- */
317 m
->f
= ((m
->f
& ~(MP_CONST
| MP_BURN
)) |
318 (f
& (MP_BURN
| MP_UNDEF
)));
322 /* --- If the number is growing in its buffer, fix it up --- */
326 memset(m
->vl
, 0, MPWS(sz
- len
));
336 /*----- That's all, folks -------------------------------------------------*/