3 * Memory allocation functions
5 * (c) 1998 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 /*----- Required header files ---------------------------------------------*/
43 /*----- Functions and macros ----------------------------------------------*/
45 /* --- @x_alloc@, @x_allocv@ --- *
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
51 * Returns: Pointer to allocated block.
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.
60 extern void *x_alloc(arena */
*a*/
, size_t /*sz*/);
61 extern void *x_allocv(arena */
*a*/
, size_t /*n*/, size_t /*sz*/);
63 /* --- @X_NEW@, @X_NEWV@ --- *
65 * Arguments: @type *p@ = a pointer to allocate
66 * @arena *a@ = pointer to underlying arena
67 * @size_t n@ = number of elements
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@.
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)
79 /* --- @x_strdup@ --- *
81 * Arguments: @arena *a@ = pointer to underlying arena
82 * @const char *s@ = pointer to a string
84 * Returns: Pointer to a copy of the string.
86 * Use: Copies a string (like @strdup@ would, if it existed). If
87 * there's not enough memory, the exception @EXC_NOMEM@ is
91 extern char *x_strdup(arena */
*a*/
, const char */
*s*/
);
93 /* --- @x_realloc@, @x_reallocv@ --- *
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@)
103 * Returns: Pointer to the resized memory block (which is almost
104 * certainly not in the same place any more).
106 * Use: Resizes a memory block. If there's not enough memory, the
107 * exception @EXC_NOMEM@ is thrown.
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.
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*/);
122 /* --- @X_RENEWV@ --- *
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
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.
136 #define X_RENEWV(p, a, n, on) \
137 do { (p) = x_reallocv((a), (p), (n), (on), sizeof(*(p))); } while (0)
139 /* --- @x_free@ --- *
141 * Arguments: @arena *a@ = pointer to underlying arena
142 * @void *p@ = pointer to a block of memory.
146 * Use: Frees a block of memory.
149 extern void x_free(arena */
*a*/
, void */
*p*/
);
150 #define x_free(a, p) A_FREE(a, p)
152 /*----- Old functions for the standard arena ------------------------------*/
154 /* --- @xmalloc@, @xmallocv@ --- *
156 * Arguments: @size_t n@ = number of elements to allocate (for @xmallocv@)
157 * @size_t sz@ = size of block to allocate
159 * Returns: Pointer to allocated block.
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.
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))
171 /* --- @XNEW@, @XNEWV@ --- *
173 * Arguments: @type *p@ = a pointer to allocate
174 * @size_t n@ = number of elements
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@.
182 #define XNEW(p) X_NEW(p, arena_global);
183 #define XNEWV(p, n) X_NEWV(p, arena_global, n);
185 /* --- @xstrdup@ --- *
187 * Arguments: @const char *s@ = pointer to a string
189 * Returns: Pointer to a copy of the string.
191 * Use: Copies a string (like @strdup@ would, if it existed). If
192 * there's not enough memory, the exception @EXC_NOMEM@ is
196 extern char *xstrdup(const char */
*s*/
);
197 #define xstrdup(p) x_strdup(arena_global, (p))
199 /* --- @xrealloc@, @xreallocv@ --- *
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@)
208 * Returns: Pointer to the resized memory block (which is almost
209 * certainly not in the same place any more).
211 * Use: Resizes a memory block. If there's not enough memory, the
212 * exception @EXC_NOMEM@ is thrown.
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.
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))
229 /* --- @XRENEWV@ --- *
231 * Arguments: @type *p@ = a pointer to allocate
232 * @size_t n, on@ = new and existing numbers of elements
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.
242 #define XRENEWV(p, n, on) X_RENEWV((p), arena_global, (n), (on))
246 * Arguments: @void *p@ = pointer to a block of memory.
250 * Use: Frees a block of memory.
253 extern void xfree(void */
*p*/
);
254 #define xfree(p) x_free(arena_global, (p))
256 /*----- That's all, folks -------------------------------------------------*/