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