3 * $Id: mparena.c,v 1.1 1999/11/17 18:02:16 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.1 1999/11/17 18:02:16 mdw
34 * New multiprecision integer arithmetic suite.
38 /*----- Header files ------------------------------------------------------*/
44 #include <mLib/alloc.h>
49 /*----- Default allocator -------------------------------------------------*/
51 static void *defalloc(mparena
*a
, size_t sz
) { return xmalloc(sz
); }
52 static void deffree(mparena
*a
, void *p
) { free(p
); }
54 mparena_ops mparena_defops
= { defalloc
, deffree
};
56 /*----- Static variables --------------------------------------------------*/
58 static mparena arena
= { 0, &mparena_defops
};
60 #define MPARENA_RESOLVE(a) do { \
61 if ((a) == MPARENA_GLOBAL) \
65 /*----- Main code ---------------------------------------------------------*/
69 * Arguments: @mparena_node *n@ = pointer to tree node to dump
73 * Use: Recursively dumps out the allocation tree.
76 static void tdump(mparena_node
*n
)
83 printf(", %u, ", n
->v
[0]);
89 /* --- @mparena_create@ --- *
91 * Arguments: @mparena *a@ = pointer to arena block
95 * Use: Initializes an MP arena so that blocks can be allocated from
99 void mparena_create(mparena
*a
)
102 a
->ops
= &mparena_defops
;
105 /* --- @mparena_setops@ --- *
107 * Arguments: @mparena *a@ = pointer to arena block
108 * @mparena_ops *ops@ = pointer to operations block or null
110 * Returns: The previous operations block.
112 * Use: Sets or queries the operations attached to an arena.
115 mparena_ops
*mparena_setops(mparena
*a
, mparena_ops
*ops
)
125 /* --- @mparena_destroy@ --- *
127 * Arguments: @mparena *a@ = pointer to arena block
131 * Use: Frees an MP arena, and all the vectors held within it. The
132 * blocks which are currently allocated can be freed into some
136 static void tfree(mparena
*a
, mparena_node
*n
)
138 a
->ops
->free(a
, n
->v
);
146 void mparena_destroy(mparena
*a
)
152 /* --- @mpalloc@ --- *
154 * Arguments: @mparena *a@ = pointer to arena block
155 * @size_t sz@ = number of digits required
157 * Returns: Pointer to a suitably sized block.
159 * Use: Allocates a lump of data suitable for use as an array of MP
163 mpw
*mpalloc(mparena
*a
, size_t sz
)
165 mparena_node
**nn
, *n
;
172 printf("*** alloc %u\n", sz
);
173 tdump(a
->root
); putchar('\n');
176 /* --- First, find a block which is big enough --- */
181 v
= a
->ops
->alloc(a
, MPWS(sz
+ 1));
190 /* --- Now try to find a smaller block which is suitable --- */
192 while (n
->left
&& n
->left
->v
[0] >= sz
) {
197 /* --- If the block we've got is still too large, start digging --- */
199 if (n
->v
[0] >= sz
* 2) {
204 /* --- I've now found a suitable block --- */
208 /* --- Remove this node from the tree --- */
215 mparena_node
*left
= n
->left
;
216 mparena_node
*p
= *nn
= n
->right
;
222 /* --- Get rid of this node now --- */
228 /* --- @mpfree@ --- *
230 * Arguments: @mparena *a@ = pointer to arena block
231 * @mpw *v@ = pointer to allocated vector
235 * Use: Returns an MP vector to an arena. It doesn't have to be
236 * returned to the arena from which it was allocated.
239 void mpfree(mparena
*a
, mpw
*v
)
241 mparena_node
**nn
, *n
;
255 n
= CREATE(mparena_node
);
256 n
->left
= n
->right
= 0;
261 printf("*** free %u\n", sz
);
262 tdump(a
->root
); putchar('\n');
266 /*----- That's all, folks -------------------------------------------------*/