chiark / gitweb /
*.[ch]: Remove unnecessary header files.
[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 <string.h>
33
34 /* --- Local headers --- */
35
36 #include "alloc.h"
37 #include "arena.h"
38 #include "exc.h"
39
40 /*----- Functions and macros ----------------------------------------------*/
41
42 /* --- @x_alloc@, @x_allocv@ --- *
43  *
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
47  *
48  * Returns:     Pointer to allocated block.
49  *
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.
55  */
56
57 void *x_alloc(arena *a, size_t sz)
58 {
59   void *p;
60
61   p = A_ALLOC(a, sz);
62     if (!p) THROW(EXC_NOMEM);
63     else return (p);
64 }
65
66 void *x_allocv(arena *a, size_t n, size_t sz)
67 {
68   void *p;
69
70   p = A_ALLOCV(a, n, sz);
71     if (!p) THROW(EXC_NOMEM);
72     else return (p);
73 }
74
75 /* --- @x_strdup@ --- *
76  *
77  * Arguments:   @arena *a@ = pointer to underlying arena
78  *              @const char *s@ = pointer to a string
79  *
80  * Returns:     Pointer to a copy of the string.
81  *
82  * Use:         Copies a string (like @strdup@ would, if it existed).  If
83  *              there's not enough memory, the exception @EXC_NOMEM@ is
84  *              thrown.
85  */
86
87 char *x_strdup(arena *a, const char *s)
88 {
89   char *p; size_t sz;
90
91   sz = strlen(s) + 1; p = x_alloc(a, sz); memcpy(p, s, sz);
92   return (p);
93 }
94
95 /* --- @x_realloc@, @x_reallocv@ --- *
96  *
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@)
104  *
105  * Returns:     Pointer to the resized memory block (which is almost
106  *              certainly not in the same place any more).
107  *
108  * Use:         Resizes a memory block.  If there's not enough memory, the
109  *              exception @EXC_NOMEM@ is thrown.
110  *
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.
117  */
118
119 void *x_realloc(arena *a, void *p, size_t sz, size_t osz)
120 {
121   p = A_REALLOC(a, p, sz, osz);
122     if (!p) THROW(EXC_NOMEM);
123     else return (p);
124 }
125
126 void *x_reallocv(arena *a, void *p, size_t n, size_t on, size_t sz)
127 {
128   p = A_REALLOCV(a, p, n, on, sz);
129     if (!p) THROW(EXC_NOMEM);
130     else return (p);
131 }
132
133 /* --- @x_free@ --- *
134  *
135  * Arguments:   @arena *a@ = pointer to underlying arena
136  *              @void *p@ = pointer to a block of memory.
137  *
138  * Returns:     ---
139  *
140  * Use:         Frees a block of memory.
141  */
142
143 void (x_free)(arena *a, void *p) { x_free(a, p); }
144
145 /*----- Old functions for the standard arena ------------------------------*/
146
147 /* --- @xmalloc@, @xmallocv@ --- *
148  *
149  * Arguments:   @size_t n@ = number of elements to allocate (for @xmallocv@)
150  *              @size_t sz@ = size of block to allocate
151  *
152  * Returns:     Pointer to allocated block.
153  *
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.
157  */
158
159 void *(xmalloc)(size_t sz) { return xmalloc(sz); }
160 void *(xmallocv)(size_t n, size_t sz) { return xmallocv(n, sz); }
161
162 /* --- @xstrdup@ --- *
163  *
164  * Arguments:   @const char *s@ = pointer to a string
165  *
166  * Returns:     Pointer to a copy of the string.
167  *
168  * Use:         Copies a string (like @strdup@ would, if it existed).  If
169  *              there's not enough memory, the exception @EXC_NOMEM@ is
170  *              thrown.
171  */
172
173 char *(xstrdup)(const char *s) { return xstrdup(s); }
174
175 /* --- @xrealloc@, @xreallocv@ --- *
176  *
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@)
183  *
184  * Returns:     Pointer to the resized memory block (which is almost
185  *              certainly not in the same place any more).
186  *
187  * Use:         Resizes a memory block.  If there's not enough memory, the
188  *              exception @EXC_NOMEM@ is thrown.
189  *
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.
196  */
197
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); }
202
203 /* --- @xfree@ --- *
204  *
205  * Arguments:   @void *p@ = pointer to a block of memory.
206  *
207  * Returns:     ---
208  *
209  * Use:         Frees a block of memory.
210  */
211
212 void (xfree)(void *p) { xfree(p); }
213
214 /*----- That's all, folks -------------------------------------------------*/