summaryrefslogtreecommitdiff
path: root/usr/src/man/man9f/id_space.9f
blob: 4f8f953a31500b2241e61086a67a5b6c33788687 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
.\"
.\" This file and its contents are supplied under the terms of the
.\" Common Development and Distribution License ("CDDL"), version 1.0.
.\" You may only use this file in accordance with the terms of version
.\" 1.0 of the CDDL.
.\"
.\" A full copy of the text of the CDDL should have accompanied this
.\" source.  A copy of the CDDL is also available via the Internet at
.\" http://www.illumos.org/license/CDDL.
.\"
.\"
.\" Copyright 2016 Joyent, Inc.
.\"
.Dd Aug 02, 2016
.Dt ID_SPACE 9F
.Os
.Sh NAME
.Nm id_space ,
.Nm id_space_create ,
.Nm id_space_destroy ,
.Nm id_space_extend ,
.Nm id_alloc ,
.Nm id_alloc_nosleep ,
.Nm id_allocff ,
.Nm id_allocff_nosleep ,
.Nm id_alloc_specific_nosleep ,
.Nm id_free
.Nd create, destroy, and use identifier spaces
.Sh SYNOPSIS
.In sys/id_space.h
.Ft "id_space_t *"
.Fo id_space_create
.Fa "const char *name"
.Fa "id_t low"
.Fa "id_t high"
.Fc
.Ft void
.Fo id_space_destroy
.Fa "id_space_t *idspace"
.Fc
.Ft void
.Fo id_space_extend
.Fa "id_t low"
.Fa "id_t high"
.Fc
.Ft id_t
.Fo id_alloc
.Fa "id_space_t *idspace"
.Fc
.Ft id_t
.Fo id_alloc_nosleep
.Fa "id_space_t *idspace"
.Fc
.Ft id_t
.Fo id_allocff
.Fa "id_space_t *idspace"
.Fc
.Ft id_t
.Fo id_allocff_nosleep
.Fa "id_space_t *idspace"
.Fc
.Ft id_t
.Fo id_allocff_specific_nosleep
.Fa "id_space_t *idspace"
.Fa "id_t id"
.Fc
.Ft void
.Fo id_free
.Fa "id_space_t *idspace"
.Fa "id_t id"
.Fc
.Sh INTERFACE STABILITY
illumos DDI specific
.Sh PARAMETERS
.Bl -tag -width Fa
.It Fa idspace
A pointer to an
.Sy id_space_t
structure allocated with the
.Fn id_space_create
function.
.It Fa id
An identifier, a signed 32-bit integer.
.It Fa name
An ASCII character string to call the identifier space.
.It Fa low
The lower end of an identifier space.
This value is included in the range.
.It Fa high
The upper end of an identifier space.
This value is excluded from the range.
.El
.Sh DESCRIPTION
The
.Sy id_space
suite of functions is used to create and manage identifier spaces.
An identifier space allows the system to manage a range of identifiers.
It tracks what values have been used and which values have not been used
and has different ways of allocating values from the identifier space.
.Pp
Identifier spaces are often used by device drivers to manage ranges of
numeric identifiers that may be disjoint.
A common use case for identifier spaces is to manage the set of allocated minor
numbers for a device driver.
.Ss Creating, Expanding and Destroying Identifier Spaces
To create an identifier space, the
.Fn id_space_create
function should be used.
A name for the id space must be passed in the
.Fa name
argument.
It should be unique.
For device drivers, often combining the name of the driver and the instance from
the
.Xr ddi_get_instance 9F
function results in a unique name.
.Pp
The values of
.Fa low
and
.Fa high
describe the range of the identifier space.
The range is inclusive on the low end and exclusive on the high end.
Mathematically, this would be represented as
.Pf [ Fa low ,
.Fa high Ns ).
.Pp
Once the
.Fn id_space_create
function has been successfully called, the returned identifier space can
be used to allocate and manage identifiers.
.Pp
Once an identifier space has been created, additional ranges of
identifiers can be added.
This process allows for disjoint ranges of values to be added to a single
identifier space.
The
.Fn id_space_extend
function is used to do this, and it adds the range
.Fa low
to
.Fa high
to the identifier space.
The range follows the same rules as with the
.Fn id_space_create
function.
It is inclusive of
.Fa low
and is exclusive of
.Fa high .
.Pp
Finally, when an identifier space is no longer being used and all of its
identifiers have been freed, the caller should call the
.Fn id_space_destroy
function to destroy the id space
.Fa idspace .
.Pp
All three of these functions,
.Fn id_space_create ,
.Fn id_space_extend ,
and
.Fn id_space_destroy
may block.
They should only be called from a context where it's safe for it to block.
This is equivalent to performing a blocking memory allocation.
.Ss Allocating Identifiers
Once an id space has been created with the
.Fn id_space_create
function, identifiers can be allocated from the space.
There are three different strategies for allocating an identifier:
.Bl -enum
.It
Allocating an identifier using the standard algorithm that attempts to
use the next identifier first.
.It
Allocating an identifier using a first fit algorithm.
These are functions with
.Em ff
in the name.
Using this will tend to keep the allocated id space more compressed.
.It
Allocating a specific id.
.El
.Pp
In addition, identifiers can be allocated in both a blocking and
non-blocking fashion.
When functions with the
.Sy _nosleep
prefix are used, they are non-blocking.
If no identifier is available, they will return an error.
.Pp
The
.Fn id_alloc
function will allocate the next identifier.
The
.Fn id_alloc_nosleep
function uses the same algorithm as
.Fn id_alloc ,
but will fail rather than block.
.Pp
The
.Fn id_allocff
function will allocate the first available identifier.
The
.Fn id_allocff_nosleep
function uses the same algorithm as
.Fn id_allocff ,
but will fail rather than block.
.Pp
The
.Fn id_alloc_specific_nosleep
function attempts to allocate the specific identifier
.Fa id
from the specified identifier space.
If
.Fa id
has already been allocated, then the function will fail.
.Ss Freeing Identifiers
Every allocated identifier must eventually be freed and returned to the
identifier space.
To free an identifier, use the
.Fn id_free
function, specifying the identifier in
.Fa id
and the identifier space it came from in
.Fa id_space .
It is a programmer error to call the
.Fn id_free
function on an identifier that has not been allocated.
.Sh CONTEXT
All of these functions may be called in
.Sy user
or
.Sy kernel
context.
The
.Fn id_alloc_nosleep ,
.Fn id_allocff_nosleep ,
and
.Fn id_alloc_specific_nosleep
functions may be called in
.Sy interrupt
context.
.Sh RETURN VALUES
Upon successful completion, the
.Fn id_space_create
function returns a pointer to an identifier space.
Otherwise,
.Dv NULL
is returned to indicate that the identifier space could not be created.
.Pp
The
.Fn id_alloc
and
.Fn id_allocff
functions always return an identifier that's in the range of the
specified identifier space.
.Pp
Upon successful completion, the
.Fn id_alloc_nosleep ,
.Fn id_allocff_nosleep ,
and
.Fn id_alloc_specific_nosleep
functions will return an identifier that's in the range of the specified
identifier space.
Otherwise,
.Sy -1
is returned to indicate that no identifier was available, or in the case
of the
.Fn id_alloc_specific_nosleep
function, that the specified identifier was not available.
.Sh SEE ALSO
.Xr kmem_alloc 9F ,
.Xr rmallocmap 9F