| UNR(9) | MidnightBSD Kernel Developer's Manual | UNR(9) |
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
#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);
The kernel unit number allocator is a generic facility, which allows to allocate unit numbers within a specified range.
new_unrhdr(low,
high, mutex)INT_MAX can be used. 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, internal mutex is used.clear_unrhdr(uh)new_unrhdr().delete_unrhdr(uh)clear_unrhdr().clean_unrhdr(uh)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.clean_unrhdrl()clean_unrhdr(), but assumes that the unr
mutex is already owned, if any.alloc_unr(uh)-1
is returned.alloc_unrl(uh)alloc_unr() except that mutex is assumed
to be already locked and thus is not used.alloc_unr_specific(uh,
item)-1 is returned.free_unr(uh,
item)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.
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 the
next_iter_unr()
function. If the allocator was modified, it is safe to free the iterator
with
free_iter_unr()
method nevertheless.
create_iter_unr(uh)next_iter_unr(handle)-1 indicates the end of
iteration, in which case -1 is returned for all
future calls.free_iter_unr(handle)The above functions are implemented in sys/kern/subr_unit.c.
Kernel unit number allocator first appeared in FreeBSD 6.0.
Kernel unit number allocator was written by Poul-Henning Kamp. This manpage was written by Gleb Smirnoff.
| April 21, 2022 | midnightbsd-4.0 |