@@@ much mess, mostly manpages
[mLib] / mem / pool.3.in
CommitLineData
9787c8a8 1.\" -*-nroff-*-
c4ccbbf9
MW
2.\"
3.\" Manual for resource pools
4.\"
5.\" (c) 2000, 2001, 2003, 2005, 2007, 2009, 2023, 2024 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 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.
16.\"
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.
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 Software
24.\" Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
25.\" USA.
26.
27.\"--------------------------------------------------------------------------
28.so ../defs.man \" @@@PRE@@@
29.
30.\"--------------------------------------------------------------------------
31.TH pool 3mLib "7 July 2000" "Straylight/Edgeware" "mLib utilities library"
9787c8a8 32.\" @pool_alloc
33.\" @pool_strdup
c5775f49 34.\" @pool_init
9787c8a8 35.\" @pool_create
36.\" @pool_destroy
37.\" @pool_sub
38.\" @pool_add
39.\" @POOL_ADD
40.\" @pool_fopen
41.\" @pool_fclose
42.\" @pool_subarena
c4ccbbf9
MW
43.
44.\"--------------------------------------------------------------------------
45.SH "NAME"
46pool \- resource pool management
47.
48.\"--------------------------------------------------------------------------
9787c8a8 49.SH "SYNOPSIS"
c4ccbbf9 50.
9787c8a8 51.nf
52.B "#include <mLib/pool.h>"
d056fbdf 53.PP
4729aa69 54.B "typedef struct { ...\& } pool;"
d056fbdf 55.PP
adec5584 56.ta 2n
4729aa69 57.B "typedef struct {"
adec5584
MW
58.B " pool_resource *next;"
59.BI " void (*destroy)(pool_resource *" r );
4729aa69 60.B "} pool_resource;"
d056fbdf 61.PP
4729aa69 62.B "typedef struct {"
adec5584
MW
63.B " FILE *fp;"
64.B " ..."
4729aa69 65.B "} pool_file;"
d056fbdf 66.PP
c5775f49 67.BI "void pool_init(pool *" p ", arena *" a );
9787c8a8 68.BI "pool *pool_create(arena *" a );
69.BI "pool *pool_sub(pool *" p );
70.BI "void pool_destroy(pool *" p );
adec5584 71.ta \w'\fBvoid pool_add('u
9787c8a8 72.BI "void pool_add(pool *" p ", pool_resource *" r ,
adec5584 73.BI " void (*" dfn ")(pool_resource *" r ));
9787c8a8 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 );
d056fbdf 79.PP
adec5584 80.ta \w'\fBvoid POOL_ADD('u
9787c8a8 81.BI "void POOL_ADD(pool *" p ", pool_resource *" r ,
adec5584 82.BI " void (*" dfn ")(pool_resource *" r ));
9787c8a8 83.fi
c4ccbbf9
MW
84.
85.\"--------------------------------------------------------------------------
9787c8a8 86.SH "DESCRIPTION"
c4ccbbf9 87.
9787c8a8 88.SS "Overview"
89A
90.I "resource pool"
91is a collection of resources (e.g., memory, files) which may be disposed
92of simultaneously.
93.PP
94A pool may be a
95.IR "root pool" ,
96in which case it stands on its own, or it may be a
d4efbcd9 97.IR "subpool"
9787c8a8 98of another pool (which may in turn either be a root pool or a subpool of
99another).
100.PP
101Pools manage memory efficiently. Memory is allocated in large chunks
102from an
103.BR arena (3),
104and given out as necessary to callers. There is no way of freeing
105memory dynamically; instead, the memory allocated by a pool is freed
106when the pool is destroyed. While allocation is rapid, there is waste
107because the allocator has to ensure that blocks are properly aligned.
108Since pools offer an arena interface, it is possible to build a
109.BR subarena (3)
110over them. This also enables memory in the subarena to be reclaimed
111when the pool is destroyed.
112.PP
113Other resources (e.g., file handles) may be added to the pool. The pool
114will automatically release any resources it has when it's destroyed.
115Attaching resources to an appropriate pool can therefore be a useful way
116of avoiding memory leaks.
c4ccbbf9 117.
9787c8a8 118.SS "Creating and destroying pools"
119A new root pool is created using
120.BR pool_create ,
121passing it an arena from which it can allocate large memory blocks.
c5775f49 122Alternatively, you can allocate a
123.B pool
124structure from somewhere and initialize it by passing its address and an
125arena to
126.BR pool_init .
9787c8a8 127.PP
128A subpool is created by calling
129.BR pool_sub ,
130naming the parent pool.
131.PP
132Pools are destroyed by passing them to
133.BR pool_destroy .
c5775f49 134Pools created by
135.B pool_create
136are completely destroyed, since the memory containing the pool structure
137is allocated from the pool itself. Subpools and pools allocated by the
138caller and initialized by
139.BR pool_init ,
140on the other hand, are
141allocated from a parent pool, and may be reused after being `destroyed'.
c4ccbbf9 142.
9787c8a8 143.SS "Memory allocation"
144Memory is allocated from a pool by calling
145.BR pool_alloc ,
146passing it the pool and the size of memory requested. There is an
147interface for copying strings,
148.BR pool_strdup ,
149since this is a common operation. Note that there is no
150.BR pool_free :
151if this is important, either use the pool's arena
152.B p->pa
153directly or create a subpool.
154.PP
155A pool provides an
156.BR arena (3)
157interface,
158.BR p->a ,
159which can be passed to other components to cause them to use the pool
160for memory allocation.
c4ccbbf9 161.
9787c8a8 162.SS "Other resources"
163Pool resources have a header of type
164.B pool_resource
4729aa69
MW
165with the structure shown in the synopsis. Resources are added to the
166pool by passing a pointer to the pool, the resource block and a
167destruction function to
9787c8a8 168.BR pool_add .
169.PP
170If your resource is freed before the pool is destroyed, manually zero
171the
172.B destroy
173field in the resource header to let the pool manager know not to free
174the resource again.
175.PP
176It's usual to allocate the resource structures from the pool's arena so
177that they're automatically freed when the pool is destroyed.
178.PP
179A
180.BR subarena (3)
181may be created for a particular pool by calling
182.BR pool_subarena .
183The subarena and its contents will be freed automatically when the pool
184is destroyed.
185.PP
186Files may be opened and registered with a pool by
187.BR pool_fopen :
188the
189.I pool
190argument specifies which pool, and the
191.I file
192and
193.I how
194arguments are passed to the standard
195.BR fopen (3)
196function. The return value is a pointer to a
197.B pool_file
198structure, containing a member
199.B fp
200which is the actual file handle. Don't call
201.B fclose
202directly on the file handle: instead pass the whole structure to
203.B pool_fclose
204which will ensure that it doesn't get closed twice by accident. It's
205advisable to close files by hand, to prevent the process from running
206out; it's just not a disaster if you forget by accident.
c4ccbbf9
MW
207.
208.\"--------------------------------------------------------------------------
9787c8a8 209.SH "SEE ALSO"
c4ccbbf9 210.
9787c8a8 211.BR alloc (3),
212.BR arena (3),
213.BR mLib (3),
c4ccbbf9
MW
214.BR sub (3).
215.
216.\"--------------------------------------------------------------------------
9787c8a8 217.SH AUTHOR
c4ccbbf9 218.
9b5ac6ff 219Mark Wooding, <mdw@distorted.org.uk>
c4ccbbf9
MW
220.
221.\"----- That's all, folks --------------------------------------------------