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,
28 /*----- Header files ------------------------------------------------------*/
30 /* --- ANSI headers --- */
36 /* --- Local headers --- */
42 /*----- Functions and macros ----------------------------------------------*/
44 /* --- @x_alloc@ --- *
46 * Arguments: @arena *a@ = pointer to underlying arena
47 * @size_t sz@ = size of block to allocate
49 * Returns: Pointer to allocated block.
51 * Use: Allocates memory. If there's not enough memory, the
52 * exception @EXC_NOMEM@ is thrown.
55 void *x_alloc(arena *a, size_t sz)
57 void *p = A_ALLOC(a, sz);
63 /* --- @x_strdup@ --- *
65 * Arguments: @arena *a@ = pointer to underlying arena
66 * @const char *s@ = pointer to a string
68 * Returns: Pointer to a copy of the string.
70 * Use: Copies a string (like @strdup@ would, if it existed). If
71 * there's not enough memory, the exception @EXC_NOMEM@ is
75 char *x_strdup(arena *a, const char *s)
77 size_t sz = strlen(s) + 1;
78 char *p = x_alloc(a, sz);
83 /* --- @x_realloc@ --- *
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
90 * Returns: Pointer to the resized memory block (which is almost
91 * certainly not in the same place any more).
93 * Use: Resizes a memory block. If there's not enough memory, the
94 * exception @EXC_NOMEM@ is thrown.
97 void *x_realloc(arena *a, void *p, size_t sz, size_t osz)
99 p = A_REALLOC(a, p, sz, osz);
105 /* --- @x_free@ --- *
107 * Arguments: @arena *a@ = pointer to underlying arena
108 * @void *p@ = pointer to a block of memory.
112 * Use: Frees a block of memory.
115 void (x_free)(arena *a, void *p) { x_free(a, p); }
117 /*----- Old functions for the standard arena ------------------------------*/
119 /* --- @xmalloc@ --- *
121 * Arguments: @size_t sz@ = size of block to allocate
123 * Returns: Pointer to allocated block.
125 * Use: Allocates memory. If there's not enough memory, the
126 * exception @EXC_NOMEM@ is thrown.
129 void *(xmalloc)(size_t sz) { return xmalloc(sz); }
131 /* --- @xstrdup@ --- *
133 * Arguments: @const char *s@ = pointer to a string
135 * Returns: Pointer to a copy of the string.
137 * Use: Copies a string (like @strdup@ would, if it existed). If
138 * there's not enough memory, the exception @EXC_NOMEM@ is
142 char *(xstrdup)(const char *s) { return xstrdup(s); }
144 /* --- @xrealloc@ --- *
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
150 * Returns: Pointer to the resized memory block (which is almost
151 * certainly not in the same place any more).
153 * Use: Resizes a memory block. If there's not enough memory, the
154 * exception @EXC_NOMEM@ is thrown.
157 void *(xrealloc)(void *p, size_t sz, size_t osz)
158 { return xrealloc(p, sz, osz); }
162 * Arguments: @void *p@ = pointer to a block of memory.
166 * Use: Frees a block of memory.
169 void (xfree)(void *p) { xfree(p); }
171 /*----- That's all, folks -------------------------------------------------*/