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