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