| 1 | /* -*-c-*- |
| 2 | * |
| 3 | * $Id: alloc.h,v 1.7 2004/04/08 01:36:11 mdw Exp $ |
| 4 | * |
| 5 | * Memory allocation functions |
| 6 | * |
| 7 | * (c) 1998 Straylight/Edgeware |
| 8 | */ |
| 9 | |
| 10 | /*----- Licensing notice --------------------------------------------------* |
| 11 | * |
| 12 | * This file is part of the mLib utilities library. |
| 13 | * |
| 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. |
| 18 | * |
| 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. |
| 23 | * |
| 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, |
| 27 | * MA 02111-1307, USA. |
| 28 | */ |
| 29 | |
| 30 | #ifndef MLIB_ALLOC_H |
| 31 | #define MLIB_ALLOC_H |
| 32 | |
| 33 | #ifdef __cplusplus |
| 34 | extern "C" { |
| 35 | #endif |
| 36 | |
| 37 | /*----- Required header files ---------------------------------------------*/ |
| 38 | |
| 39 | #include <stddef.h> |
| 40 | |
| 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 | |
| 58 | extern 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 | |
| 72 | extern 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 |
| 79 | * @size_t osz@ = size of the old block |
| 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 | |
| 88 | extern void *x_realloc(arena */*a*/, void */*p*/, |
| 89 | size_t /*sz*/, size_t /*osz*/); |
| 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 | |
| 101 | extern 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 ------------------------------*/ |
| 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 | |
| 116 | extern void *xmalloc(size_t /*sz*/); |
| 117 | #define xmalloc(sz) x_alloc(arena_global, (sz)) |
| 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 | |
| 130 | extern char *xstrdup(const char */*s*/); |
| 131 | #define xstrdup(p) x_strdup(arena_global, (p)) |
| 132 | |
| 133 | /* --- @xrealloc@ --- * |
| 134 | * |
| 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 |
| 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 | |
| 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)) |
| 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 | |
| 158 | extern void xfree(void */*p*/); |
| 159 | #define xfree(p) x_free(arena_global, (p)) |
| 160 | |
| 161 | /*----- That's all, folks -------------------------------------------------*/ |
| 162 | |
| 163 | #ifdef __cplusplus |
| 164 | } |
| 165 | #endif |
| 166 | |
| 167 | #endif |