chiark / gitweb /
@@@ random mess
[mLib] / mem / alloc.h
1 /* -*-c-*-
2  *
3  * Memory allocation functions
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_ALLOC_H
29 #define MLIB_ALLOC_H
30
31 #ifdef __cplusplus
32   extern "C" {
33 #endif
34
35 /*----- Required header files ---------------------------------------------*/
36
37 #include <stddef.h>
38
39 #ifndef MLIB_ARENA_H
40 #  include "arena.h"
41 #endif
42
43 /*----- Functions and macros ----------------------------------------------*/
44
45 /* --- @x_alloc@, @x_allocv@ --- *
46  *
47  * Arguments:   @arena *a@ = pointer to underlying arena
48  *              @size_t n@ = number of elements to allocate (for @x_allocv@)
49  *              @size_t sz@ = size of elements to allocate
50  *
51  * Returns:     Pointer to allocated block.
52  *
53  * Use:         The @x_allocv@ function allocates memory for @n@ elements of
54  *              @sz@ bytes each (or, perhaps, %%\emph{vice versa}).  The
55  *              @x_alloc@ function is the same, but with @n@ fixed equal
56  *              to 1.  If there's not enough memory, the exception
57  *              @EXC_NOMEM@ is thrown.
58  */
59
60 extern void *x_alloc(arena */*a*/, size_t /*sz*/);
61 extern void *x_allocv(arena */*a*/, size_t /*n*/, size_t /*sz*/);
62
63 /* --- @X_NEW@, @X_NEWV@ --- *
64  *
65  * Arguments:   @type *p@ = a pointer to allocate
66  *              @arena *a@ = pointer to underlying arena
67  *              @size_t n@ = number of elements (for @X_NEWV@)
68  *
69  * Returns:     ---
70  *
71  * Use:         The @X_NEW@ macro sets @p@ to point to a freshly allocated
72  *              block of memory large enough for an object of the type
73  *              pointed to by @p@.  The @X_NEWV@ macro allocated enough space
74  *              for a vector of @n@ elements, each of the type pointed to by
75  *              @p@.  If there is not enough memory, the exception
76  *              @EXC_NOMEM@ is thrown.
77  */
78
79 #define X_NEW(p, a) do { (p) = x_alloc((a), sizeof(*(p))); } while (0)
80 #define X_NEWV(p, a, n)                                                 \
81         do { (p) = x_allocv((a), (n), sizeof(*(p))); } while (0)
82
83 /* --- @x_strdup@ --- *
84  *
85  * Arguments:   @arena *a@ = pointer to underlying arena
86  *              @const char *s@ = pointer to a string
87  *
88  * Returns:     Pointer to a copy of the string.
89  *
90  * Use:         Copies a string (like @strdup@ would, if it existed).  If
91  *              there's not enough memory, the exception @EXC_NOMEM@ is
92  *              thrown.
93  */
94
95 extern char *x_strdup(arena */*a*/, const char */*s*/);
96
97 /* --- @x_realloc@, @x_reallocv@ --- *
98  *
99  * Arguments:   @arena *a@ = pointer to underlying arena
100  *              @void *p@ = pointer to a block of memory
101  *              @size_t n@ = new number of elements (for @x_reallocv@)
102  *              @size_t on@ = old  number of elements (for @x_reallocv@)
103  *              @size_t sz@ = size of elements (for @x_reallocv@) or new
104  *                      block size (for @x_realloc@)
105  *              @size_t osz@ = size of the old block (for @x_realloc@)
106  *
107  * Returns:     Pointer to the resized memory block (which is almost
108  *              certainly not in the same place any more).
109  *
110  * Use:         Resizes a memory block.  If there's not enough memory, the
111  *              exception @EXC_NOMEM@ is thrown.
112  *
113  *              The @x_reallocv@ function adjusts a block which currently has
114  *              space for @on@ elements each of size @sz@, so that it now has
115  *              enough space for @n@ elements, preserving the initial
116  *              @min(n, on)@ elements.  The @x_realloc@ function is the same,
117  *              but with @sz@ fixed equal to 1, and @n@ and @on@ renamed to
118  *              @sz@ and @osz@ for historical reasons.
119  */
120
121 extern void *x_realloc(arena */*a*/, void */*p*/,
122                        size_t /*sz*/, size_t /*osz*/);
123 extern void *x_reallocv(arena */*a*/, void */*p*/,
124                         size_t /*n*/, size_t /*on*/, size_t /*sz*/);
125
126 /* --- @X_RENEWV@ --- *
127  *
128  * Arguments:   @type *p@ = a pointer to allocate
129  *              @arena *a@ = pointer to underlying arena
130  *              @size_t n, on@ = new and existing numbers of elements
131  *
132  * Returns:     ---
133  *
134  * Use:         Adjust @p@ to point to a new block of memory with space for
135  *              @n@ elements of the type pointed to by @p@, on the assumption
136  *              that @p@ is either null or currently points to a block with
137  *              space for @on@ elements.
138  */
139
140 #define X_RENEWV(p, a, n, on)                                           \
141         do { (p) = x_reallocv((a), (p), (n), (on), sizeof(*(p))); } while (0)
142
143 /* --- @x_free@ --- *
144  *
145  * Arguments:   @arena *a@ = pointer to underlying arena
146  *              @void *p@ = pointer to a block of memory.
147  *
148  * Returns:     ---
149  *
150  * Use:         Frees a block of memory.
151  */
152
153 extern void x_free(arena */*a*/, void */*p*/);
154 #define x_free(a, p) A_FREE(a, p)
155
156 /*----- Old functions for the standard arena ------------------------------*/
157
158 /* --- @xmalloc@, @xmallocv@ --- *
159  *
160  * Arguments:   @size_t n@ = number of elements to allocate (for @xmallocv@)
161  *              @size_t sz@ = size of block to allocate
162  *
163  * Returns:     Pointer to allocated block.
164  *
165  * Use:         Allocates memory for @n@ elements each of size @sz@.  For
166  *              @xmalloc@, @n@ is fixed equal to 1.  If there's not enough
167  *              memory, the exception @EXC_NOMEM@ is thrown.
168  */
169
170 extern void *xmalloc(size_t /*sz*/);
171 extern void *xmallocv(size_t /*n*/, size_t /*sz*/);
172 #define xmalloc(sz) x_alloc(arena_global, (sz))
173 #define xmallocv(n, sz) x_allocv(arena_global, (n), (sz))
174
175 /* --- @XNEW@, @XNEWV@ --- *
176  *
177  * Arguments:   @type *p@ = a pointer to allocate
178  *              @size_t n@ = number of elements
179  *
180  * Returns:     ---
181  *
182  * Use:         Set @p@ to point to a freshly allocate block large enough for
183  *              @n@ elements each of the type pointed to by @p@.
184  */
185
186 #define XNEW(p) X_NEW(p, arena_global);
187 #define XNEWV(p, n) X_NEWV(p, arena_global, n);
188
189 /* --- @xstrdup@ --- *
190  *
191  * Arguments:   @const char *s@ = pointer to a string
192  *
193  * Returns:     Pointer to a copy of the string.
194  *
195  * Use:         Copies a string (like @strdup@ would, if it existed).  If
196  *              there's not enough memory, the exception @EXC_NOMEM@ is
197  *              thrown.
198  */
199
200 extern char *xstrdup(const char */*s*/);
201 #define xstrdup(p) x_strdup(arena_global, (p))
202
203 /* --- @xrealloc@, @xreallocv@ --- *
204  *
205  * Arguments:   @void *p@ = pointer to a block of memory
206  *              @size_t n@ = new number of elements (for @xreallocv@)
207  *              @size_t on@ = old  number of elements (for @xreallocv@)
208  *              @size_t sz@ = size of elements (for @xreallocv@) or new
209  *                      block size (for @xrealloc@)
210  *              @size_t osz@ = size of the old block (for @xrealloc@)
211  *
212  * Returns:     Pointer to the resized memory block (which is almost
213  *              certainly not in the same place any more).
214  *
215  * Use:         Resizes a memory block.  If there's not enough memory, the
216  *              exception @EXC_NOMEM@ is thrown.
217  *
218  *              The @xreallocv@ function adjusts a block which currently has
219  *              space for @on@ elements each of size @sz@, so that it now has
220  *              enough space for @n@ elements, preserving the initial @min(n,
221  *              on)@ elements.  The @xrealloc@ function is the same, but
222  *              with @sz@ fixed equal to 1, and @n@ and @on@ renamed to @sz@
223  *              and @osz@ for historical reasons.
224  */
225
226 extern void *xrealloc(void */*p*/, size_t /*sz*/, size_t /*osz*/);
227 extern void *xreallocv(void */*p*/,
228                        size_t /*n*/, size_t /*on*/, size_t /*sz*/);
229 #define xrealloc(p, sz, osz) x_realloc(arena_global, (p), (sz), (osz))
230 #define xreallocv(p, sz, osz, n) x_reallocv(arena_global,               \
231                                             (p), (sz), (osz), (n))
232
233 /* --- @XRENEWV@ --- *
234  *
235  * Arguments:   @type *p@ = a pointer to allocate
236  *              @size_t n, on@ = new and existing numbers of elements
237  *
238  * Returns:     ---
239  *
240  * Use:         Adjust @p@ to point to a new block of memory with space for
241  *              @n@ elements of the type pointed to by @p@, on the assumption
242  *              that @p@ is either null or currently points to a block with
243  *              space for @on@ elements.
244  */
245
246 #define XRENEWV(p, n, on) X_RENEWV((p), arena_global, (n), (on))
247
248 /* --- @xfree@ --- *
249  *
250  * Arguments:   @void *p@ = pointer to a block of memory.
251  *
252  * Returns:     ---
253  *
254  * Use:         Frees a block of memory.
255  */
256
257 extern void xfree(void */*p*/);
258 #define xfree(p) x_free(arena_global, (p))
259
260 /*----- That's all, folks -------------------------------------------------*/
261
262 #ifdef __cplusplus
263   }
264 #endif
265
266 #endif