chiark / gitweb /
Now @mp_drop@ checks its argument is non-NULL before attempting to free
[catacomb] / mp-mem.c
1 /* -*-c-*-
2  *
3  * $Id: mp-mem.c,v 1.4 2001/02/03 12:00:29 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.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.
36  *
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).
41  *
42  * Revision 1.2  1999/12/10 23:19:02  mdw
43  * Improve error-checking.
44  *
45  * Revision 1.1  1999/11/17 18:02:16  mdw
46  * New multiprecision integer arithmetic suite.
47  *
48  */
49
50 /*----- Header files ------------------------------------------------------*/
51
52 #include <mLib/sub.h>
53
54
55 #include "mp.h"
56
57 /*----- Main code ---------------------------------------------------------*/
58
59 /* --- @mp_new@ --- *
60  *
61  * Arguments:   @size_t sz@ = size of vector required
62  *              @unsigned f@ = flags to set
63  *
64  * Returns:     Pointer to a new MP structure.
65  *
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.
69  */
70
71 mp *mp_new(size_t sz, unsigned f)
72 {
73   mp *m = CREATE(mp);
74   m->a = (f & MP_BURN) ? MPARENA_SECURE : MPARENA_GLOBAL;
75   m->v = mpalloc(m->a, sz);
76   m->vl = m->v + sz;
77   m->sz = sz;
78   m->f = f & ~(MP_CONST | MP_DESTROYED);
79   m->ref = 1;
80   return (m);
81 }
82
83 /* --- @mp_create@ --- *
84  *
85  * Arguments:   @size_t sz@ = size of vector required
86  *
87  * Returns:     Pointer to pristine new MP structure with enough memory
88  *              bolted onto it.
89  *
90  * Use:         Creates a new multiprecision integer, initially zero.  The
91  *              integer has a single reference.
92  */
93
94 mp *mp_create(size_t sz)
95 {
96   mp *m = CREATE(mp);
97   m->v = mpalloc(MPARENA_GLOBAL, sz);
98   m->vl = m->v + sz;
99   m->sz = sz;
100   m->a = MPARENA_GLOBAL;
101   m->f = MP_UNDEF;
102   m->ref = 1;
103   return (m);
104 }
105
106 /* --- @mp_createsecure@ --- *
107  *
108  * Arguments:   @size_t sz@ = size of vector required
109  *
110  * Returns:     Pointer to pristine new MP structure with enough memory
111  *              bolted onto it.
112  *
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
116  *              is set.
117  */
118
119 mp *mp_createsecure(size_t sz)
120 {
121   mp *m = CREATE(mp);  
122   m->v = mpalloc(MPARENA_SECURE, sz);
123   m->vl = m->v + sz;
124   m->sz = sz;
125   m->a = MPARENA_SECURE;
126   m->f = MP_UNDEF | MP_BURN;
127   m->ref = 1;
128   return (m);
129 }
130
131 /* --- @mp_build@ --- *
132  *
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
136  *
137  * Returns:     ---
138  *
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.
143  */
144
145 void mp_build(mp *m, mpw *v, mpw *vl)
146 {
147   m->v = v;
148   m->vl = vl;
149   m->sz = vl - v;
150   m->f = MP_CONST;
151   m->ref = 1;
152 }
153
154 /* --- @mp_destroy@ --- *
155  *
156  * Arguments:   @mp *m@ = pointer to a multiprecision integer
157  *
158  * Returns:     ---
159  *
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.
163  */
164
165 void mp_destroy(mp *m)
166 {
167   assert(((void)"Destroying a free integer", !(m->f & MP_DESTROYED)));
168   assert(((void)"Attempted to destroy a constant", !(m->f & MP_CONST)));
169   if (m->f & MP_BURN)
170     memset(m->v, 0, MPWS(m->sz));
171   mpfree(m->a, m->v);
172   m->f |= MP_DESTROYED;
173   DESTROY(m);
174 }
175
176 /* --- @mp_copy@ --- *
177  *
178  * Arguments:   @mp *m@ = pointer to a multiprecision integer
179  *
180  * Returns:     A copy of the given multiprecision integer.
181  *
182  * Use:         Copies the given integer.  In fact you just get another
183  *              reference to the same old one again.
184  */
185
186 mp *mp_copy(mp *m) { return MP_COPY(m); }
187
188 /* --- @mp_drop@ --- *
189  *
190  * Arguments:   @mp *m@ = pointer to a multiprecision integer
191  *
192  * Returns:     ---
193  *
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.
196  */
197
198 void mp_drop(mp *m) { if (m) MP_DROP(m); }
199
200 /* --- @mp_split@ --- *
201  *
202  * Arguments:   @mp *m@ = pointer to a multiprecision integer
203  *
204  * Returns:     A reference to the same integer, possibly with a different
205  *              address.
206  *
207  * Use:         Splits off a modifiable version of the integer referred to.
208  */
209
210 mp *mp_split(mp *m)
211 {
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)); */
217 /*     m->ref--; */
218 /*     m = mm; */
219 /*   } */
220   MP_SPLIT(m);
221   return (m);
222 }
223
224 /* --- @mp_resize@ --- *
225  *
226  * Arguments:   @mp *m@ = pointer to a multiprecision integer
227  *              @size_t sz@ = new size
228  *
229  * Returns:     ---
230  *
231  * Use:         Changes an integer's size.  The length and value are not
232  *              changed.  It is an error to 
233  */
234
235 void mp_resize(mp *m, size_t sz)
236 {
237 /*   mparena *a = (m->f & MP_BURN) ? MPARENA_SECURE : MPARENA_GLOBAL; */
238 /*   mpw *v; */
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); */
247 /*   m->a = a; */
248 /*   m->v = v; */
249 /*   m->vl = v + len; */
250 }
251
252 /* --- @mp_ensure@ --- *
253  *
254  * Arguments:   @mp *m@ = pointer to a multiprecision integer
255  *              @size_t sz@ = required length
256  *
257  * Returns:     ---
258  *
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
261  */
262
263 void mp_ensure(mp *m, size_t sz)
264 {
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; */
272 /*   } */
273 }
274
275 /* --- @mp_dest@ --- *
276  *
277  * Arguments:   @mp *m@ = a suggested destination integer
278  *              @size_t sz@ = size required for result, in digits
279  *              @unsigned f@ = various flags
280  *
281  * Returns:     A pointer to an appropriate destination.
282  *
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:
286  *
287  *                * @d@ will have exactly one reference.
288  *
289  *                * If @m@ is not @MP_NEW@, then the contents of @m@ will not
290  *                  change, unless @f@ has the @MP_UNDEF@ flag set.
291  *
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
294  *                  exit.
295  *
296  *                * The size of @d@ will be at least @sz@.
297  *
298  *                * If @f@ has the @MP_BURN@ flag set, then @d@ will be
299  *                  allocated from @MPARENA_SECURE@.
300  *
301  *              Understanding this function is crucial to using Catacomb's
302  *              multiprecision integer library effectively.
303  */
304
305 mp *mp_dest(mp *m, size_t sz, unsigned f)
306 {
307   /* --- If no destination, make one --- */
308
309   if (m == MP_NEWSEC)
310     m = mp_new(sz, f | MP_UNDEF | MP_BURN);
311   else if (m == MP_NEW)
312     m = mp_new(sz, f | MP_UNDEF);
313   else {
314     size_t len = MP_LEN(m);
315     unsigned undef = (m->f | f) & MP_UNDEF;
316
317     /* --- If the value must be preserved, the block can't shrink --- */
318
319     if (!undef && sz < len)
320       sz = len;
321
322     /* --- Otherwise check whether the destination is suitable --- */
323
324     if (m->ref > 1 || (m->f & MP_CONST) ||
325         m->sz > len || !((f & ~m->f) & MP_BURN)) {
326
327       /* --- No -- allocate a new buffer --- *
328        *
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.
332        */
333       
334       mparena *a;
335       mpw *v;
336
337       if ((f & MP_BURN) || (!undef && (m->f & MP_BURN)))
338         a = MPARENA_SECURE;
339       else
340         a = MPARENA_GLOBAL;
341       v = mpalloc(a, sz);
342
343       /* --- Copy the data over --- */
344
345       if (!undef) {
346         memcpy(v, m->v, MPWS(len));
347         if (sz - len > 0)
348           memset(v + len, 0, MPWS(sz - len));
349       }
350
351       /* --- If @m@ has other references, make a new node --- *
352        *
353        * Otherwise dispose of the old buffer.
354        */
355
356       if (!(m->f & MP_CONST) && m->ref == 1) {
357         if (m->f & MP_BURN)
358           memset(m->v, 0, MPWS(m->sz));
359         mpfree(m->a, m->v);
360       } else {
361         mp *mm = CREATE(mp);
362         mm->ref = 1;
363         mm->f = m->f;
364         m->ref--;
365         m = mm;
366       }
367
368       /* --- Fix up the node --- */
369
370       m->v = v;
371       m->vl = v + sz;
372       m->sz = sz;
373       m->f = ((m->f & ~(MP_CONST | MP_BURN)) |
374               (f & (MP_BURN | MP_UNDEF)));
375       m->a = a;
376     }
377
378     /* --- If the number is growing in its buffer, fix it up --- */
379
380     else if (sz > len) {
381       if (!undef)
382         memset(m->vl, 0, MPWS(sz - len));
383       m->vl = m->v + sz;
384     }
385   }
386
387   /* --- Done --- */
388
389   return (m);
390 }
391
392 /*----- That's all, folks -------------------------------------------------*/