@@@ fltfmt mess
[mLib] / mem / alloc.h
CommitLineData
0875b58f 1/* -*-c-*-
2 *
0875b58f 3 * Memory allocation functions
4 *
5 * (c) 1998 Straylight/Edgeware
6 */
7
d4efbcd9 8/*----- Licensing notice --------------------------------------------------*
0875b58f 9 *
10 * This file is part of the mLib utilities library.
11 *
12 * mLib is free software; you can redistribute it and/or modify
c846879c 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.
d4efbcd9 16 *
0875b58f 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
c846879c 20 * GNU Library General Public License for more details.
d4efbcd9 21 *
c846879c 22 * You should have received a copy of the GNU Library General Public
0bd98442 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.
0875b58f 26 */
27
c6e0eaf0 28#ifndef MLIB_ALLOC_H
29#define MLIB_ALLOC_H
0875b58f 30
31#ifdef __cplusplus
32 extern "C" {
33#endif
34
0fd574c3 35/*----- Required header files ---------------------------------------------*/
36
0875b58f 37#include <stddef.h>
38
0fd574c3 39#ifndef MLIB_ARENA_H
40# include "arena.h"
41#endif
42
43/*----- Functions and macros ----------------------------------------------*/
44
b1a20bee 45/* --- @x_alloc@, @x_allocv@ --- *
0fd574c3 46 *
47 * Arguments: @arena *a@ = pointer to underlying arena
b1a20bee
MW
48 * @size_t n@ = number of elements to allocate (for @x_allocv@)
49 * @size_t sz@ = size of elements to allocate
0fd574c3 50 *
51 * Returns: Pointer to allocated block.
52 *
b1a20bee
MW
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.
0fd574c3 58 */
59
60extern void *x_alloc(arena */*a*/, size_t /*sz*/);
b1a20bee
MW
61extern 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)
0fd574c3 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
91extern char *x_strdup(arena */*a*/, const char */*s*/);
92
b1a20bee 93/* --- @x_realloc@, @x_reallocv@ --- *
0fd574c3 94 *
95 * Arguments: @arena *a@ = pointer to underlying arena
96 * @void *p@ = pointer to a block of memory
b1a20bee
MW
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@)
0fd574c3 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.
b1a20bee
MW
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.
0fd574c3 115 */
116
b5ea4de3 117extern void *x_realloc(arena */*a*/, void */*p*/,
118 size_t /*sz*/, size_t /*osz*/);
b1a20bee
MW
119extern 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)
0fd574c3 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
149extern 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 ------------------------------*/
0875b58f 153
b1a20bee 154/* --- @xmalloc@, @xmallocv@ --- *
0875b58f 155 *
b1a20bee
MW
156 * Arguments: @size_t n@ = number of elements to allocate (for @xmallocv@)
157 * @size_t sz@ = size of block to allocate
0875b58f 158 *
159 * Returns: Pointer to allocated block.
160 *
b1a20bee
MW
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.
0875b58f 164 */
165
166extern void *xmalloc(size_t /*sz*/);
b1a20bee 167extern void *xmallocv(size_t /*n*/, size_t /*sz*/);
0fd574c3 168#define xmalloc(sz) x_alloc(arena_global, (sz))
b1a20bee
MW
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);
0875b58f 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
196extern char *xstrdup(const char */*s*/);
0fd574c3 197#define xstrdup(p) x_strdup(arena_global, (p))
0875b58f 198
b1a20bee 199/* --- @xrealloc@, @xreallocv@ --- *
0875b58f 200 *
201 * Arguments: @void *p@ = pointer to a block of memory
b1a20bee
MW
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@)
0875b58f 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.
b1a20bee
MW
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.
0875b58f 220 */
221
b5ea4de3 222extern void *xrealloc(void */*p*/, size_t /*sz*/, size_t /*osz*/);
b1a20bee
MW
223extern void *xreallocv(void */*p*/,
224 size_t /*n*/, size_t /*on*/, size_t /*sz*/);
b5ea4de3 225#define xrealloc(p, sz, osz) x_realloc(arena_global, (p), (sz), (osz))
b1a20bee
MW
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))
0fd574c3 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
253extern void xfree(void */*p*/);
254#define xfree(p) x_free(arena_global, (p))
0875b58f 255
256/*----- That's all, folks -------------------------------------------------*/
257
258#ifdef __cplusplus
259 }
260#endif
261
262#endif