Memory allocation counting.
[u/mdw/catacomb] / mparena.h
1 /* -*-c-*-
2 *
3 * $Id: mparena.h,v 1.2 1999/12/10 23:28:59 mdw Exp $
4 *
5 * Allocation and freeing of MP buffers
6 *
7 * (c) 1999 Straylight/Edgeware
8 */
9
10 /*----- Licensing notice --------------------------------------------------*
11 *
12 * This file is part of Catacomb.
13 *
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.
18 *
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.
23 *
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,
27 * MA 02111-1307, USA.
28 */
29
30 /*----- Revision history --------------------------------------------------*
31 *
32 * $Log: mparena.h,v $
33 * Revision 1.2 1999/12/10 23:28:59 mdw
34 * Memory allocation counting.
35 *
36 * Revision 1.1 1999/11/17 18:02:16 mdw
37 * New multiprecision integer arithmetic suite.
38 *
39 */
40
41 #ifndef CATACOMB_MPARENA_H
42 #define CATACOMB_MPARENA_H
43
44 #ifdef __cplusplus
45 extern "C" {
46 #endif
47
48 /*----- Header files ------------------------------------------------------*/
49
50 #ifndef CATACOMB_MPW_H
51 # include "mpw.h"
52 #endif
53
54 /*----- Data structures ---------------------------------------------------*/
55
56 /* --- @mparena_node@ --- *
57 *
58 * For internal use by the MP arena manager. The free blocks are held in a
59 * binary tree by size, held in the first digit of each vector.
60 */
61
62 typedef struct mparena_node {
63 struct mparena_node *left, *right;
64 mpw *v;
65 } mparena_node;
66
67 /* --- @mparena@ --- *
68 *
69 * The actual arena.
70 */
71
72 typedef struct mparena {
73 mparena_node *root;
74 unsigned n;
75 struct mparena_ops *ops;
76 } mparena;
77
78 /* --- @mparena_ops@ --- *
79 *
80 * Operations required for an arena memory manager. The default manager just
81 * calls @xmalloc@ and @free@, although it's possible to envisage a more
82 * paranoid implementation which allocates locked memory pages. Switch them
83 * over with @mparena_setops@. It's usual to only do this when you've
84 * attached your extra state to the end of the @mparena@ structure.
85 */
86
87 typedef struct mparena_ops {
88 void *(*alloc)(mparena */*a*/, size_t /*sz*/);
89 void (*free)(mparena */*a*/, void */*p*/);
90 } mparena_ops;
91
92 /*----- Magical constants -------------------------------------------------*/
93
94 #define MPARENA_GLOBAL ((mparena *)0)
95
96 extern mparena_ops mparena_defaultops;
97
98 /*----- Functions provided ------------------------------------------------*/
99
100 /* --- @mparena_create@ --- *
101 *
102 * Arguments: @mparena *a@ = pointer to arena block
103 *
104 * Returns: ---
105 *
106 * Use: Initializes an MP arena so that blocks can be allocated from
107 * it.
108 */
109
110 extern void mparena_create(mparena */*a*/);
111
112 #define MPARENA_INIT { 0, 0, &mparena_defaultops }
113
114 /* --- @mparena_setops@ --- *
115 *
116 * Arguments: @mparena *a@ = pointer to arena block
117 * @mparena_ops *ops@ = pointer to operations block or null
118 *
119 * Returns: The previous operations block.
120 *
121 * Use: Sets or queries the operations attached to an arena.
122 */
123
124 extern mparena_ops *mparena_setops(mparena */*a*/, mparena_ops */*ops*/);
125
126 /* --- @mparena_destroy@ --- *
127 *
128 * Arguments: @mparena *a@ = pointer to arena block
129 *
130 * Returns: ---
131 *
132 * Use: Frees an MP arena, and all the vectors held within it. The
133 * blocks which are currently allocated can be freed into some
134 * other arena.
135 */
136
137 extern void mparena_destroy(mparena */*a*/);
138
139 /* --- @mparena_count@ --- *
140 *
141 * Arguments: @mparena *a@ = pointer to arena block
142 *
143 * Returns: Number of allocated blocks from this arena.
144 *
145 * Use: Reports the number of blocks allocated from the arena and not
146 * yet freed.
147 */
148
149 extern unsigned mparena_count(mparena */*a*/);
150
151 /* --- @mpalloc@ --- *
152 *
153 * Arguments: @mparena *a@ = pointer to arena block
154 * @size_t sz@ = number of digits required
155 *
156 * Returns: Pointer to a suitably sized block.
157 *
158 * Use: Allocates a lump of data suitable for use as an array of MP
159 * digits.
160 */
161
162 extern mpw *mpalloc(mparena */*a*/, size_t /*sz*/);
163
164 /* --- @mpfree@ --- *
165 *
166 * Arguments: @mparena *a@ = pointer to arena block
167 * @mpw *v@ = pointer to allocated vector
168 *
169 * Returns: ---
170 *
171 * Use: Returns an MP vector to an arena.
172 */
173
174 extern void mpfree(mparena */*a*/, mpw */*v*/);
175
176 /*----- That's all, folks -------------------------------------------------*/
177
178 #ifdef __cplusplus
179 }
180 #endif
181
182 #endif