math/gfreduce.[ch]: Fix out-of-bounds memory access.
[u/mdw/catacomb] / math / mparena.h
CommitLineData
d3409d5e 1/* -*-c-*-
2 *
d3409d5e 3 * Allocation and freeing of MP buffers
4 *
5 * (c) 1999 Straylight/Edgeware
6 */
7
45c0fd36 8/*----- Licensing notice --------------------------------------------------*
d3409d5e 9 *
10 * This file is part of Catacomb.
11 *
12 * Catacomb is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU Library General Public License as
14 * published by the Free Software Foundation; either version 2 of the
15 * License, or (at your option) any later version.
45c0fd36 16 *
d3409d5e 17 * Catacomb is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU Library General Public License for more details.
45c0fd36 21 *
d3409d5e 22 * You should have received a copy of the GNU Library General Public
23 * License along with Catacomb; if not, write to the Free
24 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
25 * MA 02111-1307, USA.
26 */
27
97d21728 28#ifndef CATACOMB_MPARENA_H
29#define CATACOMB_MPARENA_H
d3409d5e 30
31#ifdef __cplusplus
32 extern "C" {
33#endif
34
35/*----- Header files ------------------------------------------------------*/
36
d4745354 37#include <mLib/arena.h>
38
97d21728 39#ifndef CATACOMB_MPW_H
d3409d5e 40# include "mpw.h"
41#endif
42
43/*----- Data structures ---------------------------------------------------*/
44
45/* --- @mparena_node@ --- *
46 *
47 * For internal use by the MP arena manager. The free blocks are held in a
48 * binary tree by size, held in the first digit of each vector.
49 */
50
51typedef struct mparena_node {
52 struct mparena_node *left, *right;
53 mpw *v;
54} mparena_node;
55
56/* --- @mparena@ --- *
57 *
58 * The actual arena.
59 */
60
61typedef struct mparena {
62 mparena_node *root;
97d21728 63 unsigned n;
d4745354 64 arena *a;
d3409d5e 65} mparena;
66
d4745354 67/*----- Standard arenas ---------------------------------------------------*/
d3409d5e 68
d4745354 69extern mparena mparena_global;
70#define MPARENA_GLOBAL (&mparena_global)
d3409d5e 71
d4745354 72extern mparena mparena_secure;
73#define MPARENA_SECURE (&mparena_secure)
d3409d5e 74
75/*----- Functions provided ------------------------------------------------*/
76
77/* --- @mparena_create@ --- *
78 *
79 * Arguments: @mparena *a@ = pointer to arena block
80 *
81 * Returns: ---
82 *
83 * Use: Initializes an MP arena so that blocks can be allocated from
84 * it.
85 */
86
87extern void mparena_create(mparena */*a*/);
88
d4745354 89#define MPARENA_INIT { 0, 0, &arena_stdlib }
d3409d5e 90
d4745354 91/* --- @mparena_setarena@ --- *
d3409d5e 92 *
d4745354 93 * Arguments: @mparena *a@ = pointer to MP arena block
94 * @arena *aa@ = pointer to arena
d3409d5e 95 *
d4745354 96 * Returns: ---
d3409d5e 97 *
d4745354 98 * Use: Sets the underlying arena for an MP arena.
d3409d5e 99 */
100
d4745354 101extern void mparena_setarena(mparena */*a*/, arena */*aa*/);
d3409d5e 102
103/* --- @mparena_destroy@ --- *
104 *
105 * Arguments: @mparena *a@ = pointer to arena block
106 *
107 * Returns: ---
108 *
109 * Use: Frees an MP arena, and all the vectors held within it. The
110 * blocks which are currently allocated can be freed into some
d4745354 111 * other MP arena, as long as the underlying arenas are the
112 * same.
d3409d5e 113 */
114
115extern void mparena_destroy(mparena */*a*/);
116
97d21728 117/* --- @mparena_count@ --- *
118 *
119 * Arguments: @mparena *a@ = pointer to arena block
120 *
121 * Returns: Number of allocated blocks from this arena.
122 *
123 * Use: Reports the number of blocks allocated from the arena and not
124 * yet freed.
125 */
126
127extern unsigned mparena_count(mparena */*a*/);
128
d3409d5e 129/* --- @mpalloc@ --- *
130 *
131 * Arguments: @mparena *a@ = pointer to arena block
132 * @size_t sz@ = number of digits required
133 *
134 * Returns: Pointer to a suitably sized block.
135 *
136 * Use: Allocates a lump of data suitable for use as an array of MP
137 * digits.
138 */
139
140extern mpw *mpalloc(mparena */*a*/, size_t /*sz*/);
141
142/* --- @mpfree@ --- *
143 *
144 * Arguments: @mparena *a@ = pointer to arena block
145 * @mpw *v@ = pointer to allocated vector
146 *
147 * Returns: ---
148 *
97d21728 149 * Use: Returns an MP vector to an arena.
d3409d5e 150 */
151
152extern void mpfree(mparena */*a*/, mpw */*v*/);
153
154/*----- That's all, folks -------------------------------------------------*/
155
156#ifdef __cplusplus
157 }
158#endif
159
160#endif