3 * Allocation of known-size blocks
5 * (c) 1998 Straylight/Edgeware
8 /*----- Licensing notice --------------------------------------------------*
10 * This file is part of the mLib utilities library.
12 * mLib 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.
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
20 * GNU Library General Public License for more details.
22 * You should have received a copy of the GNU Library General Public
23 * License along with mLib; if not, write to the Free
24 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
35 /*----- Required header files ---------------------------------------------*/
47 /*----- Configuration and tuning ------------------------------------------*/
49 /* --- The largest block I'll handle here --- *
51 * Anything larger will be handed on to the underlying @alloc@.
54 #define SUB_MAXBIN 256
56 /* --- Preferred chunk size --- *
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.
62 #define SUB_CHUNK 4096
64 /*----- Other useful macros -----------------------------------------------*/
66 /* --- The granularity of bin buffers --- *
68 * All blocks allocated by the binner are a multiple of this size.
71 #define SUB_GRANULE (ALIGNOF(union align))
73 /* --- Finding the right bin for a given size --- *
75 * This chooses the correct bin for an allocation. Input is the size of
76 * block wanted; result is the bin index.
79 #define SUB_BIN(x) (((x) + SUB_GRANULE - 1)/SUB_GRANULE)
81 /* --- Convert a bin back to the block size --- *
83 * This gives the size of block contained in a given bin.
86 #define SUB_BINSZ(x) ((x)*SUB_GRANULE)
88 /* --- Number of bins required --- */
90 #define SUB_BINS (SUB_MAXBIN/SUB_GRANULE + 1)
92 /*----- Data structures ---------------------------------------------------*/
94 typedef struct subarena {
99 /*----- Global variables --------------------------------------------------*/
101 extern subarena sub_global;
103 /*----- Functions provided ------------------------------------------------*/
105 /* --- @subarena_create@ --- *
107 * Arguments: @subarena *s@ = pointer to arena to initialize
108 * @arena *a@ = pointer to underlying arena block
112 * Use: Initialize a suballocation arena based on an underlying large
116 extern void subarena_create(subarena */*s*/, arena */*a*/);
118 /* --- @subarena_destroy@ --- *
120 * Arguments: @subarena *s@ = pointer to arena to destroy
124 * Use: Destroys a suballocation arena, freeing all of the memory it
125 * contains back to the underlying large blocks arena.
128 extern void subarena_destroy(subarena */*s*/);
130 /* --- @subarena_alloc@ --- *
132 * Arguments: @subarena *s@ = pointer to arena
133 * @size_t s@ = size of chunk wanted
135 * Returns: Pointer to a block at least as large as the one wanted.
137 * Use: Allocates a small block of memory from the given pool. The
138 * exception @EXC_NOMEM@ is raised if the underlying arena is
142 extern void *subarena_alloc(subarena */*s*/, size_t /*sz*/);
144 /* --- @subarena_free@ --- *
146 * Arguments: @subarena *s@ = pointer to arena
147 * @void *p@ = address of block to free
148 * @size_t s@ = size of block
152 * Use: Frees a block allocated by @subarena_alloc@.
155 extern void subarena_free(subarena */*s*/, void */*p*/, size_t /*sz*/);
157 /* --- @A_CREATE@ --- *
159 * Arguments: @subarena *s@ = pointer to arena
160 * @type@ = type of object required; must be passable to
163 * Returns: Pointer to a block sufficiently big to hold an object of the
166 * Use: Allocates a block of the required type.
169 #define A_CREATE(a, type) subarena_alloc((a), sizeof(type))
171 /* --- @A_SUBNEW@ --- *
173 * Arguments: @type *p@ = a pointer to allocate
174 * @subarena *s@ = pointer to arena
178 * Use: Set @p@ to point to a freshly allocated block large enough to
179 * hold an object of the type pointed to by @p@. If there is
180 * not enough memory, the exception @EXC_NOMEM@ is thrown.
183 #define A_SUBNEW(p, a) \
184 do { (p) = subarena_alloc((a), sizeof(*(p))); } while (0)
186 /* --- @A_DESTROY@ --- *
188 * Arguments: @subarena *s@ = pointer to arena
189 * @void *p@ = pointer to an object
193 * Use: Frees the thing pointed to by @p@.
196 #define A_DESTROY(a, p) subarena_free((a), (p), sizeof(*p))
198 /*----- Shortcuts for the global pool -------------------------------------*/
200 /* --- @sub_alloc@ --- *
202 * Arguments: @size_t s@ = size of chunk wanted
204 * Returns: Pointer to a block at least as large as the one wanted.
206 * Use: Allocates a small block of memory from the @sub_global@ pool.
209 extern void *sub_alloc(size_t /*sz*/);
210 #define sub_alloc(sz) subarena_alloc(&sub_global, (sz))
212 /* --- @sub_free@ --- *
214 * Arguments: @void *p@ = address of block to free
215 * @size_t s@ = size of block
219 * Use: Frees a block allocated by @sub_alloc@.
222 extern void sub_free(void */*p*/, size_t /*sz*/);
223 #define sub_free(p, sz) subarena_free(&sub_global, (p), (sz))
225 /* --- @CREATE@ --- *
227 * Arguments: @type@ = type of object required; must be passable to
230 * Returns: Pointer to a block sufficiently big to hold an object of the
233 * Use: Allocates a block of the required type.
236 #define CREATE(type) sub_alloc(sizeof(type))
240 * Arguments: @type *p@ = a pointer to allocate
244 * Use: Set @p@ to point to a freshly allocated block large enough to
245 * hold an object of the type pointed to by @p@. If there is
246 * not enough memory, the exception @EXC_NOMEM@ is thrown.
249 #define NEW(p) do { (p) = sub_alloc(sizeof(*(p))); } while (0)
251 /* --- @DESTROY@ --- *
253 * Arguments: @void *p@ = pointer to an object
257 * Use: Frees the thing pointed to by @p@.
260 #define DESTROY(p) sub_free(p, sizeof(*p))
262 /* --- @sub_init@ --- *
268 * Use: Initializes the magic allocator. This is no longer
272 extern void sub_init(void);
274 /*----- That's all, folks -------------------------------------------------*/