chiark / gitweb /
234918148e49413d2a501a75bbefc1ce24a75513
[mLib] / alloc.h
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 #ifndef MLIB_ALLOC_H
29 #define MLIB_ALLOC_H
30
31 #ifdef __cplusplus
32   extern "C" {
33 #endif
34
35 /*----- Required header files ---------------------------------------------*/
36
37 #include <stddef.h>
38
39 #ifndef MLIB_ARENA_H
40 #  include "arena.h"
41 #endif
42
43 /*----- Functions and macros ----------------------------------------------*/
44
45 /* --- @x_alloc@ --- *
46  *
47  * Arguments:   @arena *a@ = pointer to underlying arena
48  *              @size_t sz@ = size of block to allocate
49  *
50  * Returns:     Pointer to allocated block.
51  *
52  * Use:         Allocates memory.  If there's not enough memory, the
53  *              exception @EXC_NOMEM@ is thrown.
54  */
55
56 extern void *x_alloc(arena */*a*/, size_t /*sz*/);
57
58 /* --- @x_strdup@ --- *
59  *
60  * Arguments:   @arena *a@ = pointer to underlying arena
61  *              @const char *s@ = pointer to a string
62  *
63  * Returns:     Pointer to a copy of the string.
64  *
65  * Use:         Copies a string (like @strdup@ would, if it existed).  If
66  *              there's not enough memory, the exception @EXC_NOMEM@ is
67  *              thrown.
68  */
69
70 extern char *x_strdup(arena */*a*/, const char */*s*/);
71
72 /* --- @x_realloc@ --- *
73  *
74  * Arguments:   @arena *a@ = pointer to underlying arena
75  *              @void *p@ = pointer to a block of memory
76  *              @size_t sz@ = new size desired for the block
77  *              @size_t osz@ = size of the old block
78  *
79  * Returns:     Pointer to the resized memory block (which is almost
80  *              certainly not in the same place any more).
81  *
82  * Use:         Resizes a memory block.  If there's not enough memory, the
83  *              exception @EXC_NOMEM@ is thrown.
84  */
85
86 extern void *x_realloc(arena */*a*/, void */*p*/,
87                        size_t /*sz*/, size_t /*osz*/);
88
89 /* --- @x_free@ --- *
90  *
91  * Arguments:   @arena *a@ = pointer to underlying arena
92  *              @void *p@ = pointer to a block of memory.
93  *
94  * Returns:     ---
95  *
96  * Use:         Frees a block of memory.
97  */
98
99 extern void x_free(arena */*a*/, void */*p*/);
100 #define x_free(a, p) A_FREE(a, p)
101
102 /*----- Old functions for the standard arena ------------------------------*/
103
104 /* --- @xmalloc@ --- *
105  *
106  * Arguments:   @size_t sz@ = size of block to allocate
107  *
108  * Returns:     Pointer to allocated block.
109  *
110  * Use:         Allocates memory.  If there's not enough memory, the
111  *              exception @EXC_NOMEM@ is thrown.
112  */
113
114 extern void *xmalloc(size_t /*sz*/);
115 #define xmalloc(sz) x_alloc(arena_global, (sz))
116
117 /* --- @xstrdup@ --- *
118  *
119  * Arguments:   @const char *s@ = pointer to a string
120  *
121  * Returns:     Pointer to a copy of the string.
122  *
123  * Use:         Copies a string (like @strdup@ would, if it existed).  If
124  *              there's not enough memory, the exception @EXC_NOMEM@ is
125  *              thrown.
126  */
127
128 extern char *xstrdup(const char */*s*/);
129 #define xstrdup(p) x_strdup(arena_global, (p))
130
131 /* --- @xrealloc@ --- *
132  *
133  * Arguments:   @void *p@ = pointer to a block of memory
134  *              @size_t sz@ = new size desired for the block
135  *              @size_t osz@ = size of the old block
136  *
137  * Returns:     Pointer to the resized memory block (which is almost
138  *              certainly not in the same place any more).
139  *
140  * Use:         Resizes a memory block.  If there's not enough memory, the
141  *              exception @EXC_NOMEM@ is thrown.
142  */
143
144 extern void *xrealloc(void */*p*/, size_t /*sz*/, size_t /*osz*/);
145 #define xrealloc(p, sz, osz) x_realloc(arena_global, (p), (sz), (osz))
146
147 /* --- @xfree@ --- *
148  *
149  * Arguments:   @void *p@ = pointer to a block of memory.
150  *
151  * Returns:     ---
152  *
153  * Use:         Frees a block of memory.
154  */
155
156 extern void xfree(void */*p*/);
157 #define xfree(p) x_free(arena_global, (p))
158
159 /*----- That's all, folks -------------------------------------------------*/
160
161 #ifdef __cplusplus
162   }
163 #endif
164
165 #endif