General robustification.
[u/mdw/catacomb] / mparena.c
CommitLineData
d3409d5e 1/* -*-c-*-
2 *
02d7884d 3 * $Id: mparena.c,v 1.6 2004/04/03 03:32:05 mdw Exp $
d3409d5e 4 *
5 * Allocation and freeing of MP buffers
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: mparena.c,v $
02d7884d 33 * Revision 1.6 2004/04/03 03:32:05 mdw
34 * General robustification.
35 *
d4745354 36 * Revision 1.5 2000/06/17 11:35:48 mdw
37 * Overhaul to use mLib's arena system underneath.
38 *
97d21728 39 * Revision 1.4 1999/12/10 23:28:52 mdw
40 * Memory allocation counting.
41 *
48d66950 42 * Revision 1.3 1999/11/22 13:58:00 mdw
43 * Document the tweakables.
44 *
12a9a6e5 45 * Revision 1.2 1999/11/21 22:14:19 mdw
46 * Fix bug. Improve diagnostic capabilities.
47 *
d3409d5e 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 <stdio.h>
56#include <stdlib.h>
57#include <string.h>
58
d4745354 59#include <mLib/arena.h>
60#include <mLib/exc.h>
d3409d5e 61#include <mLib/sub.h>
62
63#include "mparena.h"
64
12a9a6e5 65/*----- Tweakables --------------------------------------------------------*/
66
48d66950 67/* --- @MPARENA_TRIVIAL@ --- *
68 *
69 * Make the allocator a passthrough. It immediately calls the underlying
70 * allocation functions rather than attempting to keep track of blocks
71 * itself.
72 */
73
12a9a6e5 74/* #define MPARENA_TRIVIAL */
75
48d66950 76/* --- @MPARENA_DEBUG@ --- *
77 *
78 * The name of an output trace file to which logging information about the
79 * state of arena trees should be written. If unset, no logging is done.
80 */
81
12a9a6e5 82/* #define MPARENA_DEBUG "mparena.out" */
83
d3409d5e 84/*----- Static variables --------------------------------------------------*/
85
12a9a6e5 86#ifdef MPARENA_DEBUG
87 static FILE *debugfp = 0;
88
89# define MPARENA_OPENFILE do { \
90 if (!debugfp) { \
91 if ((debugfp = fopen(MPARENA_DEBUG, "w")) == 0) { \
92 fprintf(stderr, "couldn't open debug output file\n"); \
93 exit(EXIT_FAILURE); \
94 } \
95 } \
96 } while (0)
97
98#endif
99
d4745354 100/*----- Standard arenas ---------------------------------------------------*/
d3409d5e 101
d4745354 102mparena mparena_global = MPARENA_INIT;
103mparena mparena_secure = MPARENA_INIT;
d3409d5e 104
105/*----- Main code ---------------------------------------------------------*/
106
107/* --- @tdump@ --- *
108 *
109 * Arguments: @mparena_node *n@ = pointer to tree node to dump
110 *
111 * Returns: ---
112 *
113 * Use: Recursively dumps out the allocation tree.
114 */
115
12a9a6e5 116#ifdef MPARENA_DEBUG
117
d3409d5e 118static void tdump(mparena_node *n)
119{
120 if (!n)
12a9a6e5 121 putc('*', debugfp);
d3409d5e 122 else {
12a9a6e5 123 putc('(', debugfp);
d3409d5e 124 tdump(n->left);
12a9a6e5 125 fprintf(debugfp, ", %u, ", n->v[0]);
d3409d5e 126 tdump(n->right);
12a9a6e5 127 putc(')', debugfp);
d3409d5e 128 }
129}
130
12a9a6e5 131#endif
132
d3409d5e 133/* --- @mparena_create@ --- *
134 *
135 * Arguments: @mparena *a@ = pointer to arena block
136 *
137 * Returns: ---
138 *
139 * Use: Initializes an MP arena so that blocks can be allocated from
140 * it.
141 */
142
143void mparena_create(mparena *a)
144{
145 a->root = 0;
97d21728 146 a->n = 0;
d4745354 147 a->a = &arena_stdlib;
d3409d5e 148}
149
d4745354 150/* --- @mparena_setarena@ --- *
d3409d5e 151 *
d4745354 152 * Arguments: @mparena *a@ = pointer to MP arena block
153 * @arena *aa@ = pointer to arena
d3409d5e 154 *
d4745354 155 * Returns: ---
d3409d5e 156 *
d4745354 157 * Use: Sets the underlying arena for an MP arena.
d3409d5e 158 */
159
d4745354 160extern void mparena_setarena(mparena *a, arena *aa) { a->a = aa; }
d3409d5e 161
162/* --- @mparena_destroy@ --- *
163 *
164 * Arguments: @mparena *a@ = pointer to arena block
165 *
166 * Returns: ---
167 *
168 * Use: Frees an MP arena, and all the vectors held within it. The
169 * blocks which are currently allocated can be freed into some
170 * other arena.
171 */
172
173static void tfree(mparena *a, mparena_node *n)
174{
d4745354 175 A_FREE(a->a, n->v);
d3409d5e 176 if (n->left)
177 tfree(a, n->left);
178 if (n->right)
179 tfree(a, n->right);
180 DESTROY(n);
181}
182
183void mparena_destroy(mparena *a)
184{
185 tfree(a, a->root);
186 a->root = 0;
187}
188
97d21728 189/* --- @mparena_count@ --- *
190 *
191 * Arguments: @mparena *a@ = pointer to arena block
192 *
193 * Returns: Number of allocated blocks from this arena.
194 *
195 * Use: Reports the number of blocks allocated from the arena and not
196 * yet freed.
197 */
198
199unsigned mparena_count(mparena *a)
200{
97d21728 201 return (a->n);
202}
203
d3409d5e 204/* --- @mpalloc@ --- *
205 *
206 * Arguments: @mparena *a@ = pointer to arena block
207 * @size_t sz@ = number of digits required
208 *
209 * Returns: Pointer to a suitably sized block.
210 *
211 * Use: Allocates a lump of data suitable for use as an array of MP
212 * digits.
213 */
214
12a9a6e5 215#ifdef MPARENA_TRIVIAL
216
217mpw *mpalloc(mparena *a, size_t sz)
218{
d4745354 219 mpw *v;
02d7884d 220 if (!sz) return (0);
221 a->n++;
d4745354 222 v = A_ALLOC(a->a, MPWS(sz));
223 if (!v)
224 THROW(EXC_NOMEM);
225 return (v);
12a9a6e5 226}
227
228#else
229
d3409d5e 230mpw *mpalloc(mparena *a, size_t sz)
231{
232 mparena_node **nn, *n;
233 mpw *v;
234
d3409d5e 235 nn = &a->root;
236
12a9a6e5 237#ifdef MPARENA_DEBUG
238 MPARENA_OPENFILE;
239 fprintf(debugfp, "alloc %u\n before: ", sz);
240 tdump(a->root); putc('\n', debugfp);
d3409d5e 241#endif
242
243 /* --- First, find a block which is big enough --- */
244
245again:
246 n = *nn;
247 if (!n) {
12a9a6e5 248#ifdef MPARENA_DEBUG
249 fputs(" failed\n", debugfp);
250#endif
d4745354 251 if ((v = A_ALLOC(a->a, MPWS(sz + 1))) == 0)
252 THROW(EXC_NOMEM);
d3409d5e 253 v[0] = sz;
97d21728 254 a->n++;
d3409d5e 255 return (v + 1);
256 }
257 if (n->v[0] < sz) {
258 nn = &n->right;
259 goto again;
260 }
261
262 /* --- Now try to find a smaller block which is suitable --- */
263
264 while (n->left && n->left->v[0] >= sz) {
265 nn = &n->left;
266 n = *nn;
267 }
268
269 /* --- If the block we've got is still too large, start digging --- */
270
12a9a6e5 271 if (n->v[0] > sz * 2) {
d3409d5e 272 nn = &n->left;
273 goto again;
274 }
275
276 /* --- I've now found a suitable block --- */
277
278 v = n->v;
279
280 /* --- Remove this node from the tree --- */
281
282 if (!n->left)
283 *nn = n->right;
284 else if (!n->right)
285 *nn = n->left;
286 else {
287 mparena_node *left = n->left;
288 mparena_node *p = *nn = n->right;
289 while (p->left)
290 p = p->left;
291 p->left = left;
292 }
293
12a9a6e5 294#ifdef MPARENA_DEBUG
295 fputs(" after: ", debugfp);
296 tdump(a->root); putc('\n', debugfp);
297#endif
298
d3409d5e 299 /* --- Get rid of this node now --- */
300
301 DESTROY(n);
97d21728 302 a->n++;
d3409d5e 303 return (v + 1);
304}
305
12a9a6e5 306#endif
307
d3409d5e 308/* --- @mpfree@ --- *
309 *
310 * Arguments: @mparena *a@ = pointer to arena block
311 * @mpw *v@ = pointer to allocated vector
312 *
313 * Returns: ---
314 *
97d21728 315 * Use: Returns an MP vector to an arena.
d3409d5e 316 */
317
12a9a6e5 318#ifdef MPARENA_TRIVIAL
319
320void mpfree(mparena *a, mpw *v)
321{
02d7884d 322 if (!v) return;
323 a->n--;
d4745354 324 A_FREE(a->a, v);
12a9a6e5 325}
326
327#else
328
d3409d5e 329void mpfree(mparena *a, mpw *v)
330{
331 mparena_node **nn, *n;
332 size_t sz = *--v;
333
12a9a6e5 334#ifdef MPARENA_DEBUG
335 MPARENA_OPENFILE;
336 fprintf(debugfp, "free %u\n before: ", sz);
337 tdump(a->root); putc('\n', debugfp);
338#endif
339
340 nn = &a->root;
d3409d5e 341 while (*nn) {
342 n = *nn;
343 if (n->v[0] > sz)
344 nn = &n->left;
345 else
346 nn = &n->right;
347 }
348
349 n = CREATE(mparena_node);
350 n->left = n->right = 0;
351 n->v = v;
352 *nn = n;
97d21728 353 a->n--;
d3409d5e 354
12a9a6e5 355#ifdef MPARENA_DEBUG
356 fputs(" after: ", debugfp);
357 tdump(a->root); putc('\n', debugfp);
d3409d5e 358#endif
359}
360
12a9a6e5 361#endif
362
d3409d5e 363/*----- That's all, folks -------------------------------------------------*/