3 * $Id: alloc.c,v 1.6 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,
30 /*----- Header files ------------------------------------------------------*/
32 /* --- ANSI headers --- */
38 /* --- Local headers --- */
44 /*----- Functions and macros ----------------------------------------------*/
46 /* --- @x_alloc@ --- *
48 * Arguments: @arena *a@ = pointer to underlying arena
49 * @size_t sz@ = size of block to allocate
51 * Returns: Pointer to allocated block.
53 * Use: Allocates memory. If there's not enough memory, the
54 * exception @EXC_NOMEM@ is thrown.
57 void *x_alloc(arena
*a
, size_t sz
)
59 void *p
= A_ALLOC(a
, sz
);
65 /* --- @x_strdup@ --- *
67 * Arguments: @arena *a@ = pointer to underlying arena
68 * @const char *s@ = pointer to a string
70 * Returns: Pointer to a copy of the string.
72 * Use: Copies a string (like @strdup@ would, if it existed). If
73 * there's not enough memory, the exception @EXC_NOMEM@ is
77 char *x_strdup(arena
*a
, const char *s
)
79 size_t sz
= strlen(s
) + 1;
80 char *p
= x_alloc(a
, sz
);
85 /* --- @x_realloc@ --- *
87 * Arguments: @arena *a@ = pointer to underlying arena
88 * @void *p@ = pointer to a block of memory
89 * @size_t sz@ = new size desired for the block
90 * @size_t osz@ = size of the old block
92 * Returns: Pointer to the resized memory block (which is almost
93 * certainly not in the same place any more).
95 * Use: Resizes a memory block. If there's not enough memory, the
96 * exception @EXC_NOMEM@ is thrown.
99 void *x_realloc(arena
*a
, void *p
, size_t sz
, size_t osz
)
101 p
= A_REALLOC(a
, p
, sz
, osz
);
107 /* --- @x_free@ --- *
109 * Arguments: @arena *a@ = pointer to underlying arena
110 * @void *p@ = pointer to a block of memory.
114 * Use: Frees a block of memory.
117 void (x_free
)(arena
*a
, void *p
) { x_free(a
, p
); }
119 /*----- Old functions for the standard arena ------------------------------*/
121 /* --- @xmalloc@ --- *
123 * Arguments: @size_t sz@ = size of block to allocate
125 * Returns: Pointer to allocated block.
127 * Use: Allocates memory. If there's not enough memory, the
128 * exception @EXC_NOMEM@ is thrown.
131 void *(xmalloc
)(size_t sz
) { return xmalloc(sz
); }
133 /* --- @xstrdup@ --- *
135 * Arguments: @const char *s@ = pointer to a string
137 * Returns: Pointer to a copy of the string.
139 * Use: Copies a string (like @strdup@ would, if it existed). If
140 * there's not enough memory, the exception @EXC_NOMEM@ is
144 char *(xstrdup
)(const char *s
) { return xstrdup(s
); }
146 /* --- @xrealloc@ --- *
148 * Arguments: @void *p@ = pointer to a block of memory
149 * @size_t sz@ = new size desired for the block
150 * @size_t osz@ = size of the old block
152 * Returns: Pointer to the resized memory block (which is almost
153 * certainly not in the same place any more).
155 * Use: Resizes a memory block. If there's not enough memory, the
156 * exception @EXC_NOMEM@ is thrown.
159 void *(xrealloc
)(void *p
, size_t sz
, size_t osz
)
160 { return xrealloc(p
, sz
, osz
); }
164 * Arguments: @void *p@ = pointer to a block of memory.
168 * Use: Frees a block of memory.
171 void (xfree
)(void *p
) { xfree(p
); }
173 /*----- That's all, folks -------------------------------------------------*/