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 */ |
53 | char *p; /* Free area in this chunk */ |
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 */ |
67 | } pool; |
68 | |
69 | typedef struct pool_file { |
70 | pool_resource r; /* A pool resource record */ |
71 | FILE *fp; /* The actual file handle */ |
72 | } pool_file; |
73 | |
74 | /*----- Basic pool management ---------------------------------------------*/ |
75 | |
76 | /* --- @pool_alloc@ --- * |
77 | * |
78 | * Arguments: @pool *p@ = pool to allocate from |
79 | * @size_t sz@ = size of block wanted |
80 | * |
81 | * Returns: Pointer to the requested block. |
82 | * |
83 | * Use: Allocates memory from a resource pool. Memory is never freed |
84 | * from pools: it is released when the pool is destroyed. |
85 | */ |
86 | |
87 | extern void *pool_alloc(pool */*p*/, size_t /*sz*/); |
88 | |
89 | /* --- @pool_strdup@ --- * |
90 | * |
91 | * Arguments: @pool *p@ = pool to allocate from |
92 | * @const char *s@ = pointer to string |
93 | * |
94 | * Returns: A pointer to a copy of the string. |
95 | * |
96 | * Use: Allocates a copy of a string. |
97 | */ |
98 | |
99 | extern char *pool_strdup(pool */*p*/, const char */*s*/); |
100 | |
101 | /* --- @pool_create@ --- * |
102 | * |
103 | * Arguments: @arena *a@ = pointer to an arena to allocate memory from |
104 | * |
105 | * Returns: A newly created resource pool. |
106 | * |
107 | * Use: Creates a resource pool which is not a child of any other |
108 | * resource pool. |
109 | */ |
110 | |
111 | extern pool *pool_create(arena */*a*/); |
112 | |
113 | /* --- @pool_destroy@ --- * |
114 | * |
115 | * Arguments: @pool *p@ = pointer to pool to destroy |
116 | * |
117 | * Returns: --- |
118 | * |
119 | * Use: Destroys a pool, freeing all of the resources within it. If |
120 | * this is a root pool, its memory will be deallocated; if it's |
121 | * a subpool, it is emptied and can be used again. |
122 | */ |
123 | |
124 | extern void pool_destroy(pool */*p*/); |
125 | |
126 | /* --- @pool_sub@ --- * |
127 | * |
128 | * Arguments: @pool *p@ = pointer to parent pool |
129 | * |
130 | * Returns: A new child pool of the parent. |
131 | * |
132 | * Use: Creates a subpool. The subpool can either be destroyed on |
133 | * its own, or will be automatically destroyed at the same time |
134 | * as the parent. |
135 | */ |
136 | |
137 | extern pool *pool_sub(pool */*p*/); |
138 | |
139 | /* --- @pool_add@ --- * |
140 | * |
141 | * Arguments: @pool *p@ = pointer to pool to add the resource to |
142 | * @pool_resource *r@ = pointer to resource block |
143 | * @void (*dfn)(pool_resource *r)@ = destruction function |
144 | * |
145 | * Returns: --- |
146 | * |
147 | * Use: Adds a resource to a pool. |
148 | */ |
149 | |
150 | #define POOL_ADD(p, rr, dfn) do { \ |
151 | pool *_p = (p); \ |
152 | pool_resource *_r = (rr); \ |
153 | _r->next = _p->r; \ |
154 | _r->destroy = dfn; \ |
155 | _p->r = _r; \ |
156 | } while (0) |
157 | |
158 | extern void pool_add(pool */*p*/, pool_resource */*r*/, |
159 | void (*/*dfn*/)(pool_resource */*r*/)); |
160 | |
161 | /*----- Various simple resource types -------------------------------------*/ |
162 | |
163 | /* --- @pool_fopen@ --- * |
164 | * |
165 | * Arguments: @pool *p@ = pointer to a pool |
166 | * @const char *file@ = name of the file to open |
167 | * @const char *how@ = string specifying opening parameters |
168 | * |
169 | * Returns: A pointer to a pool resource containing an open file handle, |
170 | * or null if the file open filed. |
171 | * |
172 | * Use: Opens a file so that it will be freed again when a pool is |
173 | * destroyed. |
174 | */ |
175 | |
176 | extern pool_file *pool_fopen(pool */*p*/, |
177 | const char */*file*/, const char */*how*/); |
178 | |
179 | /* --- @pool_fclose@ --- * |
180 | * |
181 | * Arguments: @pool_file *pf@ = pointer to a file resource |
182 | * |
183 | * Returns: The response from the @fclose@ function. |
184 | * |
185 | * Use: Closes a file. It is not an error to close a file multiple |
186 | * times. |
187 | */ |
188 | |
189 | extern int pool_fclose(pool_file */*pf*/); |
190 | |
191 | /* --- @pool_subarena@ --- * |
192 | * |
193 | * Arguments: @pool *p@ = pointer to the pool |
194 | * |
195 | * Returns: A subarena built from the pool's memory allocator. |
196 | * |
197 | * Use: Creates a suballocation arena attached to a pool. The arena |
198 | * and all of its memory will be freed when the pool is |
199 | * destroyed. |
200 | */ |
201 | |
202 | extern subarena *pool_subarena(pool */*p*/); |
203 | |
204 | /*----- That's all, folks -------------------------------------------------*/ |
205 | |
206 | #ifdef __cplusplus |
207 | } |
208 | #endif |
209 | |
210 | #endif |