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 sizeof(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_DESTROY@ --- *
173 * Arguments: @subarena *s@ = pointer to arena
174 * @void *p@ = pointer to an object
178 * Use: Frees the thing pointed to by @p@.
181 #define A_DESTROY(a, p) subarena_free((a), (p), sizeof(*p))
183 /*----- Shortcuts for the global pool -------------------------------------*/
185 /* --- @sub_alloc@ --- *
187 * Arguments: @size_t s@ = size of chunk wanted
189 * Returns: Pointer to a block at least as large as the one wanted.
191 * Use: Allocates a small block of memory from the @sub_global@ pool.
194 extern void *sub_alloc(size_t /*sz*/);
195 #define sub_alloc(sz) subarena_alloc(&sub_global, (sz))
197 /* --- @sub_free@ --- *
199 * Arguments: @void *p@ = address of block to free
200 * @size_t s@ = size of block
204 * Use: Frees a block allocated by @sub_alloc@.
207 extern void sub_free(void */
*p*/
, size_t /*sz*/);
208 #define sub_free(p, sz) subarena_free(&sub_global, (p), (sz))
210 /* --- @CREATE@ --- *
212 * Arguments: @type@ = type of object required; must be passable to
215 * Returns: Pointer to a block sufficiently big to hold an object of the
218 * Use: Allocates a block of the required type.
221 #define CREATE(type) sub_alloc(sizeof(type))
223 /* --- @DESTROY@ --- *
225 * Arguments: @void *p@ = pointer to an object
229 * Use: Frees the thing pointed to by @p@.
232 #define DESTROY(p) sub_free(p, sizeof(*p))
234 /* --- @sub_init@ --- *
240 * Use: Initializes the magic allocator. This is no longer
244 extern void sub_init(void);
246 /*----- That's all, folks -------------------------------------------------*/