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