3 * $Id: mparena.c,v 1.5 2000/06/17 11:35:48 mdw Exp $
5 * Allocation and freeing of MP buffers
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.5 2000/06/17 11:35:48 mdw
34 * Overhaul to use mLib's arena system underneath.
36 * Revision 1.4 1999/12/10 23:28:52 mdw
37 * Memory allocation counting.
39 * Revision 1.3 1999/11/22 13:58:00 mdw
40 * Document the tweakables.
42 * Revision 1.2 1999/11/21 22:14:19 mdw
43 * Fix bug. Improve diagnostic capabilities.
45 * Revision 1.1 1999/11/17 18:02:16 mdw
46 * New multiprecision integer arithmetic suite.
50 /*----- Header files ------------------------------------------------------*/
56 #include <mLib/arena.h>
62 /*----- Tweakables --------------------------------------------------------*/
64 /* --- @MPARENA_TRIVIAL@ --- *
66 * Make the allocator a passthrough. It immediately calls the underlying
67 * allocation functions rather than attempting to keep track of blocks
71 /* #define MPARENA_TRIVIAL */
73 /* --- @MPARENA_DEBUG@ --- *
75 * The name of an output trace file to which logging information about the
76 * state of arena trees should be written. If unset, no logging is done.
79 /* #define MPARENA_DEBUG "mparena.out" */
81 /*----- Static variables --------------------------------------------------*/
84 static FILE *debugfp
= 0;
86 # define MPARENA_OPENFILE do { \
88 if ((debugfp = fopen(MPARENA_DEBUG, "w")) == 0) { \
89 fprintf(stderr, "couldn't open debug output file\n"); \
97 /*----- Standard arenas ---------------------------------------------------*/
99 mparena mparena_global
= MPARENA_INIT
;
100 mparena mparena_secure
= MPARENA_INIT
;
102 /*----- Main code ---------------------------------------------------------*/
106 * Arguments: @mparena_node *n@ = pointer to tree node to dump
110 * Use: Recursively dumps out the allocation tree.
115 static void tdump(mparena_node
*n
)
122 fprintf(debugfp
, ", %u, ", n
->v
[0]);
130 /* --- @mparena_create@ --- *
132 * Arguments: @mparena *a@ = pointer to arena block
136 * Use: Initializes an MP arena so that blocks can be allocated from
140 void mparena_create(mparena
*a
)
144 a
->a
= &arena_stdlib
;
147 /* --- @mparena_setarena@ --- *
149 * Arguments: @mparena *a@ = pointer to MP arena block
150 * @arena *aa@ = pointer to arena
154 * Use: Sets the underlying arena for an MP arena.
157 extern void mparena_setarena(mparena
*a
, arena
*aa
) { a
->a
= aa
; }
159 /* --- @mparena_destroy@ --- *
161 * Arguments: @mparena *a@ = pointer to arena block
165 * Use: Frees an MP arena, and all the vectors held within it. The
166 * blocks which are currently allocated can be freed into some
170 static void tfree(mparena
*a
, mparena_node
*n
)
180 void mparena_destroy(mparena
*a
)
186 /* --- @mparena_count@ --- *
188 * Arguments: @mparena *a@ = pointer to arena block
190 * Returns: Number of allocated blocks from this arena.
192 * Use: Reports the number of blocks allocated from the arena and not
196 unsigned mparena_count(mparena
*a
)
201 /* --- @mpalloc@ --- *
203 * Arguments: @mparena *a@ = pointer to arena block
204 * @size_t sz@ = number of digits required
206 * Returns: Pointer to a suitably sized block.
208 * Use: Allocates a lump of data suitable for use as an array of MP
212 #ifdef MPARENA_TRIVIAL
214 mpw
*mpalloc(mparena
*a
, size_t sz
)
217 v
= A_ALLOC(a
->a
, MPWS(sz
));
225 mpw
*mpalloc(mparena
*a
, size_t sz
)
227 mparena_node
**nn
, *n
;
234 fprintf(debugfp
, "alloc %u\n before: ", sz
);
235 tdump(a
->root
); putc('\n', debugfp
);
238 /* --- First, find a block which is big enough --- */
244 fputs(" failed\n", debugfp
);
246 if ((v
= A_ALLOC(a
->a
, MPWS(sz
+ 1))) == 0)
257 /* --- Now try to find a smaller block which is suitable --- */
259 while (n
->left
&& n
->left
->v
[0] >= sz
) {
264 /* --- If the block we've got is still too large, start digging --- */
266 if (n
->v
[0] > sz
* 2) {
271 /* --- I've now found a suitable block --- */
275 /* --- Remove this node from the tree --- */
282 mparena_node
*left
= n
->left
;
283 mparena_node
*p
= *nn
= n
->right
;
290 fputs(" after: ", debugfp
);
291 tdump(a
->root
); putc('\n', debugfp
);
294 /* --- Get rid of this node now --- */
303 /* --- @mpfree@ --- *
305 * Arguments: @mparena *a@ = pointer to arena block
306 * @mpw *v@ = pointer to allocated vector
310 * Use: Returns an MP vector to an arena.
313 #ifdef MPARENA_TRIVIAL
315 void mpfree(mparena
*a
, mpw
*v
)
322 void mpfree(mparena
*a
, mpw
*v
)
324 mparena_node
**nn
, *n
;
329 fprintf(debugfp
, "free %u\n before: ", sz
);
330 tdump(a
->root
); putc('\n', debugfp
);
342 n
= CREATE(mparena_node
);
343 n
->left
= n
->right
= 0;
349 fputs(" after: ", debugfp
);
350 tdump(a
->root
); putc('\n', debugfp
);
356 /*----- That's all, folks -------------------------------------------------*/