3 * Abstraction for memory allocation arenas
5 * (c) 2000 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 /*----- Header files ------------------------------------------------------*/
39 /*----- Data structures ---------------------------------------------------*/
41 /* --- An arena structure --- */
43 typedef struct arena {
44 const struct arena_ops *ops;
47 typedef struct arena_ops {
48 void *(*alloc)(arena */*a*/, size_t /*sz*/);
49 void *(*realloc)(arena */*a*/, void */*p*/, size_t /*sz*/, size_t /*osz*/);
50 void (*free)(arena */*a*/, void */*p*/);
51 void (*purge)(arena */*a*/);
54 /*----- Global variables --------------------------------------------------*/
56 extern arena *arena_global; /* Standard global arena */
57 extern arena arena_stdlib; /* Arena based on @malloc@/@free@ */
59 /*----- Allocation utilities ----------------------------------------------*/
61 /* --- @ALLOCV_SAFE_P@, @NEWV_SAFE_P@ ---
63 * Arguments: @type *p@ = pointer to a vector (for @NEWV_SAFE_P@; not
65 * @size_t n@ = number of elements
66 * @size_t sz@ = element size (for @ALLOCV_SAFE_P@)
68 * Returns: Nonzero if the product of @n@ and @sz@ (or @sizeof(*p)@) is
69 * representable in type @size_t@.
72 #define ALLOCV_SAFE_P(n, sz) ((n) <= (size_t)-1/(sz))
73 #define NEWV_SAFE_P(p, n) (ALLOCV_SAFE_P((n), sizeof(*(p))))
75 /*----- Arena implementation utilities ------------------------------------*/
77 /* --- @arena_fakerealloc@ --- *
79 * Arguments: @arena *a@ = pointer to arena block
80 * @void *p@ = pointer to memory block to resize
81 * @size_t sz@ = size desired for the block
82 * @size_t osz@ = size of the old block
86 * Use: Standard fake @realloc@ function, for use if you don't
87 * support @realloc@ properly.
90 extern void *arena_fakerealloc(arena */*a*/, void */*p*/,
91 size_t /*sz*/, size_t /*osz*/);
93 /* --- @a_alloc@, @A_ALLOC@, @a_allocv@, @A_ALLOCV@ --- *
95 * Arguments: @arena *a@ = pointer to arena
96 * @size_t n@ = number of element (for @a_allocv@, @A_ALLOCV@)
97 * @size_t sz@ = size to each element
99 * Returns: Pointer to the newly allocated block, or null on failure.
101 * Use: Allocate and return a block from the arena @a@ large enough
102 * for a vector of @n@ elements each @sz@ bytes in size. If
103 * sufficient storage is not available, return a null pointer.
104 * (Some arenas may choose to abort the program instead.)
107 extern void *a_alloc(arena */*a*/, size_t /*sz*/);
108 extern void *a_allocv(arena */*a*/, size_t /*n*/, size_t /*sz*/);
110 #define A_ALLOC(a, sz) (((a)->ops->alloc)((a), (sz)))
111 #define A_ALLOCV(a, n, sz) \
112 (ALLOCV_SAFE_P((n), (sz)) ? ((a)->ops->alloc)((a), (n)*(sz)) : 0)
114 /* --- @A_NEW@, @A_NEWV@ --- *
116 * Arguments: @type *p@ = pointer to object type
117 * @arena *a@ = pointer to arena
118 * @size_t n@ = number of elements to allocate (for @A_NEWV@)
122 * Use: Set @p@ to point to a freshly allocated block of memory large
123 * enough for @n@ elements of the type that it points to, or a
128 #define A_NEW(p, a) do { (p) = A_ALLOC((a), sizeof(*(p))); } while (0)
129 #define A_NEWV(p, a, n) \
130 do { (p) = A_ALLOCV((a), (n), sizeof(*(p))); } while (0)
132 /* --- @a_realloc, @A_REALLOC@, @a_reallocv@, @A_REALLOCV@ --- *
134 * Arguments: @arena *a@ = pointer to arena
135 * @void *p@ = pointer to existing block
136 * @size_t n@ = new number of elements (for @a_reallocv@,
138 * @size_t on@ = old number of elements (for @a_reallocv@,
140 * @size_t sz@ = size of elements (for @a_reallocv@,
141 * @A_REALLOCV@) or new block size (@a_realloc@,
143 * @size_t osz@ = old block size (@a_realloc@, @A_REALLOC@)
145 * Returns: Pointer to the new block, or null on failure.
147 * Use: The @a_reallocv@ function and @A_REALLOCV@ macro adjust a
148 * block which currently has space for @on@ elements each of
149 * size @sz@, so that it now has enough space for @n@ elements,
150 * preserving the initial @min(n, on)@ elements. The
151 * @a_realloc@ function and @A_REALLOC@ macro are the same, but
152 * with @sz@ fixed equal to 1, and @n@ and @on@ renamed to @sz@
153 * and @osz@ for historical reasons.
156 extern void *a_realloc(arena */*a*/, void */*p*/,
157 size_t /*sz*/, size_t /*osz*/);
158 extern void *a_reallocv(arena */*a*/, void */*p*/,
159 size_t /*n*/, size_t /*on*/, size_t /*sz*/);
161 #define A_REALLOC(a, p, sz, osz) \
162 (((a)->ops->realloc)((a), (p), (sz), (osz)))
163 #define A_REALLOCV(a, p, n, on, sz) \
164 (ALLOCV_SAFE_P((n), (sz)) ? \
165 ((a)->ops->realloc)((a), (p), (n)*(sz), (on)*(sz)) : 0)
167 /* --- @A_RENEWV@ --- *
169 * Arguments: @type *q@ = pointer to set
170 * @type *p@ = pointer to existing allocation
171 * @arena *a@ = pointer to arena
172 * @size_t n@ = number of elements to allocate
176 * Use: On entry, @p@ should point to a block of memory allocated
177 * from @a@, with enough space for @on@ elements of the type
178 * pointed to by @p@ and @q@. The macro attempts to adjusts
179 * this block so that it has enough space for @n@ elements,
180 * preserving the initial @min(n, on)@ elements, and stores a
181 * pointer to the block's new address in @q@ if successful; on
182 * failure, @q@ is set to a null pointer and the block remains
187 #define A_RENEWV(q, p, a, n, on) do { \
188 (q) = !sizeof((*(p)) = (*(q))) + \
189 A_REALLOCV((a), (p), (n), (on), sizeof(*(p))); \
192 /* --- @a_free@, @A_FREE@ --- *
194 * Arguments: @arena *a@ = pointer to arena
195 * @void *p@ = pointer to existing block
199 * Use: Release the block pointed to by @p@.
202 extern void a_free(arena */*a*/, void */*p*/);
204 #define A_FREE(a, p) (((a)->ops->free)((a), (p)))
206 /*----- That's all, folks -------------------------------------------------*/