chiark / gitweb /
Implement some missing functions.
[catacomb] / mp-mem.c
1 /* -*-c-*-
2  *
3  * $Id: mp-mem.c,v 1.5 2001/06/16 12:57:00 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.5  2001/06/16 12:57:00  mdw
34  * Implement some missing functions.
35  *
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.
39  *
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).
44  *
45  * Revision 1.2  1999/12/10 23:19:02  mdw
46  * Improve error-checking.
47  *
48  * Revision 1.1  1999/11/17 18:02:16  mdw
49  * New multiprecision integer arithmetic suite.
50  *
51  */
52
53 /*----- Header files ------------------------------------------------------*/
54
55 #include <mLib/sub.h>
56
57
58 #include "mp.h"
59
60 /*----- Main code ---------------------------------------------------------*/
61
62 /* --- @mp_new@ --- *
63  *
64  * Arguments:   @size_t sz@ = size of vector required
65  *              @unsigned f@ = flags to set
66  *
67  * Returns:     Pointer to a new MP structure.
68  *
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.
72  */
73
74 mp *mp_new(size_t sz, unsigned f)
75 {
76   mp *m = CREATE(mp);
77   m->a = (f & MP_BURN) ? MPARENA_SECURE : MPARENA_GLOBAL;
78   m->v = mpalloc(m->a, sz);
79   m->vl = m->v + sz;
80   m->sz = sz;
81   m->f = f & ~(MP_CONST | MP_DESTROYED);
82   m->ref = 1;
83   return (m);
84 }
85
86 /* --- @mp_create@ --- *
87  *
88  * Arguments:   @size_t sz@ = size of vector required
89  *
90  * Returns:     Pointer to pristine new MP structure with enough memory
91  *              bolted onto it.
92  *
93  * Use:         Creates a new multiprecision integer, initially zero.  The
94  *              integer has a single reference.
95  */
96
97 mp *mp_create(size_t sz)
98 {
99   mp *m = CREATE(mp);
100   m->v = mpalloc(MPARENA_GLOBAL, sz);
101   m->vl = m->v + sz;
102   m->sz = sz;
103   m->a = MPARENA_GLOBAL;
104   m->f = MP_UNDEF;
105   m->ref = 1;
106   return (m);
107 }
108
109 /* --- @mp_createsecure@ --- *
110  *
111  * Arguments:   @size_t sz@ = size of vector required
112  *
113  * Returns:     Pointer to pristine new MP structure with enough memory
114  *              bolted onto it.
115  *
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
119  *              is set.
120  */
121
122 mp *mp_createsecure(size_t sz)
123 {
124   mp *m = CREATE(mp);  
125   m->v = mpalloc(MPARENA_SECURE, sz);
126   m->vl = m->v + sz;
127   m->sz = sz;
128   m->a = MPARENA_SECURE;
129   m->f = MP_UNDEF | MP_BURN;
130   m->ref = 1;
131   return (m);
132 }
133
134 /* --- @mp_build@ --- *
135  *
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
139  *
140  * Returns:     ---
141  *
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.
146  */
147
148 void mp_build(mp *m, mpw *v, mpw *vl)
149 {
150   m->v = v;
151   m->vl = vl;
152   m->sz = vl - v;
153   m->f = MP_CONST;
154   m->ref = 1;
155 }
156
157 /* --- @mp_destroy@ --- *
158  *
159  * Arguments:   @mp *m@ = pointer to a multiprecision integer
160  *
161  * Returns:     ---
162  *
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.
166  */
167
168 void mp_destroy(mp *m)
169 {
170   assert(((void)"Destroying a free integer", !(m->f & MP_DESTROYED)));
171   assert(((void)"Attempted to destroy a constant", !(m->f & MP_CONST)));
172   if (m->f & MP_BURN)
173     memset(m->v, 0, MPWS(m->sz));
174   mpfree(m->a, m->v);
175   m->f |= MP_DESTROYED;
176   DESTROY(m);
177 }
178
179 /* --- @mp_copy@ --- *
180  *
181  * Arguments:   @mp *m@ = pointer to a multiprecision integer
182  *
183  * Returns:     A copy of the given multiprecision integer.
184  *
185  * Use:         Copies the given integer.  In fact you just get another
186  *              reference to the same old one again.
187  */
188
189 mp *mp_copy(mp *m) { return MP_COPY(m); }
190
191 /* --- @mp_drop@ --- *
192  *
193  * Arguments:   @mp *m@ = pointer to a multiprecision integer
194  *
195  * Returns:     ---
196  *
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.
199  */
200
201 void mp_drop(mp *m) { if (m) MP_DROP(m); }
202
203 /* --- @mp_split@ --- *
204  *
205  * Arguments:   @mp *m@ = pointer to a multiprecision integer
206  *
207  * Returns:     A reference to the same integer, possibly with a different
208  *              address.
209  *
210  * Use:         Splits off a modifiable version of the integer referred to.
211  */
212
213 mp *mp_split(mp *m) { MP_SPLIT(m); return (m); }
214
215 /* --- @mp_resize@ --- *
216  *
217  * Arguments:   @mp *m@ = pointer to a multiprecision integer
218  *              @size_t sz@ = new size
219  *
220  * Returns:     ---
221  *
222  * Use:         Changes an integer's size.  The length and value are not
223  *              changed.  It is an error to 
224  */
225
226 void mp_resize(mp *m, size_t sz) { MP_RESIZE(m, sz); }
227
228 /* --- @mp_ensure@ --- *
229  *
230  * Arguments:   @mp *m@ = pointer to a multiprecision integer
231  *              @size_t sz@ = required length
232  *
233  * Returns:     ---
234  *
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
237  */
238
239 void mp_ensure(mp *m, size_t sz) { MP_ENSURE(m, sz); }
240
241 /* --- @mp_dest@ --- *
242  *
243  * Arguments:   @mp *m@ = a suggested destination integer
244  *              @size_t sz@ = size required for result, in digits
245  *              @unsigned f@ = various flags
246  *
247  * Returns:     A pointer to an appropriate destination.
248  *
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:
252  *
253  *                * @d@ will have exactly one reference.
254  *
255  *                * If @m@ is not @MP_NEW@, then the contents of @m@ will not
256  *                  change, unless @f@ has the @MP_UNDEF@ flag set.
257  *
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
260  *                  exit.
261  *
262  *                * The size of @d@ will be at least @sz@.
263  *
264  *                * If @f@ has the @MP_BURN@ flag set, then @d@ will be
265  *                  allocated from @MPARENA_SECURE@.
266  *
267  *              Understanding this function is crucial to using Catacomb's
268  *              multiprecision integer library effectively.
269  */
270
271 mp *mp_dest(mp *m, size_t sz, unsigned f)
272 {
273   /* --- If no destination, make one --- */
274
275   if (m == MP_NEWSEC)
276     m = mp_new(sz, f | MP_UNDEF | MP_BURN);
277   else if (m == MP_NEW)
278     m = mp_new(sz, f | MP_UNDEF);
279   else {
280     size_t len = MP_LEN(m);
281     unsigned undef = (m->f | f) & MP_UNDEF;
282
283     /* --- If the value must be preserved, the block can't shrink --- */
284
285     if (!undef && sz < len)
286       sz = len;
287
288     /* --- Otherwise check whether the destination is suitable --- */
289
290     if (m->ref > 1 || (m->f & MP_CONST) ||
291         m->sz > len || !((f & ~m->f) & MP_BURN)) {
292
293       /* --- No -- allocate a new buffer --- *
294        *
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.
298        */
299       
300       mparena *a;
301       mpw *v;
302
303       if ((f & MP_BURN) || (!undef && (m->f & MP_BURN)))
304         a = MPARENA_SECURE;
305       else
306         a = MPARENA_GLOBAL;
307       v = mpalloc(a, sz);
308
309       /* --- Copy the data over --- */
310
311       if (!undef) {
312         memcpy(v, m->v, MPWS(len));
313         if (sz - len > 0)
314           memset(v + len, 0, MPWS(sz - len));
315       }
316
317       /* --- If @m@ has other references, make a new node --- *
318        *
319        * Otherwise dispose of the old buffer.
320        */
321
322       if (!(m->f & MP_CONST) && m->ref == 1) {
323         if (m->f & MP_BURN)
324           memset(m->v, 0, MPWS(m->sz));
325         mpfree(m->a, m->v);
326       } else {
327         mp *mm = CREATE(mp);
328         mm->ref = 1;
329         mm->f = m->f;
330         m->ref--;
331         m = mm;
332       }
333
334       /* --- Fix up the node --- */
335
336       m->v = v;
337       m->vl = v + sz;
338       m->sz = sz;
339       m->f = ((m->f & ~(MP_CONST | MP_BURN)) |
340               (f & (MP_BURN | MP_UNDEF)));
341       m->a = a;
342     }
343
344     /* --- If the number is growing in its buffer, fix it up --- */
345
346     else if (sz > len) {
347       if (!undef)
348         memset(m->vl, 0, MPWS(sz - len));
349       m->vl = m->v + sz;
350     }
351   }
352
353   /* --- Done --- */
354
355   return (m);
356 }
357
358 /*----- That's all, folks -------------------------------------------------*/