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