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