chiark / gitweb /
Support for 24-bit types.
[mLib] / alloc.c
CommitLineData
0875b58f 1/* -*-c-*-
2 *
0fd574c3 3 * $Id: alloc.c,v 1.4 2000/06/17 10:35:51 mdw Exp $
0875b58f 4 *
5 * Memory allocation functions
6 *
7 * (c) 1998 Straylight/Edgeware
8 */
9
c846879c 10/*----- Licensing notice --------------------------------------------------*
0875b58f 11 *
12 * This file is part of the mLib utilities library.
13 *
14 * mLib is free software; you can redistribute it and/or modify
c846879c 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 *
0875b58f 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
c846879c 22 * GNU Library General Public License for more details.
23 *
24 * You should have received a copy of the GNU Library General Public
0bd98442 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.
0875b58f 28 */
29
30/*----- Revision history --------------------------------------------------*
31 *
32 * $Log: alloc.c,v $
0fd574c3 33 * Revision 1.4 2000/06/17 10:35:51 mdw
34 * Major overhaul for arena support.
35 *
0bd98442 36 * Revision 1.3 1999/05/06 19:51:35 mdw
37 * Reformatted the LGPL notice a little bit.
38 *
c846879c 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
0875b58f 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"
0fd574c3 58#include "arena.h"
0875b58f 59#include "exc.h"
0875b58f 60
0fd574c3 61/*----- Functions and macros ----------------------------------------------*/
0875b58f 62
0fd574c3 63/* --- @x_alloc@ --- *
0875b58f 64 *
0fd574c3 65 * Arguments: @arena *a@ = pointer to underlying arena
66 * @size_t sz@ = size of block to allocate
0875b58f 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
0fd574c3 74void *x_alloc(arena *a, size_t sz)
0875b58f 75{
0fd574c3 76 void *p = A_ALLOC(a, sz);
0875b58f 77 if (!p)
78 THROW(EXC_NOMEM);
79 return (p);
80}
81
0fd574c3 82/* --- @x_strdup@ --- *
0875b58f 83 *
0fd574c3 84 * Arguments: @arena *a@ = pointer to underlying arena
85 * @const char *s@ = pointer to a string
0875b58f 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
0fd574c3 94char *x_strdup(arena *a, const char *s)
0875b58f 95{
96 size_t sz = strlen(s) + 1;
0fd574c3 97 char *p = x_alloc(a, sz);
0875b58f 98 memcpy(p, s, sz);
99 return (p);
100}
101
0fd574c3 102/* --- @x_realloc@ --- *
0875b58f 103 *
0fd574c3 104 * Arguments: @arena *a@ = pointer to underlying arena
105 * @void *p@ = pointer to a block of memory
0875b58f 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
0fd574c3 115void *x_realloc(arena *a, void *p, size_t sz)
0875b58f 116{
0fd574c3 117 p = A_REALLOC(a, p, sz);
0875b58f 118 if (!p)
119 THROW(EXC_NOMEM);
120 return (p);
121}
122
0fd574c3 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
0875b58f 187/*----- That's all, folks -------------------------------------------------*/