]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - share/man/man9/zone.9
This commit was generated by cvs2svn to compensate for changes in r156678,
[FreeBSD/FreeBSD.git] / share / man / man9 / zone.9
1 .\"-
2 .\" Copyright (c) 2001 Dag-Erling Coïdan Smørgrav
3 .\" All rights reserved.
4 .\"
5 .\" Redistribution and use in source and binary forms, with or without
6 .\" modification, are permitted provided that the following conditions
7 .\" are met:
8 .\" 1. Redistributions of source code must retain the above copyright
9 .\"    notice, this list of conditions and the following disclaimer.
10 .\" 2. Redistributions in binary form must reproduce the above copyright
11 .\"    notice, this list of conditions and the following disclaimer in the
12 .\"    documentation and/or other materials provided with the distribution.
13 .\"
14 .\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 .\" ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 .\" SUCH DAMAGE.
25 .\"
26 .\" $FreeBSD$
27 .\"
28 .Dd July 21, 2003
29 .Dt ZONE 9
30 .Os
31 .Sh NAME
32 .Nm uma_zcreate ,
33 .Nm uma_zalloc ,
34 .Nm uma_zfree ,
35 .Nm uma_zdestroy ,
36 .Nm uma_zone_set_max
37 .Nd zone allocator
38 .Sh SYNOPSIS
39 .In sys/param.h
40 .In sys/queue.h
41 .In vm/uma.h
42 .Ft uma_zone_t
43 .Fo uma_zcreate
44 .Fa "char *name" "int size"
45 .Fa "uma_ctor ctor" "uma_dtor dtor" "uma_init uminit" "uma_fini fini"
46 .Fa "int align" "u_int16_t flags"
47 .Fc
48 .Ft "void *"
49 .Fn uma_zalloc "uma_zone_t zone" "int flags"
50 .Ft void
51 .Fn uma_zfree "uma_zone_t zone" "void *item"
52 .Ft void
53 .Fn uma_zdestroy "uma_zone_t zone"
54 .Ft void
55 .Fn uma_zone_set_max "uma_zone_t zone" "int nitems"
56 .Sh DESCRIPTION
57 The zone allocator provides an efficient interface for managing
58 dynamically-sized collections of items of similar size.
59 The zone allocator can work with preallocated zones as well as with
60 runtime-allocated ones, and is therefore available much earlier in the
61 boot process than other memory management routines.
62 .Pp
63 A zone is an extensible collection of items of identical size.
64 The zone allocator keeps track of which items are in use and which
65 are not, and provides functions for allocating items from the zone and
66 for releasing them back (which makes them available for later use).
67 .Pp
68 The zone allocator stores state information inside the items proper
69 while they are not allocated,
70 so structures that will be managed by the zone allocator
71 and wish to use the type stable property of zones by leaving some fields
72 pre-filled between allocations, must reserve
73 two pointers at the very beginning for internal use by the zone
74 allocator, as follows:
75 .Bd -literal -offset indent
76 struct my_item {
77         struct my_item  *z_rsvd1;
78         struct my_item  *z_rsvd2;
79         /* rest of structure */
80 };
81 .Ed
82 .Pp
83 Alternatively they should assume those entries corrupted
84 after each allocation.
85 After the first allocation of an item,
86 it will have been cleared to zeroes, however subsequent allocations
87 will retain the contents as of the last free, with the exception of the
88 fields mentioned above.
89 .Pp
90 The
91 .Fn uma_zcreate
92 function creates a new zone from which items may then be allocated from.
93 The
94 .Fa name
95 argument is a text name of the zone for debugging and stats; this memory
96 should not be freed until the zone has been deallocated.
97 .Pp
98 The
99 .Fa ctor
100 and
101 .Fa dtor
102 arguments are callback functions that are called by
103 the uma subsystem at the time of the call to
104 .Fn uma_zalloc
105 and
106 .Fn uma_zfree
107 respectively.
108 Their purpose is to provide hooks for initializing or
109 destroying things that need to be done at the time of the allocation
110 or release of a resource.
111 A good usage for the
112 .Fa ctor
113 and
114 .Fa dtor
115 callbacks
116 might be to adjust a global count of the number of objects allocated.
117 .Pp
118 The
119 .Fa uminit
120 and
121 .Fa fini
122 arguments are used to optimize the allocation of
123 objects from the zone.
124 They are called by the uma subsystem whenever
125 it needs to allocate or free several items to satisfy requests or memory
126 pressure.
127 A good use for the
128 .Fa uminit
129 and
130 .Fa fini
131 callbacks might be to
132 initialize and destroy mutexes contained within the object.
133 This would
134 allow one to re-use already initialized mutexes when an object is returned
135 from the uma subsystem's object cache.
136 They are not called on each call to
137 .Fn uma_zalloc
138 and
139 .Fn uma_zfree
140 but rather in a batch mode on several objects.
141 .Pp
142 To allocate an item from a zone, simply call
143 .Fn uma_zalloc
144 with a pointer to that zone
145 and set the
146 .Fa flags
147 argument to selected flags as documented in
148 .Xr malloc 9 .
149 It will return a pointer to an item if successful,
150 or
151 .Dv NULL
152 in the rare case where all items in the zone are in use and the
153 allocator is unable to grow the zone
154 or when
155 .Dv M_NOWAIT
156 is specified.
157 .Pp
158 Items are released back to the zone from which they were allocated by
159 calling
160 .Fn uma_zfree
161 with a pointer to the zone and a pointer to the item.
162 .Pp
163 Created zones,
164 which are empty,
165 can be destroyed using
166 .Fn uma_zdestroy ,
167 freeing all memory that was allocated for the zone.
168 All items allocated from the zone with
169 .Fn uma_zalloc
170 must have been freed with
171 .Fn uma_zfree
172 before.
173 .Pp
174 The purpose of
175 .Fn uma_zone_set_max
176 is to limit the maximum amount of memory that the system can dedicated
177 toward the zone specified by the
178 .Fa zone
179 argument.
180 The
181 .Fa nitems
182 argument gives the upper limit of items in the zone.
183 This limits the total number of items in the zone which includes:
184 allocated items, free items and free items in the per-cpu caches.
185 On systems with more than one CPU it may not be possible to allocate
186 the specified number of items even when there is no shortage of memory,
187 because all of the remaining free items may be in the caches of the
188 other CPUs when the limit is hit.
189 .Sh RETURN VALUES
190 The
191 .Fn uma_zalloc
192 function returns a pointer to an item, or
193 .Dv NULL
194 if the zone ran out of unused items and the allocator was unable to
195 enlarge it.
196 .Sh SEE ALSO
197 .Xr malloc 9
198 .Sh HISTORY
199 The zone allocator first appeared in
200 .Fx 3.0 .
201 It was radically changed in
202 .Fx 5.0
203 to function as a slab allocator.
204 .Sh AUTHORS
205 .An -nosplit
206 The zone allocator was written by
207 .An John S. Dyson .
208 The zone allocator was rewritten in large parts by
209 .An Jeff Roberson Aq jeff@FreeBSD.org
210 to function as a slab allocator.
211 .Pp
212 This manual page was written by
213 .An Dag-Erling Sm\(/orgrav Aq des@FreeBSD.org .
214 Changes for UMA by
215 .An Jeroen Ruigrok van der Werven Aq asmodai@FreeBSD.org .