UNR(9)
πΈ UNR(9) β the kernel unit number allocator, bestie edition πΈ
Kernel Developer's Manual β¨ okay so this is the glow-up of the UNR(9) page and we are gonna make unit numbers make sense, fr fr.
β¨ NAME β¨
new_unrhdr, clear_unrhdr, delete_unrhdr, alloc_unr, alloc_unr_specific, free_unr β the 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 that lets you allocate unit numbers inside a range you pick. It's giving "hand me a number, any number, but likeβ¦ within bounds" π
new_unrhdr(low, high, mutex)β spin up a brand new unit number allocator entity, bestie β¨ Thelowandhigharguments say what the minimum and maximum number of unit numbers is. Here's the tea: there's no cost associated with the range of unit numbers, so unless the resource is actually finite, you can just useINT_MAX. Ifmutexis notNULL, it gets used for locking when allocating and freeing units. If it isNULL, the internal mutex gets used instead. Either way, covered π«Άclear_unrhdr(uh)β wipe every unit out of the unit number allocator entity you point at. This resets the whole thing as if it had just been initialized withnew_unrhdr(). Fresh start energy πdelete_unrhdr(uh)β delete that unit number allocator entity. Heads up though π this frees the memory associated with the entity β it does not free any units. If you wanna free all the units, that's aclear_unrhdr()job, not this one. Don't mix them up, iykyk.alloc_unr(uh)β hand back a new unit number. The lowest free number is always allocated, no cap. This function does not allocate memory and never sleeps β but it may block on a mutex, so keep that in mind. And if there are no free unit numbers left? You get-1. Sad but true πalloc_unrl(uh)β literally the same asalloc_unr()except the mutex is assumed to already be locked, so it isn't used. That's the whole difference, periodt.alloc_unr_specific(uh, item)β allocate one specific unit number, your chosen one π This function allocates memory and thus may sleep, so plan accordingly. On success you get the allocated unit number back. If the number you asked for is already allocated, or it's out of the range, you get-1πfree_unr(uh, item)β free a unit number that was previously allocated. This function may require allocating memory, and thus it can sleep π΄ Also: there is no pre-locked variant. None. Don't go looking for one.
π₯ CODE REFERENCES π₯
All the functions above live in sys/kern/subr_unit.c. That's the address, bestie π
π HISTORY π
The kernel unit number allocator first appeared in FreeBSD 6.0. Vintage but still eating π
π AUTHORS π
The kernel unit number allocator was written by Poul-Henning Kamp. This manpage was written by Gleb Smirnoff. Legends, honestly π«Ά
okay that's the whole page, no skips, no cuts, every flag and every vibe intact π
Debian Β· October 4, 2017 Β· UNR(9) β mwah, that's the sign-off bestie, ty for reading, go allocate responsibly β¨ππ¦
π¦ where this page comes from
- debian bookworm: freebsd-manpages 12.2-1 Β· BSD-2-Clause-FreeBSD AND BSD-4-Clause
- debian trixie: freebsd-manpages 12.2-2 Β· BSD-2-Clause-FreeBSD AND BSD-4-Clause
- ubuntu jammy: freebsd-manpages 12.2-1 Β· BSD-2-Clause-FreeBSD AND BSD-4-Clause
- ubuntu noble: freebsd-manpages 12.2-1 Β· BSD-2-Clause-FreeBSD AND BSD-4-Clause
- ubuntu questing: freebsd-manpages 12.2-2
- ubuntu resolute: freebsd-manpages 12.2-2build1 Β· BSD-2-Clause-FreeBSD AND BSD-4-Clause
- ubuntu stonking: freebsd-manpages 12.2-2build1 Β· BSD-2-Clause-FreeBSD AND BSD-4-Clause
- Copyright (c) 2005 Gleb Smirnoff <glebius@FreeBSD.org>
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms