| 1 | /* -*-c-*- |
| 2 | * |
| 3 | * Abstraction for memory allocation arenas |
| 4 | * |
| 5 | * (c) 2000 Straylight/Edgeware |
| 6 | */ |
| 7 | |
| 8 | /*----- Licensing notice --------------------------------------------------* |
| 9 | * |
| 10 | * This file is part of the mLib utilities library. |
| 11 | * |
| 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. |
| 16 | * |
| 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. |
| 21 | * |
| 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, |
| 25 | * MA 02111-1307, USA. |
| 26 | */ |
| 27 | |
| 28 | #ifndef MLIB_ARENA_H |
| 29 | #define MLIB_ARENA_H |
| 30 | |
| 31 | #ifdef __cplusplus |
| 32 | extern "C" { |
| 33 | #endif |
| 34 | |
| 35 | /*----- Header files ------------------------------------------------------*/ |
| 36 | |
| 37 | #include <stdlib.h> |
| 38 | |
| 39 | /*----- Data structures ---------------------------------------------------*/ |
| 40 | |
| 41 | /* --- An arena structure --- */ |
| 42 | |
| 43 | typedef struct arena { |
| 44 | const struct arena_ops *ops; |
| 45 | } arena; |
| 46 | |
| 47 | typedef struct arena_ops { |
| 48 | void *(*alloc)(arena */*a*/, size_t /*sz*/); |
| 49 | void *(*realloc)(arena */*a*/, void */*p*/, size_t /*sz*/, size_t /*osz*/); |
| 50 | void (*free)(arena */*a*/, void */*p*/); |
| 51 | void (*purge)(arena */*a*/); |
| 52 | } arena_ops; |
| 53 | |
| 54 | /*----- Global variables --------------------------------------------------*/ |
| 55 | |
| 56 | extern arena *arena_global; /* Standard global arena */ |
| 57 | extern arena arena_stdlib; /* Arena based on @malloc@/@free@ */ |
| 58 | |
| 59 | /*----- Functions provided ------------------------------------------------*/ |
| 60 | |
| 61 | /* --- @arena_fakerealloc@ --- * |
| 62 | * |
| 63 | * Arguments: @arena *a@ = pointer to arena block |
| 64 | * @void *p@ = pointer to memory block to resize |
| 65 | * @size_t sz@ = size desired for the block |
| 66 | * @size_t osz@ = size of the old block |
| 67 | * |
| 68 | * Returns: --- |
| 69 | * |
| 70 | * Use: Standard fake @realloc@ function, for use if you don't |
| 71 | * support @realloc@ properly. |
| 72 | */ |
| 73 | |
| 74 | extern void *arena_fakerealloc(arena */*a*/, void */*p*/, |
| 75 | size_t /*sz*/, size_t /*osz*/); |
| 76 | |
| 77 | /* --- Useful macros --- */ |
| 78 | |
| 79 | #define A_ALLOC(a, sz) (((a)->ops->alloc)((a), (sz))) |
| 80 | #define A_REALLOC(a, p, sz, osz) (((a)->ops->realloc)((a), (p), (sz), (osz))) |
| 81 | #define A_FREE(a, p) (((a)->ops->free)((a), (p))) |
| 82 | |
| 83 | /* --- Simple function equivalents --- */ |
| 84 | |
| 85 | extern void *a_alloc(arena */*a*/, size_t /*sz*/); |
| 86 | extern void *a_realloc(arena */*a*/, void */*p*/, |
| 87 | size_t /*sz*/, size_t /*osz*/); |
| 88 | extern void a_free(arena */*a*/, void */*p*/); |
| 89 | |
| 90 | /*----- That's all, folks -------------------------------------------------*/ |
| 91 | |
| 92 | #ifdef __cplusplus |
| 93 | } |
| 94 | #endif |
| 95 | |
| 96 | #endif |