---
name: "uma"
section: "9"
platform: "linux"
package: "freebsd-manpages"
version: "6.0-5"
release: "Debian 4.0 (Etch)"
platforms: ["linux", "freebsd"]
url: "https://new.carta.tech/uma"
---

# uma(9)

zone allocator

## NAME

**uma\_zcreate**, **uma\_zalloc**, **uma\_zfree**, **uma\_zdestroy**, **uma\_zone\_set\_max** – zone allocator

## SYNOPSIS

**#include \<sys/param.h\>**

**#include \<sys/queue.h\>**

**#include \<vm/uma.h\>**

*uma\_zone\_t*\

**uma\_zcreate**(*char \*name*, *int size*, *uma\_ctor ctor*, *uma\_dtor dtor*, *uma\_init uminit*, *uma\_fini fini*, *int align*, *u\_int16\_t flags*);

*void \**\

**uma\_zalloc**(*uma\_zone\_t zone*, *int flags*);

*void*\

**uma\_zfree**(*uma\_zone\_t zone*, *void \*item*);

*void*\

**uma\_zdestroy**(*uma\_zone\_t zone*);

*void*\

**uma\_zone\_set\_max**(*uma\_zone\_t zone*, *int nitems*);

## DESCRIPTION

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.

The **uma\_zcreate**() function creates a new zone from which items may then be allocated from. The *name* argument is a text name of the zone for debugging and stats; this memory should not be freed until the zone has been deallocated.

The *ctor* and *dtor* arguments are callback functions that are called by the uma subsystem at the time of the call to **uma\_zalloc**() and **uma\_zfree**() respectively. Their purpose is to provide hooks for initializing or destroying things that need to be done at the time of the allocation or release of a resource. A good usage for the *ctor* and *dtor* callbacks might be to adjust a global count of the number of objects allocated.

The *uminit* and *fini* arguments are used to optimize the allocation of objects from the zone. They are called by the uma subsystem whenever it needs to allocate or free several items to satisfy requests or memory pressure. A good use for the *uminit* and *fini* callbacks might be to initialize and destroy mutexes contained within the object. This would allow one to re-use already initialized mutexes when an object is returned from the uma subsystem's object cache. They are not called on each call to **uma\_zalloc**() and **uma\_zfree**() but rather in a batch mode on several objects.

To allocate an item from a zone, simply call **uma\_zalloc**() with a pointer to that zone and set the *flags* argument to selected flags as documented in [malloc(9)](/malloc/9.md). It will return a pointer to an item if successful, or NULL in the rare case where all items in the zone are in use and the allocator is unable to grow the zone or when M\_NOWAIT is specified.

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

Created zones, which are empty, can be destroyed using **uma\_zdestroy**(), freeing all memory that was allocated for the zone. All items allocated from the zone with **uma\_zalloc**() must have been freed with **uma\_zfree**() before.

The purpose of **uma\_zone\_set\_max**() is to limit the maximum amount of memory that the system can dedicated toward the zone specified by the *zone* argument. The *nitems* argument gives the upper limit of items in the zone. This limits the total number of items in the zone which includes: allocated items, free items and free items in the per-cpu caches. On systems with more than one CPU it may not be possible to allocate the specified number of items even when there is no shortage of memory, because all of the remaining free items may be in the caches of the other CPUs when the limit is hit.

## RETURN VALUES

The **uma\_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.

## SEE ALSO

[malloc(9)](/malloc/9.md)

## HISTORY

The zone allocator first appeared in FreeBSD 3.0. It was radically changed in FreeBSD 5.0 to function as a slab allocator.

## AUTHORS

The zone allocator was written by John S. Dyson. The zone allocator was rewritten in large parts by Jeff Roberson ⟨jeff@FreeBSD.org⟩ to function as a slab allocator.

This manual page was written by Dag-Erling Smørgrav ⟨des@FreeBSD.org⟩. Changes for UMA by Jeroen Ruigrok van der Werven ⟨asmodai@FreeBSD.org⟩.
