xref: /freebsd/share/man/man9/unr.9 (revision fa9896e082a1046ff4fbc75fcba4d18d1f2efc19)
15d80bf64SDag-Erling Smørgrav.\" Copyright (c) 2005 Gleb Smirnoff <glebius@FreeBSD.org>
25d80bf64SDag-Erling Smørgrav.\" All rights reserved.
35d80bf64SDag-Erling Smørgrav.\"
45d80bf64SDag-Erling Smørgrav.\" Redistribution and use in source and binary forms, with or without
55d80bf64SDag-Erling Smørgrav.\" modification, are permitted provided that the following conditions
65d80bf64SDag-Erling Smørgrav.\" are met:
75d80bf64SDag-Erling Smørgrav.\" 1. Redistributions of source code must retain the above copyright
85d80bf64SDag-Erling Smørgrav.\"    notice, this list of conditions and the following disclaimer.
95d80bf64SDag-Erling Smørgrav.\" 2. Redistributions in binary form must reproduce the above copyright
105d80bf64SDag-Erling Smørgrav.\"    notice, this list of conditions and the following disclaimer in the
115d80bf64SDag-Erling Smørgrav.\"    documentation and/or other materials provided with the distribution.
125d80bf64SDag-Erling Smørgrav.\"
135d80bf64SDag-Erling Smørgrav.\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
145d80bf64SDag-Erling Smørgrav.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
155d80bf64SDag-Erling Smørgrav.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
165d80bf64SDag-Erling Smørgrav.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
175d80bf64SDag-Erling Smørgrav.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
185d80bf64SDag-Erling Smørgrav.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
195d80bf64SDag-Erling Smørgrav.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
205d80bf64SDag-Erling Smørgrav.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
215d80bf64SDag-Erling Smørgrav.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
225d80bf64SDag-Erling Smørgrav.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
235d80bf64SDag-Erling Smørgrav.\" SUCH DAMAGE.
245d80bf64SDag-Erling Smørgrav.\"
25e59b940dSKonstantin Belousov.Dd April 21, 2022
265d80bf64SDag-Erling Smørgrav.Dt UNR 9
275d80bf64SDag-Erling Smørgrav.Os
285d80bf64SDag-Erling Smørgrav.Sh NAME
295d80bf64SDag-Erling Smørgrav.Nm new_unrhdr ,
30f8c37080SKonstantin Belousov.Nm clean_unrhdr ,
31333dcaa4SMatt Joras.Nm clear_unrhdr ,
325d80bf64SDag-Erling Smørgrav.Nm delete_unrhdr ,
335d80bf64SDag-Erling Smørgrav.Nm alloc_unr ,
345d80bf64SDag-Erling Smørgrav.Nm alloc_unr_specific ,
35*ea95173dSKonstantin Belousov.Nm free_unr ,
36*ea95173dSKonstantin Belousov.Nm create_iter_unr ,
37*ea95173dSKonstantin Belousov.Nm next_iter_unr ,
38*ea95173dSKonstantin Belousov.Nm free_iter_unr
395d80bf64SDag-Erling Smørgrav.Nd "kernel unit number allocator"
405d80bf64SDag-Erling Smørgrav.Sh SYNOPSIS
415d80bf64SDag-Erling Smørgrav.In sys/systm.h
425d80bf64SDag-Erling Smørgrav.Ft "struct unrhdr *"
435d80bf64SDag-Erling Smørgrav.Fn new_unrhdr "int low" "int high" "struct mtx *mutex"
445d80bf64SDag-Erling Smørgrav.Ft void
45f8c37080SKonstantin Belousov.Fn clean_unrhdr "struct unrhdr *uh"
46f8c37080SKonstantin Belousov.Ft void
47f8c37080SKonstantin Belousov.Fn clean_unrhdrl "struct unrhdr *uh"
48f8c37080SKonstantin Belousov.Ft void
49333dcaa4SMatt Joras.Fn clear_unrhdr "struct unrhdr *uh"
50333dcaa4SMatt Joras.Ft void
515d80bf64SDag-Erling Smørgrav.Fn delete_unrhdr "struct unrhdr *uh"
525d80bf64SDag-Erling Smørgrav.Ft int
535d80bf64SDag-Erling Smørgrav.Fn alloc_unr "struct unrhdr *uh"
545d80bf64SDag-Erling Smørgrav.Ft int
555d80bf64SDag-Erling Smørgrav.Fn alloc_unrl "struct unrhdr *uh"
565d80bf64SDag-Erling Smørgrav.Ft int
575d80bf64SDag-Erling Smørgrav.Fn alloc_unr_specific "struct unrhdr *uh" "u_int item"
585d80bf64SDag-Erling Smørgrav.Ft void
595d80bf64SDag-Erling Smørgrav.Fn free_unr "struct unrhdr *uh" "u_int item"
60*ea95173dSKonstantin Belousov.Ft void *
61*ea95173dSKonstantin Belousov.Fn create_iter_unr "struct unrhdr *uh"
62*ea95173dSKonstantin Belousov.Ft int
63*ea95173dSKonstantin Belousov.Fn next_iter_unr "void *handle"
64*ea95173dSKonstantin Belousov.Ft void
65*ea95173dSKonstantin Belousov.Fn free_iter_unr "void *handle"
665d80bf64SDag-Erling Smørgrav.Sh DESCRIPTION
675d80bf64SDag-Erling SmørgravThe kernel unit number allocator is a generic facility, which allows to allocate
685d80bf64SDag-Erling Smørgravunit numbers within a specified range.
695d80bf64SDag-Erling Smørgrav.Bl -tag -width indent
705d80bf64SDag-Erling Smørgrav.It Fn new_unrhdr low high mutex
715d80bf64SDag-Erling SmørgravInitialize a new unit number allocator entity.
725d80bf64SDag-Erling SmørgravThe
735d80bf64SDag-Erling Smørgrav.Fa low
745d80bf64SDag-Erling Smørgravand
755d80bf64SDag-Erling Smørgrav.Fa high
765d80bf64SDag-Erling Smørgravarguments
775d80bf64SDag-Erling Smørgravspecify minimum and maximum number of unit numbers.
785d80bf64SDag-Erling SmørgravThere is no cost associated with the range of unit numbers, so unless the resource
795d80bf64SDag-Erling Smørgravreally is finite,
805d80bf64SDag-Erling Smørgrav.Dv INT_MAX
815d80bf64SDag-Erling Smørgravcan be used.
825d80bf64SDag-Erling SmørgravIf
835d80bf64SDag-Erling Smørgrav.Fa mutex
845d80bf64SDag-Erling Smørgravis not
855d80bf64SDag-Erling Smørgrav.Dv NULL ,
865d80bf64SDag-Erling Smørgravit is used for locking when allocating and freeing units.
87e59b940dSKonstantin BelousovIf the passed value is the token
88e59b940dSKonstantin Belousov.Va UNR_NO_MTX ,
89e59b940dSKonstantin Belousovthen no locking is applied internally.
905d80bf64SDag-Erling SmørgravOtherwise, internal mutex is used.
91333dcaa4SMatt Joras.It Fn clear_unrhdr uh
92333dcaa4SMatt JorasClear all units from the specified unit number allocator entity.
93333dcaa4SMatt JorasThis function resets the entity as if it were just initialized with
94333dcaa4SMatt Joras.Fn new_unrhdr .
955d80bf64SDag-Erling Smørgrav.It Fn delete_unrhdr uh
96333dcaa4SMatt JorasDelete specified unit number allocator entity.
97333dcaa4SMatt JorasThis function frees the memory associated with the entity, it does not free
98333dcaa4SMatt Jorasany units.
99333dcaa4SMatt JorasTo free all units use
100333dcaa4SMatt Joras.Fn clear_unrhdr .
101f8c37080SKonstantin Belousov.It Fn clean_unrhdr uh
102f8c37080SKonstantin BelousovFreeing unit numbers might result in some internal memory becoming unused.
103f8c37080SKonstantin BelousovThere are
104f8c37080SKonstantin Belousov.Nm unit
105f8c37080SKonstantin Belousovallocator consumers that cannot tolerate taking
106f8c37080SKonstantin Belousov.Xr malloc 9
107f8c37080SKonstantin Belousovlocks to free the memory, while having their unit mutex locked.
108f8c37080SKonstantin BelousovFor this reason, free of the unused memory after delete is postponed
109f8c37080SKonstantin Belousovuntil the consumer can afford calling into the
110f8c37080SKonstantin Belousov.Xr malloc 9
111f8c37080SKonstantin Belousovsubsystem.
112f8c37080SKonstantin BelousovCall
113f8c37080SKonstantin Belousov.Fn clean_unrhdr uh
114f8c37080SKonstantin Belousovto do the cleanup.
115f8c37080SKonstantin BelousovIn particular, this needs to be done before freeing a unr, if
116f8c37080SKonstantin Belousova deletion of units could have been performed.
117f8c37080SKonstantin Belousov.It Fn clean_unrhdrl
118f8c37080SKonstantin BelousovSame as
119f8c37080SKonstantin Belousov.Fn clean_unrhdr ,
120f8c37080SKonstantin Belousovbut assumes that the unr mutex is already owned, if any.
1215d80bf64SDag-Erling Smørgrav.It Fn alloc_unr uh
1225d80bf64SDag-Erling SmørgravReturn a new unit number.
1235d80bf64SDag-Erling SmørgravThe lowest free number is always allocated.
1245d80bf64SDag-Erling SmørgravThis function does not allocate memory and never sleeps, however it may
1255d80bf64SDag-Erling Smørgravblock on a mutex.
1265d80bf64SDag-Erling SmørgravIf no free unit numbers are left,
1275d80bf64SDag-Erling Smørgrav.Li \-1
1285d80bf64SDag-Erling Smørgravis returned.
1295d80bf64SDag-Erling Smørgrav.It Fn alloc_unrl uh
1305d80bf64SDag-Erling SmørgravSame as
1315d80bf64SDag-Erling Smørgrav.Fn alloc_unr
1325d80bf64SDag-Erling Smørgravexcept that mutex is assumed to be already locked and thus is not used.
1335d80bf64SDag-Erling Smørgrav.It Fn alloc_unr_specific uh item
1345d80bf64SDag-Erling SmørgravAllocate a specific unit number.
1355d80bf64SDag-Erling SmørgravThis function allocates memory and thus may sleep.
1365d80bf64SDag-Erling SmørgravThe allocated unit number is returned on success.
1375d80bf64SDag-Erling SmørgravIf the specified number is already allocated or out of the range,
1385d80bf64SDag-Erling Smørgrav.Li \-1
1395d80bf64SDag-Erling Smørgravis returned.
1405d80bf64SDag-Erling Smørgrav.It Fn free_unr uh item
1415d80bf64SDag-Erling SmørgravFree a previously allocated unit number.
1425d80bf64SDag-Erling SmørgravThis function may require allocating memory, and thus it can sleep.
1435d80bf64SDag-Erling SmørgravThere is no pre-locked variant.
1445d80bf64SDag-Erling Smørgrav.El
145*ea95173dSKonstantin Belousov.Sh ITERATOR INTERFACE
146*ea95173dSKonstantin BelousovThe
147*ea95173dSKonstantin Belousov.Nm unr
148*ea95173dSKonstantin Belousovfacility provides an interface to iterate over all allocated units
149*ea95173dSKonstantin Belousovfor the given
150*ea95173dSKonstantin Belousov.Dv unrhdr .
151*ea95173dSKonstantin BelousovIterators are identified by an opaque handle.
152*ea95173dSKonstantin BelousovMore than one iterators can operate simultaneously; the iterator position
153*ea95173dSKonstantin Belousovdata is recorded only in the iterator handle.
154*ea95173dSKonstantin Belousov.Pp
155*ea95173dSKonstantin BelousovConsumers must ensure that the unit allocator is not modified between
156*ea95173dSKonstantin Belousovcalls to the iterator functions.
157*ea95173dSKonstantin BelousovIn particular, the internal allocator mutex cannot provide consistency,
158*ea95173dSKonstantin Belousovbecause it is acquired and dropped inside the
159*ea95173dSKonstantin Belousov.Fn next_iter_unr
160*ea95173dSKonstantin Belousovfunction.
161*ea95173dSKonstantin BelousovIf the allocator was modified, it is safe to free the iterator with
162*ea95173dSKonstantin Belousov.Fn free_iter_unr
163*ea95173dSKonstantin Belousovmethod nevertheless.
164*ea95173dSKonstantin Belousov.Bl -tag -width indent
165*ea95173dSKonstantin Belousov.It Fn create_iter_unr uh
166*ea95173dSKonstantin BelousovCreate an iterator.
167*ea95173dSKonstantin BelousovReturn the handle that should be passed to other iterator functions.
168*ea95173dSKonstantin Belousov.It Fn next_iter_unr handle
169*ea95173dSKonstantin BelousovReturn the value of the next unit.
170*ea95173dSKonstantin BelousovUnits are returned in ascending order.
171*ea95173dSKonstantin BelousovA return value of
172*ea95173dSKonstantin Belousov.Li \-1
173*ea95173dSKonstantin Belousovindicates the end of iteration, in which
174*ea95173dSKonstantin Belousovcase
175*ea95173dSKonstantin Belousov.Li \-1
176*ea95173dSKonstantin Belousovis returned for all future calls.
177*ea95173dSKonstantin Belousov.It Fn free_iter_unr handle
178*ea95173dSKonstantin BelousovFree the iterator, handle is no longer valid.
179*ea95173dSKonstantin Belousov.El
1805d80bf64SDag-Erling Smørgrav.Sh CODE REFERENCES
1815d80bf64SDag-Erling SmørgravThe above functions are implemented in
1825d80bf64SDag-Erling Smørgrav.Pa sys/kern/subr_unit.c .
1835d80bf64SDag-Erling Smørgrav.Sh HISTORY
1845d80bf64SDag-Erling SmørgravKernel unit number allocator first appeared in
1855d80bf64SDag-Erling Smørgrav.Fx 6.0 .
1865d80bf64SDag-Erling Smørgrav.Sh AUTHORS
1875d80bf64SDag-Erling Smørgrav.An -nosplit
1885d80bf64SDag-Erling SmørgravKernel unit number allocator was written by
1895d80bf64SDag-Erling Smørgrav.An Poul-Henning Kamp .
1905d80bf64SDag-Erling SmørgravThis manpage was written by
1915d80bf64SDag-Erling Smørgrav.An Gleb Smirnoff .
192