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,
28 /*----- Header files ------------------------------------------------------*/
30 /* --- ANSI headers --- */
34 /* --- Local headers --- */
40 /*----- Functions and macros ----------------------------------------------*/
42 /* --- @x_alloc@, @x_allocv@ --- *
44 * Arguments: @arena *a@ = pointer to underlying arena
45 * @size_t n@ = number of elements to allocate (for @x_allocv@)
46 * @size_t sz@ = size of elements to allocate
48 * Returns: Pointer to allocated block.
50 * Use: The @x_allocv@ function allocates memory for @n@ elements of
51 * @sz@ bytes each (or, perhaps, %%\emph{vice versa}). The
52 * @x_alloc@ function is the same, but with @n@ fixed equal
53 * to 1. If there's not enough memory, the exception
54 * @EXC_NOMEM@ is thrown.
57 void *x_alloc(arena *a, size_t sz)
62 if (!p) THROW(EXC_NOMEM);
66 void *x_allocv(arena *a, size_t n, size_t sz)
70 p = A_ALLOCV(a, n, sz);
71 if (!p) THROW(EXC_NOMEM);
75 /* --- @x_strdup@ --- *
77 * Arguments: @arena *a@ = pointer to underlying arena
78 * @const char *s@ = pointer to a string
80 * Returns: Pointer to a copy of the string.
82 * Use: Copies a string (like @strdup@ would, if it existed). If
83 * there's not enough memory, the exception @EXC_NOMEM@ is
87 char *x_strdup(arena *a, const char *s)
91 sz = strlen(s) + 1; p = x_alloc(a, sz); memcpy(p, s, sz);
95 /* --- @x_realloc@, @x_reallocv@ --- *
97 * Arguments: @arena *a@ = pointer to underlying arena
98 * @void *p@ = pointer to a block of memory
99 * @size_t n@ = new number of elements (for @x_reallocv@)
100 * @size_t on@ = old number of elements (for @x_reallocv@)
101 * @size_t sz@ = size of elements (for @x_reallocv@) or new
102 * block size (for @x_realloc@)
103 * @size_t osz@ = size of the old block (for @x_realloc@)
105 * Returns: Pointer to the resized memory block (which is almost
106 * certainly not in the same place any more).
108 * Use: Resizes a memory block. If there's not enough memory, the
109 * exception @EXC_NOMEM@ is thrown.
111 * The @x_reallocv@ function adjusts a block which currently has
112 * space for @on@ elements each of size @sz@, so that it now has
113 * enough space for @n@ elements, preserving the initial @min(n,
114 * on)@ elements. The @x_realloc@ function is the same, but
115 * with @sz@ fixed equal to 1, and @n@ and @on@ renamed to @sz@
116 * and @osz@ for historical reasons.
119 void *x_realloc(arena *a, void *p, size_t sz, size_t osz)
121 p = A_REALLOC(a, p, sz, osz);
122 if (!p) THROW(EXC_NOMEM);
126 void *x_reallocv(arena *a, void *p, size_t n, size_t on, size_t sz)
128 p = A_REALLOCV(a, p, n, on, sz);
129 if (!p) THROW(EXC_NOMEM);
133 /* --- @x_free@ --- *
135 * Arguments: @arena *a@ = pointer to underlying arena
136 * @void *p@ = pointer to a block of memory.
140 * Use: Frees a block of memory.
143 void (x_free)(arena *a, void *p) { x_free(a, p); }
145 /*----- Old functions for the standard arena ------------------------------*/
147 /* --- @xmalloc@, @xmallocv@ --- *
149 * Arguments: @size_t n@ = number of elements to allocate (for @xmallocv@)
150 * @size_t sz@ = size of block to allocate
152 * Returns: Pointer to allocated block.
154 * Use: Allocates memory for @n@ elements each of size @sz@. For
155 * @xmalloc@, @n@ is fixed equal to 1. If there's not enough
156 * memory, the exception @EXC_NOMEM@ is thrown.
159 void *(xmalloc)(size_t sz) { return xmalloc(sz); }
160 void *(xmallocv)(size_t n, size_t sz) { return xmallocv(n, sz); }
162 /* --- @xstrdup@ --- *
164 * Arguments: @const char *s@ = pointer to a string
166 * Returns: Pointer to a copy of the string.
168 * Use: Copies a string (like @strdup@ would, if it existed). If
169 * there's not enough memory, the exception @EXC_NOMEM@ is
173 char *(xstrdup)(const char *s) { return xstrdup(s); }
175 /* --- @xrealloc@, @xreallocv@ --- *
177 * Arguments: @void *p@ = pointer to a block of memory
178 * @size_t n@ = new number of elements (for @xreallocv@)
179 * @size_t on@ = old number of elements (for @xreallocv@)
180 * @size_t sz@ = size of elements (for @xreallocv@) or new
181 * block size (for @xrealloc@)
182 * @size_t osz@ = size of the old block (for @xrealloc@)
184 * Returns: Pointer to the resized memory block (which is almost
185 * certainly not in the same place any more).
187 * Use: Resizes a memory block. If there's not enough memory, the
188 * exception @EXC_NOMEM@ is thrown.
190 * The @xreallocv@ function adjusts a block which currently has
191 * space for @on@ elements each of size @sz@, so that it now has
192 * enough space for @n@ elements, preserving the initial @min(n,
193 * on)@ elements. The @xrealloc@ function is the same, but
194 * with @sz@ fixed equal to 1, and @n@ and @on@ renamed to @sz@
195 * and @osz@ for historical reasons.
198 void *(xrealloc)(void *p, size_t sz, size_t osz)
199 { return xrealloc(p, sz, osz); }
200 void *(xreallocv)(void *p, size_t n, size_t on, size_t sz)
201 { return xreallocv(p, n, on, sz); }
205 * Arguments: @void *p@ = pointer to a block of memory.
209 * Use: Frees a block of memory.
212 void (xfree)(void *p) { xfree(p); }
214 /*----- That's all, folks -------------------------------------------------*/