3 .\" Manual for resource pools
5 .\" (c) 2000, 2001, 2003, 2005, 2007, 2009, 2023, 2024 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 it under
13 .\" the terms of the GNU Library General Public License as published by
14 .\" the Free Software Foundation; either version 2 of the License, or (at
15 .\" your option) any later version.
17 .\" mLib is distributed in the hope that it will be useful, but WITHOUT
18 .\" ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
19 .\" FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
20 .\" 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 Software
24 .\" Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
27 .\"--------------------------------------------------------------------------
28 .so ../defs.man \" @@@PRE@@@
30 .\"--------------------------------------------------------------------------
31 .TH pool 3mLib "7 July 2000" "Straylight/Edgeware" "mLib utilities library"
44 .\"--------------------------------------------------------------------------
46 pool \- resource pool management
48 .\"--------------------------------------------------------------------------
52 .B "#include <mLib/pool.h>"
54 .B "typedef struct { ...\& } pool;"
58 .B " pool_resource *next;"
59 .BI " void (*destroy)(pool_resource *" r );
67 .BI "void pool_init(pool *" p ", arena *" a );
68 .BI "pool *pool_create(arena *" a );
69 .BI "pool *pool_sub(pool *" p );
70 .BI "void pool_destroy(pool *" p );
71 .ta \w'\fBvoid pool_add('u
72 .BI "void pool_add(pool *" p ", pool_resource *" r ,
73 .BI " void (*" dfn ")(pool_resource *" r ));
74 .BI "void *pool_alloc(pool *" p ", size_t " sz );
75 .BI "char *pool_strdup(pool *" p ", const char *" s );
76 .BI "pool_file *pool_fopen(pool *" p ", const char *" file ", const char *" how );
77 .BI "int pool_fclose(pool_file *" pf );
78 .BI "subarena *pool_subarena(pool *" p );
80 .ta \w'\fBvoid POOL_ADD('u
81 .BI "void POOL_ADD(pool *" p ", pool_resource *" r ,
82 .BI " void (*" dfn ")(pool_resource *" r ));
85 .\"--------------------------------------------------------------------------
91 is a collection of resources (e.g., memory, files) which may be disposed
96 in which case it stands on its own, or it may be a
98 of another pool (which may in turn either be a root pool or a subpool of
101 Pools manage memory efficiently. Memory is allocated in large chunks
104 and given out as necessary to callers. There is no way of freeing
105 memory dynamically; instead, the memory allocated by a pool is freed
106 when the pool is destroyed. While allocation is rapid, there is waste
107 because the allocator has to ensure that blocks are properly aligned.
108 Since pools offer an arena interface, it is possible to build a
110 over them. This also enables memory in the subarena to be reclaimed
111 when the pool is destroyed.
113 Other resources (e.g., file handles) may be added to the pool. The pool
114 will automatically release any resources it has when it's destroyed.
115 Attaching resources to an appropriate pool can therefore be a useful way
116 of avoiding memory leaks.
118 .SS "Creating and destroying pools"
119 A new root pool is created using
121 passing it an arena from which it can allocate large memory blocks.
122 Alternatively, you can allocate a
124 structure from somewhere and initialize it by passing its address and an
128 A subpool is created by calling
130 naming the parent pool.
132 Pools are destroyed by passing them to
136 are completely destroyed, since the memory containing the pool structure
137 is allocated from the pool itself. Subpools and pools allocated by the
138 caller and initialized by
140 on the other hand, are
141 allocated from a parent pool, and may be reused after being `destroyed'.
143 .SS "Memory allocation"
144 Memory is allocated from a pool by calling
146 passing it the pool and the size of memory requested. There is an
147 interface for copying strings,
149 since this is a common operation. Note that there is no
151 if this is important, either use the pool's arena
153 directly or create a subpool.
159 which can be passed to other components to cause them to use the pool
160 for memory allocation.
162 .SS "Other resources"
163 Pool resources have a header of type
165 with the structure shown in the synopsis. Resources are added to the
166 pool by passing a pointer to the pool, the resource block and a
167 destruction function to
170 If your resource is freed before the pool is destroyed, manually zero
173 field in the resource header to let the pool manager know not to free
176 It's usual to allocate the resource structures from the pool's arena so
177 that they're automatically freed when the pool is destroyed.
181 may be created for a particular pool by calling
183 The subarena and its contents will be freed automatically when the pool
186 Files may be opened and registered with a pool by
190 argument specifies which pool, and the
194 arguments are passed to the standard
196 function. The return value is a pointer to a
198 structure, containing a member
200 which is the actual file handle. Don't call
202 directly on the file handle: instead pass the whole structure to
204 which will ensure that it doesn't get closed twice by accident. It's
205 advisable to close files by hand, to prevent the process from running
206 out; it's just not a disaster if you forget by accident.
208 .\"--------------------------------------------------------------------------
216 .\"--------------------------------------------------------------------------
219 Mark Wooding, <mdw@distorted.org.uk>
221 .\"----- That's all, folks --------------------------------------------------