@@@ fltfmt mess
[mLib] / mem / arena.h
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 /* --- @ALLOCV_SAFE_P@, @NEWV_SAFE_P@ ---
62 *
63 * Arguments: @type *p@ = pointer to a vector (for @NEWV_SAFE_P@; not
64 * evaluated)
65 * @size_t n@ = number of elements
66 * @size_t sz@ = element size (for @ALLOCV_SAFE_P@)
67 *
68 * Returns: Nonzero if the product of @n@ and @sz@ (or @sizeof(*p)@) is
69 * representable in type @size_t@.
70 */
71
72 #define ALLOCV_SAFE_P(n, sz) ((n) <= (size_t)-1/(sz))
73 #define NEWV_SAFE_P(p, n) (ALLOCV_SAFE_P((n), sizeof(*(p))))
74
75 /* --- @arena_fakerealloc@ --- *
76 *
77 * Arguments: @arena *a@ = pointer to arena block
78 * @void *p@ = pointer to memory block to resize
79 * @size_t sz@ = size desired for the block
80 * @size_t osz@ = size of the old block
81 *
82 * Returns: ---
83 *
84 * Use: Standard fake @realloc@ function, for use if you don't
85 * support @realloc@ properly.
86 */
87
88 extern void *arena_fakerealloc(arena */*a*/, void */*p*/,
89 size_t /*sz*/, size_t /*osz*/);
90
91 /* --- Useful macros --- */
92
93 #define A_ALLOC(a, sz) (((a)->ops->alloc)((a), (sz)))
94 #define A_ALLOCV(a, n, sz) \
95 (ALLOCV_SAFE_P((n), (sz)) ? ((a)->ops->alloc)((a), (n)*(sz)) : 0)
96 #define A_NEW(p, a) do { (p) = A_ALLOC((a), sizeof(*(p))); } while (0)
97 #define A_NEWV(p, a, n) \
98 do { (p) = A_ALLOCV((a), (n), sizeof(*(p))); } while (0)
99 #define A_REALLOC(a, p, sz, osz) \
100 (((a)->ops->realloc)((a), (p), (sz), (osz)))
101 #define A_REALLOCV(a, p, n, on, sz) \
102 (ALLOCV_SAFE_P((n), (sz)) ? \
103 ((a)->ops->realloc)((a), (p), (n)*(sz), (on)*(sz)) : 0)
104 #define A_RENEWV(q, p, a, n, on) do { \
105 (q) = !sizeof((*(p)) = (*(q))) + \
106 A_REALLOCV((a), (p), (n), (on), sizeof(*(p))); \
107 } while (0)
108 #define A_FREE(a, p) (((a)->ops->free)((a), (p)))
109
110 /* --- Simple function equivalents --- */
111
112 extern void *a_alloc(arena */*a*/, size_t /*sz*/);
113 extern void *a_allocv(arena */*a*/, size_t /*n*/, size_t /*sz*/);
114 extern void *a_realloc(arena */*a*/, void */*p*/,
115 size_t /*sz*/, size_t /*osz*/);
116 extern void *a_reallocv(arena */*a*/, void */*p*/,
117 size_t /*n*/, size_t /*on*/, size_t /*sz*/);
118 extern void a_free(arena */*a*/, void */*p*/);
119
120 /*----- That's all, folks -------------------------------------------------*/
121
122 #ifdef __cplusplus
123 }
124 #endif
125
126 #endif