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 --- */
36 /* --- Local headers --- */
42 /*----- Functions and macros ----------------------------------------------*/
44 /* --- @x_alloc@, @x_allocn@ --- *
46 * Arguments: @arena *a@ = pointer to underlying arena
47 * @size_t n@ = number of elements to allocate (for @x_allocv@)
48 * @size_t sz@ = size of elements to allocate
50 * Returns: Pointer to allocated block.
52 * Use: The @x_allocn@ function allocates memory for @n@ elements of
53 * @sz@ bytes each (or, perhaps, %%\emph{vice versa}). The
54 * @x_alloc@ function is the same, but with @n@ fixed equal
55 * to 1. If there's not enough memory, the exception
56 * @EXC_NOMEM@ is thrown.
59 void *x_alloc(arena
*a
, size_t sz
)
64 if (!p
) THROW(EXC_NOMEM
);
68 void *x_allocv(arena
*a
, size_t n
, size_t sz
)
72 p
= A_ALLOCV(a
, n
, sz
);
73 if (!p
) THROW(EXC_NOMEM
);
77 /* --- @x_strdup@ --- *
79 * Arguments: @arena *a@ = pointer to underlying arena
80 * @const char *s@ = pointer to a string
82 * Returns: Pointer to a copy of the string.
84 * Use: Copies a string (like @strdup@ would, if it existed). If
85 * there's not enough memory, the exception @EXC_NOMEM@ is
89 char *x_strdup(arena
*a
, const char *s
)
93 sz
= strlen(s
) + 1; p
= x_alloc(a
, sz
); memcpy(p
, s
, sz
);
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 @min(n,
116 * on)@ elements. The @x_realloc@ function is the same, but
117 * with @sz@ fixed equal to 1, and @n@ and @on@ renamed to @sz@
118 * and @osz@ for historical reasons.
121 void *x_realloc(arena
*a
, void *p
, size_t sz
, size_t osz
)
123 p
= A_REALLOC(a
, p
, sz
, osz
);
124 if (!p
) THROW(EXC_NOMEM
);
128 void *x_reallocv(arena
*a
, void *p
, size_t n
, size_t on
, size_t sz
)
130 p
= A_REALLOCV(a
, p
, n
, on
, sz
);
131 if (!p
) THROW(EXC_NOMEM
);
135 /* --- @x_free@ --- *
137 * Arguments: @arena *a@ = pointer to underlying arena
138 * @void *p@ = pointer to a block of memory.
142 * Use: Frees a block of memory.
145 void (x_free
)(arena
*a
, void *p
) { x_free(a
, p
); }
147 /*----- Old functions for the standard arena ------------------------------*/
149 /* --- @xmalloc@, @xmallocv@ --- *
151 * Arguments: @size_t n@ = number of elements to allocate (for @xmallocv@)
152 * @size_t sz@ = size of block to allocate
154 * Returns: Pointer to allocated block.
156 * Use: Allocates memory for @n@ elements each of size @sz@. For
157 * @xmalloc@, @n@ is fixed equal to 1. If there's not enough
158 * memory, the exception @EXC_NOMEM@ is thrown.
161 void *(xmalloc
)(size_t sz
) { return xmalloc(sz
); }
162 void *(xmallocv
)(size_t n
, size_t sz
) { return xmallocv(n
, sz
); }
164 /* --- @xstrdup@ --- *
166 * Arguments: @const char *s@ = pointer to a string
168 * Returns: Pointer to a copy of the string.
170 * Use: Copies a string (like @strdup@ would, if it existed). If
171 * there's not enough memory, the exception @EXC_NOMEM@ is
175 char *(xstrdup
)(const char *s
) { return xstrdup(s
); }
177 /* --- @xrealloc@, @xreallocv@ --- *
179 * Arguments: @void *p@ = pointer to a block of memory
180 * @size_t n@ = new number of elements (for @xreallocv@)
181 * @size_t on@ = old number of elements (for @xreallocv@)
182 * @size_t sz@ = size of elements (for @xreallocv@) or new
183 * block size (for @xrealloc@)
184 * @size_t osz@ = size of the old block (for @xrealloc@)
186 * Returns: Pointer to the resized memory block (which is almost
187 * certainly not in the same place any more).
189 * Use: Resizes a memory block. If there's not enough memory, the
190 * exception @EXC_NOMEM@ is thrown.
192 * The @xreallocv@ function adjusts a block which currently has
193 * space for @on@ elements each of size @sz@, so that it now has
194 * enough space for @n@ elements, preserving the initial @min(n,
195 * on)@ elements. The @xrealloc@ function is the same, but
196 * with @sz@ fixed equal to 1, and @n@ and @on@ renamed to @sz@
197 * and @osz@ for historical reasons.
200 void *(xrealloc
)(void *p
, size_t sz
, size_t osz
)
201 { return xrealloc(p
, sz
, osz
); }
202 void *(xreallocv
)(void *p
, size_t n
, size_t on
, size_t sz
)
203 { return xreallocv(p
, n
, on
, sz
); }
207 * Arguments: @void *p@ = pointer to a block of memory.
211 * Use: Frees a block of memory.
214 void (xfree
)(void *p
) { xfree(p
); }
216 /*----- That's all, folks -------------------------------------------------*/