utils/gprintf.c: Return the correct number of output characters.
[mLib] / mem / sub.3
... / ...
CommitLineData
1.\" -*-nroff-*-
2.de VS
3.sp 1
4.RS
5.nf
6.ft B
7..
8.de VE
9.ft R
10.fi
11.RE
12.sp 1
13..
14.TH sub 3 "8 May 1999" "Straylight/Edgeware" "mLib utilities library"
15.SH NAME
16sub \- efficient allocation and freeing of small blocks
17.\" @subarena_create
18.\" @subarena_destroy
19.\" @subarena_alloc
20.\" @subarena_free
21.\" @sub_alloc
22.\" @sub_free
23.\" @sub_init
24.\"
25.\" @A_CREATE
26.\" @A_DESTROY
27.\" @CREATE
28.\" @DESTROY
29.\"
30.SH SYNOPSIS
31.nf
32.B "#include <mLib/sub.h>"
33
34.B "typedef struct { ...\& } subarena;"
35
36.BI "void subarena_create(subarena *" s ", arena *" a );
37.BI "void subarena_destroy(subarena *" s );
38.BI "void subarena_alloc(subarena *" s ", size_t " sz );
39.BI "void subarena_free(subarena *" s ", void *" p ", size_t " sz );
40
41.B "void sub_init(void);"
42.BI "void *sub_alloc(size_t " sz );
43.BI "void sub_free(void *" p ", size_t " sz );
44
45.BI "void *A_CREATE(subarena *" s ", " type );
46.BI "void A_DESTROY(subarena *" s ", " type " *" p );
47.BI "void *CREATE(" type );
48.BI "void DESTROY(" type " *" p );
49.fi
50.SH DESCRIPTION
51The
52.B subarena
53collection of functions and macros implement an efficient allocator for
54small blocks of known sizes, constructed from a general allocator
55implemented by an
56.BR arena (3).
57Free blocks of the same size are linked together in list, making freeing
58and allocation fast. The `free' operation requires the block size as an
59argument, so there's no data overhead for an allocated block. The
60system takes advantage of this by allocating big chunks from the
61underlying arena and splitting the chunks into smaller blocks of the
62right size, so the space and time overhead from the underlying allocator
63is divided over many blocks.
64.PP
65Calling
66.B subarena_alloc
67allocates a block of
68.I sz
69bytes from the subarena
70.IR s .
71If there isn't enough memory to allocate the block, the
72exception
73.B EXC_NOMEM
74is raised.
75.PP
76The
77.B subarena_free
78function frees a block allocated by
79.B subarena_alloc
80from the same subarena. You must know the size of the block in advance.
81Note that
82.B subarena_free
83never gives memory back to the underlying allocator. Free sub-blocks
84are just made available to later calls of
85.BR subarena_alloc .
86.PP
87Don't try to free blocks allocated by
88.B subarena_alloc
89to the underlying arena's
90.I free
91function, or to try freeing blocks obtained directly from the arena's
92.I alloc
93function using
94.BR subarena_free .
95If you do, you'll get what you deserve.
96.PP
97The pair of macros
98.B A_CREATE
99and
100.B A_DESTROY
101are intended to provide a slightly more natural interface to
102allocation. The call
103.VS
104mystruct *p = subarena_alloc(s, sizeof(mystruct));
105.VE
106can be replaced by
107.VS
108mystruct p = A_CREATE(s, mystruct);
109.VE
110Similarly, the block can be freed by saying
111.VS
112A_DESTROY(s, p)
113.VE
114rather than the more cumbersome
115.VS
116subarena_free(s, p, sizeof(*p));
117.VE
118There is a standard subarena
119.B sub_global
120which uses
121.B arena_global
122as its underlying allocator (obtained the first time the subarena is
123used). The functions
124.B sub_alloc
125and
126.B sub_free
127and the macros
128.B CREATE
129and
130.B DESTROY
131use this subarena.
132.PP
133The function
134.B sub_init
135ought to be called before any of the other functions as a matter of good
136taste, but actually the system will initialize itself the first time
137it's used.
138.SH "SEE ALSO"
139.BR arena (3),
140.BR exc (3),
141.BR alloc (3),
142.BR mLib (3).
143.SH AUTHOR
144Mark Wooding, <mdw@distorted.org.uk>