| 1 | /* -*-c-*- |
| 2 | * |
| 3 | * Allocation of known-size blocks |
| 4 | * |
| 5 | * (c) 1998 Straylight/Edgeware |
| 6 | */ |
| 7 | |
| 8 | /*----- Licensing notice --------------------------------------------------* |
| 9 | * |
| 10 | * This file is part of the mLib utilities library. |
| 11 | * |
| 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. |
| 16 | * |
| 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. |
| 21 | * |
| 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, |
| 25 | * MA 02111-1307, USA. |
| 26 | */ |
| 27 | |
| 28 | #ifndef MLIB_SUB_H |
| 29 | #define MLIB_SUB_H |
| 30 | |
| 31 | #ifdef __cplusplus |
| 32 | extern "C" { |
| 33 | #endif |
| 34 | |
| 35 | /*----- Required header files ---------------------------------------------*/ |
| 36 | |
| 37 | #include <stdlib.h> |
| 38 | |
| 39 | #ifndef MLIB_ALIGN_H |
| 40 | # include "align.h" |
| 41 | #endif |
| 42 | |
| 43 | #ifndef MLIB_ARENA_H |
| 44 | # include "arena.h" |
| 45 | #endif |
| 46 | |
| 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 | * |
| 68 | * All blocks allocated by the binner are a multiple of this size. |
| 69 | */ |
| 70 | |
| 71 | #define SUB_GRANULE sizeof(union align) |
| 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 | |
| 94 | typedef struct subarena { |
| 95 | arena *a; |
| 96 | void *bin[SUB_BINS]; |
| 97 | } subarena; |
| 98 | |
| 99 | /*----- Global variables --------------------------------------------------*/ |
| 100 | |
| 101 | extern subarena sub_global; |
| 102 | |
| 103 | /*----- Functions provided ------------------------------------------------*/ |
| 104 | |
| 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 | |
| 116 | extern 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 | |
| 128 | extern 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 | * |
| 135 | * Returns: Pointer to a block at least as large as the one wanted. |
| 136 | * |
| 137 | * Use: Allocates a small block of memory from the given pool. The |
| 138 | * exception @EXC_NOMEM@ is raised if the underlying arena is |
| 139 | * full. |
| 140 | */ |
| 141 | |
| 142 | extern void *subarena_alloc(subarena */*s*/, size_t /*sz*/); |
| 143 | |
| 144 | /* --- @subarena_free@ --- * |
| 145 | * |
| 146 | * Arguments: @subarena *s@ = pointer to arena |
| 147 | * @void *p@ = address of block to free |
| 148 | * @size_t s@ = size of block |
| 149 | * |
| 150 | * Returns: --- |
| 151 | * |
| 152 | * Use: Frees a block allocated by @subarena_alloc@. |
| 153 | */ |
| 154 | |
| 155 | extern 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 | |
| 185 | /* --- @sub_alloc@ --- * |
| 186 | * |
| 187 | * Arguments: @size_t s@ = size of chunk wanted |
| 188 | * |
| 189 | * Returns: Pointer to a block at least as large as the one wanted. |
| 190 | * |
| 191 | * Use: Allocates a small block of memory from the @sub_global@ pool. |
| 192 | */ |
| 193 | |
| 194 | extern void *sub_alloc(size_t /*sz*/); |
| 195 | #define sub_alloc(sz) subarena_alloc(&sub_global, (sz)) |
| 196 | |
| 197 | /* --- @sub_free@ --- * |
| 198 | * |
| 199 | * Arguments: @void *p@ = address of block to free |
| 200 | * @size_t s@ = size of block |
| 201 | * |
| 202 | * Returns: --- |
| 203 | * |
| 204 | * Use: Frees a block allocated by @sub_alloc@. |
| 205 | */ |
| 206 | |
| 207 | extern void sub_free(void */*p*/, size_t /*sz*/); |
| 208 | #define sub_free(p, sz) subarena_free(&sub_global, (p), (sz)) |
| 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 | * |
| 236 | * Arguments: --- |
| 237 | * |
| 238 | * Returns: --- |
| 239 | * |
| 240 | * Use: Initializes the magic allocator. This is no longer |
| 241 | * necessary. |
| 242 | */ |
| 243 | |
| 244 | extern void sub_init(void); |
| 245 | |
| 246 | /*----- That's all, folks -------------------------------------------------*/ |
| 247 | |
| 248 | #ifdef __cplusplus |
| 249 | } |
| 250 | #endif |
| 251 | |
| 252 | #endif |