chiark / gitweb /
struct/sym.c: Fix loading following table extension.
[mLib] / mem / arena.h
1 /* -*-c-*-
2  *
3  * Abstraction for memory allocation arenas
4  *
5  * (c) 2000 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_ARENA_H
29 #define MLIB_ARENA_H
30
31 #ifdef __cplusplus
32   extern "C" {
33 #endif
34
35 /*----- Header files ------------------------------------------------------*/
36
37 #include <stdlib.h>
38
39 /*----- Data structures ---------------------------------------------------*/
40
41 /* --- An arena structure --- */
42
43 typedef struct arena {
44   const struct arena_ops *ops;
45 } arena;
46
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*/);
52 } arena_ops;
53
54 /*----- Global variables --------------------------------------------------*/
55
56 extern arena *arena_global;             /* Standard global arena */
57 extern arena arena_stdlib;              /* Arena based on @malloc@/@free@ */
58
59 /*----- Allocation utilities ----------------------------------------------*/
60
61 /* --- @ALLOCV_SAFE_P@, @NEWV_SAFE_P@ ---
62  *
63  * Arguments:   @type *p@ = pointer to a vector (for @NEWV_SAFE_P@; not
64  *                      evaluated)
65  *              @size_t n@ = number of elements
66  *              @size_t sz@ = element size (for @ALLOCV_SAFE_P@)
67  *
68  * Returns:     Nonzero if the product of @n@ and @sz@ (or @sizeof(*p)@) is
69  *              representable in type @size_t@.
70  */
71
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))))
74
75 /*----- Arena implementation utilities ------------------------------------*/
76
77 /* --- @arena_fakerealloc@ --- *
78  *
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
83  *
84  * Returns:     ---
85  *
86  * Use:         Standard fake @realloc@ function, for use if you don't
87  *              support @realloc@ properly.
88  */
89
90 extern void *arena_fakerealloc(arena */*a*/, void */*p*/,
91                                size_t /*sz*/, size_t /*osz*/);
92
93 /* --- @a_alloc@, @A_ALLOC@, @a_allocv@, @A_ALLOCV@ --- *
94  *
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
98  *
99  * Returns:     Pointer to the newly allocated block, or null on failure.
100  *
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.)
105  */
106
107 extern void *a_alloc(arena */*a*/, size_t /*sz*/);
108 extern void *a_allocv(arena */*a*/, size_t /*n*/, size_t /*sz*/);
109
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)
113
114 /* --- @A_NEW@, @A_NEWV@ --- *
115  *
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@)
119  *
120  * Returns:     ---
121  *
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
124  *              null pointer.
125  *
126  */
127
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)
131
132 /* --- @a_realloc, @A_REALLOC@, @a_reallocv@, @A_REALLOCV@ --- *
133  *
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@,
137  *                      @A_REALLOCV@)
138  *              @size_t on@ = old number of elements (for @a_reallocv@,
139  *                      @A_REALLOCV@)
140  *              @size_t sz@ = size of elements (for @a_reallocv@,
141  *                      @A_REALLOCV@) or new block size (@a_realloc@,
142  *                      @A_REALLOC@)
143  *              @size_t osz@ = old block size (@a_realloc@, @A_REALLOC@)
144  *
145  * Returns:     Pointer to the new block, or null on failure.
146  *
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.
154  */
155
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*/);
160
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)
166
167 /* --- @A_RENEWV@ --- *
168  *
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
173  *
174  * Returns:     ---
175  *
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
183  *              unchanged.
184  *
185  */
186
187 #define A_RENEWV(q, p, a, n, on) do {                                   \
188         (q) = !sizeof((*(p)) = (*(q))) +                                \
189               A_REALLOCV((a), (p), (n), (on), sizeof(*(p)));            \
190 } while (0)
191
192 /* --- @a_free@, @A_FREE@ --- *
193  *
194  * Arguments:   @arena *a@ = pointer to arena
195  *              @void *p@ = pointer to existing block
196  *
197  * Returns:     ---
198  *
199  * Use:         Release the block pointed to by @p@.
200  */
201
202 extern void a_free(arena */*a*/, void */*p*/);
203
204 #define A_FREE(a, p) (((a)->ops->free)((a), (p)))
205
206 /*----- That's all, folks -------------------------------------------------*/
207
208 #ifdef __cplusplus
209   }
210 #endif
211
212 #endif