3 * Allocation and freeing of MP buffers
5 * (c) 1999 Straylight/Edgeware
8 /*----- Licensing notice --------------------------------------------------*
10 * This file is part of Catacomb.
12 * Catacomb is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU Library General Public License as
14 * published by the Free Software Foundation; either version 2 of the
15 * License, or (at your option) any later version.
17 * Catacomb is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU Library General Public License for more details.
22 * You should have received a copy of the GNU Library General Public
23 * License along with Catacomb; if not, write to the Free
24 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
28 /*----- Header files ------------------------------------------------------*/
34 #include <mLib/arena.h>
40 /*----- Tweakables --------------------------------------------------------*/
42 /* --- @MPARENA_TRIVIAL@ --- *
44 * Make the allocator a passthrough. It immediately calls the underlying
45 * allocation functions rather than attempting to keep track of blocks
49 #define MPARENA_TRIVIAL
51 /* --- @MPARENA_DEBUG@ --- *
53 * The name of an output trace file to which logging information about the
54 * state of arena trees should be written. If unset, no logging is done.
57 /* #define MPARENA_DEBUG "mparena.out" */
59 /*----- Static variables --------------------------------------------------*/
62 static FILE *debugfp = 0;
64 # define MPARENA_OPENFILE do { \
66 if ((debugfp = fopen(MPARENA_DEBUG, "w")) == 0) { \
67 fprintf(stderr, "couldn't open debug output file\n"); \
75 /*----- Standard arenas ---------------------------------------------------*/
77 mparena mparena_global = MPARENA_INIT;
78 mparena mparena_secure = MPARENA_INIT;
80 /*----- Main code ---------------------------------------------------------*/
84 * Arguments: @mparena_node *n@ = pointer to tree node to dump
88 * Use: Recursively dumps out the allocation tree.
93 static void tdump(mparena_node *n)
100 fprintf(debugfp, ", %u, ", n->v[0]);
108 /* --- @mparena_create@ --- *
110 * Arguments: @mparena *a@ = pointer to arena block
114 * Use: Initializes an MP arena so that blocks can be allocated from
118 void mparena_create(mparena *a)
122 a->a = &arena_stdlib;
125 /* --- @mparena_setarena@ --- *
127 * Arguments: @mparena *a@ = pointer to MP arena block
128 * @arena *aa@ = pointer to arena
132 * Use: Sets the underlying arena for an MP arena.
135 extern void mparena_setarena(mparena *a, arena *aa) { a->a = aa; }
137 /* --- @mparena_destroy@ --- *
139 * Arguments: @mparena *a@ = pointer to arena block
143 * Use: Frees an MP arena, and all the vectors held within it. The
144 * blocks which are currently allocated can be freed into some
148 static void tfree(mparena *a, mparena_node *n)
158 void mparena_destroy(mparena *a)
164 /* --- @mparena_count@ --- *
166 * Arguments: @mparena *a@ = pointer to arena block
168 * Returns: Number of allocated blocks from this arena.
170 * Use: Reports the number of blocks allocated from the arena and not
174 unsigned mparena_count(mparena *a)
179 /* --- @mpalloc@ --- *
181 * Arguments: @mparena *a@ = pointer to arena block
182 * @size_t sz@ = number of digits required
184 * Returns: Pointer to a suitably sized block.
186 * Use: Allocates a lump of data suitable for use as an array of MP
190 #ifdef MPARENA_TRIVIAL
192 mpw *mpalloc(mparena *a, size_t sz)
197 v = A_ALLOC(a->a, MPWS(sz));
205 mpw *mpalloc(mparena *a, size_t sz)
207 mparena_node **nn, *n;
214 fprintf(debugfp, "alloc %u\n before: ", sz);
215 tdump(a->root); putc('\n', debugfp);
218 /* --- First, find a block which is big enough --- */
224 fputs(" failed\n", debugfp);
226 if ((v = A_ALLOC(a->a, MPWS(sz + 1))) == 0)
237 /* --- Now try to find a smaller block which is suitable --- */
239 while (n->left && n->left->v[0] >= sz) {
244 /* --- If the block we've got is still too large, start digging --- */
246 if (n->v[0] > sz * 2) {
251 /* --- I've now found a suitable block --- */
255 /* --- Remove this node from the tree --- */
262 mparena_node *left = n->left;
263 mparena_node *p = *nn = n->right;
270 fputs(" after: ", debugfp);
271 tdump(a->root); putc('\n', debugfp);
274 /* --- Get rid of this node now --- */
283 /* --- @mpfree@ --- *
285 * Arguments: @mparena *a@ = pointer to arena block
286 * @mpw *v@ = pointer to allocated vector
290 * Use: Returns an MP vector to an arena.
293 #ifdef MPARENA_TRIVIAL
295 void mpfree(mparena *a, mpw *v)
304 void mpfree(mparena *a, mpw *v)
306 mparena_node **nn, *n;
311 fprintf(debugfp, "free %u\n before: ", sz);
312 tdump(a->root); putc('\n', debugfp);
324 n = CREATE(mparena_node);
325 n->left = n->right = 0;
331 fputs(" after: ", debugfp);
332 tdump(a->root); putc('\n', debugfp);
338 /*----- That's all, folks -------------------------------------------------*/