chiark / gitweb /
Infrastructure: Strip away crufty CVS $Id$ tags.
[mLib] / sub.h
CommitLineData
0875b58f 1/* -*-c-*-
0875b58f 2 *
3 * Allocation of known-size blocks
4 *
5 * (c) 1998 Straylight/Edgeware
6 */
7
d4efbcd9 8/*----- Licensing notice --------------------------------------------------*
0875b58f 9 *
10 * This file is part of the mLib utilities library.
11 *
12 * mLib is free software; you can redistribute it and/or modify
c846879c 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.
d4efbcd9 16 *
0875b58f 17 * mLib 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
c846879c 20 * GNU Library General Public License for more details.
d4efbcd9 21 *
c846879c 22 * You should have received a copy of the GNU Library General Public
0bd98442 23 * License along with mLib; if not, write to the Free
24 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
25 * MA 02111-1307, USA.
0875b58f 26 */
27
c6e0eaf0 28#ifndef MLIB_SUB_H
29#define MLIB_SUB_H
0875b58f 30
31#ifdef __cplusplus
32 extern "C" {
33#endif
34
35/*----- Required header files ---------------------------------------------*/
36
37#include <stdlib.h>
38
c5775f49 39#ifndef MLIB_ALIGN_H
40# include "align.h"
41#endif
42
0fd574c3 43#ifndef MLIB_ARENA_H
44# include "arena.h"
0875b58f 45#endif
46
0fd574c3 47/*----- Configuration and tuning ------------------------------------------*/
48
49/* --- The largest block I'll handle here --- *
50 *
51 * Anything larger will be handed on to the underlying @alloc@.
52 */
53
54#define SUB_MAXBIN 256
55
56/* --- Preferred chunk size --- *
57 *
58 * When a bin is empty, I'll allocate a large chunk of approximately this
59 * size and divvy it up into small bin-sized blocks.
60 */
61
62#define SUB_CHUNK 4096
63
64/*----- Other useful macros -----------------------------------------------*/
65
66/* --- The granularity of bin buffers --- *
67 *
c5775f49 68 * All blocks allocated by the binner are a multiple of this size.
0fd574c3 69 */
70
c5775f49 71#define SUB_GRANULE sizeof(union align)
0fd574c3 72
73/* --- Finding the right bin for a given size --- *
74 *
75 * This chooses the correct bin for an allocation. Input is the size of
76 * block wanted; result is the bin index.
77 */
78
79#define SUB_BIN(x) (((x) + SUB_GRANULE - 1) / SUB_GRANULE)
80
81/* --- Convert a bin back to the block size --- *
82 *
83 * This gives the size of block contained in a given bin.
84 */
85
86#define SUB_BINSZ(x) ((x) * SUB_GRANULE)
87
88/* --- Number of bins required --- */
89
90#define SUB_BINS (SUB_MAXBIN / SUB_GRANULE + 1)
91
92/*----- Data structures ---------------------------------------------------*/
93
94typedef struct subarena {
95 arena *a;
96 void *bin[SUB_BINS];
97} subarena;
98
99/*----- Global variables --------------------------------------------------*/
100
101extern subarena sub_global;
102
0875b58f 103/*----- Functions provided ------------------------------------------------*/
104
0fd574c3 105/* --- @subarena_create@ --- *
106 *
107 * Arguments: @subarena *s@ = pointer to arena to initialize
108 * @arena *a@ = pointer to underlying arena block
109 *
110 * Returns: ---
111 *
112 * Use: Initialize a suballocation arena based on an underlying large
113 * blocks arena.
114 */
115
116extern void subarena_create(subarena */*s*/, arena */*a*/);
117
118/* --- @subarena_destroy@ --- *
119 *
120 * Arguments: @subarena *s@ = pointer to arena to destroy
121 *
122 * Returns: ---
123 *
124 * Use: Destroys a suballocation arena, freeing all of the memory it
125 * contains back to the underlying large blocks arena.
126 */
127
128extern void subarena_destroy(subarena */*s*/);
129
130/* --- @subarena_alloc@ --- *
131 *
132 * Arguments: @subarena *s@ = pointer to arena
133 * @size_t s@ = size of chunk wanted
134 *
d4efbcd9 135 * Returns: Pointer to a block at least as large as the one wanted.
0fd574c3 136 *
d4efbcd9 137 * Use: Allocates a small block of memory from the given pool. The
0fd574c3 138 * exception @EXC_NOMEM@ is raised if the underlying arena is
139 * full.
140 */
141
142extern void *subarena_alloc(subarena */*s*/, size_t /*sz*/);
143
144/* --- @subarena_free@ --- *
145 *
d4efbcd9 146 * Arguments: @subarena *s@ = pointer to arena
0fd574c3 147 * @void *p@ = address of block to free
d4efbcd9 148 * @size_t s@ = size of block
0fd574c3 149 *
d4efbcd9 150 * Returns: ---
0fd574c3 151 *
d4efbcd9 152 * Use: Frees a block allocated by @subarena_alloc@.
0fd574c3 153 */
154
155extern void subarena_free(subarena */*s*/, void */*p*/, size_t /*sz*/);
156
157/* --- @A_CREATE@ --- *
158 *
159 * Arguments: @subarena *s@ = pointer to arena
160 * @type@ = type of object required; must be passable to
161 * @sizeof@
162 *
163 * Returns: Pointer to a block sufficiently big to hold an object of the
164 * named type.
165 *
166 * Use: Allocates a block of the required type.
167 */
168
169#define A_CREATE(a, type) subarena_alloc((a), sizeof(type))
170
171/* --- @A_DESTROY@ --- *
172 *
173 * Arguments: @subarena *s@ = pointer to arena
174 * @void *p@ = pointer to an object
175 *
176 * Returns: ---
177 *
178 * Use: Frees the thing pointed to by @p@.
179 */
180
181#define A_DESTROY(a, p) subarena_free((a), (p), sizeof(*p))
182
183/*----- Shortcuts for the global pool -------------------------------------*/
184
0875b58f 185/* --- @sub_alloc@ --- *
186 *
d4efbcd9 187 * Arguments: @size_t s@ = size of chunk wanted
0875b58f 188 *
d4efbcd9 189 * Returns: Pointer to a block at least as large as the one wanted.
0875b58f 190 *
d4efbcd9 191 * Use: Allocates a small block of memory from the @sub_global@ pool.
0875b58f 192 */
193
0fd574c3 194extern void *sub_alloc(size_t /*sz*/);
195#define sub_alloc(sz) subarena_alloc(&sub_global, (sz))
0875b58f 196
197/* --- @sub_free@ --- *
198 *
d4efbcd9
MW
199 * Arguments: @void *p@ = address of block to free
200 * @size_t s@ = size of block
0875b58f 201 *
d4efbcd9 202 * Returns: ---
0875b58f 203 *
d4efbcd9 204 * Use: Frees a block allocated by @sub_alloc@.
0875b58f 205 */
206
0fd574c3 207extern void sub_free(void */*p*/, size_t /*sz*/);
208#define sub_free(p, sz) subarena_free(&sub_global, (p), (sz))
0875b58f 209
210/* --- @CREATE@ --- *
211 *
212 * Arguments: @type@ = type of object required; must be passable to
213 * @sizeof@
214 *
215 * Returns: Pointer to a block sufficiently big to hold an object of the
216 * named type.
217 *
218 * Use: Allocates a block of the required type.
219 */
220
221#define CREATE(type) sub_alloc(sizeof(type))
222
223/* --- @DESTROY@ --- *
224 *
225 * Arguments: @void *p@ = pointer to an object
226 *
227 * Returns: ---
228 *
229 * Use: Frees the thing pointed to by @p@.
230 */
231
232#define DESTROY(p) sub_free(p, sizeof(*p))
233
234/* --- @sub_init@ --- *
235 *
d4efbcd9 236 * Arguments: ---
0875b58f 237 *
d4efbcd9 238 * Returns: ---
0875b58f 239 *
d4efbcd9 240 * Use: Initializes the magic allocator. This is no longer
0fd574c3 241 * necessary.
0875b58f 242 */
243
0fd574c3 244extern void sub_init(void);
0875b58f 245
246/*----- That's all, folks -------------------------------------------------*/
247
248#ifdef __cplusplus
249 }
250#endif
251
252#endif