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: head/share/man/man9/zone.9 179880 2008-06-19 18:33:38Z remko $
27.\"
28.Dd June 19, 2008
29.Dt ZONE 9
30.Os
31.Sh NAME
32.Nm uma_zcreate ,
33.Nm uma_zalloc ,
34.Nm uma_zalloc_arg ,
35.Nm uma_zfree ,
36.Nm uma_zfree_arg ,
37.Nm uma_zdestroy ,
38.Nm uma_zone_set_max
39.Nd zone allocator
40.Sh SYNOPSIS
41.In sys/param.h
42.In sys/queue.h
43.In vm/uma.h
44.Ft uma_zone_t
45.Fo uma_zcreate
46.Fa "char *name" "int size"
47.Fa "uma_ctor ctor" "uma_dtor dtor" "uma_init uminit" "uma_fini fini"
48.Fa "int align" "u_int16_t flags"
49.Fc
50.Ft "void *"
51.Fn uma_zalloc "uma_zone_t zone" "int flags"
52.Ft "void *"
53.Fn uma_zalloc_arg "uma_zone_t zone" "void *arg" "int flags"
54.Ft void
55.Fn uma_zfree "uma_zone_t zone" "void *item"
56.Ft void
57.Fn uma_zfree_arg "uma_zone_t zone" "void *item" "void *arg"
58.Ft void
59.Fn uma_zdestroy "uma_zone_t zone"
60.Ft void
61.Fn uma_zone_set_max "uma_zone_t zone" "int nitems"
62.Sh DESCRIPTION
63The zone allocator provides an efficient interface for managing
64dynamically-sized collections of items of similar size.
65The zone allocator can work with preallocated zones as well as with
66runtime-allocated ones, and is therefore available much earlier in the
67boot process than other memory management routines.
68.Pp
69A zone is an extensible collection of items of identical size.
70The zone allocator keeps track of which items are in use and which
71are not, and provides functions for allocating items from the zone and
72for releasing them back (which makes them available for later use).
73.Pp
74After the first allocation of an item,
75it will have been cleared to zeroes, however subsequent allocations
76will retain the contents as of the last free.
77.Pp
78The
79.Fn uma_zcreate
80function creates a new zone from which items may then be allocated from.
81The
82.Fa name
83argument is a text name of the zone for debugging and stats; this memory
84should not be freed until the zone has been deallocated.
85.Pp
86The
87.Fa ctor
88and
89.Fa dtor
90arguments are callback functions that are called by
91the uma subsystem at the time of the call to
92.Fn uma_zalloc
93and
94.Fn uma_zfree
95respectively.
96Their purpose is to provide hooks for initializing or
97destroying things that need to be done at the time of the allocation
98or release of a resource.
99A good usage for the
100.Fa ctor
101and
102.Fa dtor
103callbacks
104might be to adjust a global count of the number of objects allocated.
105.Pp
106The
107.Fa uminit
108and
109.Fa fini
110arguments are used to optimize the allocation of
111objects from the zone.
112They are called by the uma subsystem whenever
113it needs to allocate or free several items to satisfy requests or memory
114pressure.
115A good use for the
116.Fa uminit
117and
118.Fa fini
119callbacks might be to
120initialize and destroy mutexes contained within the object.
121This would
122allow one to re-use already initialized mutexes when an object is returned
123from the uma subsystem's object cache.
124They are not called on each call to
125.Fn uma_zalloc
126and
127.Fn uma_zfree
128but rather in a batch mode on several objects.
129.Pp
130To allocate an item from a zone, simply call
131.Fn uma_zalloc
132with a pointer to that zone
133and set the
134.Fa flags
135argument to selected flags as documented in
136.Xr malloc 9 .
137It will return a pointer to an item if successful,
138or
139.Dv NULL
140in the rare case where all items in the zone are in use and the
141allocator is unable to grow the zone
142or when
143.Dv M_NOWAIT
144is specified.
145.Pp
146Items are released back to the zone from which they were allocated by
147calling
148.Fn uma_zfree
149with a pointer to the zone and a pointer to the item.
150.Pp
151The variations
152.Fn uma_zalloc_arg
153and
154.Fn uma_zfree_arg
155allow to
156specify an argument for the
157.Dv ctor
158and
159.Dv dtor
160functions, respectively.
161.Pp
162Created zones,
163which are empty,
164can be destroyed using
165.Fn uma_zdestroy ,
166freeing all memory that was allocated for the zone.
167All items allocated from the zone with
168.Fn uma_zalloc
169must have been freed with
170.Fn uma_zfree
171before.
172.Pp
173The purpose of
174.Fn uma_zone_set_max
175is to limit the maximum amount of memory that the system can dedicated
176toward the zone specified by the
177.Fa zone
178argument.
179The
180.Fa nitems
181argument gives the upper limit of items in the zone.
182This limits the total number of items in the zone which includes:
183allocated items, free items and free items in the per-cpu caches.
184On systems with more than one CPU it may not be possible to allocate
185the specified number of items even when there is no shortage of memory,
186because all of the remaining free items may be in the caches of the
187other CPUs when the limit is hit.
188.Sh RETURN VALUES
189The
190.Fn uma_zalloc
191function returns a pointer to an item, or
192.Dv NULL
193if the zone ran out of unused items and the allocator was unable to
194enlarge it.
195.Sh SEE ALSO
196.Xr malloc 9
197.Sh HISTORY
198The zone allocator first appeared in
199.Fx 3.0 .
200It was radically changed in
201.Fx 5.0
202to function as a slab allocator.
203.Sh AUTHORS
204.An -nosplit
205The zone allocator was written by
206.An John S. Dyson .
207The zone allocator was rewritten in large parts by
208.An Jeff Roberson Aq jeff@FreeBSD.org
209to function as a slab allocator.
210.Pp
211This manual page was written by
212.An Dag-Erling Sm\(/orgrav Aq des@FreeBSD.org .
213Changes for UMA by
214.An Jeroen Ruigrok van der Werven Aq asmodai@FreeBSD.org .
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: head/share/man/man9/zone.9 179880 2008-06-19 18:33:38Z remko $
27.\"
28.Dd June 19, 2008
29.Dt ZONE 9
30.Os
31.Sh NAME
32.Nm uma_zcreate ,
33.Nm uma_zalloc ,
34.Nm uma_zalloc_arg ,
35.Nm uma_zfree ,
36.Nm uma_zfree_arg ,
37.Nm uma_zdestroy ,
38.Nm uma_zone_set_max
39.Nd zone allocator
40.Sh SYNOPSIS
41.In sys/param.h
42.In sys/queue.h
43.In vm/uma.h
44.Ft uma_zone_t
45.Fo uma_zcreate
46.Fa "char *name" "int size"
47.Fa "uma_ctor ctor" "uma_dtor dtor" "uma_init uminit" "uma_fini fini"
48.Fa "int align" "u_int16_t flags"
49.Fc
50.Ft "void *"
51.Fn uma_zalloc "uma_zone_t zone" "int flags"
52.Ft "void *"
53.Fn uma_zalloc_arg "uma_zone_t zone" "void *arg" "int flags"
54.Ft void
55.Fn uma_zfree "uma_zone_t zone" "void *item"
56.Ft void
57.Fn uma_zfree_arg "uma_zone_t zone" "void *item" "void *arg"
58.Ft void
59.Fn uma_zdestroy "uma_zone_t zone"
60.Ft void
61.Fn uma_zone_set_max "uma_zone_t zone" "int nitems"
62.Sh DESCRIPTION
63The zone allocator provides an efficient interface for managing
64dynamically-sized collections of items of similar size.
65The zone allocator can work with preallocated zones as well as with
66runtime-allocated ones, and is therefore available much earlier in the
67boot process than other memory management routines.
68.Pp
69A zone is an extensible collection of items of identical size.
70The zone allocator keeps track of which items are in use and which
71are not, and provides functions for allocating items from the zone and
72for releasing them back (which makes them available for later use).
73.Pp
74After the first allocation of an item,
75it will have been cleared to zeroes, however subsequent allocations
76will retain the contents as of the last free.
77.Pp
78The
79.Fn uma_zcreate
80function creates a new zone from which items may then be allocated from.
81The
82.Fa name
83argument is a text name of the zone for debugging and stats; this memory
84should not be freed until the zone has been deallocated.
85.Pp
86The
87.Fa ctor
88and
89.Fa dtor
90arguments are callback functions that are called by
91the uma subsystem at the time of the call to
92.Fn uma_zalloc
93and
94.Fn uma_zfree
95respectively.
96Their purpose is to provide hooks for initializing or
97destroying things that need to be done at the time of the allocation
98or release of a resource.
99A good usage for the
100.Fa ctor
101and
102.Fa dtor
103callbacks
104might be to adjust a global count of the number of objects allocated.
105.Pp
106The
107.Fa uminit
108and
109.Fa fini
110arguments are used to optimize the allocation of
111objects from the zone.
112They are called by the uma subsystem whenever
113it needs to allocate or free several items to satisfy requests or memory
114pressure.
115A good use for the
116.Fa uminit
117and
118.Fa fini
119callbacks might be to
120initialize and destroy mutexes contained within the object.
121This would
122allow one to re-use already initialized mutexes when an object is returned
123from the uma subsystem's object cache.
124They are not called on each call to
125.Fn uma_zalloc
126and
127.Fn uma_zfree
128but rather in a batch mode on several objects.
129.Pp
130To allocate an item from a zone, simply call
131.Fn uma_zalloc
132with a pointer to that zone
133and set the
134.Fa flags
135argument to selected flags as documented in
136.Xr malloc 9 .
137It will return a pointer to an item if successful,
138or
139.Dv NULL
140in the rare case where all items in the zone are in use and the
141allocator is unable to grow the zone
142or when
143.Dv M_NOWAIT
144is specified.
145.Pp
146Items are released back to the zone from which they were allocated by
147calling
148.Fn uma_zfree
149with a pointer to the zone and a pointer to the item.
150.Pp
151The variations
152.Fn uma_zalloc_arg
153and
154.Fn uma_zfree_arg
155allow to
156specify an argument for the
157.Dv ctor
158and
159.Dv dtor
160functions, respectively.
161.Pp
162Created zones,
163which are empty,
164can be destroyed using
165.Fn uma_zdestroy ,
166freeing all memory that was allocated for the zone.
167All items allocated from the zone with
168.Fn uma_zalloc
169must have been freed with
170.Fn uma_zfree
171before.
172.Pp
173The purpose of
174.Fn uma_zone_set_max
175is to limit the maximum amount of memory that the system can dedicated
176toward the zone specified by the
177.Fa zone
178argument.
179The
180.Fa nitems
181argument gives the upper limit of items in the zone.
182This limits the total number of items in the zone which includes:
183allocated items, free items and free items in the per-cpu caches.
184On systems with more than one CPU it may not be possible to allocate
185the specified number of items even when there is no shortage of memory,
186because all of the remaining free items may be in the caches of the
187other CPUs when the limit is hit.
188.Sh RETURN VALUES
189The
190.Fn uma_zalloc
191function returns a pointer to an item, or
192.Dv NULL
193if the zone ran out of unused items and the allocator was unable to
194enlarge it.
195.Sh SEE ALSO
196.Xr malloc 9
197.Sh HISTORY
198The zone allocator first appeared in
199.Fx 3.0 .
200It was radically changed in
201.Fx 5.0
202to function as a slab allocator.
203.Sh AUTHORS
204.An -nosplit
205The zone allocator was written by
206.An John S. Dyson .
207The zone allocator was rewritten in large parts by
208.An Jeff Roberson Aq jeff@FreeBSD.org
209to function as a slab allocator.
210.Pp
211This manual page was written by
212.An Dag-Erling Sm\(/orgrav Aq des@FreeBSD.org .
213Changes for UMA by
214.An Jeroen Ruigrok van der Werven Aq asmodai@FreeBSD.org .