UNR(9)
π UNR(9) β Kernel Developerβs Manual π
okay so bestie, this is the UNR(9) page, and itβs literally all about the kernel unit number allocator β the lil system that hands out unit numbers inside a range. iconic behavior, no cap. π
πΈ NAME πΈ
new_unrhdr, clean_unrhdr, clear_unrhdr, delete_unrhdr, alloc_unr, alloc_unr_specific, free_unr, create_iter_unr, next_iter_unr, free_iter_unr β kernel unit number allocator. π
β¨ SYNOPSIS β¨
#include <sys/systm.h>
struct unrhdr *
new_unrhdr(int low, int high, struct mtx *mutex);
void
clean_unrhdr(struct unrhdr *uh);
void
clean_unrhdrl(struct unrhdr *uh);
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);
void *
create_iter_unr(struct unrhdr *uh);
int
next_iter_unr(void *handle);
void
free_iter_unr(void *handle);
π¦ DESCRIPTION π¦
The kernel unit number allocator is a generic facility, which allows you to allocate unit numbers within a specified range. itβs giving βtiny number bouncer for kernel resourcesβ and honestly? obsessed. π«Ά
π new_unrhdr(low, high, mutex) π
Initialize a new unit number allocator entity. The low and high arguments specify the 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. bestie, that range is lowkey huge and thatβs fine. π
If mutex is not NULL, it is used for locking when allocating and freeing units. If the passed value is the token UNR_NO_MTX, then no locking is applied internally. Otherwise, an 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(). clean slate, fresh vibe, periodt. β¨
π₯ delete_unrhdr(uh) π₯
Delete the 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(). important distinction, donβt get it twisted. π
π clean_unrhdr(uh) π
Freeing unit numbers might result in some internal memory becoming unused. There are unit allocator consumers that cannot tolerate taking malloc(9) locks to free the memory, while having their unit mutex locked. For this reason, free of the unused memory after delete is postponed until the consumer can afford calling into the malloc(9) subsystem.
Call clean_unrhdr(uh) to do the cleanup. In particular, this needs to be done before freeing a unr, if a deletion of units could have been performed. π§
this one is the βplease tidy up before you dipβ function, okay? donβt skip it if it applies.
π¦ clean_unrhdrl() π¦
Same as clean_unrhdr(), but assumes that the unr mutex is already owned, if any. iykyk. π
β 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. no free numbers? tragic. π
π alloc_unrl(uh) π
Same as alloc_unr() except that the mutex is assumed to be already locked and thus is not used. pre-locked energy. π
π« 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. so close, yet so far. π
πΈ 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. noted, bestie. π
π§ ITERATOR INTERFACE π§
The unr facility provides an interface to iterate over all allocated units for the given unrhdr. Iterators are identified by an opaque handle. More than one iterators can operate simultaneously; the iterator position data is recorded only in the iterator handle. cute, independent, main character energy. π
Consumers must ensure that the unit allocator is not modified between calls to the iterator functions. In particular, the internal allocator mutex cannot provide consistency, because it is acquired and dropped inside thenext_iter_unr()function. If the allocator was modified, it is safe to free the iterator with thefree_iter_unr()method nevertheless. π«Ά
π create_iter_unr(uh) π
Create an iterator. Return the handle that should be passed to other iterator functions. β¨
π next_iter_unr(handle) π
Return the value of the next unit. Units are returned in ascending order. A return value of -1 indicates the end of iteration, in which case -1 is returned for all future calls. once itβs over, itβs over. π
π₯ free_iter_unr(handle) π₯
Free the iterator, handle is no longer valid. bye bestie, handle has left the chat. π
π CODE REFERENCES π
The above functions are implemented in sys/kern/subr_unit.c. thatβs the source, bestie. go look if you need the receipts. π
π HISTORY π
Kernel unit number allocator first appeared in FreeBSD 6.0. vintage but still slaying. β
π AUTHORS π
Kernel unit number allocator was written by Poul-Henning Kamp. This manpage was written by Gleb Smirnoff. legends, fr fr. π
and thatβs the page, bestie β this version is from Debian, dated April 21, 2022, for UNR(9). slay, bye! π β¨
π¦ where this page comes from
- freebsd 14.5: base 14.5
- freebsd 15.1: base 15.1
- Copyright (c) 2005 Gleb Smirnoff <glebius@FreeBSD.org>
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms