3 * Memory allocation functions
5 * (c) 1998 Straylight/Edgeware
8 /*----- Licensing notice --------------------------------------------------*
10 * This file is part of the mLib utilities library.
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.
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.
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,
35 /*----- Required header files ---------------------------------------------*/
43 /*----- Functions and macros ----------------------------------------------*/
45 /* --- @x_alloc@ --- *
47 * Arguments: @arena *a@ = pointer to underlying arena
48 * @size_t sz@ = size of block to allocate
50 * Returns: Pointer to allocated block.
52 * Use: Allocates memory. If there's not enough memory, the
53 * exception @EXC_NOMEM@ is thrown.
56 extern void *x_alloc(arena */
*a*/
, size_t /*sz*/);
58 /* --- @x_strdup@ --- *
60 * Arguments: @arena *a@ = pointer to underlying arena
61 * @const char *s@ = pointer to a string
63 * Returns: Pointer to a copy of the string.
65 * Use: Copies a string (like @strdup@ would, if it existed). If
66 * there's not enough memory, the exception @EXC_NOMEM@ is
70 extern char *x_strdup(arena */
*a*/
, const char */
*s*/
);
72 /* --- @x_realloc@ --- *
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
79 * Returns: Pointer to the resized memory block (which is almost
80 * certainly not in the same place any more).
82 * Use: Resizes a memory block. If there's not enough memory, the
83 * exception @EXC_NOMEM@ is thrown.
86 extern void *x_realloc(arena */
*a*/
, void */
*p*/
,
87 size_t /*sz*/, size_t /*osz*/);
91 * Arguments: @arena *a@ = pointer to underlying arena
92 * @void *p@ = pointer to a block of memory.
96 * Use: Frees a block of memory.
99 extern void x_free(arena */
*a*/
, void */
*p*/
);
100 #define x_free(a, p) A_FREE(a, p)
102 /*----- Old functions for the standard arena ------------------------------*/
104 /* --- @xmalloc@ --- *
106 * Arguments: @size_t sz@ = size of block to allocate
108 * Returns: Pointer to allocated block.
110 * Use: Allocates memory. If there's not enough memory, the
111 * exception @EXC_NOMEM@ is thrown.
114 extern void *xmalloc(size_t /*sz*/);
115 #define xmalloc(sz) x_alloc(arena_global, (sz))
117 /* --- @xstrdup@ --- *
119 * Arguments: @const char *s@ = pointer to a string
121 * Returns: Pointer to a copy of the string.
123 * Use: Copies a string (like @strdup@ would, if it existed). If
124 * there's not enough memory, the exception @EXC_NOMEM@ is
128 extern char *xstrdup(const char */
*s*/
);
129 #define xstrdup(p) x_strdup(arena_global, (p))
131 /* --- @xrealloc@ --- *
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
137 * Returns: Pointer to the resized memory block (which is almost
138 * certainly not in the same place any more).
140 * Use: Resizes a memory block. If there's not enough memory, the
141 * exception @EXC_NOMEM@ is thrown.
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))
149 * Arguments: @void *p@ = pointer to a block of memory.
153 * Use: Frees a block of memory.
156 extern void xfree(void */
*p*/
);
157 #define xfree(p) x_free(arena_global, (p))
159 /*----- That's all, folks -------------------------------------------------*/