chiark / gitweb /
Major overhaul for arena support.
[mLib] / alloc.c
... / ...
CommitLineData
1/* -*-c-*-
2 *
3 * $Id: alloc.c,v 1.4 2000/06/17 10:35:51 mdw Exp $
4 *
5 * Memory allocation functions
6 *
7 * (c) 1998 Straylight/Edgeware
8 */
9
10/*----- Licensing notice --------------------------------------------------*
11 *
12 * This file is part of the mLib utilities library.
13 *
14 * mLib is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU Library General Public License as
16 * published by the Free Software Foundation; either version 2 of the
17 * License, or (at your option) any later version.
18 *
19 * mLib is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU Library General Public License for more details.
23 *
24 * You should have received a copy of the GNU Library General Public
25 * License along with mLib; if not, write to the Free
26 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
27 * MA 02111-1307, USA.
28 */
29
30/*----- Revision history --------------------------------------------------*
31 *
32 * $Log: alloc.c,v $
33 * Revision 1.4 2000/06/17 10:35:51 mdw
34 * Major overhaul for arena support.
35 *
36 * Revision 1.3 1999/05/06 19:51:35 mdw
37 * Reformatted the LGPL notice a little bit.
38 *
39 * Revision 1.2 1999/05/05 18:50:31 mdw
40 * Change licensing conditions to LGPL.
41 *
42 * Revision 1.1.1.1 1998/06/17 23:44:42 mdw
43 * Initial version of mLib
44 *
45 */
46
47/*----- Header files ------------------------------------------------------*/
48
49/* --- ANSI headers --- */
50
51#include <stdio.h>
52#include <stdlib.h>
53#include <string.h>
54
55/* --- Local headers --- */
56
57#include "alloc.h"
58#include "arena.h"
59#include "exc.h"
60
61/*----- Functions and macros ----------------------------------------------*/
62
63/* --- @x_alloc@ --- *
64 *
65 * Arguments: @arena *a@ = pointer to underlying arena
66 * @size_t sz@ = size of block to allocate
67 *
68 * Returns: Pointer to allocated block.
69 *
70 * Use: Allocates memory. If there's not enough memory, the
71 * exception @EXC_NOMEM@ is thrown.
72 */
73
74void *x_alloc(arena *a, size_t sz)
75{
76 void *p = A_ALLOC(a, sz);
77 if (!p)
78 THROW(EXC_NOMEM);
79 return (p);
80}
81
82/* --- @x_strdup@ --- *
83 *
84 * Arguments: @arena *a@ = pointer to underlying arena
85 * @const char *s@ = pointer to a string
86 *
87 * Returns: Pointer to a copy of the string.
88 *
89 * Use: Copies a string (like @strdup@ would, if it existed). If
90 * there's not enough memory, the exception @EXC_NOMEM@ is
91 * thrown.
92 */
93
94char *x_strdup(arena *a, const char *s)
95{
96 size_t sz = strlen(s) + 1;
97 char *p = x_alloc(a, sz);
98 memcpy(p, s, sz);
99 return (p);
100}
101
102/* --- @x_realloc@ --- *
103 *
104 * Arguments: @arena *a@ = pointer to underlying arena
105 * @void *p@ = pointer to a block of memory
106 * @size_t sz@ = new size desired for the block
107 *
108 * Returns: Pointer to the resized memory block (which is almost
109 * certainly not in the same place any more).
110 *
111 * Use: Resizes a memory block. If there's not enough memory, the
112 * exception @EXC_NOMEM@ is thrown.
113 */
114
115void *x_realloc(arena *a, void *p, size_t sz)
116{
117 p = A_REALLOC(a, p, sz);
118 if (!p)
119 THROW(EXC_NOMEM);
120 return (p);
121}
122
123/* --- @x_free@ --- *
124 *
125 * Arguments: @arena *a@ = pointer to underlying arena
126 * @void *p@ = pointer to a block of memory.
127 *
128 * Returns: ---
129 *
130 * Use: Frees a block of memory.
131 */
132
133void (x_free)(arena *a, void *p) { x_free(a, p); }
134
135/*----- Old functions for the standard arena ------------------------------*/
136
137/* --- @xmalloc@ --- *
138 *
139 * Arguments: @size_t sz@ = size of block to allocate
140 *
141 * Returns: Pointer to allocated block.
142 *
143 * Use: Allocates memory. If there's not enough memory, the
144 * exception @EXC_NOMEM@ is thrown.
145 */
146
147void *(xmalloc)(size_t sz) { return xmalloc(sz); }
148
149/* --- @xstrdup@ --- *
150 *
151 * Arguments: @const char *s@ = pointer to a string
152 *
153 * Returns: Pointer to a copy of the string.
154 *
155 * Use: Copies a string (like @strdup@ would, if it existed). If
156 * there's not enough memory, the exception @EXC_NOMEM@ is
157 * thrown.
158 */
159
160char *(xstrdup)(const char *s) { return xstrdup(s); }
161
162/* --- @xrealloc@ --- *
163 *
164 * Arguments: @void *p@ = pointer to a block of memory
165 * @size_t sz@ = new size desired for the block
166 *
167 * Returns: Pointer to the resized memory block (which is almost
168 * certainly not in the same place any more).
169 *
170 * Use: Resizes a memory block. If there's not enough memory, the
171 * exception @EXC_NOMEM@ is thrown.
172 */
173
174void *(xrealloc)(void *p, size_t sz) { return xrealloc(p, sz); }
175
176/* --- @xfree@ --- *
177 *
178 * Arguments: @void *p@ = pointer to a block of memory.
179 *
180 * Returns: ---
181 *
182 * Use: Frees a block of memory.
183 */
184
185void (xfree)(void *p) { xfree(p); }
186
187/*----- That's all, folks -------------------------------------------------*/