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