@@@ fltfmt mess
[mLib] / mem / alloc.c
1 /* -*-c-*-
2 *
3 * Memory allocation functions
4 *
5 * (c) 1998 Straylight/Edgeware
6 */
7
8 /*----- Licensing notice --------------------------------------------------*
9 *
10 * This file is part of the mLib utilities library.
11 *
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.
16 *
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.
21 *
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,
25 * MA 02111-1307, USA.
26 */
27
28 /*----- Header files ------------------------------------------------------*/
29
30 /* --- ANSI headers --- */
31
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include <string.h>
35
36 /* --- Local headers --- */
37
38 #include "alloc.h"
39 #include "arena.h"
40 #include "exc.h"
41
42 /*----- Functions and macros ----------------------------------------------*/
43
44 /* --- @x_alloc@, @x_allocn@ --- *
45 *
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
49 *
50 * Returns: Pointer to allocated block.
51 *
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.
57 */
58
59 void *x_alloc(arena *a, size_t sz)
60 {
61 void *p;
62
63 p = A_ALLOC(a, sz);
64 if (!p) THROW(EXC_NOMEM);
65 else return (p);
66 }
67
68 void *x_allocv(arena *a, size_t n, size_t sz)
69 {
70 void *p;
71
72 p = A_ALLOCV(a, n, sz);
73 if (!p) THROW(EXC_NOMEM);
74 else return (p);
75 }
76
77 /* --- @x_strdup@ --- *
78 *
79 * Arguments: @arena *a@ = pointer to underlying arena
80 * @const char *s@ = pointer to a string
81 *
82 * Returns: Pointer to a copy of the string.
83 *
84 * Use: Copies a string (like @strdup@ would, if it existed). If
85 * there's not enough memory, the exception @EXC_NOMEM@ is
86 * thrown.
87 */
88
89 char *x_strdup(arena *a, const char *s)
90 {
91 char *p; size_t sz;
92
93 sz = strlen(s) + 1; p = x_alloc(a, sz); memcpy(p, s, sz);
94 return (p);
95 }
96
97 /* --- @x_realloc@, @x_reallocv@ --- *
98 *
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@)
106 *
107 * Returns: Pointer to the resized memory block (which is almost
108 * certainly not in the same place any more).
109 *
110 * Use: Resizes a memory block. If there's not enough memory, the
111 * exception @EXC_NOMEM@ is thrown.
112 *
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.
119 */
120
121 void *x_realloc(arena *a, void *p, size_t sz, size_t osz)
122 {
123 p = A_REALLOC(a, p, sz, osz);
124 if (!p) THROW(EXC_NOMEM);
125 else return (p);
126 }
127
128 void *x_reallocv(arena *a, void *p, size_t n, size_t on, size_t sz)
129 {
130 p = A_REALLOCV(a, p, n, on, sz);
131 if (!p) THROW(EXC_NOMEM);
132 else return (p);
133 }
134
135 /* --- @x_free@ --- *
136 *
137 * Arguments: @arena *a@ = pointer to underlying arena
138 * @void *p@ = pointer to a block of memory.
139 *
140 * Returns: ---
141 *
142 * Use: Frees a block of memory.
143 */
144
145 void (x_free)(arena *a, void *p) { x_free(a, p); }
146
147 /*----- Old functions for the standard arena ------------------------------*/
148
149 /* --- @xmalloc@, @xmallocv@ --- *
150 *
151 * Arguments: @size_t n@ = number of elements to allocate (for @xmallocv@)
152 * @size_t sz@ = size of block to allocate
153 *
154 * Returns: Pointer to allocated block.
155 *
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.
159 */
160
161 void *(xmalloc)(size_t sz) { return xmalloc(sz); }
162 void *(xmallocv)(size_t n, size_t sz) { return xmallocv(n, sz); }
163
164 /* --- @xstrdup@ --- *
165 *
166 * Arguments: @const char *s@ = pointer to a string
167 *
168 * Returns: Pointer to a copy of the string.
169 *
170 * Use: Copies a string (like @strdup@ would, if it existed). If
171 * there's not enough memory, the exception @EXC_NOMEM@ is
172 * thrown.
173 */
174
175 char *(xstrdup)(const char *s) { return xstrdup(s); }
176
177 /* --- @xrealloc@, @xreallocv@ --- *
178 *
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@)
185 *
186 * Returns: Pointer to the resized memory block (which is almost
187 * certainly not in the same place any more).
188 *
189 * Use: Resizes a memory block. If there's not enough memory, the
190 * exception @EXC_NOMEM@ is thrown.
191 *
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.
198 */
199
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); }
204
205 /* --- @xfree@ --- *
206 *
207 * Arguments: @void *p@ = pointer to a block of memory.
208 *
209 * Returns: ---
210 *
211 * Use: Frees a block of memory.
212 */
213
214 void (xfree)(void *p) { xfree(p); }
215
216 /*----- That's all, folks -------------------------------------------------*/