3 * $Id: mparena.c,v 1.2 1999/11/21 22:14:19 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.2 1999/11/21 22:14:19 mdw
34 * Fix bug. Improve diagnostic capabilities.
36 * Revision 1.1 1999/11/17 18:02:16 mdw
37 * New multiprecision integer arithmetic suite.
41 /*----- Header files ------------------------------------------------------*/
47 #include <mLib/alloc.h>
52 /*----- Tweakables --------------------------------------------------------*/
54 /* #define MPARENA_TRIVIAL */
56 /* #define MPARENA_DEBUG "mparena.out" */
58 /*----- Default allocator -------------------------------------------------*/
60 static void *defalloc(mparena
*a
, size_t sz
) { return xmalloc(sz
); }
61 static void deffree(mparena
*a
, void *p
) { free(p
); }
63 mparena_ops mparena_defops
= { defalloc
, deffree
};
65 /*----- Static variables --------------------------------------------------*/
68 static FILE *debugfp
= 0;
70 # define MPARENA_OPENFILE do { \
72 if ((debugfp = fopen(MPARENA_DEBUG, "w")) == 0) { \
73 fprintf(stderr, "couldn't open debug output file\n"); \
81 static mparena arena
= { 0, &mparena_defops
};
83 #define MPARENA_RESOLVE(a) do { \
84 if ((a) == MPARENA_GLOBAL) \
88 /*----- Main code ---------------------------------------------------------*/
92 * Arguments: @mparena_node *n@ = pointer to tree node to dump
96 * Use: Recursively dumps out the allocation tree.
101 static void tdump(mparena_node
*n
)
108 fprintf(debugfp
, ", %u, ", n
->v
[0]);
116 /* --- @mparena_create@ --- *
118 * Arguments: @mparena *a@ = pointer to arena block
122 * Use: Initializes an MP arena so that blocks can be allocated from
126 void mparena_create(mparena
*a
)
129 a
->ops
= &mparena_defops
;
132 /* --- @mparena_setops@ --- *
134 * Arguments: @mparena *a@ = pointer to arena block
135 * @mparena_ops *ops@ = pointer to operations block or null
137 * Returns: The previous operations block.
139 * Use: Sets or queries the operations attached to an arena.
142 mparena_ops
*mparena_setops(mparena
*a
, mparena_ops
*ops
)
152 /* --- @mparena_destroy@ --- *
154 * Arguments: @mparena *a@ = pointer to arena block
158 * Use: Frees an MP arena, and all the vectors held within it. The
159 * blocks which are currently allocated can be freed into some
163 static void tfree(mparena
*a
, mparena_node
*n
)
165 a
->ops
->free(a
, n
->v
);
173 void mparena_destroy(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
)
195 return (a
->ops
->alloc(a
, MPWS(sz
)));
200 mpw
*mpalloc(mparena
*a
, size_t sz
)
202 mparena_node
**nn
, *n
;
210 fprintf(debugfp
, "alloc %u\n before: ", sz
);
211 tdump(a
->root
); putc('\n', debugfp
);
216 /* --- First, find a block which is big enough --- */
222 fputs(" failed\n", debugfp
);
224 v
= a
->ops
->alloc(a
, MPWS(sz
+ 1));
233 /* --- Now try to find a smaller block which is suitable --- */
235 while (n
->left
&& n
->left
->v
[0] >= sz
) {
240 /* --- If the block we've got is still too large, start digging --- */
242 if (n
->v
[0] > sz
* 2) {
247 /* --- I've now found a suitable block --- */
251 /* --- Remove this node from the tree --- */
258 mparena_node
*left
= n
->left
;
259 mparena_node
*p
= *nn
= n
->right
;
266 fputs(" after: ", debugfp
);
267 tdump(a
->root
); putc('\n', debugfp
);
270 /* --- Get rid of this node now --- */
278 /* --- @mpfree@ --- *
280 * Arguments: @mparena *a@ = pointer to arena block
281 * @mpw *v@ = pointer to allocated vector
285 * Use: Returns an MP vector to an arena. It doesn't have to be
286 * returned to the arena from which it was allocated.
289 #ifdef MPARENA_TRIVIAL
291 void mpfree(mparena
*a
, mpw
*v
)
299 void mpfree(mparena
*a
, mpw
*v
)
301 mparena_node
**nn
, *n
;
308 fprintf(debugfp
, "free %u\n before: ", sz
);
309 tdump(a
->root
); putc('\n', debugfp
);
321 n
= CREATE(mparena_node
);
322 n
->left
= n
->right
= 0;
327 fputs(" after: ", debugfp
);
328 tdump(a
->root
); putc('\n', debugfp
);
334 /*----- That's all, folks -------------------------------------------------*/