@@@ fltfmt wip
[mLib] / mem / arena.3.in
CommitLineData
cededfbe 1.\" -*-nroff-*-
c4ccbbf9
MW
2.\"
3.\" Manual for configurable memory management
4.\"
5.\" (c) 2000, 2001, 2005, 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 arena 3mLib "3 June 2000" "Straylight/Edgeware" "mLib utilities library"
cededfbe 32.\" @arena_global
33.\" @arena_stdlib
b1a20bee 34.
cededfbe 35.\" @arena_fakemalloc
b1a20bee
MW
36.
37.\" @ALLOCV_SAFE_P
38.\" @NEWV_SAFE_P
39.
cededfbe 40.\" @A_ALLOC
b1a20bee
MW
41.\" @A_ALLOCV
42.\" @A_NEW
43.\" @A_NEWV
cededfbe 44.\" @A_REALLOC
b1a20bee 45.\" @A_RENEWV
cededfbe 46.\" @A_FREE
c4ccbbf9 47.
b1a20bee
MW
48.\" @a_alloc
49.\" @a_allocv
50.\" @a_realloc
51.\" @a_reallocv
52.\" @a_free
53.
c4ccbbf9
MW
54.\"--------------------------------------------------------------------------
55.SH "NAME"
56arena \- control of memory allocation
57.
58.\"--------------------------------------------------------------------------
cededfbe 59.SH "SYNOPSIS"
c4ccbbf9 60.
cededfbe 61.nf
62.B "#include <mLib/arena.h>"
d056fbdf 63.PP
adec5584 64.ta 2n
4729aa69 65.B "typedef struct {"
adec5584 66.B " const struct arena_ops *ops";
4729aa69 67.B "} arena;"
d056fbdf 68.PP
4729aa69 69.B "typedef struct {"
adec5584
MW
70.BI " void *(*alloc)(arena *" a ", size_t " sz );
71.BI " void *(*realloc)(arena *" a ", void *" p ", size_t " sz ", size_t " osz );
72.BI " void *(*free)(arena *" a ", void *" p );
73.BI " void *(*purge)(arena *" a );
4729aa69 74.B "} arena_ops;"
d056fbdf 75.PP
cededfbe 76.BI "arena *arena_global;"
77.BI "arena arena_stdlib;"
d056fbdf 78.PP
adec5584 79.ta \w'\fBvoid *arena_fakerealloc('u
b5ea4de3 80.BI "void *arena_fakerealloc(arena *" a ", void *" p ,
adec5584 81.BI " size_t " sz ", size_t " osz );
d056fbdf 82.PP
b1a20bee
MW
83.BI "int ALLOCV_SAFE_P(size_t " n ", size_t " sz );
84.BI "int NEWV_SAFE_P(" type " *" p ", size_t " n );
85.PP
cededfbe 86.BI "void *a_alloc(arena *" a ", size_t " sz );
b1a20bee 87.BI "void *a_allocv(arena *" a ", size_t " n ", size_t " sz );
b5ea4de3 88.BI "void *a_realloc(arena *" a ", void *" p ", size_t " sz ", size_t " osz );
b1a20bee
MW
89.ta \w'\fBvoid a_reallocv('u
90.BI "void *a_reallocv(arena *" a ", void *" p ,
91.BI " size_t " n ", size_t " on ", size_t " sz );
cededfbe 92.BI "void a_free(arena *" a );
d056fbdf 93.PP
cededfbe 94.BI "void *A_ALLOC(arena *" a ", size_t " sz );
b1a20bee
MW
95.BI "void *A_ALLOCV(arena *" a ", size_t " n ", size_t " sz );
96.BI "void A_NEW(" type " *" p ", arena *" a );
97.BI "void A_NEWV(" type " *" p ", arena *" a ", size_t " n );
b5ea4de3 98.BI "void *A_REALLOC(arena *" a ", void *" p ", size_t " sz ", size_t " osz );
b1a20bee
MW
99.ta \w'\fBvoid A_REALLOCV('u
100.BI "void *A_REALLOCV(arena *" a ", void *" p ,
101.BI " size_t " n ", size_t " on ", size_t " sz );
102.ta \w'\fBvoid A_RENEWV('u
103.BI "void A_RENEWV(" type " *" q ", " type " *" p ", arena *" a ,
104.BI " size_t " n ", size_t " on ", size_t " sz );
cededfbe 105.BI "void A_FREE(arena *" a );
106.fi
c4ccbbf9
MW
107.
108.\"--------------------------------------------------------------------------
cededfbe 109.SH "DESCRIPTION"
c4ccbbf9 110.
cededfbe 111An
112.I arena
113is a place from which blocks of memory may be allocated and freed. The
114basic
115.B mLib
116library provides a single standard arena,
117.BR arena_stdlib ,
118which uses the usual
119.BR malloc (3)
120and
121.BR free (3)
122calls. The global variable
123.B arena_global
124is a pointer to a `current' arena, which is a good choice to use if
125you can't think of anything better.
126.PP
127The macros
128.BR A_ALLOC ,
129.B A_REALLOC
130and
131.B A_FREE
132behave like the standard C functions
133.BR malloc (3),
134.BR realloc (3)
135and
136.BR free (3),
137allocating, resizing and releasing blocks from a given arena. There are
138function-call equivalents with lower-case names too. For a more
139convenient interface to memory allocation, see
140.BR alloc (3).
141.PP
b5ea4de3 142.B Note:
143The
144.B realloc
145function has an extra argument
146.I osz
147specifying the old size of the block. This is for the benefit of arena
148handlers which can't easily find the old block's size.
149.PP
b1a20bee
MW
150The macro
151.B ALLOCV_SAFE_P
152returns nonzero if the product
dc6eea4e 153.IR n "\ \*(mu\ " sz
b1a20bee
MW
154is representable in type
155.B size_t
156and zero otherwise;
157i.e., it returns true if it would be safe to try to allocate
dc6eea4e 158.IR n "\ \*(mu\ " sz
b1a20bee
MW
159bytes.
160The macro
161.BR A_ALLOCV
162allocates space for an array of
163.I n
164elements each of size
165.IR sz ,
166somewhat like
167.BR calloc (3),
168except that
169.B A_ALLOCV
170does not clear the allocated memory.
171Likewise, the macro
172.B A_REALLOCV
173resizes the block pointed to by
174.IR p ,
175which previously had space for
176.I on
177elements of size
178.IR sz ,
179so that it now has enough space for
180.I n
181elements,
182returning the new block address.
183There are also function-call equivalents of these macros.
184.PP
185Finally, the macro
186.B A_NEW
187sets its argument
188.I p
189to point to a freshly allocated block of memory
190large enough for the type that
191.I p
192points to,
193or to null if allocation failed.
194The macro
195.B A_NEWV
196sets
197.I p
198to point to memory large enough for
199.I n
200elements, each of the type that
201.I p
202points to.
203The macro
204.B A_RENEWV
205sets
206.I q
207to point to memory large enough for
208.I n
209elements, each of the type that
210.I p
211points to.
c4ccbbf9 212.
cededfbe 213.SS "Defining new arenas"
214An
215.B arena
216is a structure containing a single member,
217.BR ops ,
218which is a pointer to a structure of type
219.BR arena_ops .
220The
221.B arena
222structure may be followed in memory by data which is used by the arena's
223manager to maintain its state.
224.PP
225The
226.B arena_ops
227table contains function pointers which are called to perform various
228memory allocation tasks:
229.TP
4729aa69 230.BI "void *(*alloc)(arena *" a ", size_t " sz );
cededfbe 231Allocates a block of memory, of at least
232.I sz
233bytes in size, appropriately aligned, and returns its address.
b5ea4de3 234.nf
cededfbe 235.TP
4729aa69 236.BI "void *(*realloc)(arena *" a ", void *" p ", size_t " sz ", size_t " osz );
b5ea4de3 237.fi
cededfbe 238Resizes the block pointed to by
239.IR p ,
b5ea4de3 240with
241.I osz
242interesting bytes in it,
cededfbe 243so that it is at least
244.I sz
245bytes long. You can use
246.B arena_fakerealloc
247here, to fake resizing by allocating, copying and freeing, if your arena
248doesn't make doing something more efficient easy.
249.TP
4729aa69 250.BI "void (*free)(arena *" a ", void *" p );
cededfbe 251Frees the block pointed to by
252.IR p .
253.TP
4729aa69 254.BI "void (*purge)(arena *" a );
cededfbe 255Frees all blocks in the arena. Used when the arena is being destroyed.
256.PP
257The behaviour of the
258.IR alloc ,
259.I realloc
260and
261.I free
262calls with respect to null pointers and zero-sized blocks is as
263specified by the ANSI C standard.
c4ccbbf9
MW
264.
265.\"--------------------------------------------------------------------------
cededfbe 266.SH "SEE ALSO"
c4ccbbf9 267.
cededfbe 268.BR alloc (3),
269.BR mLib (3).
c4ccbbf9
MW
270.
271.\"--------------------------------------------------------------------------
cededfbe 272.SH AUTHOR
c4ccbbf9 273.
9b5ac6ff 274Mark Wooding, <mdw@distorted.org.uk>
c4ccbbf9
MW
275.
276.\"----- That's all, folks --------------------------------------------------