34f655c1 |
1 | /* -*-c-*- |
2 | * |
b5ea4de3 |
3 | * $Id: arena.h,v 1.2 2000/07/16 12:29:16 mdw Exp $ |
34f655c1 |
4 | * |
5 | * Abstraction for memory allocation arenas |
6 | * |
7 | * (c) 2000 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 | /*----- Revision history --------------------------------------------------* |
31 | * |
32 | * $Log: arena.h,v $ |
b5ea4de3 |
33 | * Revision 1.2 2000/07/16 12:29:16 mdw |
34 | * Change to arena `realloc' interface, to fix a design bug. |
35 | * |
34f655c1 |
36 | * Revision 1.1 2000/06/17 10:37:53 mdw |
37 | * Basic arena management. |
38 | * |
39 | */ |
40 | |
41 | #ifndef MLIB_ARENA_H |
42 | #define MLIB_ARENA_H |
43 | |
44 | #ifdef __cplusplus |
45 | extern "C" { |
46 | #endif |
47 | |
48 | /*----- Header files ------------------------------------------------------*/ |
49 | |
50 | #include <stdlib.h> |
51 | |
52 | /*----- Data structures ---------------------------------------------------*/ |
53 | |
54 | /* --- An arena structure --- */ |
55 | |
56 | typedef struct arena { |
57 | struct arena_ops *ops; |
58 | } arena; |
59 | |
60 | typedef struct arena_ops { |
61 | void *(*alloc)(arena */*a*/, size_t /*sz*/); |
b5ea4de3 |
62 | void *(*realloc)(arena */*a*/, void */*p*/, size_t /*sz*/, size_t /*osz*/); |
34f655c1 |
63 | void (*free)(arena */*a*/, void */*p*/); |
64 | void (*purge)(arena */*a*/); |
65 | } arena_ops; |
66 | |
67 | /*----- Global variables --------------------------------------------------*/ |
68 | |
69 | extern arena *arena_global; /* Standard global arena */ |
70 | extern arena arena_stdlib; /* Arena based on @malloc@/@free@ */ |
71 | |
72 | /*----- Functions provided ------------------------------------------------*/ |
73 | |
74 | /* --- @arena_fakerealloc@ --- * |
75 | * |
76 | * Arguments: @arena *a@ = pointer to arena block |
77 | * @void *p@ = pointer to memory block to resize |
78 | * @size_t sz@ = size desired for the block |
b5ea4de3 |
79 | * @size_t osz@ = size of the old block |
34f655c1 |
80 | * |
81 | * Returns: --- |
82 | * |
83 | * Use: Standard fake @realloc@ function, for use if you don't |
84 | * support @realloc@ properly. |
85 | */ |
86 | |
b5ea4de3 |
87 | extern void *arena_fakerealloc(arena */*a*/, void */*p*/, |
88 | size_t /*sz*/, size_t /*osz*/); |
34f655c1 |
89 | |
90 | /* --- Useful macros --- */ |
91 | |
92 | #define A_ALLOC(a, sz) (((a)->ops->alloc)((a), (sz))) |
b5ea4de3 |
93 | #define A_REALLOC(a, p, sz, osz) (((a)->ops->realloc)((a), (p), (sz), (osz))) |
34f655c1 |
94 | #define A_FREE(a, p) (((a)->ops->free)((a), (p))) |
95 | |
96 | /* --- Simple function equivalents --- */ |
97 | |
98 | extern void *a_alloc(arena */*a*/, size_t /*sz*/); |
b5ea4de3 |
99 | extern void *a_realloc(arena */*a*/, void */*p*/, |
100 | size_t /*sz*/, size_t /*osz*/); |
34f655c1 |
101 | extern void a_free(arena */*a*/, void */*p*/); |
102 | |
103 | /*----- That's all, folks -------------------------------------------------*/ |
104 | |
105 | #ifdef __cplusplus |
106 | } |
107 | #endif |
108 | |
109 | #endif |