Commit | Line | Data |
---|---|---|
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 | |
47 | sub \- 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 | 75 | The |
c5775f49 | 76 | .B subarena |
b6b9d458 | 77 | collection of functions and macros implement an efficient allocator for |
c5775f49 | 78 | small blocks of known sizes, constructed from a general allocator |
79 | implemented by an | |
80 | .BR arena (3). | |
81 | Free blocks of the same size are linked together in list, making freeing | |
82 | and allocation fast. The `free' operation requires the block size as an | |
83 | argument, so there's no data overhead for an allocated block. The | |
84 | system takes advantage of this by allocating big chunks from the | |
85 | underlying arena and splitting the chunks into smaller blocks of the | |
86 | right size, so the space and time overhead from the underlying allocator | |
87 | is divided over many blocks. | |
b6b9d458 | 88 | .PP |
89 | Calling | |
c5775f49 | 90 | .B subarena_alloc |
b6b9d458 | 91 | allocates a block of |
92 | .I sz | |
c5775f49 | 93 | bytes from the subarena |
d4efbcd9 | 94 | .IR s . |
c5775f49 | 95 | If there isn't enough memory to allocate the block, the |
b6b9d458 | 96 | exception |
97 | .B EXC_NOMEM | |
98 | is raised. | |
99 | .PP | |
100 | The | |
c5775f49 | 101 | .B subarena_free |
b6b9d458 | 102 | function frees a block allocated by |
c5775f49 | 103 | .B subarena_alloc |
104 | from the same subarena. You must know the size of the block in advance. | |
105 | Note that | |
106 | .B subarena_free | |
b6b9d458 | 107 | never gives memory back to the underlying allocator. Free sub-blocks |
108 | are just made available to later calls of | |
c5775f49 | 109 | .BR subarena_alloc . |
b6b9d458 | 110 | .PP |
c5775f49 | 111 | Don't try to free blocks allocated by |
112 | .B subarena_alloc | |
113 | to the underlying arena's | |
114 | .I free | |
115 | function, or to try freeing blocks obtained directly from the arena's | |
d4efbcd9 | 116 | .I alloc |
c5775f49 | 117 | function using |
118 | .BR subarena_free . | |
b6b9d458 | 119 | If you do, you'll get what you deserve. |
120 | .PP | |
121 | The pair of macros | |
c5775f49 | 122 | .B A_CREATE |
b6b9d458 | 123 | and |
c5775f49 | 124 | .B A_DESTROY |
b6b9d458 | 125 | are intended to provide a slightly more natural interface to |
126 | allocation. The call | |
127 | .VS | |
c5775f49 | 128 | mystruct *p = subarena_alloc(s, sizeof(mystruct)); |
b6b9d458 | 129 | .VE |
130 | can be replaced by | |
131 | .VS | |
c5775f49 | 132 | mystruct p = A_CREATE(s, mystruct); |
b6b9d458 | 133 | .VE |
134 | Similarly, the block can be freed by saying | |
135 | .VS | |
c5775f49 | 136 | A_DESTROY(s, p) |
b6b9d458 | 137 | .VE |
d2a91066 | 138 | rather than the more cumbersome |
b6b9d458 | 139 | .VS |
c5775f49 | 140 | subarena_free(s, p, sizeof(*p)); |
b6b9d458 | 141 | .VE |
c5775f49 | 142 | There is a standard subarena |
143 | .B sub_global | |
144 | which uses | |
145 | .B arena_global | |
146 | as its underlying allocator (obtained the first time the subarena is | |
147 | used). The functions | |
148 | .B sub_alloc | |
149 | and | |
150 | .B sub_free | |
151 | and the macros | |
152 | .B CREATE | |
153 | and | |
154 | .B DESTROY | |
155 | use this subarena. | |
156 | .PP | |
b6b9d458 | 157 | The function |
158 | .B sub_init | |
c5775f49 | 159 | ought to be called before any of the other functions as a matter of good |
160 | taste, but actually the system will initialize itself the first time | |
161 | it'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 | 174 | Mark Wooding, <mdw@distorted.org.uk> |
c4ccbbf9 MW |
175 | . |
176 | .\"----- That's all, folks -------------------------------------------------- |