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