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