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 (for @X_NEWV@)
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.
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)
83 /* --- @x_strdup@ --- *
85 * Arguments: @arena *a@ = pointer to underlying arena
86 * @const char *s@ = pointer to a string
88 * Returns: Pointer to a copy of the string.
90 * Use: Copies a string (like @strdup@ would, if it existed). If
91 * there's not enough memory, the exception @EXC_NOMEM@ is
95 extern char *x_strdup(arena */*a*/, const char */*s*/);
97 /* --- @x_realloc@, @x_reallocv@ --- *
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@)
107 * Returns: Pointer to the resized memory block (which is almost
108 * certainly not in the same place any more).
110 * Use: Resizes a memory block. If there's not enough memory, the
111 * exception @EXC_NOMEM@ is thrown.
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.
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*/);
126 /* --- @X_RENEWV@ --- *
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
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.
140 #define X_RENEWV(p, a, n, on) \
141 do { (p) = x_reallocv((a), (p), (n), (on), sizeof(*(p))); } while (0)
143 /* --- @x_free@ --- *
145 * Arguments: @arena *a@ = pointer to underlying arena
146 * @void *p@ = pointer to a block of memory.
150 * Use: Frees a block of memory.
153 extern void x_free(arena */*a*/, void */*p*/);
154 #define x_free(a, p) A_FREE(a, p)
156 /*----- Old functions for the standard arena ------------------------------*/
158 /* --- @xmalloc@, @xmallocv@ --- *
160 * Arguments: @size_t n@ = number of elements to allocate (for @xmallocv@)
161 * @size_t sz@ = size of block to allocate
163 * Returns: Pointer to allocated block.
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.
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))
175 /* --- @XNEW@, @XNEWV@ --- *
177 * Arguments: @type *p@ = a pointer to allocate
178 * @size_t n@ = number of elements
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@.
186 #define XNEW(p) X_NEW(p, arena_global);
187 #define XNEWV(p, n) X_NEWV(p, arena_global, n);
189 /* --- @xstrdup@ --- *
191 * Arguments: @const char *s@ = pointer to a string
193 * Returns: Pointer to a copy of the string.
195 * Use: Copies a string (like @strdup@ would, if it existed). If
196 * there's not enough memory, the exception @EXC_NOMEM@ is
200 extern char *xstrdup(const char */*s*/);
201 #define xstrdup(p) x_strdup(arena_global, (p))
203 /* --- @xrealloc@, @xreallocv@ --- *
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@)
212 * Returns: Pointer to the resized memory block (which is almost
213 * certainly not in the same place any more).
215 * Use: Resizes a memory block. If there's not enough memory, the
216 * exception @EXC_NOMEM@ is thrown.
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.
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))
233 /* --- @XRENEWV@ --- *
235 * Arguments: @type *p@ = a pointer to allocate
236 * @size_t n, on@ = new and existing numbers of elements
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.
246 #define XRENEWV(p, n, on) X_RENEWV((p), arena_global, (n), (on))
250 * Arguments: @void *p@ = pointer to a block of memory.
254 * Use: Frees a block of memory.
257 extern void xfree(void */*p*/);
258 #define xfree(p) x_free(arena_global, (p))
260 /*----- That's all, folks -------------------------------------------------*/