3 * $Id: mp-mem.c,v 1.2 1999/12/10 23:19:02 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 /*----- Revision history --------------------------------------------------*
33 * Revision 1.2 1999/12/10 23:19:02 mdw
34 * Improve error-checking.
36 * Revision 1.1 1999/11/17 18:02:16 mdw
37 * New multiprecision integer arithmetic suite.
41 /*----- Header files ------------------------------------------------------*/
45 /*----- Main code ---------------------------------------------------------*/
47 /* --- @mp_create@ --- *
49 * Arguments: @size_t sz@ = size of vector required
51 * Returns: Pointer to pristine new MP structure with enough memory
54 * Use: Creates a new multiprecision integer, initially zero. The
55 * integer has a single reference.
58 mp
*mp_create(size_t sz
)
69 /* --- @mp_build@ --- *
71 * Arguments: @mp *m@ = pointer to an MP block to fill in
72 * @mpw *v@ = pointer to a word array
73 * @mpw *vl@ = pointer just past end of array
77 * Use: Creates a multiprecision integer representing some smallish
78 * number. You must provide storage for the number and dispose
79 * of it when you've finished with it. The number is marked as
80 * constant while it exists.
83 void mp_build(mp
*m
, mpw
*v
, mpw
*vl
)
92 /* --- @mp_destroy@ --- *
94 * Arguments: @mp *m@ = pointer to a multiprecision integer
98 * Use: Destroys a multiprecision integer. The reference count isn't
99 * checked. Don't use this function if you don't know what
100 * you're doing: use @mp_drop@ instead.
103 void mp_destroy(mp
*m
)
105 assert(((void)"Destroying a free integer", !(m
->f
& MP_DESTROYED
)));
106 assert(((void)"Attempted to destroy a constant", !(m
->f
& MP_CONST
)));
108 memset(m
->v
, 0, MPWS(m
->sz
));
110 m
->f
|= MP_DESTROYED
;
114 /* --- @mp_copy@ --- *
116 * Arguments: @mp *m@ = pointer to a multiprecision integer
118 * Returns: A copy of the given multiprecision integer.
120 * Use: Copies the given integer. In fact you just get another
121 * reference to the same old one again.
124 mp
*mp_copy(mp
*m
) { return MP_COPY(m
); }
126 /* --- @mp_drop@ --- *
128 * Arguments: @mp *m@ = pointer to a multiprecision integer
132 * Use: Drops a reference to an integer which isn't wanted any more.
133 * If there are no more references, the integer is destroyed.
136 void mp_drop(mp
*m
) { MP_DROP(m
); }
138 /* --- @mp_split@ --- *
140 * Arguments: @mp *m@ = pointer to a multiprecision integer
142 * Returns: A reference to the same integer, possibly with a different
145 * Use: Splits off a modifiable version of the integer referred to.
148 mp
*mp_split(mp
*m
) { MP_SPLIT(m
); return (m
); }
150 /* --- @mp_resize@ --- *
152 * Arguments: @mp *m@ = pointer to a multiprecision integer
153 * @size_t sz@ = new size
157 * Use: Resizes the vector containing the integer's digits. The new
158 * size must be at least as large as the current integer's
159 * length. This isn't really intended for client use.
162 void mp_resize(mp
*m
, size_t sz
) { MP_RESIZE(m
, sz
); }
164 /* --- @mp_ensure@ --- *
166 * Arguments: @mp *m@ = pointer to a multiprecision integer
167 * @size_t sz@ = required size
171 * Use: Ensures that the integer has enough space for @sz@ digits.
172 * The value is not changed.
175 void mp_ensure(mp
*m
, size_t sz
) { MP_ENSURE(m
, sz
); }
177 /* --- @mp_modify@ --- *
179 * Arguments: @mp *m@ = pointer to a multiprecision integer
180 * @size_t sz@ = size required
182 * Returns: Pointer to the integer (possibly different).
184 * Use: Prepares an integer to be overwritten. It's split off from
185 * other references to the same integer, and sufficient space is
189 mp
*mp_modify(mp
*m
, size_t sz
) { MP_MODIFY(m
, sz
); return (m
); }
191 /*----- That's all, folks -------------------------------------------------*/