---
name: "alloc_unrl"
section: "9freebsd"
platform: "linux"
package: "freebsd-manpages"
version: "12.2-2"
release: "Debian 13 (Trixie)"
platforms: ["linux"]
url: "https://new.carta.tech/alloc_unr/9freebsd"
---

# alloc\_unrl(9freebsd)

kernel unit number allocator

Part of [alloc\_unr(9freebsd)](/alloc_unr/9freebsd.md).

## NAME

**new\_unrhdr**, **clear\_unrhdr**, **delete\_unrhdr**, **alloc\_unr**, **alloc\_unr\_specific**, **free\_unr** – kernel unit number allocator

## SYNOPSIS

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

*struct unrhdr \**\

**new\_unrhdr**(*int low*, *int high*, *struct mtx \*mutex*);

*void*\

**clear\_unrhdr**(*struct unrhdr \*uh*);

*void*\

**delete\_unrhdr**(*struct unrhdr \*uh*);

*int*\

**alloc\_unr**(*struct unrhdr \*uh*);

*int*\

**alloc\_unrl**(*struct unrhdr \*uh*);

*int*\

**alloc\_unr\_specific**(*struct unrhdr \*uh*, *u\_int item*);

*void*\

**free\_unr**(*struct unrhdr \*uh*, *u\_int item*);

## DESCRIPTION

The kernel unit number allocator is a generic facility, which allows to allocate unit numbers within a specified range.

- **new\_unrhdr**(*low*, *high*, *mutex*)

  Initialize a new unit number allocator entity. The *low* and *high* arguments specify minimum and maximum number of unit numbers. There is no cost associated with the range of unit numbers, so unless the resource really is finite, INT\_MAX can be used. If *mutex* is not NULL, it is used for locking when allocating and freeing units. Otherwise, internal mutex is used.

- **clear\_unrhdr**(*uh*)

  Clear all units from the specified unit number allocator entity. This function resets the entity as if it were just initialized with **new\_unrhdr**().

- **delete\_unrhdr**(*uh*)

  Delete specified unit number allocator entity. This function frees the memory associated with the entity, it does not free any units. To free all units use **clear\_unrhdr**().

- **alloc\_unr**(*uh*)

  Return a new unit number. The lowest free number is always allocated. This function does not allocate memory and never sleeps, however it may block on a mutex. If no free unit numbers are left, -1 is returned.

- **alloc\_unrl**(*uh*)

  Same as **alloc\_unr**() except that mutex is assumed to be already locked and thus is not used.

- **alloc\_unr\_specific**(*uh*, *item*)

  Allocate a specific unit number. This function allocates memory and thus may sleep. The allocated unit number is returned on success. If the specified number is already allocated or out of the range, -1 is returned.

- **free\_unr**(*uh*, *item*)

  Free a previously allocated unit number. This function may require allocating memory, and thus it can sleep. There is no pre-locked variant.

## CODE REFERENCES

The above functions are implemented in *sys/kern/subr\_unit.c*.

## HISTORY

Kernel unit number allocator first appeared in FreeBSD 6.0.

## AUTHORS

Kernel unit number allocator was written by Poul-Henning Kamp. This manpage was written by Gleb Smirnoff.
