@@@ much mess, mostly manpages
[mLib] / mem / sub.3.in
CommitLineData
b6b9d458 1.\" -*-nroff-*-
c4ccbbf9
MW
2.\"
3.\" Manual for efficient small-block allocation
4.\"
5.\" (c) 1999, 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 sub 3mLib "8 May 1999" "Straylight/Edgeware" "mLib utilities library"
c5775f49 32.\" @subarena_create
33.\" @subarena_destroy
34.\" @subarena_alloc
35.\" @subarena_free
08da152e 36.\" @sub_alloc
37.\" @sub_free
38.\" @sub_init
c4ccbbf9 39.
c5775f49 40.\" @A_CREATE
41.\" @A_DESTROY
08da152e 42.\" @CREATE
43.\" @DESTROY
c4ccbbf9
MW
44.
45.\"--------------------------------------------------------------------------
46.SH NAME
47sub \- efficient allocation and freeing of small blocks
48.
49.\"--------------------------------------------------------------------------
b6b9d458 50.SH SYNOPSIS
c4ccbbf9 51.
b6b9d458 52.nf
53.B "#include <mLib/sub.h>"
d056fbdf 54.PP
4729aa69 55.B "typedef struct { ...\& } subarena;"
d056fbdf 56.PP
c5775f49 57.BI "void subarena_create(subarena *" s ", arena *" a );
58.BI "void subarena_destroy(subarena *" s );
59.BI "void subarena_alloc(subarena *" s ", size_t " sz );
60.BI "void subarena_free(subarena *" s ", void *" p ", size_t " sz );
d056fbdf 61.PP
b6b9d458 62.B "void sub_init(void);"
63.BI "void *sub_alloc(size_t " sz );
64.BI "void sub_free(void *" p ", size_t " sz );
d056fbdf 65.PP
c5775f49 66.BI "void *A_CREATE(subarena *" s ", " type );
67.BI "void A_DESTROY(subarena *" s ", " type " *" p );
b6b9d458 68.BI "void *CREATE(" type );
69.BI "void DESTROY(" type " *" p );
70.fi
c4ccbbf9
MW
71.
72.\"--------------------------------------------------------------------------
b6b9d458 73.SH DESCRIPTION
c4ccbbf9 74.
b6b9d458 75The
c5775f49 76.B subarena
b6b9d458 77collection of functions and macros implement an efficient allocator for
c5775f49 78small blocks of known sizes, constructed from a general allocator
79implemented by an
80.BR arena (3).
81Free blocks of the same size are linked together in list, making freeing
82and allocation fast. The `free' operation requires the block size as an
83argument, so there's no data overhead for an allocated block. The
84system takes advantage of this by allocating big chunks from the
85underlying arena and splitting the chunks into smaller blocks of the
86right size, so the space and time overhead from the underlying allocator
87is divided over many blocks.
b6b9d458 88.PP
89Calling
c5775f49 90.B subarena_alloc
b6b9d458 91allocates a block of
92.I sz
c5775f49 93bytes from the subarena
d4efbcd9 94.IR s .
c5775f49 95If there isn't enough memory to allocate the block, the
b6b9d458 96exception
97.B EXC_NOMEM
98is raised.
99.PP
100The
c5775f49 101.B subarena_free
b6b9d458 102function frees a block allocated by
c5775f49 103.B subarena_alloc
104from the same subarena. You must know the size of the block in advance.
105Note that
106.B subarena_free
b6b9d458 107never gives memory back to the underlying allocator. Free sub-blocks
108are just made available to later calls of
c5775f49 109.BR subarena_alloc .
b6b9d458 110.PP
c5775f49 111Don't try to free blocks allocated by
112.B subarena_alloc
113to the underlying arena's
114.I free
115function, or to try freeing blocks obtained directly from the arena's
d4efbcd9 116.I alloc
c5775f49 117function using
118.BR subarena_free .
b6b9d458 119If you do, you'll get what you deserve.
120.PP
121The pair of macros
c5775f49 122.B A_CREATE
b6b9d458 123and
c5775f49 124.B A_DESTROY
b6b9d458 125are intended to provide a slightly more natural interface to
126allocation. The call
127.VS
c5775f49 128mystruct *p = subarena_alloc(s, sizeof(mystruct));
b6b9d458 129.VE
130can be replaced by
131.VS
c5775f49 132mystruct p = A_CREATE(s, mystruct);
b6b9d458 133.VE
134Similarly, the block can be freed by saying
135.VS
c5775f49 136A_DESTROY(s, p)
b6b9d458 137.VE
d2a91066 138rather than the more cumbersome
b6b9d458 139.VS
c5775f49 140subarena_free(s, p, sizeof(*p));
b6b9d458 141.VE
c5775f49 142There is a standard subarena
143.B sub_global
144which uses
145.B arena_global
146as its underlying allocator (obtained the first time the subarena is
147used). The functions
148.B sub_alloc
149and
150.B sub_free
151and the macros
152.B CREATE
153and
154.B DESTROY
155use this subarena.
156.PP
b6b9d458 157The function
158.B sub_init
c5775f49 159ought to be called before any of the other functions as a matter of good
160taste, but actually the system will initialize itself the first time
161it's used.
c4ccbbf9
MW
162.
163.\"--------------------------------------------------------------------------
08da152e 164.SH "SEE ALSO"
c4ccbbf9 165.
c5775f49 166.BR arena (3),
08da152e 167.BR exc (3),
168.BR alloc (3),
169.BR mLib (3).
c4ccbbf9
MW
170.
171.\"--------------------------------------------------------------------------
b6b9d458 172.SH AUTHOR
c4ccbbf9 173.
9b5ac6ff 174Mark Wooding, <mdw@distorted.org.uk>
c4ccbbf9
MW
175.
176.\"----- That's all, folks --------------------------------------------------