Improvements to increase clarity, fix misstatements, add missing
[rsync/rsync.git] / lib / pool_alloc.3
... / ...
CommitLineData
1.ds d \-\^\-
2.ds o \fR[\fP
3.ds c \fR]\fP
4.ds | \fR|\fP
5.de D
6\\.B \*d\\$1
7..
8.de DI
9\\.BI \*d\\$1 \\$2
10..
11.de DR
12\\.BR \*d\\$1 \\$2
13..
14.de Di
15\\.BI \*d\\$1 " \\$2"
16..
17.de Db
18\\.B \*d\\$1 " \\$2"
19..
20.de Df
21\\.B \*d\*ono\*c\\$1
22..
23.de See
24See \fB\\$1\fP for details.
25..
26.de SeeIn
27See \fB\\$1\fP in \fB\\$2\fP for details.
28..
29.TH POOL_ALLOC 3
30.SH NAME
31pool_alloc, pool_free, pool_talloc, pool_tfree, pool_create, pool_destroy
32\- Allocate and free memory in managed allocation pools.
33.SH SYNOPSIS
34.B #include "pool_alloc.h"
35
36\fBstruct alloc_pool *pool_create(size_t \fIsize\fB, size_t \fIquantum\fB, void (*\fIbomb\fB)(char *), int \fIflags\fB);
37
38\fBvoid pool_destroy(struct alloc_pool *\fIpool\fB);
39
40\fBvoid *pool_alloc(struct alloc_pool *\fIpool\fB, size_t \fIsize\fB, char *\fImsg\fB);
41
42\fBvoid pool_free(struct alloc_pool *\fIpool\fB, size_t \fIsize\fB, void *\fIaddr\fB);
43
44\fBvoid *pool_talloc(struct alloc_pool *\fIpool\fB, \fItype\fB), int \fIcount\fB, char *\fImsg\fB);
45
46\fBvoid pool_tfree(struct alloc_pool *\fIpool\fB, \fItype\fB, int \fIcount\fB, void *\fIaddr\fB);
47.SH DESCRIPTION
48.P
49The pool allocation routines use
50.B malloc()
51for underlying memory management.
52What allocation pools do is cause memory within a given pool
53to be allocated in large contiguous blocks
54(called extents) that will be reusable when freed. Unlike
55.BR malloc() ,
56the allocations are not managed individually.
57Instead, each extent tracks the total free memory within the
58extent. Each extent can either be used to allocate memory
59or to manage the freeing of memory within that extent.
60When an extent has less free memory than a given
61allocation request (or at the request of the user),
62memory within that extent ceases to be used for allocation,
63and a new extent is added to the pool.
64.P
65This form of memory management is suited to large numbers of small
66related allocations that are held for a while
67and then freed as a group.
68Because the
69underlying allocations are done in large contiguous extents,
70when an extent is freed, it releases a large enough
71contiguous block of memory to allow the memory to be returned
72to the OS for use by whatever program needs it.
73You can allocate from one or more memory pools and/or
74.B malloc()
75all at the same time without interfering with how pools work.
76.P
77.B pool_create()
78Creates an allocation pool for subsequent calls to the pool
79allocation functions.
80When an extent is created for allocations it will be
81.I size
82bytes.
83Allocations from the pool have their sizes rounded up to a
84multiple of
85.I quantum
86bytes in length.
87Specifying
88.B 0
89for
90.I quantum
91will produce a quantum that should meet maximal alignment
92on most platforms.
93If
94.B POOL_QALIGN
95is set in the
96.IR flags ,
97allocations will be aligned to addresses that are a
98multiple of
99.IR quantum .
100If
101.B POOL_CLEAR
102is set in the
103.IR flags ,
104all allocations from the pool will be initialized to zeros.
105You may specify a
106.B NULL
107for the
108.I bomb
109function pointer if you don't wish to use it. (See the
110.B pool_alloc()
111function for how it is used.)
112.P
113.B pool_destroy()
114destroys an allocation
115.I pool
116and frees all its associated memory.
117.P
118.B pool_alloc()
119allocates
120.I size
121bytes from the specified
122.IR pool .
123If
124.I size
125is
126.BR 0 ,
127.I quantum
128bytes will be allocated.
129If the pool has been created with
130.BR POOL_QALIGN ,
131every chunk of memory that is returned will be suitably aligned.
132You can use this with the default
133.I quantum
134size to ensure that all memory can store a variable of any type.
135If the requested memory cannot be allocated, the
136.I bomb()
137function will be called with
138.I msg
139as its sole argument (if the function was defined at the time
140the pool was created), and then a
141.B NULL
142address is returned (assuming that the bomb function didn't exit).
143.P
144.B pool_free()
145frees
146.I size
147bytes pointed to by an
148.I addr
149that was previously allocated in the specified
150.IR pool .
151If
152.I size
153is
154.BR 0 ,
155.I quantum
156bytes will be freed.
157The memory freed within an extent will not be reusable until
158all of the memory in that extent has been freed with one
159exception: the most recent pool allocation may be freed back
160into the pool prior to making any further allocations.
161If enough free calls are made to indicate that an extent has no
162remaining allocated objects (as computed by the total freed size for
163an extent), its memory will be completely freed back to the system.
164If
165.I addr
166is
167.BR 0 ,
168no memory will be freed, but subsequent allocations will come
169from a new extent.
170.P
171.B pool_talloc()
172is a macro that takes a
173.I type
174and a
175.I count
176instead of a
177.IR size .
178It casts the return value to the correct pointer type.
179.P
180.B pool_tfree
181is a macro that calls
182.B pool_free
183on memory that was allocated by
184.BR pool_talloc() .
185.SH RETURN VALUE
186.B pool_create()
187returns a pointer to
188.BR "struct alloc_pool" .
189.P
190.B pool_alloc()
191and
192.B pool_talloc()
193return pointers to the allocated memory,
194or NULL if the request fails.
195The return type of
196.B pool_alloc()
197will normally require casting to the desired type but
198.B pool_talloc()
199will returns a pointer of the requested
200.IR type .
201.P
202.BR pool_free() ,
203.B pool_tfree()
204and
205.B pool_destroy()
206return no value.
207.SH SEE ALSO
208.nf
209malloc(3)
210.SH AUTHOR
211pool_alloc was created by J.W. Schultz of Pegasystems Technologies.
212.SH BUGS AND ISSUES