3 * $Id: mparena.c,v 1.4 1999/12/10 23:28:52 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.4 1999/12/10 23:28:52 mdw
34 * Memory allocation counting.
36 * Revision 1.3 1999/11/22 13:58:00 mdw
37 * Document the tweakables.
39 * Revision 1.2 1999/11/21 22:14:19 mdw
40 * Fix bug. Improve diagnostic capabilities.
42 * Revision 1.1 1999/11/17 18:02:16 mdw
43 * New multiprecision integer arithmetic suite.
47 /*----- Header files ------------------------------------------------------*/
53 #include <mLib/alloc.h>
58 /*----- Tweakables --------------------------------------------------------*/
60 /* --- @MPARENA_TRIVIAL@ --- *
62 * Make the allocator a passthrough. It immediately calls the underlying
63 * allocation functions rather than attempting to keep track of blocks
67 /* #define MPARENA_TRIVIAL */
69 /* --- @MPARENA_DEBUG@ --- *
71 * The name of an output trace file to which logging information about the
72 * state of arena trees should be written. If unset, no logging is done.
75 /* #define MPARENA_DEBUG "mparena.out" */
77 /*----- Default allocator -------------------------------------------------*/
79 static void *defalloc(mparena
*a
, size_t sz
) { return xmalloc(sz
); }
80 static void deffree(mparena
*a
, void *p
) { free(p
); }
82 mparena_ops mparena_defaultops
= { defalloc
, deffree
};
84 /*----- Static variables --------------------------------------------------*/
87 static FILE *debugfp
= 0;
89 # define MPARENA_OPENFILE do { \
91 if ((debugfp = fopen(MPARENA_DEBUG, "w")) == 0) { \
92 fprintf(stderr, "couldn't open debug output file\n"); \
100 static mparena arena
= MPARENA_INIT
;
102 #define MPARENA_RESOLVE(a) do { \
103 if ((a) == MPARENA_GLOBAL) \
107 /*----- Main code ---------------------------------------------------------*/
111 * Arguments: @mparena_node *n@ = pointer to tree node to dump
115 * Use: Recursively dumps out the allocation tree.
120 static void tdump(mparena_node
*n
)
127 fprintf(debugfp
, ", %u, ", n
->v
[0]);
135 /* --- @mparena_create@ --- *
137 * Arguments: @mparena *a@ = pointer to arena block
141 * Use: Initializes an MP arena so that blocks can be allocated from
145 void mparena_create(mparena
*a
)
149 a
->ops
= &mparena_defaultops
;
152 /* --- @mparena_setops@ --- *
154 * Arguments: @mparena *a@ = pointer to arena block
155 * @mparena_ops *ops@ = pointer to operations block or null
157 * Returns: The previous operations block.
159 * Use: Sets or queries the operations attached to an arena.
162 mparena_ops
*mparena_setops(mparena
*a
, mparena_ops
*ops
)
172 /* --- @mparena_destroy@ --- *
174 * Arguments: @mparena *a@ = pointer to arena block
178 * Use: Frees an MP arena, and all the vectors held within it. The
179 * blocks which are currently allocated can be freed into some
183 static void tfree(mparena
*a
, mparena_node
*n
)
185 a
->ops
->free(a
, n
->v
);
193 void mparena_destroy(mparena
*a
)
199 /* --- @mparena_count@ --- *
201 * Arguments: @mparena *a@ = pointer to arena block
203 * Returns: Number of allocated blocks from this arena.
205 * Use: Reports the number of blocks allocated from the arena and not
209 unsigned mparena_count(mparena
*a
)
215 /* --- @mpalloc@ --- *
217 * Arguments: @mparena *a@ = pointer to arena block
218 * @size_t sz@ = number of digits required
220 * Returns: Pointer to a suitably sized block.
222 * Use: Allocates a lump of data suitable for use as an array of MP
226 #ifdef MPARENA_TRIVIAL
228 mpw
*mpalloc(mparena
*a
, size_t sz
)
231 return (a
->ops
->alloc(a
, MPWS(sz
)));
236 mpw
*mpalloc(mparena
*a
, size_t sz
)
238 mparena_node
**nn
, *n
;
246 fprintf(debugfp
, "alloc %u\n before: ", sz
);
247 tdump(a
->root
); putc('\n', debugfp
);
250 /* --- First, find a block which is big enough --- */
256 fputs(" failed\n", debugfp
);
258 v
= a
->ops
->alloc(a
, MPWS(sz
+ 1));
268 /* --- Now try to find a smaller block which is suitable --- */
270 while (n
->left
&& n
->left
->v
[0] >= sz
) {
275 /* --- If the block we've got is still too large, start digging --- */
277 if (n
->v
[0] > sz
* 2) {
282 /* --- I've now found a suitable block --- */
286 /* --- Remove this node from the tree --- */
293 mparena_node
*left
= n
->left
;
294 mparena_node
*p
= *nn
= n
->right
;
301 fputs(" after: ", debugfp
);
302 tdump(a
->root
); putc('\n', debugfp
);
305 /* --- Get rid of this node now --- */
314 /* --- @mpfree@ --- *
316 * Arguments: @mparena *a@ = pointer to arena block
317 * @mpw *v@ = pointer to allocated vector
321 * Use: Returns an MP vector to an arena.
324 #ifdef MPARENA_TRIVIAL
326 void mpfree(mparena
*a
, mpw
*v
)
334 void mpfree(mparena
*a
, mpw
*v
)
336 mparena_node
**nn
, *n
;
343 fprintf(debugfp
, "free %u\n before: ", sz
);
344 tdump(a
->root
); putc('\n', debugfp
);
356 n
= CREATE(mparena_node
);
357 n
->left
= n
->right
= 0;
363 fputs(" after: ", debugfp
);
364 tdump(a
->root
); putc('\n', debugfp
);
370 /*----- That's all, folks -------------------------------------------------*/