Fix long-standing stupidity in @mp_dest@.
[u/mdw/catacomb] / mp-mem.c
1 /* -*-c-*-
2 *
3 * $Id: mp-mem.c,v 1.6 2004/04/03 03:30:22 mdw Exp $
4 *
5 * Memory management for multiprecision numbers
6 *
7 * (c) 1999 Straylight/Edgeware
8 */
9
10 /*----- Licensing notice --------------------------------------------------*
11 *
12 * This file is part of Catacomb.
13 *
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.
18 *
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.
23 *
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,
27 * MA 02111-1307, USA.
28 */
29
30 /*----- Revision history --------------------------------------------------*
31 *
32 * $Log: mp-mem.c,v $
33 * Revision 1.6 2004/04/03 03:30:22 mdw
34 * Fix long-standing stupidity in @mp_dest@.
35 *
36 * Revision 1.5 2001/06/16 12:57:00 mdw
37 * Implement some missing functions.
38 *
39 * Revision 1.4 2001/02/03 12:00:29 mdw
40 * Now @mp_drop@ checks its argument is non-NULL before attempting to free
41 * it. Note that the macro version @MP_DROP@ doesn't do this.
42 *
43 * Revision 1.3 2000/06/17 11:45:09 mdw
44 * Major memory management overhaul. Added arena support. Use the secure
45 * arena for secret integers. Replace and improve the MP management macros
46 * (e.g., replace MP_MODIFY by MP_DEST).
47 *
48 * Revision 1.2 1999/12/10 23:19:02 mdw
49 * Improve error-checking.
50 *
51 * Revision 1.1 1999/11/17 18:02:16 mdw
52 * New multiprecision integer arithmetic suite.
53 *
54 */
55
56 /*----- Header files ------------------------------------------------------*/
57
58 #include <mLib/sub.h>
59
60
61 #include "mp.h"
62
63 /*----- Main code ---------------------------------------------------------*/
64
65 /* --- @mp_new@ --- *
66 *
67 * Arguments: @size_t sz@ = size of vector required
68 * @unsigned f@ = flags to set
69 *
70 * Returns: Pointer to a new MP structure.
71 *
72 * Use: Allocates a new multiprecision integer. The data space is
73 * allocated from either the standard global or secret arena,
74 * depending on the initial flags requested.
75 */
76
77 mp *mp_new(size_t sz, unsigned f)
78 {
79 mp *m = CREATE(mp);
80 m->a = (f & MP_BURN) ? MPARENA_SECURE : MPARENA_GLOBAL;
81 m->v = mpalloc(m->a, sz);
82 m->vl = m->v + sz;
83 m->sz = sz;
84 m->f = f & ~(MP_CONST | MP_DESTROYED);
85 m->ref = 1;
86 return (m);
87 }
88
89 /* --- @mp_create@ --- *
90 *
91 * Arguments: @size_t sz@ = size of vector required
92 *
93 * Returns: Pointer to pristine new MP structure with enough memory
94 * bolted onto it.
95 *
96 * Use: Creates a new multiprecision integer, initially zero. The
97 * integer has a single reference.
98 */
99
100 mp *mp_create(size_t sz)
101 {
102 mp *m = CREATE(mp);
103 m->v = mpalloc(MPARENA_GLOBAL, sz);
104 m->vl = m->v + sz;
105 m->sz = sz;
106 m->a = MPARENA_GLOBAL;
107 m->f = MP_UNDEF;
108 m->ref = 1;
109 return (m);
110 }
111
112 /* --- @mp_createsecure@ --- *
113 *
114 * Arguments: @size_t sz@ = size of vector required
115 *
116 * Returns: Pointer to pristine new MP structure with enough memory
117 * bolted onto it.
118 *
119 * Use: Creates a new multiprecision integer with indeterminate
120 * contents. The integer has a single reference. The integer's
121 * data space is allocated from the secure arena. Its burn flag
122 * is set.
123 */
124
125 mp *mp_createsecure(size_t sz)
126 {
127 mp *m = CREATE(mp);
128 m->v = mpalloc(MPARENA_SECURE, sz);
129 m->vl = m->v + sz;
130 m->sz = sz;
131 m->a = MPARENA_SECURE;
132 m->f = MP_UNDEF | MP_BURN;
133 m->ref = 1;
134 return (m);
135 }
136
137 /* --- @mp_build@ --- *
138 *
139 * Arguments: @mp *m@ = pointer to an MP block to fill in
140 * @mpw *v@ = pointer to a word array
141 * @mpw *vl@ = pointer just past end of array
142 *
143 * Returns: ---
144 *
145 * Use: Creates a multiprecision integer representing some smallish
146 * number. You must provide storage for the number and dispose
147 * of it when you've finished with it. The number is marked as
148 * constant while it exists.
149 */
150
151 void mp_build(mp *m, mpw *v, mpw *vl)
152 {
153 m->v = v;
154 m->vl = vl;
155 m->sz = vl - v;
156 m->f = MP_CONST;
157 m->ref = 1;
158 }
159
160 /* --- @mp_destroy@ --- *
161 *
162 * Arguments: @mp *m@ = pointer to a multiprecision integer
163 *
164 * Returns: ---
165 *
166 * Use: Destroys a multiprecision integer. The reference count isn't
167 * checked. Don't use this function if you don't know what
168 * you're doing: use @mp_drop@ instead.
169 */
170
171 void mp_destroy(mp *m)
172 {
173 assert(((void)"Destroying a free integer", !(m->f & MP_DESTROYED)));
174 assert(((void)"Attempted to destroy a constant", !(m->f & MP_CONST)));
175 if (m->f & MP_BURN)
176 memset(m->v, 0, MPWS(m->sz));
177 mpfree(m->a, m->v);
178 m->f |= MP_DESTROYED;
179 DESTROY(m);
180 }
181
182 /* --- @mp_copy@ --- *
183 *
184 * Arguments: @mp *m@ = pointer to a multiprecision integer
185 *
186 * Returns: A copy of the given multiprecision integer.
187 *
188 * Use: Copies the given integer. In fact you just get another
189 * reference to the same old one again.
190 */
191
192 mp *mp_copy(mp *m) { return MP_COPY(m); }
193
194 /* --- @mp_drop@ --- *
195 *
196 * Arguments: @mp *m@ = pointer to a multiprecision integer
197 *
198 * Returns: ---
199 *
200 * Use: Drops a reference to an integer which isn't wanted any more.
201 * If there are no more references, the integer is destroyed.
202 */
203
204 void mp_drop(mp *m) { if (m) MP_DROP(m); }
205
206 /* --- @mp_split@ --- *
207 *
208 * Arguments: @mp *m@ = pointer to a multiprecision integer
209 *
210 * Returns: A reference to the same integer, possibly with a different
211 * address.
212 *
213 * Use: Splits off a modifiable version of the integer referred to.
214 */
215
216 mp *mp_split(mp *m) { MP_SPLIT(m); return (m); }
217
218 /* --- @mp_resize@ --- *
219 *
220 * Arguments: @mp *m@ = pointer to a multiprecision integer
221 * @size_t sz@ = new size
222 *
223 * Returns: ---
224 *
225 * Use: Changes an integer's size. The length and value are not
226 * changed. It is an error to
227 */
228
229 void mp_resize(mp *m, size_t sz) { MP_RESIZE(m, sz); }
230
231 /* --- @mp_ensure@ --- *
232 *
233 * Arguments: @mp *m@ = pointer to a multiprecision integer
234 * @size_t sz@ = required length
235 *
236 * Returns: ---
237 *
238 * Use: Changes an integer's length. If there is not enough space
239 * allocated for the new length then the size is increased. It
240 */
241
242 void mp_ensure(mp *m, size_t sz) { MP_ENSURE(m, sz); }
243
244 /* --- @mp_dest@ --- *
245 *
246 * Arguments: @mp *m@ = a suggested destination integer
247 * @size_t sz@ = size required for result, in digits
248 * @unsigned f@ = various flags
249 *
250 * Returns: A pointer to an appropriate destination.
251 *
252 * Use: Converts a suggested destination into a real destination with
253 * the required properties. If the real destination is @d@,
254 * then the following properties will hold:
255 *
256 * * @d@ will have exactly one reference.
257 *
258 * * If @m@ is not @MP_NEW@, then the contents of @m@ will not
259 * change, unless @f@ has the @MP_UNDEF@ flag set.
260 *
261 * * If @m@ is not @MP_NEW@, then he reference count of @m@ on
262 * entry is equal to the sum of the counts of @d@ and @m@ on
263 * exit.
264 *
265 * * The size of @d@ will be at least @sz@.
266 *
267 * * If @f@ has the @MP_BURN@ flag set, then @d@ will be
268 * allocated from @MPARENA_SECURE@.
269 *
270 * Understanding this function is crucial to using Catacomb's
271 * multiprecision integer library effectively.
272 */
273
274 mp *mp_dest(mp *m, size_t sz, unsigned f)
275 {
276 /* --- If no destination, make one --- */
277
278 if (m == MP_NEWSEC)
279 m = mp_new(sz, f | MP_UNDEF | MP_BURN);
280 else if (m == MP_NEW)
281 m = mp_new(sz, f | MP_UNDEF);
282 else {
283 size_t len = MP_LEN(m);
284 unsigned undef = (m->f | f) & MP_UNDEF;
285
286 /* --- If the value must be preserved, the block can't shrink --- */
287
288 if (!undef && sz < len)
289 sz = len;
290
291 /* --- Otherwise check whether the destination is suitable --- */
292
293 if (m->ref > 1 || (m->f & MP_CONST) ||
294 sz > m->sz || !((f & ~m->f) & MP_BURN)) {
295
296 /* --- No -- allocate a new buffer --- *
297 *
298 * The buffer must be secure if (a) the caller requested a secure
299 * buffer, or (b) the old buffer is secure and I'm not allowed to
300 * discard the old contents.
301 */
302
303 mparena *a;
304 mpw *v;
305
306 if ((f & MP_BURN) || (!undef && (m->f & MP_BURN)))
307 a = MPARENA_SECURE;
308 else
309 a = MPARENA_GLOBAL;
310 v = mpalloc(a, sz);
311
312 /* --- Copy the data over --- */
313
314 if (!undef) {
315 memcpy(v, m->v, MPWS(len));
316 if (sz - len > 0)
317 memset(v + len, 0, MPWS(sz - len));
318 }
319
320 /* --- If @m@ has other references, make a new node --- *
321 *
322 * Otherwise dispose of the old buffer.
323 */
324
325 if (!(m->f & MP_CONST) && m->ref == 1) {
326 if (m->f & MP_BURN)
327 memset(m->v, 0, MPWS(m->sz));
328 mpfree(m->a, m->v);
329 } else {
330 mp *mm = CREATE(mp);
331 mm->ref = 1;
332 mm->f = m->f;
333 m->ref--;
334 m = mm;
335 }
336
337 /* --- Fix up the node --- */
338
339 m->v = v;
340 m->vl = v + sz;
341 m->sz = sz;
342 m->f = ((m->f & ~(MP_CONST | MP_BURN)) |
343 (f & (MP_BURN | MP_UNDEF)));
344 m->a = a;
345 }
346
347 /* --- If the number is growing in its buffer, fix it up --- */
348
349 else if (sz > len) {
350 if (!undef)
351 memset(m->vl, 0, MPWS(sz - len));
352 m->vl = m->v + sz;
353 }
354 }
355
356 /* --- Done --- */
357
358 return (m);
359 }
360
361 /*----- That's all, folks -------------------------------------------------*/