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