3 .\" Manual for efficient small-block allocation
5 .\" (c) 1999, 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 sub 3mLib "8 May 1999" "Straylight/Edgeware" "mLib utilities library"
47 .\"--------------------------------------------------------------------------
49 sub \- efficient allocation and freeing of small blocks
51 .\"--------------------------------------------------------------------------
55 .B "#include <mLib/sub.h>"
57 .B "typedef struct { ...\& } subarena;"
59 .BI "void subarena_create(subarena *" s ", arena *" a );
60 .BI "void subarena_destroy(subarena *" s );
61 .BI "void subarena_alloc(subarena *" s ", size_t " sz );
62 .BI "void *A_CREATE(subarena *" s ", " type );
63 .BI "A_SUBNEW(" type "* " p ", subarena *" s );
64 .BI "void subarena_free(subarena *" s ", void *" p ", size_t " sz );
65 .BI "void A_DESTROY(subarena *" s ", " type " *" p );
67 .B "void sub_init(void);"
68 .BI "void *sub_alloc(size_t " sz );
69 .BI "void *CREATE(" type );
70 .BI "NEW(" type "* " p );
71 .BI "void sub_free(void *" p ", size_t " sz );
72 .BI "void DESTROY(" type " *" p );
75 .\"--------------------------------------------------------------------------
80 collection of functions and macros implement an efficient allocator for
81 small blocks of known sizes, constructed from a general allocator
84 Free blocks of the same size are linked together in list, making freeing
85 and allocation fast. The `free' operation requires the block size as an
86 argument, so there's no data overhead for an allocated block. The
87 system takes advantage of this by allocating big chunks from the
88 underlying arena and splitting the chunks into smaller blocks of the
89 right size, so the space and time overhead from the underlying allocator
90 is divided over many blocks.
96 bytes from the subarena
98 If there isn't enough memory to allocate the block, the
105 function frees a block allocated by
107 from the same subarena. You must know the size of the block in advance.
110 never gives memory back to the underlying allocator. Free sub-blocks
111 are just made available to later calls of
114 Don't try to free blocks allocated by
116 to the underlying arena's
118 function, or to try freeing blocks obtained directly from the arena's
122 If you do, you'll get what you deserve.
129 are intended to provide a slightly more natural interface to
132 mystruct *p = subarena_alloc(s, sizeof(mystruct));
136 mystruct *p = A_CREATE(s, mystruct);
143 Similarly, the block can be freed by saying
147 rather than the more cumbersome
149 subarena_free(s, p, sizeof(*p));
151 There is a standard subarena
155 as its underlying allocator (obtained the first time the subarena is
169 ought to be called before any of the other functions as a matter of good
170 taste, but actually the system will initialize itself the first time
173 .\"--------------------------------------------------------------------------
181 .\"--------------------------------------------------------------------------
184 Mark Wooding, <mdw@distorted.org.uk>
186 .\"----- That's all, folks --------------------------------------------------