chiark / gitweb /
Infrastructure: Split the files into subdirectories.
[mLib] / mem / alloc.c
CommitLineData
0875b58f 1/* -*-c-*-
0875b58f 2 *
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
0fd574c3 44/* --- @x_alloc@ --- *
0875b58f 45 *
0fd574c3 46 * Arguments: @arena *a@ = pointer to underlying arena
47 * @size_t sz@ = size of block to allocate
0875b58f 48 *
49 * Returns: Pointer to allocated block.
50 *
51 * Use: Allocates memory. If there's not enough memory, the
52 * exception @EXC_NOMEM@ is thrown.
53 */
54
0fd574c3 55void *x_alloc(arena *a, size_t sz)
0875b58f 56{
0fd574c3 57 void *p = A_ALLOC(a, sz);
0875b58f 58 if (!p)
59 THROW(EXC_NOMEM);
60 return (p);
61}
62
0fd574c3 63/* --- @x_strdup@ --- *
0875b58f 64 *
0fd574c3 65 * Arguments: @arena *a@ = pointer to underlying arena
66 * @const char *s@ = pointer to a string
0875b58f 67 *
68 * Returns: Pointer to a copy of the string.
69 *
70 * Use: Copies a string (like @strdup@ would, if it existed). If
71 * there's not enough memory, the exception @EXC_NOMEM@ is
72 * thrown.
73 */
74
0fd574c3 75char *x_strdup(arena *a, const char *s)
0875b58f 76{
77 size_t sz = strlen(s) + 1;
0fd574c3 78 char *p = x_alloc(a, sz);
0875b58f 79 memcpy(p, s, sz);
80 return (p);
81}
82
0fd574c3 83/* --- @x_realloc@ --- *
0875b58f 84 *
0fd574c3 85 * Arguments: @arena *a@ = pointer to underlying arena
86 * @void *p@ = pointer to a block of memory
0875b58f 87 * @size_t sz@ = new size desired for the block
b5ea4de3 88 * @size_t osz@ = size of the old block
0875b58f 89 *
90 * Returns: Pointer to the resized memory block (which is almost
91 * certainly not in the same place any more).
92 *
93 * Use: Resizes a memory block. If there's not enough memory, the
94 * exception @EXC_NOMEM@ is thrown.
95 */
96
b5ea4de3 97void *x_realloc(arena *a, void *p, size_t sz, size_t osz)
0875b58f 98{
b5ea4de3 99 p = A_REALLOC(a, p, sz, osz);
0875b58f 100 if (!p)
101 THROW(EXC_NOMEM);
102 return (p);
103}
104
0fd574c3 105/* --- @x_free@ --- *
106 *
107 * Arguments: @arena *a@ = pointer to underlying arena
108 * @void *p@ = pointer to a block of memory.
109 *
110 * Returns: ---
111 *
112 * Use: Frees a block of memory.
113 */
114
115void (x_free)(arena *a, void *p) { x_free(a, p); }
116
117/*----- Old functions for the standard arena ------------------------------*/
118
119/* --- @xmalloc@ --- *
120 *
121 * Arguments: @size_t sz@ = size of block to allocate
122 *
123 * Returns: Pointer to allocated block.
124 *
125 * Use: Allocates memory. If there's not enough memory, the
126 * exception @EXC_NOMEM@ is thrown.
127 */
128
129void *(xmalloc)(size_t sz) { return xmalloc(sz); }
130
131/* --- @xstrdup@ --- *
132 *
133 * Arguments: @const char *s@ = pointer to a string
134 *
135 * Returns: Pointer to a copy of the string.
136 *
137 * Use: Copies a string (like @strdup@ would, if it existed). If
138 * there's not enough memory, the exception @EXC_NOMEM@ is
139 * thrown.
140 */
141
142char *(xstrdup)(const char *s) { return xstrdup(s); }
143
144/* --- @xrealloc@ --- *
145 *
146 * Arguments: @void *p@ = pointer to a block of memory
147 * @size_t sz@ = new size desired for the block
b5ea4de3 148 * @size_t osz@ = size of the old block
0fd574c3 149 *
150 * Returns: Pointer to the resized memory block (which is almost
151 * certainly not in the same place any more).
152 *
153 * Use: Resizes a memory block. If there's not enough memory, the
154 * exception @EXC_NOMEM@ is thrown.
155 */
156
b5ea4de3 157void *(xrealloc)(void *p, size_t sz, size_t osz)
158{ return xrealloc(p, sz, osz); }
0fd574c3 159
160/* --- @xfree@ --- *
161 *
162 * Arguments: @void *p@ = pointer to a block of memory.
163 *
164 * Returns: ---
165 *
166 * Use: Frees a block of memory.
167 */
168
169void (xfree)(void *p) { xfree(p); }
170
0875b58f 171/*----- That's all, folks -------------------------------------------------*/