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