@@@ much mess, mostly manpages
[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
34.\" @arena_fakemalloc
35.\" @a_alloc
36.\" @a_realloc
37.\" @a_free
38.\" @A_ALLOC
39.\" @A_REALLOC
40.\" @A_FREE
c4ccbbf9
MW
41.
42.\"--------------------------------------------------------------------------
43.SH "NAME"
44arena \- control of memory allocation
45.
46.\"--------------------------------------------------------------------------
cededfbe 47.SH "SYNOPSIS"
c4ccbbf9 48.
cededfbe 49.nf
50.B "#include <mLib/arena.h>"
d056fbdf 51.PP
adec5584 52.ta 2n
4729aa69 53.B "typedef struct {"
adec5584 54.B " const struct arena_ops *ops";
4729aa69 55.B "} arena;"
d056fbdf 56.PP
4729aa69 57.B "typedef struct {"
adec5584
MW
58.BI " void *(*alloc)(arena *" a ", size_t " sz );
59.BI " void *(*realloc)(arena *" a ", void *" p ", size_t " sz ", size_t " osz );
60.BI " void *(*free)(arena *" a ", void *" p );
61.BI " void *(*purge)(arena *" a );
4729aa69 62.B "} arena_ops;"
d056fbdf 63.PP
cededfbe 64.BI "arena *arena_global;"
65.BI "arena arena_stdlib;"
d056fbdf 66.PP
adec5584 67.ta \w'\fBvoid *arena_fakerealloc('u
b5ea4de3 68.BI "void *arena_fakerealloc(arena *" a ", void *" p ,
adec5584 69.BI " size_t " sz ", size_t " osz );
d056fbdf 70.PP
cededfbe 71.BI "void *a_alloc(arena *" a ", size_t " sz );
b5ea4de3 72.BI "void *a_realloc(arena *" a ", void *" p ", size_t " sz ", size_t " osz );
cededfbe 73.BI "void a_free(arena *" a );
d056fbdf 74.PP
cededfbe 75.BI "void *A_ALLOC(arena *" a ", size_t " sz );
b5ea4de3 76.BI "void *A_REALLOC(arena *" a ", void *" p ", size_t " sz ", size_t " osz );
cededfbe 77.BI "void A_FREE(arena *" a );
78.fi
c4ccbbf9
MW
79.
80.\"--------------------------------------------------------------------------
cededfbe 81.SH "DESCRIPTION"
c4ccbbf9 82.
cededfbe 83An
84.I arena
85is a place from which blocks of memory may be allocated and freed. The
86basic
87.B mLib
88library provides a single standard arena,
89.BR arena_stdlib ,
90which uses the usual
91.BR malloc (3)
92and
93.BR free (3)
94calls. The global variable
95.B arena_global
96is a pointer to a `current' arena, which is a good choice to use if
97you can't think of anything better.
98.PP
99The macros
100.BR A_ALLOC ,
101.B A_REALLOC
102and
103.B A_FREE
104behave like the standard C functions
105.BR malloc (3),
106.BR realloc (3)
107and
108.BR free (3),
109allocating, resizing and releasing blocks from a given arena. There are
110function-call equivalents with lower-case names too. For a more
111convenient interface to memory allocation, see
112.BR alloc (3).
113.PP
b5ea4de3 114.B Note:
115The
116.B realloc
117function has an extra argument
118.I osz
119specifying the old size of the block. This is for the benefit of arena
120handlers which can't easily find the old block's size.
121.PP
c4ccbbf9 122.
cededfbe 123.SS "Defining new arenas"
124An
125.B arena
126is a structure containing a single member,
127.BR ops ,
128which is a pointer to a structure of type
129.BR arena_ops .
130The
131.B arena
132structure may be followed in memory by data which is used by the arena's
133manager to maintain its state.
134.PP
135The
136.B arena_ops
137table contains function pointers which are called to perform various
138memory allocation tasks:
139.TP
4729aa69 140.BI "void *(*alloc)(arena *" a ", size_t " sz );
cededfbe 141Allocates a block of memory, of at least
142.I sz
143bytes in size, appropriately aligned, and returns its address.
b5ea4de3 144.nf
cededfbe 145.TP
4729aa69 146.BI "void *(*realloc)(arena *" a ", void *" p ", size_t " sz ", size_t " osz );
b5ea4de3 147.fi
cededfbe 148Resizes the block pointed to by
149.IR p ,
b5ea4de3 150with
151.I osz
152interesting bytes in it,
cededfbe 153so that it is at least
154.I sz
155bytes long. You can use
156.B arena_fakerealloc
157here, to fake resizing by allocating, copying and freeing, if your arena
158doesn't make doing something more efficient easy.
159.TP
4729aa69 160.BI "void (*free)(arena *" a ", void *" p );
cededfbe 161Frees the block pointed to by
162.IR p .
163.TP
4729aa69 164.BI "void (*purge)(arena *" a );
cededfbe 165Frees all blocks in the arena. Used when the arena is being destroyed.
166.PP
167The behaviour of the
168.IR alloc ,
169.I realloc
170and
171.I free
172calls with respect to null pointers and zero-sized blocks is as
173specified by the ANSI C standard.
c4ccbbf9
MW
174.
175.\"--------------------------------------------------------------------------
cededfbe 176.SH "SEE ALSO"
c4ccbbf9 177.
cededfbe 178.BR alloc (3),
179.BR mLib (3).
c4ccbbf9
MW
180.
181.\"--------------------------------------------------------------------------
cededfbe 182.SH AUTHOR
c4ccbbf9 183.
9b5ac6ff 184Mark Wooding, <mdw@distorted.org.uk>
c4ccbbf9
MW
185.
186.\"----- That's all, folks --------------------------------------------------