Commit | Line | Data |
---|---|---|
d94be366 | 1 | /* -*-c-*- |
2 | * | |
d94be366 | 3 | * Resource pool handling |
4 | * | |
5 | * (c) 2000 Straylight/Edgeware | |
6 | */ | |
7 | ||
d4efbcd9 | 8 | /*----- Licensing notice --------------------------------------------------* |
d94be366 | 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. | |
d4efbcd9 | 16 | * |
d94be366 | 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. | |
d4efbcd9 | 21 | * |
d94be366 | 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 | ||
d94be366 | 28 | #ifndef MLIB_POOL_H |
29 | #define MLIB_POOL_H | |
30 | ||
31 | #ifdef __cplusplus | |
32 | extern "C" { | |
33 | #endif | |
34 | ||
35 | /*----- Header files ------------------------------------------------------*/ | |
36 | ||
37 | #include <stdio.h> | |
38 | ||
39 | #ifndef MLIB_ARENA_H | |
40 | # include "arena.h" | |
41 | #endif | |
42 | ||
43 | #ifndef MLIB_SUB_H | |
44 | # include "sub.h" | |
45 | #endif | |
46 | ||
47 | /*----- Data structures ---------------------------------------------------*/ | |
48 | ||
49 | #define POOL_CHUNKSZ 65536 | |
50 | ||
51 | typedef struct pool_chunk { | |
52 | struct pool_chunk *next; /* Next memory chunk in the chain */ | |
b1a20bee | 53 | unsigned char *p; /* Free area in this chunk */ |
d94be366 | 54 | size_t left; /* Amount of memory left */ |
55 | } pool_chunk; | |
56 | ||
57 | typedef struct pool_resource { | |
58 | struct pool_resource *next; /* Next resource in the chain */ | |
59 | void (*destroy)(struct pool_resource */*r*/); /* Destruction function */ | |
60 | } pool_resource; | |
61 | ||
62 | typedef struct pool { | |
63 | arena a; /* The arena for allocating memory */ | |
64 | pool_chunk *c; /* Pointer to memory chunk list */ | |
65 | pool_resource *r; /* Pointer to resource list */ | |
66 | arena *pa; /* Arena for real allocation */ | |
b1a20bee MW |
67 | unsigned f; /* Flags */ |
68 | #define PF_VALGRIND 1u /* Special valgrind hacks active */ | |
d94be366 | 69 | } pool; |
70 | ||
71 | typedef struct pool_file { | |
72 | pool_resource r; /* A pool resource record */ | |
73 | FILE *fp; /* The actual file handle */ | |
74 | } pool_file; | |
75 | ||
76 | /*----- Basic pool management ---------------------------------------------*/ | |
77 | ||
78 | /* --- @pool_alloc@ --- * | |
79 | * | |
80 | * Arguments: @pool *p@ = pool to allocate from | |
81 | * @size_t sz@ = size of block wanted | |
82 | * | |
83 | * Returns: Pointer to the requested block. | |
84 | * | |
85 | * Use: Allocates memory from a resource pool. Memory is never freed | |
86 | * from pools: it is released when the pool is destroyed. | |
87 | */ | |
88 | ||
89 | extern void *pool_alloc(pool */*p*/, size_t /*sz*/); | |
90 | ||
91 | /* --- @pool_strdup@ --- * | |
92 | * | |
93 | * Arguments: @pool *p@ = pool to allocate from | |
94 | * @const char *s@ = pointer to string | |
95 | * | |
96 | * Returns: A pointer to a copy of the string. | |
97 | * | |
98 | * Use: Allocates a copy of a string. | |
99 | */ | |
100 | ||
101 | extern char *pool_strdup(pool */*p*/, const char */*s*/); | |
102 | ||
103 | /* --- @pool_create@ --- * | |
104 | * | |
105 | * Arguments: @arena *a@ = pointer to an arena to allocate memory from | |
106 | * | |
107 | * Returns: A newly created resource pool. | |
108 | * | |
109 | * Use: Creates a resource pool which is not a child of any other | |
110 | * resource pool. | |
111 | */ | |
112 | ||
113 | extern pool *pool_create(arena */*a*/); | |
114 | ||
115 | /* --- @pool_destroy@ --- * | |
116 | * | |
117 | * Arguments: @pool *p@ = pointer to pool to destroy | |
118 | * | |
119 | * Returns: --- | |
120 | * | |
121 | * Use: Destroys a pool, freeing all of the resources within it. If | |
122 | * this is a root pool, its memory will be deallocated; if it's | |
123 | * a subpool, it is emptied and can be used again. | |
124 | */ | |
125 | ||
126 | extern void pool_destroy(pool */*p*/); | |
127 | ||
128 | /* --- @pool_sub@ --- * | |
129 | * | |
130 | * Arguments: @pool *p@ = pointer to parent pool | |
131 | * | |
132 | * Returns: A new child pool of the parent. | |
133 | * | |
134 | * Use: Creates a subpool. The subpool can either be destroyed on | |
135 | * its own, or will be automatically destroyed at the same time | |
136 | * as the parent. | |
137 | */ | |
138 | ||
139 | extern pool *pool_sub(pool */*p*/); | |
140 | ||
141 | /* --- @pool_add@ --- * | |
142 | * | |
143 | * Arguments: @pool *p@ = pointer to pool to add the resource to | |
144 | * @pool_resource *r@ = pointer to resource block | |
145 | * @void (*dfn)(pool_resource *r)@ = destruction function | |
146 | * | |
147 | * Returns: --- | |
148 | * | |
149 | * Use: Adds a resource to a pool. | |
150 | */ | |
151 | ||
152 | #define POOL_ADD(p, rr, dfn) do { \ | |
153 | pool *_p = (p); \ | |
154 | pool_resource *_r = (rr); \ | |
155 | _r->next = _p->r; \ | |
156 | _r->destroy = dfn; \ | |
157 | _p->r = _r; \ | |
158 | } while (0) | |
159 | ||
160 | extern void pool_add(pool */*p*/, pool_resource */*r*/, | |
161 | void (*/*dfn*/)(pool_resource */*r*/)); | |
162 | ||
163 | /*----- Various simple resource types -------------------------------------*/ | |
164 | ||
165 | /* --- @pool_fopen@ --- * | |
166 | * | |
167 | * Arguments: @pool *p@ = pointer to a pool | |
168 | * @const char *file@ = name of the file to open | |
169 | * @const char *how@ = string specifying opening parameters | |
170 | * | |
171 | * Returns: A pointer to a pool resource containing an open file handle, | |
172 | * or null if the file open filed. | |
173 | * | |
174 | * Use: Opens a file so that it will be freed again when a pool is | |
175 | * destroyed. | |
176 | */ | |
177 | ||
178 | extern pool_file *pool_fopen(pool */*p*/, | |
179 | const char */*file*/, const char */*how*/); | |
180 | ||
181 | /* --- @pool_fclose@ --- * | |
182 | * | |
183 | * Arguments: @pool_file *pf@ = pointer to a file resource | |
184 | * | |
185 | * Returns: The response from the @fclose@ function. | |
186 | * | |
187 | * Use: Closes a file. It is not an error to close a file multiple | |
188 | * times. | |
189 | */ | |
190 | ||
191 | extern int pool_fclose(pool_file */*pf*/); | |
192 | ||
193 | /* --- @pool_subarena@ --- * | |
194 | * | |
195 | * Arguments: @pool *p@ = pointer to the pool | |
196 | * | |
197 | * Returns: A subarena built from the pool's memory allocator. | |
198 | * | |
199 | * Use: Creates a suballocation arena attached to a pool. The arena | |
200 | * and all of its memory will be freed when the pool is | |
201 | * destroyed. | |
202 | */ | |
203 | ||
204 | extern subarena *pool_subarena(pool */*p*/); | |
205 | ||
206 | /*----- That's all, folks -------------------------------------------------*/ | |
207 | ||
208 | #ifdef __cplusplus | |
209 | } | |
210 | #endif | |
211 | ||
212 | #endif |