3 * $Id: alloc.h,v 1.7 2004/04/08 01:36:11 mdw Exp $
5 * Memory allocation functions
7 * (c) 1998 Straylight/Edgeware
10 /*----- Licensing notice --------------------------------------------------*
12 * This file is part of the mLib utilities library.
14 * mLib is free software; you can redistribute it and/or modify
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.
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
22 * GNU Library General Public License for more details.
24 * You should have received a copy of the GNU Library General Public
25 * License along with mLib; if not, write to the Free
26 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
37 /*----- Required header files ---------------------------------------------*/
45 /*----- Functions and macros ----------------------------------------------*/
47 /* --- @x_alloc@ --- *
49 * Arguments: @arena *a@ = pointer to underlying arena
50 * @size_t sz@ = size of block to allocate
52 * Returns: Pointer to allocated block.
54 * Use: Allocates memory. If there's not enough memory, the
55 * exception @EXC_NOMEM@ is thrown.
58 extern void *x_alloc(arena */*a*/, size_t /*sz*/);
60 /* --- @x_strdup@ --- *
62 * Arguments: @arena *a@ = pointer to underlying arena
63 * @const char *s@ = pointer to a string
65 * Returns: Pointer to a copy of the string.
67 * Use: Copies a string (like @strdup@ would, if it existed). If
68 * there's not enough memory, the exception @EXC_NOMEM@ is
72 extern char *x_strdup(arena */*a*/, const char */*s*/);
74 /* --- @x_realloc@ --- *
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
79 * @size_t osz@ = size of the old block
81 * Returns: Pointer to the resized memory block (which is almost
82 * certainly not in the same place any more).
84 * Use: Resizes a memory block. If there's not enough memory, the
85 * exception @EXC_NOMEM@ is thrown.
88 extern void *x_realloc(arena */*a*/, void */*p*/,
89 size_t /*sz*/, size_t /*osz*/);
93 * Arguments: @arena *a@ = pointer to underlying arena
94 * @void *p@ = pointer to a block of memory.
98 * Use: Frees a block of memory.
101 extern void x_free(arena */*a*/, void */*p*/);
102 #define x_free(a, p) A_FREE(a, p)
104 /*----- Old functions for the standard arena ------------------------------*/
106 /* --- @xmalloc@ --- *
108 * Arguments: @size_t sz@ = size of block to allocate
110 * Returns: Pointer to allocated block.
112 * Use: Allocates memory. If there's not enough memory, the
113 * exception @EXC_NOMEM@ is thrown.
116 extern void *xmalloc(size_t /*sz*/);
117 #define xmalloc(sz) x_alloc(arena_global, (sz))
119 /* --- @xstrdup@ --- *
121 * Arguments: @const char *s@ = pointer to a string
123 * Returns: Pointer to a copy of the string.
125 * Use: Copies a string (like @strdup@ would, if it existed). If
126 * there's not enough memory, the exception @EXC_NOMEM@ is
130 extern char *xstrdup(const char */*s*/);
131 #define xstrdup(p) x_strdup(arena_global, (p))
133 /* --- @xrealloc@ --- *
135 * Arguments: @void *p@ = pointer to a block of memory
136 * @size_t sz@ = new size desired for the block
137 * @size_t osz@ = size of the old block
139 * Returns: Pointer to the resized memory block (which is almost
140 * certainly not in the same place any more).
142 * Use: Resizes a memory block. If there's not enough memory, the
143 * exception @EXC_NOMEM@ is thrown.
146 extern void *xrealloc(void */*p*/, size_t /*sz*/, size_t /*osz*/);
147 #define xrealloc(p, sz, osz) x_realloc(arena_global, (p), (sz), (osz))
151 * Arguments: @void *p@ = pointer to a block of memory.
155 * Use: Frees a block of memory.
158 extern void xfree(void */*p*/);
159 #define xfree(p) x_free(arena_global, (p))
161 /*----- That's all, folks -------------------------------------------------*/