chiark / gitweb /
Release 2.2.0. Yay.
[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 <stdio.h>
33 #include <stdlib.h>
34 #include <string.h>
35
36 /* --- Local headers --- */
37
38 #include "alloc.h"
39 #include "arena.h"
40 #include "exc.h"
41
42 /*----- Functions and macros ----------------------------------------------*/
43
44 /* --- @x_alloc@ --- *
45  *
46  * Arguments:   @arena *a@ = pointer to underlying arena
47  *              @size_t sz@ = size of block to allocate
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
55 void *x_alloc(arena *a, size_t sz)
56 {
57   void *p = A_ALLOC(a, sz);
58   if (!p)
59     THROW(EXC_NOMEM);
60   return (p);
61 }
62
63 /* --- @x_strdup@ --- *
64  *
65  * Arguments:   @arena *a@ = pointer to underlying arena
66  *              @const char *s@ = pointer to a string
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
75 char *x_strdup(arena *a, const char *s)
76 {
77   size_t sz = strlen(s) + 1;
78   char *p = x_alloc(a, sz);
79   memcpy(p, s, sz);
80   return (p);
81 }
82
83 /* --- @x_realloc@ --- *
84  *
85  * Arguments:   @arena *a@ = pointer to underlying arena
86  *              @void *p@ = pointer to a block of memory
87  *              @size_t sz@ = new size desired for the block
88  *              @size_t osz@ = size of the old block
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
97 void *x_realloc(arena *a, void *p, size_t sz, size_t osz)
98 {
99   p = A_REALLOC(a, p, sz, osz);
100   if (!p)
101     THROW(EXC_NOMEM);
102   return (p);
103 }
104
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
115 void (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
129 void *(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
142 char *(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
148  *              @size_t osz@ = size of the old block
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
157 void *(xrealloc)(void *p, size_t sz, size_t osz)
158 { return xrealloc(p, sz, osz); }
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
169 void (xfree)(void *p) { xfree(p); }
170
171 /*----- That's all, folks -------------------------------------------------*/