3 * $Id: mp-mem.c,v 1.1 1999/11/17 18:02:16 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.1 1999/11/17 18:02:16 mdw
34 * New multiprecision integer arithmetic suite.
38 /*----- Header files ------------------------------------------------------*/
42 /*----- Main code ---------------------------------------------------------*/
44 /* --- @mp_create@ --- *
46 * Arguments: @size_t sz@ = size of vector required
48 * Returns: Pointer to pristine new MP structure with enough memory
51 * Use: Creates a new multiprecision integer, initially zero. The
52 * integer has a single reference.
55 mp
*mp_create(size_t sz
)
66 /* --- @mp_build@ --- *
68 * Arguments: @mp *m@ = pointer to an MP block to fill in
69 * @mpw *v@ = pointer to a word array
70 * @mpw *vl@ = pointer just past end of array
74 * Use: Creates a multiprecision integer representing some smallish
75 * number. You must provide storage for the number and dispose
76 * of it when you've finished with it. The number is marked as
77 * constant while it exists.
80 void mp_build(mp
*m
, mpw
*v
, mpw
*vl
)
89 /* --- @mp_destroy@ --- *
91 * Arguments: @mp *m@ = pointer to a multiprecision integer
95 * Use: Destroys a multiprecision integer. The reference count isn't
96 * checked. Don't use this function if you don't know what
97 * you're doing: use @mp_drop@ instead.
100 void mp_destroy(mp
*m
)
105 memset(m
->v
, 0, MPWS(m
->sz
));
110 /* --- @mp_copy@ --- *
112 * Arguments: @mp *m@ = pointer to a multiprecision integer
114 * Returns: A copy of the given multiprecision integer.
116 * Use: Copies the given integer. In fact you just get another
117 * reference to the same old one again.
120 mp
*mp_copy(mp
*m
) { return MP_COPY(m
); }
122 /* --- @mp_drop@ --- *
124 * Arguments: @mp *m@ = pointer to a multiprecision integer
128 * Use: Drops a reference to an integer which isn't wanted any more.
129 * If there are no more references, the integer is destroyed.
132 void mp_drop(mp
*m
) { MP_DROP(m
); }
134 /* --- @mp_split@ --- *
136 * Arguments: @mp *m@ = pointer to a multiprecision integer
138 * Returns: A reference to the same integer, possibly with a different
141 * Use: Splits off a modifiable version of the integer referred to.
144 mp
*mp_split(mp
*m
) { MP_SPLIT(m
); return (m
); }
146 /* --- @mp_resize@ --- *
148 * Arguments: @mp *m@ = pointer to a multiprecision integer
149 * @size_t sz@ = new size
153 * Use: Resizes the vector containing the integer's digits. The new
154 * size must be at least as large as the current integer's
155 * length. This isn't really intended for client use.
158 void mp_resize(mp
*m
, size_t sz
) { MP_RESIZE(m
, sz
); }
160 /* --- @mp_ensure@ --- *
162 * Arguments: @mp *m@ = pointer to a multiprecision integer
163 * @size_t sz@ = required size
167 * Use: Ensures that the integer has enough space for @sz@ digits.
168 * The value is not changed.
171 void mp_ensure(mp
*m
, size_t sz
) { MP_ENSURE(m
, sz
); }
173 /* --- @mp_modify@ --- *
175 * Arguments: @mp *m@ = pointer to a multiprecision integer
176 * @size_t sz@ = size required
178 * Returns: Pointer to the integer (possibly different).
180 * Use: Prepares an integer to be overwritten. It's split off from
181 * other references to the same integer, and sufficient space is
185 mp
*mp_modify(mp
*m
, size_t sz
) { MP_MODIFY(m
, sz
); return (m
); }
187 /*----- That's all, folks -------------------------------------------------*/