man.bsd.lv manual page server

Manual Page Search Parameters

ZONE(9) Kernel Developer's Manual ZONE(9)

zbootinit, zinitna, zinit, zdestroy, zalloc, zfreezone allocator

#include <sys/param.h>
#include <sys/queue.h>
#include <vm/vm_zone.h>

void
zbootinit(vm_zone_t z, char *name, size_t size, void *item, long nitems);

int
zinitna(vm_zone_t z, char *name, size_t size, long nentries, uint32_t flags);

vm_zone_t
zinit(char *name, size_t size, long nentries, uint32_t flags);

void
zdestroy(vm_zone_t z);

void *
zalloc(vm_zone_t z);

void
zfree(vm_zone_t z, void *item);

The zone allocator is deprecated. Use <sys/objcache.h> for new developments.

The zone allocator provides an efficient interface for managing dynamically-sized collections of items of similar size. The zone allocator can work with preallocated zones as well as with runtime-allocated ones, and is therefore available much earlier in the boot process than other memory management routines.

A zone is an extensible collection of items of identical size. The zone allocator keeps track of which items are in use and which are not, and provides functions for allocating items from the zone and for releasing them back (which makes them available for later use).

The zone allocator stores state information inside the items proper while they are not allocated, so structures that will be managed by the zone allocator and wish to use the type stable property of zones by leaving some fields pre-filled between allocations, must reserve two pointers at the very beginning for internal use by the zone allocator, as follows:

struct my_item {
        struct my_item  *z_rsvd1;
        struct my_item  *z_rsvd2;
        /* rest of structure */
};

Alternatively they should assume those entries corrupted after each allocation. After the first allocation of an item, it will have been cleared to zeroes, however subsequent allocations will retain the contents as of the last free, with the exception of the fields mentioned above.

Zones are created in one of two fashions, depending how far along the boot process is.

If the VM system is fully initialized, a dynamically allocated zone can be created using (). The name argument should be a pointer to a short, descriptive name for the zone; it is used for statistics and debugging purposes. The size and nentries are the size of the items held by the zone and the initial size (in items) of the zone, respectively. The flags argument should have the ZONE_INTERRUPT bit set if there is a chance that items may be allocated from the zone in interrupt context; note that in this case, the zone will never grow larger than nentries items. The flags argument should have the ZONE_DESTROYABLE bit set if the zone is to be destroyed with zdestroy().

If the VM system is not yet fully initialized, the zone allocator cannot dynamically allocate VM pages from which to dole out items, so the caller needs to provide a static pool of items. In this case, the initialization is done in two stages: first, () is called before first use of the zone; later, when the VM system is up, the initialization of the zone is completed by calling zinitna().

The first argument to () is a pointer to a static struct vm_zone to initialize. The second and third are the name of the zone and the size of the items it will hold. The fourth argument is a pointer to a static array of items from which the zone allocator will draw until the zone is fully initialized. The nitems argument is the number of items in the array.

The arguments to () are the same as for zinit(), with the addition of a pointer to the zone to initialize.

To release all the memory allocated for a zone, call (). Only zones created with zinit() and with the ZONE_DESTROYABLE flag can be destroyed.

To allocate an item from a zone, simply call () with a pointer to that zone; it will return a pointer to an item, or NULL in the rare case where all items in the zone are in use and the allocator is unable to grow the zone.

Items are released back to the zone from which they were allocated by calling () with a pointer to the zone and a pointer to the item.

The zinitna() function returns 1 on success and 0 on failure; the only failure case is inability to preallocate address space for an interrupt-safe zone.

The zinit() function returns a pointer to a fully initialized struct vm_zone, or NULL if it was unable to kmalloc() a struct vm_zone or the ZONE_INTERRUPT flag was specified and zinitna() failed to preallocate address space.

The zalloc() function returns a pointer to an item, or NULL if the zone ran out of unused items and the allocator was unable to enlarge it.

memory(9)

The zone allocator first appeared in FreeBSD 3.0.

The zone allocator was written by John S. Dyson.

This manual page was written by Dag-Erling Coïdan Smørgrav <des@FreeBSD.org>.

April 6, 2018 DragonFly-5.6.1