Commit | Line | Data |
---|---|---|
0875b58f | 1 | /* -*-c-*- |
2 | * | |
0875b58f | 3 | * Memory allocation functions |
4 | * | |
5 | * (c) 1998 Straylight/Edgeware | |
6 | */ | |
7 | ||
d4efbcd9 | 8 | /*----- Licensing notice --------------------------------------------------* |
0875b58f | 9 | * |
10 | * This file is part of the mLib utilities library. | |
11 | * | |
12 | * mLib is free software; you can redistribute it and/or modify | |
c846879c | 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. | |
d4efbcd9 | 16 | * |
0875b58f | 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 | |
c846879c | 20 | * GNU Library General Public License for more details. |
d4efbcd9 | 21 | * |
c846879c | 22 | * You should have received a copy of the GNU Library General Public |
0bd98442 | 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. | |
0875b58f | 26 | */ |
27 | ||
0875b58f | 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" | |
0fd574c3 | 39 | #include "arena.h" |
0875b58f | 40 | #include "exc.h" |
0875b58f | 41 | |
0fd574c3 | 42 | /*----- Functions and macros ----------------------------------------------*/ |
0875b58f | 43 | |
b1a20bee | 44 | /* --- @x_alloc@, @x_allocn@ --- * |
0875b58f | 45 | * |
0fd574c3 | 46 | * Arguments: @arena *a@ = pointer to underlying arena |
b1a20bee MW |
47 | * @size_t n@ = number of elements to allocate (for @x_allocv@) |
48 | * @size_t sz@ = size of elements to allocate | |
0875b58f | 49 | * |
50 | * Returns: Pointer to allocated block. | |
51 | * | |
b1a20bee MW |
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. | |
0875b58f | 57 | */ |
58 | ||
0fd574c3 | 59 | void *x_alloc(arena *a, size_t sz) |
0875b58f | 60 | { |
b1a20bee MW |
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); | |
0875b58f | 75 | } |
76 | ||
0fd574c3 | 77 | /* --- @x_strdup@ --- * |
0875b58f | 78 | * |
0fd574c3 | 79 | * Arguments: @arena *a@ = pointer to underlying arena |
80 | * @const char *s@ = pointer to a string | |
0875b58f | 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 | ||
0fd574c3 | 89 | char *x_strdup(arena *a, const char *s) |
0875b58f | 90 | { |
b1a20bee MW |
91 | char *p; size_t sz; |
92 | ||
93 | sz = strlen(s) + 1; p = x_alloc(a, sz); memcpy(p, s, sz); | |
0875b58f | 94 | return (p); |
95 | } | |
96 | ||
b1a20bee | 97 | /* --- @x_realloc@, @x_reallocv@ --- * |
0875b58f | 98 | * |
0fd574c3 | 99 | * Arguments: @arena *a@ = pointer to underlying arena |
100 | * @void *p@ = pointer to a block of memory | |
b1a20bee MW |
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@) | |
0875b58f | 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. | |
b1a20bee MW |
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. | |
0875b58f | 119 | */ |
120 | ||
b5ea4de3 | 121 | void *x_realloc(arena *a, void *p, size_t sz, size_t osz) |
0875b58f | 122 | { |
b5ea4de3 | 123 | p = A_REALLOC(a, p, sz, osz); |
b1a20bee MW |
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); | |
0875b58f | 133 | } |
134 | ||
0fd574c3 | 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 | ||
b1a20bee | 149 | /* --- @xmalloc@, @xmallocv@ --- * |
0fd574c3 | 150 | * |
b1a20bee MW |
151 | * Arguments: @size_t n@ = number of elements to allocate (for @xmallocv@) |
152 | * @size_t sz@ = size of block to allocate | |
0fd574c3 | 153 | * |
154 | * Returns: Pointer to allocated block. | |
155 | * | |
b1a20bee MW |
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. | |
0fd574c3 | 159 | */ |
160 | ||
161 | void *(xmalloc)(size_t sz) { return xmalloc(sz); } | |
b1a20bee | 162 | void *(xmallocv)(size_t n, size_t sz) { return xmallocv(n, sz); } |
0fd574c3 | 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 | ||
b1a20bee | 177 | /* --- @xrealloc@, @xreallocv@ --- * |
0fd574c3 | 178 | * |
179 | * Arguments: @void *p@ = pointer to a block of memory | |
b1a20bee MW |
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@) | |
0fd574c3 | 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. | |
b1a20bee MW |
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. | |
0fd574c3 | 198 | */ |
199 | ||
b5ea4de3 | 200 | void *(xrealloc)(void *p, size_t sz, size_t osz) |
b1a20bee MW |
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); } | |
0fd574c3 | 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 | ||
0875b58f | 216 | /*----- That's all, folks -------------------------------------------------*/ |