3 * $Id: sub.h,v 1.8 2004/04/08 01:36:13 mdw Exp $
5 * Allocation of known-size blocks
7 * (c) 1998 Straylight/Edgeware
10 /*----- Licensing notice --------------------------------------------------*
12 * This file is part of the mLib utilities library.
14 * mLib 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.
19 * mLib 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.
24 * You should have received a copy of the GNU Library General Public
25 * License along with mLib; if not, write to the Free
26 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
37 /*----- Required header files ---------------------------------------------*/
49 /*----- Configuration and tuning ------------------------------------------*/
51 /* --- The largest block I'll handle here --- *
53 * Anything larger will be handed on to the underlying @alloc@.
56 #define SUB_MAXBIN 256
58 /* --- Preferred chunk size --- *
60 * When a bin is empty, I'll allocate a large chunk of approximately this
61 * size and divvy it up into small bin-sized blocks.
64 #define SUB_CHUNK 4096
66 /*----- Other useful macros -----------------------------------------------*/
68 /* --- The granularity of bin buffers --- *
70 * All blocks allocated by the binner are a multiple of this size.
73 #define SUB_GRANULE sizeof(union align)
75 /* --- Finding the right bin for a given size --- *
77 * This chooses the correct bin for an allocation. Input is the size of
78 * block wanted; result is the bin index.
81 #define SUB_BIN(x) (((x) + SUB_GRANULE - 1) / SUB_GRANULE)
83 /* --- Convert a bin back to the block size --- *
85 * This gives the size of block contained in a given bin.
88 #define SUB_BINSZ(x) ((x) * SUB_GRANULE)
90 /* --- Number of bins required --- */
92 #define SUB_BINS (SUB_MAXBIN / SUB_GRANULE + 1)
94 /*----- Data structures ---------------------------------------------------*/
96 typedef struct subarena
{
101 /*----- Global variables --------------------------------------------------*/
103 extern subarena sub_global
;
105 /*----- Functions provided ------------------------------------------------*/
107 /* --- @subarena_create@ --- *
109 * Arguments: @subarena *s@ = pointer to arena to initialize
110 * @arena *a@ = pointer to underlying arena block
114 * Use: Initialize a suballocation arena based on an underlying large
118 extern void subarena_create(subarena */
*s*/
, arena */
*a*/
);
120 /* --- @subarena_destroy@ --- *
122 * Arguments: @subarena *s@ = pointer to arena to destroy
126 * Use: Destroys a suballocation arena, freeing all of the memory it
127 * contains back to the underlying large blocks arena.
130 extern void subarena_destroy(subarena */
*s*/
);
132 /* --- @subarena_alloc@ --- *
134 * Arguments: @subarena *s@ = pointer to arena
135 * @size_t s@ = size of chunk wanted
137 * Returns: Pointer to a block at least as large as the one wanted.
139 * Use: Allocates a small block of memory from the given pool. The
140 * exception @EXC_NOMEM@ is raised if the underlying arena is
144 extern void *subarena_alloc(subarena */
*s*/
, size_t /*sz*/);
146 /* --- @subarena_free@ --- *
148 * Arguments: @subarena *s@ = pointer to arena
149 * @void *p@ = address of block to free
150 * @size_t s@ = size of block
154 * Use: Frees a block allocated by @subarena_alloc@.
157 extern void subarena_free(subarena */
*s*/
, void */
*p*/
, size_t /*sz*/);
159 /* --- @A_CREATE@ --- *
161 * Arguments: @subarena *s@ = pointer to arena
162 * @type@ = type of object required; must be passable to
165 * Returns: Pointer to a block sufficiently big to hold an object of the
168 * Use: Allocates a block of the required type.
171 #define A_CREATE(a, type) subarena_alloc((a), sizeof(type))
173 /* --- @A_DESTROY@ --- *
175 * Arguments: @subarena *s@ = pointer to arena
176 * @void *p@ = pointer to an object
180 * Use: Frees the thing pointed to by @p@.
183 #define A_DESTROY(a, p) subarena_free((a), (p), sizeof(*p))
185 /*----- Shortcuts for the global pool -------------------------------------*/
187 /* --- @sub_alloc@ --- *
189 * Arguments: @size_t s@ = size of chunk wanted
191 * Returns: Pointer to a block at least as large as the one wanted.
193 * Use: Allocates a small block of memory from the @sub_global@ pool.
196 extern void *sub_alloc(size_t /*sz*/);
197 #define sub_alloc(sz) subarena_alloc(&sub_global, (sz))
199 /* --- @sub_free@ --- *
201 * Arguments: @void *p@ = address of block to free
202 * @size_t s@ = size of block
206 * Use: Frees a block allocated by @sub_alloc@.
209 extern void sub_free(void */
*p*/
, size_t /*sz*/);
210 #define sub_free(p, sz) subarena_free(&sub_global, (p), (sz))
212 /* --- @CREATE@ --- *
214 * Arguments: @type@ = type of object required; must be passable to
217 * Returns: Pointer to a block sufficiently big to hold an object of the
220 * Use: Allocates a block of the required type.
223 #define CREATE(type) sub_alloc(sizeof(type))
225 /* --- @DESTROY@ --- *
227 * Arguments: @void *p@ = pointer to an object
231 * Use: Frees the thing pointed to by @p@.
234 #define DESTROY(p) sub_free(p, sizeof(*p))
236 /* --- @sub_init@ --- *
242 * Use: Initializes the magic allocator. This is no longer
246 extern void sub_init(void);
248 /*----- That's all, folks -------------------------------------------------*/