3 * $Id: mparena.c,v 1.3 1999/11/22 13:58:00 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.3 1999/11/22 13:58:00 mdw
34 * Document the tweakables.
36 * Revision 1.2 1999/11/21 22:14:19 mdw
37 * Fix bug. Improve diagnostic capabilities.
39 * Revision 1.1 1999/11/17 18:02:16 mdw
40 * New multiprecision integer arithmetic suite.
44 /*----- Header files ------------------------------------------------------*/
50 #include <mLib/alloc.h>
55 /*----- Tweakables --------------------------------------------------------*/
57 /* --- @MPARENA_TRIVIAL@ --- *
59 * Make the allocator a passthrough. It immediately calls the underlying
60 * allocation functions rather than attempting to keep track of blocks
64 /* #define MPARENA_TRIVIAL */
66 /* --- @MPARENA_DEBUG@ --- *
68 * The name of an output trace file to which logging information about the
69 * state of arena trees should be written. If unset, no logging is done.
72 /* #define MPARENA_DEBUG "mparena.out" */
74 /*----- Default allocator -------------------------------------------------*/
76 static void *defalloc(mparena
*a
, size_t sz
) { return xmalloc(sz
); }
77 static void deffree(mparena
*a
, void *p
) { free(p
); }
79 mparena_ops mparena_defops
= { defalloc
, deffree
};
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 static mparena arena
= { 0, &mparena_defops
};
99 #define MPARENA_RESOLVE(a) do { \
100 if ((a) == MPARENA_GLOBAL) \
104 /*----- Main code ---------------------------------------------------------*/
108 * Arguments: @mparena_node *n@ = pointer to tree node to dump
112 * Use: Recursively dumps out the allocation tree.
117 static void tdump(mparena_node
*n
)
124 fprintf(debugfp
, ", %u, ", n
->v
[0]);
132 /* --- @mparena_create@ --- *
134 * Arguments: @mparena *a@ = pointer to arena block
138 * Use: Initializes an MP arena so that blocks can be allocated from
142 void mparena_create(mparena
*a
)
145 a
->ops
= &mparena_defops
;
148 /* --- @mparena_setops@ --- *
150 * Arguments: @mparena *a@ = pointer to arena block
151 * @mparena_ops *ops@ = pointer to operations block or null
153 * Returns: The previous operations block.
155 * Use: Sets or queries the operations attached to an arena.
158 mparena_ops
*mparena_setops(mparena
*a
, mparena_ops
*ops
)
168 /* --- @mparena_destroy@ --- *
170 * Arguments: @mparena *a@ = pointer to arena block
174 * Use: Frees an MP arena, and all the vectors held within it. The
175 * blocks which are currently allocated can be freed into some
179 static void tfree(mparena
*a
, mparena_node
*n
)
181 a
->ops
->free(a
, n
->v
);
189 void mparena_destroy(mparena
*a
)
195 /* --- @mpalloc@ --- *
197 * Arguments: @mparena *a@ = pointer to arena block
198 * @size_t sz@ = number of digits required
200 * Returns: Pointer to a suitably sized block.
202 * Use: Allocates a lump of data suitable for use as an array of MP
206 #ifdef MPARENA_TRIVIAL
208 mpw
*mpalloc(mparena
*a
, size_t sz
)
211 return (a
->ops
->alloc(a
, MPWS(sz
)));
216 mpw
*mpalloc(mparena
*a
, size_t sz
)
218 mparena_node
**nn
, *n
;
226 fprintf(debugfp
, "alloc %u\n before: ", sz
);
227 tdump(a
->root
); putc('\n', debugfp
);
232 /* --- First, find a block which is big enough --- */
238 fputs(" failed\n", debugfp
);
240 v
= a
->ops
->alloc(a
, MPWS(sz
+ 1));
249 /* --- Now try to find a smaller block which is suitable --- */
251 while (n
->left
&& n
->left
->v
[0] >= sz
) {
256 /* --- If the block we've got is still too large, start digging --- */
258 if (n
->v
[0] > sz
* 2) {
263 /* --- I've now found a suitable block --- */
267 /* --- Remove this node from the tree --- */
274 mparena_node
*left
= n
->left
;
275 mparena_node
*p
= *nn
= n
->right
;
282 fputs(" after: ", debugfp
);
283 tdump(a
->root
); putc('\n', debugfp
);
286 /* --- Get rid of this node now --- */
294 /* --- @mpfree@ --- *
296 * Arguments: @mparena *a@ = pointer to arena block
297 * @mpw *v@ = pointer to allocated vector
301 * Use: Returns an MP vector to an arena. It doesn't have to be
302 * returned to the arena from which it was allocated.
305 #ifdef MPARENA_TRIVIAL
307 void mpfree(mparena
*a
, mpw
*v
)
315 void mpfree(mparena
*a
, mpw
*v
)
317 mparena_node
**nn
, *n
;
324 fprintf(debugfp
, "free %u\n before: ", sz
);
325 tdump(a
->root
); putc('\n', debugfp
);
337 n
= CREATE(mparena_node
);
338 n
->left
= n
->right
= 0;
343 fputs(" after: ", debugfp
);
344 tdump(a
->root
); putc('\n', debugfp
);
350 /*----- That's all, folks -------------------------------------------------*/