3 * $Id: mparena.c,v 1.7 2004/04/08 01:36:15 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 /*----- Header files ------------------------------------------------------*/
36 #include <mLib/arena.h>
42 /*----- Tweakables --------------------------------------------------------*/
44 /* --- @MPARENA_TRIVIAL@ --- *
46 * Make the allocator a passthrough. It immediately calls the underlying
47 * allocation functions rather than attempting to keep track of blocks
51 /* #define MPARENA_TRIVIAL */
53 /* --- @MPARENA_DEBUG@ --- *
55 * The name of an output trace file to which logging information about the
56 * state of arena trees should be written. If unset, no logging is done.
59 /* #define MPARENA_DEBUG "mparena.out" */
61 /*----- Static variables --------------------------------------------------*/
64 static FILE *debugfp
= 0;
66 # define MPARENA_OPENFILE do { \
68 if ((debugfp = fopen(MPARENA_DEBUG, "w")) == 0) { \
69 fprintf(stderr, "couldn't open debug output file\n"); \
77 /*----- Standard arenas ---------------------------------------------------*/
79 mparena mparena_global
= MPARENA_INIT
;
80 mparena mparena_secure
= MPARENA_INIT
;
82 /*----- Main code ---------------------------------------------------------*/
86 * Arguments: @mparena_node *n@ = pointer to tree node to dump
90 * Use: Recursively dumps out the allocation tree.
95 static void tdump(mparena_node
*n
)
102 fprintf(debugfp
, ", %u, ", n
->v
[0]);
110 /* --- @mparena_create@ --- *
112 * Arguments: @mparena *a@ = pointer to arena block
116 * Use: Initializes an MP arena so that blocks can be allocated from
120 void mparena_create(mparena
*a
)
124 a
->a
= &arena_stdlib
;
127 /* --- @mparena_setarena@ --- *
129 * Arguments: @mparena *a@ = pointer to MP arena block
130 * @arena *aa@ = pointer to arena
134 * Use: Sets the underlying arena for an MP arena.
137 extern void mparena_setarena(mparena
*a
, arena
*aa
) { a
->a
= aa
; }
139 /* --- @mparena_destroy@ --- *
141 * Arguments: @mparena *a@ = pointer to arena block
145 * Use: Frees an MP arena, and all the vectors held within it. The
146 * blocks which are currently allocated can be freed into some
150 static void tfree(mparena
*a
, mparena_node
*n
)
160 void mparena_destroy(mparena
*a
)
166 /* --- @mparena_count@ --- *
168 * Arguments: @mparena *a@ = pointer to arena block
170 * Returns: Number of allocated blocks from this arena.
172 * Use: Reports the number of blocks allocated from the arena and not
176 unsigned mparena_count(mparena
*a
)
181 /* --- @mpalloc@ --- *
183 * Arguments: @mparena *a@ = pointer to arena block
184 * @size_t sz@ = number of digits required
186 * Returns: Pointer to a suitably sized block.
188 * Use: Allocates a lump of data suitable for use as an array of MP
192 #ifdef MPARENA_TRIVIAL
194 mpw
*mpalloc(mparena
*a
, size_t sz
)
199 v
= A_ALLOC(a
->a
, MPWS(sz
));
207 mpw
*mpalloc(mparena
*a
, size_t sz
)
209 mparena_node
**nn
, *n
;
216 fprintf(debugfp
, "alloc %u\n before: ", sz
);
217 tdump(a
->root
); putc('\n', debugfp
);
220 /* --- First, find a block which is big enough --- */
226 fputs(" failed\n", debugfp
);
228 if ((v
= A_ALLOC(a
->a
, MPWS(sz
+ 1))) == 0)
239 /* --- Now try to find a smaller block which is suitable --- */
241 while (n
->left
&& n
->left
->v
[0] >= sz
) {
246 /* --- If the block we've got is still too large, start digging --- */
248 if (n
->v
[0] > sz
* 2) {
253 /* --- I've now found a suitable block --- */
257 /* --- Remove this node from the tree --- */
264 mparena_node
*left
= n
->left
;
265 mparena_node
*p
= *nn
= n
->right
;
272 fputs(" after: ", debugfp
);
273 tdump(a
->root
); putc('\n', debugfp
);
276 /* --- Get rid of this node now --- */
285 /* --- @mpfree@ --- *
287 * Arguments: @mparena *a@ = pointer to arena block
288 * @mpw *v@ = pointer to allocated vector
292 * Use: Returns an MP vector to an arena.
295 #ifdef MPARENA_TRIVIAL
297 void mpfree(mparena
*a
, mpw
*v
)
306 void mpfree(mparena
*a
, mpw
*v
)
308 mparena_node
**nn
, *n
;
313 fprintf(debugfp
, "free %u\n before: ", sz
);
314 tdump(a
->root
); putc('\n', debugfp
);
326 n
= CREATE(mparena_node
);
327 n
->left
= n
->right
= 0;
333 fputs(" after: ", debugfp
);
334 tdump(a
->root
); putc('\n', debugfp
);
340 /*----- That's all, folks -------------------------------------------------*/