3 * Resource pool handling
5 * (c) 2000 Straylight/Edgeware
8 /*----- Licensing notice --------------------------------------------------*
10 * This file is part of the mLib utilities library.
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.
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.
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,
35 /*----- Header files ------------------------------------------------------*/
47 /*----- Data structures ---------------------------------------------------*/
49 #define POOL_CHUNKSZ 65536
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 */
57 typedef struct pool_resource
{
58 struct pool_resource
*next
; /* Next resource in the chain */
59 void (*destroy
)(struct pool_resource */
*r*/
); /* Destruction function */
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 */
69 typedef struct pool_file
{
70 pool_resource r
; /* A pool resource record */
71 FILE *fp
; /* The actual file handle */
74 /*----- Basic pool management ---------------------------------------------*/
76 /* --- @pool_alloc@ --- *
78 * Arguments: @pool *p@ = pool to allocate from
79 * @size_t sz@ = size of block wanted
81 * Returns: Pointer to the requested block.
83 * Use: Allocates memory from a resource pool. Memory is never freed
84 * from pools: it is released when the pool is destroyed.
87 extern void *pool_alloc(pool */
*p*/
, size_t /*sz*/);
89 /* --- @pool_strdup@ --- *
91 * Arguments: @pool *p@ = pool to allocate from
92 * @const char *s@ = pointer to string
94 * Returns: A pointer to a copy of the string.
96 * Use: Allocates a copy of a string.
99 extern char *pool_strdup(pool */
*p*/
, const char */
*s*/
);
101 /* --- @pool_create@ --- *
103 * Arguments: @arena *a@ = pointer to an arena to allocate memory from
105 * Returns: A newly created resource pool.
107 * Use: Creates a resource pool which is not a child of any other
111 extern pool
*pool_create(arena */
*a*/
);
113 /* --- @pool_destroy@ --- *
115 * Arguments: @pool *p@ = pointer to pool to destroy
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.
124 extern void pool_destroy(pool */
*p*/
);
126 /* --- @pool_sub@ --- *
128 * Arguments: @pool *p@ = pointer to parent pool
130 * Returns: A new child pool of the parent.
132 * Use: Creates a subpool. The subpool can either be destroyed on
133 * its own, or will be automatically destroyed at the same time
137 extern pool
*pool_sub(pool */
*p*/
);
139 /* --- @pool_add@ --- *
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
147 * Use: Adds a resource to a pool.
150 #define POOL_ADD(p, rr, dfn) do { \
152 pool_resource *_r = (rr); \
158 extern void pool_add(pool */
*p*/
, pool_resource */
*r*/
,
159 void (*/
*dfn*/
)(pool_resource */
*r*/
));
161 /*----- Various simple resource types -------------------------------------*/
163 /* --- @pool_fopen@ --- *
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
169 * Returns: A pointer to a pool resource containing an open file handle,
170 * or null if the file open filed.
172 * Use: Opens a file so that it will be freed again when a pool is
176 extern pool_file
*pool_fopen(pool */
*p*/
,
177 const char */
*file*/
, const char */
*how*/
);
179 /* --- @pool_fclose@ --- *
181 * Arguments: @pool_file *pf@ = pointer to a file resource
183 * Returns: The response from the @fclose@ function.
185 * Use: Closes a file. It is not an error to close a file multiple
189 extern int pool_fclose(pool_file */
*pf*/
);
191 /* --- @pool_subarena@ --- *
193 * Arguments: @pool *p@ = pointer to the pool
195 * Returns: A subarena built from the pool's memory allocator.
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
202 extern subarena
*pool_subarena(pool */
*p*/
);
204 /*----- That's all, folks -------------------------------------------------*/