xref: /onnv-gate/usr/src/uts/common/os/id_space.c (revision 0:68f95e015346)
1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate  * CDDL HEADER START
3*0Sstevel@tonic-gate  *
4*0Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*0Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*0Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*0Sstevel@tonic-gate  * with the License.
8*0Sstevel@tonic-gate  *
9*0Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*0Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*0Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*0Sstevel@tonic-gate  * and limitations under the License.
13*0Sstevel@tonic-gate  *
14*0Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*0Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*0Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*0Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*0Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*0Sstevel@tonic-gate  *
20*0Sstevel@tonic-gate  * CDDL HEADER END
21*0Sstevel@tonic-gate  */
22*0Sstevel@tonic-gate /*
23*0Sstevel@tonic-gate  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
24*0Sstevel@tonic-gate  * Use is subject to license terms.
25*0Sstevel@tonic-gate  */
26*0Sstevel@tonic-gate 
27*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
28*0Sstevel@tonic-gate 
29*0Sstevel@tonic-gate #include <sys/types.h>
30*0Sstevel@tonic-gate #include <sys/id_space.h>
31*0Sstevel@tonic-gate #include <sys/debug.h>
32*0Sstevel@tonic-gate 
33*0Sstevel@tonic-gate /*
34*0Sstevel@tonic-gate  * ID Spaces
35*0Sstevel@tonic-gate  *
36*0Sstevel@tonic-gate  *   The id_space_t provides a simple implementation of a managed range of
37*0Sstevel@tonic-gate  *   integer identifiers using a vmem arena.  An ID space guarantees that the
38*0Sstevel@tonic-gate  *   next identifer returned by an allocation is larger than the previous one,
39*0Sstevel@tonic-gate  *   unless there are no larger slots remaining in the range.  In this case,
40*0Sstevel@tonic-gate  *   the ID space will return the first available slot in the lower part of the
41*0Sstevel@tonic-gate  *   range (viewing the previous identifier as a partitioning element).  If no
42*0Sstevel@tonic-gate  *   slots are available, id_alloc() will sleep until an identifier becomes
43*0Sstevel@tonic-gate  *   available.  Accordingly, id_space allocations must be initiated from
44*0Sstevel@tonic-gate  *   contexts where sleeping is acceptable.  id_alloc_nosleep() will return
45*0Sstevel@tonic-gate  *   -1 if no slots are available or if the system is low on memory.  If
46*0Sstevel@tonic-gate  *   id_alloc_nosleep() fails, callers should not try to extend the ID
47*0Sstevel@tonic-gate  *   space.  This is to avoid making a possible low-memory situation
48*0Sstevel@tonic-gate  *   worse.
49*0Sstevel@tonic-gate  *
50*0Sstevel@tonic-gate  *   As an ID space is designed for representing a range of id_t's, there
51*0Sstevel@tonic-gate  *   is a preexisting maximal range: [0, MAXUID].  ID space requests
52*0Sstevel@tonic-gate  *   outside that range will fail on a DEBUG kernel.
53*0Sstevel@tonic-gate  *
54*0Sstevel@tonic-gate  *   (Presently, the id_space_t abstraction supports only direct allocations; ID
55*0Sstevel@tonic-gate  *   reservation, in which an ID is allocated but placed in a internal
56*0Sstevel@tonic-gate  *   dictionary for later use, should be added when a consuming subsystem
57*0Sstevel@tonic-gate  *   arrives.)
58*0Sstevel@tonic-gate  */
59*0Sstevel@tonic-gate 
60*0Sstevel@tonic-gate /*
61*0Sstevel@tonic-gate  * Create an arena to represent the range [low, high).
62*0Sstevel@tonic-gate  * Caller must be in a context in which VM_SLEEP is legal.
63*0Sstevel@tonic-gate  */
64*0Sstevel@tonic-gate id_space_t *
65*0Sstevel@tonic-gate id_space_create(const char *name, id_t low, id_t high)
66*0Sstevel@tonic-gate {
67*0Sstevel@tonic-gate 	ASSERT(low >= 0);
68*0Sstevel@tonic-gate 	ASSERT(low < high);
69*0Sstevel@tonic-gate 
70*0Sstevel@tonic-gate 	return (vmem_create(name, (void *)(uintptr_t)(low + 1), high - low, 1,
71*0Sstevel@tonic-gate 	    NULL, NULL, NULL, 0, VM_SLEEP | VMC_IDENTIFIER));
72*0Sstevel@tonic-gate }
73*0Sstevel@tonic-gate 
74*0Sstevel@tonic-gate /*
75*0Sstevel@tonic-gate  * Destroy a previously created ID space.
76*0Sstevel@tonic-gate  * No restrictions on caller's context.
77*0Sstevel@tonic-gate  */
78*0Sstevel@tonic-gate void
79*0Sstevel@tonic-gate id_space_destroy(id_space_t *isp)
80*0Sstevel@tonic-gate {
81*0Sstevel@tonic-gate 	vmem_destroy(isp);
82*0Sstevel@tonic-gate }
83*0Sstevel@tonic-gate 
84*0Sstevel@tonic-gate void
85*0Sstevel@tonic-gate id_space_extend(id_space_t *isp, id_t low, id_t high)
86*0Sstevel@tonic-gate {
87*0Sstevel@tonic-gate 	(void) vmem_add(isp,
88*0Sstevel@tonic-gate 	    (void *)(uintptr_t)(low + 1), high - low, VM_SLEEP);
89*0Sstevel@tonic-gate }
90*0Sstevel@tonic-gate 
91*0Sstevel@tonic-gate /*
92*0Sstevel@tonic-gate  * Allocate an id_t from specified ID space.
93*0Sstevel@tonic-gate  * Caller must be in a context in which VM_SLEEP is legal.
94*0Sstevel@tonic-gate  */
95*0Sstevel@tonic-gate id_t
96*0Sstevel@tonic-gate id_alloc(id_space_t *isp)
97*0Sstevel@tonic-gate {
98*0Sstevel@tonic-gate 	return ((id_t)(uintptr_t)
99*0Sstevel@tonic-gate 	    vmem_alloc(isp, 1, VM_SLEEP | VM_NEXTFIT) - 1);
100*0Sstevel@tonic-gate }
101*0Sstevel@tonic-gate 
102*0Sstevel@tonic-gate /*
103*0Sstevel@tonic-gate  * Allocate an id_t from specified ID space.
104*0Sstevel@tonic-gate  * Returns -1 on failure (see module block comments for more information on
105*0Sstevel@tonic-gate  * failure modes).
106*0Sstevel@tonic-gate  */
107*0Sstevel@tonic-gate id_t
108*0Sstevel@tonic-gate id_alloc_nosleep(id_space_t *isp)
109*0Sstevel@tonic-gate {
110*0Sstevel@tonic-gate 	return ((id_t)(uintptr_t)
111*0Sstevel@tonic-gate 	    vmem_alloc(isp, 1, VM_NOSLEEP | VM_NEXTFIT) - 1);
112*0Sstevel@tonic-gate }
113*0Sstevel@tonic-gate 
114*0Sstevel@tonic-gate /*
115*0Sstevel@tonic-gate  * Free a previously allocated ID.
116*0Sstevel@tonic-gate  * No restrictions on caller's context.
117*0Sstevel@tonic-gate  */
118*0Sstevel@tonic-gate void
119*0Sstevel@tonic-gate id_free(id_space_t *isp, id_t id)
120*0Sstevel@tonic-gate {
121*0Sstevel@tonic-gate 	vmem_free(isp, (void *)(uintptr_t)(id + 1), 1);
122*0Sstevel@tonic-gate }
123