xref: /freebsd/share/man/man9/zone.9 (revision 0fddbf874719b9bd50cf66ac26d1140bb3f2be69)
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 January 27, 2001
29.Dt ZONE 9
30.Os
31.Sh NAME
32.Nm zbootinit ,
33.Nm zinitna ,
34.Nm zinit ,
35.Nm zalloc ,
36.Nm zfree ,
37.Nm zdestroy
38.Nd zone allocator
39.Sh SYNOPSIS
40.Fd #include <sys/param.h>
41.Fd #include <vm/vm_zone.h>
42.Ft void
43.Fn zbootinit "vm_zone_t z" "char *name" "int size" "void *item" "int nitems"
44.Ft int
45.Fn zinitna "vm_zone_t z" "struct vm_object *obj" "char *name" "int size" "int nentries" "int flags" "int zalloc"
46.Ft vm_zone_t
47.Fn zinit "char *name" "int size" "int nentries" "int flags" "int zalloc"
48.Ft void *
49.Fn zalloc "vm_zone_t z"
50.Ft void
51.Fn zfree "vm_zone_t z" "void *item"
52.Ft void
53.Fn zdestroy "vm_zone_t z"
54.Sh DESCRIPTION
55The zone allocator provides an efficient interface for managing
56dynamically-sized collections of items of similar size.
57The zone allocator can work with preallocated zones as well as with
58runtime-allocated ones, and is therefore available much earlier in the
59boot process than other memory management routines.
60.Pp
61A zone is an extensible collection of items of identical size.
62The zone allocator keeps track of which items are in use and which
63aren't, and provides functions for allocating items from the zone and
64for releasing them back (which makes them available for later use).
65.Pp
66The zone allocator stores state information inside the items proper,
67so structures that will be managed by the zone allocator must reserve
68two pointers at the very beginning for internal use by the zone
69allocator, as follows:
70.Bd -literal
71struct my_item {
72        struct my_item  *z_rsvd1;
73        struct my_item  *z_rsvd2;
74        /* rest of structure */
75};
76.Ed
77.Pp
78Zones are created in one of two fashions, depending how far along the
79boot process is.
80.Pp
81If the VM system is fully initialized, a dynamically allocated zone can
82be created using
83.Fn zinit .
84The
85.Fa name
86argument should be a pointer to a short, descriptive name for the
87zone; it is used for statistics and debugging purposes.
88The
89.Fa size
90and
91.Fa nentries
92are the size of the items held by the zone and the initial size (in
93items) of the zone, respectively.
94The
95.Fa flags
96argument should be set to
97.Dv ZONE_INTERRUPT
98if there is a chance that items may be allocated from the zone in
99interrupt context; note that in this case, the zone will never grow
100larger than
101.Fa nentries
102items.
103In all other cases,
104.Fa flags
105should be set to 0.
106The final argument,
107.Fa zalloc ,
108indicates the number of VM pages by which the zone should grow every
109time it fills up.
110.Pp
111If the VM system is not yet fully initialized, the zone allocator
112cannot dynamically allocate VM pages from which to dole out items, so
113the caller needs to provide a static pool of items.
114In this case, the initialization is done in two stages: first,
115.Fn zbootinit
116is called before first use of the zone; later, when the VM system is
117up, the initialization of the zone is completed by calling
118.Fn zinitna .
119.Pp
120The first argument to
121.Fn zbootinit
122is a pointer to a static
123.Vt "struct vm_zone"
124to initialize.
125The second and third are the name of the zone and the size of the
126items it will hold.
127The fourth argument is a pointer to a static array of items from which
128the zone allocator will draw until the zone is fully initialized.
129The
130.Fa nitems
131argument is the number of items in the array.
132.Pp
133The arguments to
134.Fa zinitna
135are the same as for
136.Fa zinit ,
137with the addition of a pointer to the zone to initialize, and a
138pointer to a
139.Vt "struct vm_object"
140from which to allocate pages in the
141.Dv ZONE_INTERRUPT
142case.
143.Pp
144To allocate an item from a zone, simply call
145.Fn zalloc
146with a pointer to that zone; it will return a pointer to an item, or
147.Dv NULL
148in the rare case where all items in the zone are in use and the
149allocator is unable to grow the zone.
150.Pp
151Items are released back to the zone from which they were allocated by
152calling
153.Fn zfree
154with a pointer to the zone and a pointer to the item.
155.Pp
156Zones created with
157.Fn zinit
158or
159.Fn zinitna
160can be destroyed using
161.Fn zdestroy ,
162freeing all memory that was allocated for the zone.
163All items allocated from the zone with
164.Fn zalloc
165must have been freed with
166.Fn zfree
167before.
168.Sh RETURN VALUES
169The
170.Fn zinitna
171function returns 1 on success and 0 on failure; the only failure case
172is inability to preallocate address space for an interrupt-safe zone.
173.Pp
174The
175.Fn zinit
176function returns a pointer to a fully initialized
177.Vt "struct vm_zone" ,
178or
179.Dv NULL
180if it was unable to
181.Fn malloc
182a
183.Vt "struct vm_zone"
184or the
185.Dv ZONE_INTERRUPT
186flag was specified and
187.Fn zinitna
188failed to preallocate address space.
189.Pp
190The
191.Fn zalloc
192function returns a pointer to an item, or
193.Dv NULL
194if the zone ran out of unused items and the allocator was unable to
195enlarge it.
196.Sh SEE ALSO
197.Xr malloc 9
198.Sh HISTORY
199The zone allocator first appeared in
200.Fx 3.0 .
201.Sh AUTHORS
202.An -nosplit
203The zone allocator was written by
204.An John S. Dyson .
205.Pp
206This manual page was written by
207.An Dag-Erling Co\(:idan Sm\(/orgrav Aq des@FreeBSD.org .
208