| 1 | /* -*-c-*- |
| 2 | * |
| 3 | * $Id: mp-mem.c,v 1.1 1999/11/17 18:02:16 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.1 1999/11/17 18:02:16 mdw |
| 34 | * New multiprecision integer arithmetic suite. |
| 35 | * |
| 36 | */ |
| 37 | |
| 38 | /*----- Header files ------------------------------------------------------*/ |
| 39 | |
| 40 | #include "mp.h" |
| 41 | |
| 42 | /*----- Main code ---------------------------------------------------------*/ |
| 43 | |
| 44 | /* --- @mp_create@ --- * |
| 45 | * |
| 46 | * Arguments: @size_t sz@ = size of vector required |
| 47 | * |
| 48 | * Returns: Pointer to pristine new MP structure with enough memory |
| 49 | * bolted onto it. |
| 50 | * |
| 51 | * Use: Creates a new multiprecision integer, initially zero. The |
| 52 | * integer has a single reference. |
| 53 | */ |
| 54 | |
| 55 | mp *mp_create(size_t sz) |
| 56 | { |
| 57 | mp *m = CREATE(mp); |
| 58 | m->v = MP_ALLOC(sz); |
| 59 | m->vl = m->v + sz; |
| 60 | m->sz = sz; |
| 61 | m->f = MP_UNDEF; |
| 62 | m->ref = 1; |
| 63 | return (m); |
| 64 | } |
| 65 | |
| 66 | /* --- @mp_build@ --- * |
| 67 | * |
| 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 |
| 71 | * |
| 72 | * Returns: --- |
| 73 | * |
| 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. |
| 78 | */ |
| 79 | |
| 80 | void mp_build(mp *m, mpw *v, mpw *vl) |
| 81 | { |
| 82 | m->v = v; |
| 83 | m->vl = vl; |
| 84 | m->sz = vl - v; |
| 85 | m->f = MP_CONST; |
| 86 | m->ref = 1; |
| 87 | } |
| 88 | |
| 89 | /* --- @mp_destroy@ --- * |
| 90 | * |
| 91 | * Arguments: @mp *m@ = pointer to a multiprecision integer |
| 92 | * |
| 93 | * Returns: --- |
| 94 | * |
| 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. |
| 98 | */ |
| 99 | |
| 100 | void mp_destroy(mp *m) |
| 101 | { |
| 102 | if (m->f & MP_CONST) |
| 103 | return; |
| 104 | if (m->f & MP_BURN) |
| 105 | memset(m->v, 0, MPWS(m->sz)); |
| 106 | MP_FREE(m->v); |
| 107 | DESTROY(m); |
| 108 | } |
| 109 | |
| 110 | /* --- @mp_copy@ --- * |
| 111 | * |
| 112 | * Arguments: @mp *m@ = pointer to a multiprecision integer |
| 113 | * |
| 114 | * Returns: A copy of the given multiprecision integer. |
| 115 | * |
| 116 | * Use: Copies the given integer. In fact you just get another |
| 117 | * reference to the same old one again. |
| 118 | */ |
| 119 | |
| 120 | mp *mp_copy(mp *m) { return MP_COPY(m); } |
| 121 | |
| 122 | /* --- @mp_drop@ --- * |
| 123 | * |
| 124 | * Arguments: @mp *m@ = pointer to a multiprecision integer |
| 125 | * |
| 126 | * Returns: --- |
| 127 | * |
| 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. |
| 130 | */ |
| 131 | |
| 132 | void mp_drop(mp *m) { MP_DROP(m); } |
| 133 | |
| 134 | /* --- @mp_split@ --- * |
| 135 | * |
| 136 | * Arguments: @mp *m@ = pointer to a multiprecision integer |
| 137 | * |
| 138 | * Returns: A reference to the same integer, possibly with a different |
| 139 | * address. |
| 140 | * |
| 141 | * Use: Splits off a modifiable version of the integer referred to. |
| 142 | */ |
| 143 | |
| 144 | mp *mp_split(mp *m) { MP_SPLIT(m); return (m); } |
| 145 | |
| 146 | /* --- @mp_resize@ --- * |
| 147 | * |
| 148 | * Arguments: @mp *m@ = pointer to a multiprecision integer |
| 149 | * @size_t sz@ = new size |
| 150 | * |
| 151 | * Returns: --- |
| 152 | * |
| 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. |
| 156 | */ |
| 157 | |
| 158 | void mp_resize(mp *m, size_t sz) { MP_RESIZE(m, sz); } |
| 159 | |
| 160 | /* --- @mp_ensure@ --- * |
| 161 | * |
| 162 | * Arguments: @mp *m@ = pointer to a multiprecision integer |
| 163 | * @size_t sz@ = required size |
| 164 | * |
| 165 | * Returns: --- |
| 166 | * |
| 167 | * Use: Ensures that the integer has enough space for @sz@ digits. |
| 168 | * The value is not changed. |
| 169 | */ |
| 170 | |
| 171 | void mp_ensure(mp *m, size_t sz) { MP_ENSURE(m, sz); } |
| 172 | |
| 173 | /* --- @mp_modify@ --- * |
| 174 | * |
| 175 | * Arguments: @mp *m@ = pointer to a multiprecision integer |
| 176 | * @size_t sz@ = size required |
| 177 | * |
| 178 | * Returns: Pointer to the integer (possibly different). |
| 179 | * |
| 180 | * Use: Prepares an integer to be overwritten. It's split off from |
| 181 | * other references to the same integer, and sufficient space is |
| 182 | * allocated. |
| 183 | */ |
| 184 | |
| 185 | mp *mp_modify(mp *m, size_t sz) { MP_MODIFY(m, sz); return (m); } |
| 186 | |
| 187 | /*----- That's all, folks -------------------------------------------------*/ |