configure.ac: Lightly modernize.
[mLib] / mem / arena.3
... / ...
CommitLineData
1.\" -*-nroff-*-
2.TH arena 3 "3 June 2000" "Straylight/Edgeware" "mLib utilities library"
3.SH "NAME"
4arena \- control of memory allocation
5.\" @arena_global
6.\" @arena_stdlib
7.\" @arena_fakemalloc
8.\" @a_alloc
9.\" @a_realloc
10.\" @a_free
11.\" @A_ALLOC
12.\" @A_REALLOC
13.\" @A_FREE
14.SH "SYNOPSIS"
15.nf
16.B "#include <mLib/arena.h>"
17
18.ta 2n
19.B "typedef struct {"
20.B " const struct arena_ops *ops";
21.B "} arena;"
22
23.B "typedef struct {"
24.BI " void *(*alloc)(arena *" a ", size_t " sz );
25.BI " void *(*realloc)(arena *" a ", void *" p ", size_t " sz ", size_t " osz );
26.BI " void *(*free)(arena *" a ", void *" p );
27.BI " void *(*purge)(arena *" a );
28.B "} arena_ops;"
29
30.BI "arena *arena_global;"
31.BI "arena arena_stdlib;"
32
33.ta \w'\fBvoid *arena_fakerealloc('u
34.BI "void *arena_fakerealloc(arena *" a ", void *" p ,
35.BI " size_t " sz ", size_t " osz );
36
37.BI "void *a_alloc(arena *" a ", size_t " sz );
38.BI "void *a_realloc(arena *" a ", void *" p ", size_t " sz ", size_t " osz );
39.BI "void a_free(arena *" a );
40
41.BI "void *A_ALLOC(arena *" a ", size_t " sz );
42.BI "void *A_REALLOC(arena *" a ", void *" p ", size_t " sz ", size_t " osz );
43.BI "void A_FREE(arena *" a );
44.fi
45.SH "DESCRIPTION"
46An
47.I arena
48is a place from which blocks of memory may be allocated and freed. The
49basic
50.B mLib
51library provides a single standard arena,
52.BR arena_stdlib ,
53which uses the usual
54.BR malloc (3)
55and
56.BR free (3)
57calls. The global variable
58.B arena_global
59is a pointer to a `current' arena, which is a good choice to use if
60you can't think of anything better.
61.PP
62The macros
63.BR A_ALLOC ,
64.B A_REALLOC
65and
66.B A_FREE
67behave like the standard C functions
68.BR malloc (3),
69.BR realloc (3)
70and
71.BR free (3),
72allocating, resizing and releasing blocks from a given arena. There are
73function-call equivalents with lower-case names too. For a more
74convenient interface to memory allocation, see
75.BR alloc (3).
76.PP
77.B Note:
78The
79.B realloc
80function has an extra argument
81.I osz
82specifying the old size of the block. This is for the benefit of arena
83handlers which can't easily find the old block's size.
84.PP
85.SS "Defining new arenas"
86An
87.B arena
88is a structure containing a single member,
89.BR ops ,
90which is a pointer to a structure of type
91.BR arena_ops .
92The
93.B arena
94structure may be followed in memory by data which is used by the arena's
95manager to maintain its state.
96.PP
97The
98.B arena_ops
99table contains function pointers which are called to perform various
100memory allocation tasks:
101.TP
102.BI "void *(*alloc)(arena *" a ", size_t " sz );
103Allocates a block of memory, of at least
104.I sz
105bytes in size, appropriately aligned, and returns its address.
106.nf
107.TP
108.BI "void *(*realloc)(arena *" a ", void *" p ", size_t " sz ", size_t " osz );
109.fi
110Resizes the block pointed to by
111.IR p ,
112with
113.I osz
114interesting bytes in it,
115so that it is at least
116.I sz
117bytes long. You can use
118.B arena_fakerealloc
119here, to fake resizing by allocating, copying and freeing, if your arena
120doesn't make doing something more efficient easy.
121.TP
122.BI "void (*free)(arena *" a ", void *" p );
123Frees the block pointed to by
124.IR p .
125.TP
126.BI "void (*purge)(arena *" a );
127Frees all blocks in the arena. Used when the arena is being destroyed.
128.PP
129The behaviour of the
130.IR alloc ,
131.I realloc
132and
133.I free
134calls with respect to null pointers and zero-sized blocks is as
135specified by the ANSI C standard.
136.SH "SEE ALSO"
137.BR alloc (3),
138.BR mLib (3).
139.SH AUTHOR
140Mark Wooding, <mdw@distorted.org.uk>