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