10Sstevel@tonic-gate /*
20Sstevel@tonic-gate * CDDL HEADER START
30Sstevel@tonic-gate *
40Sstevel@tonic-gate * The contents of this file are subject to the terms of the
510696SDavid.Hollister@Sun.COM * Common Development and Distribution License (the "License").
610696SDavid.Hollister@Sun.COM * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate *
80Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate * See the License for the specific language governing permissions
110Sstevel@tonic-gate * and limitations under the License.
120Sstevel@tonic-gate *
130Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate *
190Sstevel@tonic-gate * CDDL HEADER END
200Sstevel@tonic-gate */
210Sstevel@tonic-gate /*
22*12633Sjohn.levon@sun.com * Copyright (c) 2000, 2010, Oracle and/or its affiliates. All rights reserved.
230Sstevel@tonic-gate */
240Sstevel@tonic-gate
250Sstevel@tonic-gate #include <sys/types.h>
260Sstevel@tonic-gate #include <sys/id_space.h>
270Sstevel@tonic-gate #include <sys/debug.h>
280Sstevel@tonic-gate
290Sstevel@tonic-gate /*
300Sstevel@tonic-gate * ID Spaces
310Sstevel@tonic-gate *
320Sstevel@tonic-gate * The id_space_t provides a simple implementation of a managed range of
330Sstevel@tonic-gate * integer identifiers using a vmem arena. An ID space guarantees that the
340Sstevel@tonic-gate * next identifer returned by an allocation is larger than the previous one,
350Sstevel@tonic-gate * unless there are no larger slots remaining in the range. In this case,
360Sstevel@tonic-gate * the ID space will return the first available slot in the lower part of the
370Sstevel@tonic-gate * range (viewing the previous identifier as a partitioning element). If no
3810696SDavid.Hollister@Sun.COM * slots are available, id_alloc()/id_allocff() will sleep until an
3910696SDavid.Hollister@Sun.COM * identifier becomes available. Accordingly, id_space allocations must be
4010696SDavid.Hollister@Sun.COM * initiated from contexts where sleeping is acceptable. id_alloc_nosleep()/
4110696SDavid.Hollister@Sun.COM * id_allocff_nosleep() will return -1 if no slots are available or if the
4210696SDavid.Hollister@Sun.COM * system is low on memory. If id_alloc_nosleep() fails, callers should
4310696SDavid.Hollister@Sun.COM * not try to extend the ID space. This is to avoid making a possible
4410696SDavid.Hollister@Sun.COM * low-memory situation worse.
450Sstevel@tonic-gate *
460Sstevel@tonic-gate * As an ID space is designed for representing a range of id_t's, there
4710696SDavid.Hollister@Sun.COM * is a preexisting maximal range: [0, MAXUID]. ID space requests outside
4810696SDavid.Hollister@Sun.COM * that range will fail on a DEBUG kernel. The id_allocff*() functions
49*12633Sjohn.levon@sun.com * return the first available id, and should be used when there is benefit
5010696SDavid.Hollister@Sun.COM * to having a compact allocated range.
510Sstevel@tonic-gate *
520Sstevel@tonic-gate * (Presently, the id_space_t abstraction supports only direct allocations; ID
530Sstevel@tonic-gate * reservation, in which an ID is allocated but placed in a internal
540Sstevel@tonic-gate * dictionary for later use, should be added when a consuming subsystem
550Sstevel@tonic-gate * arrives.)
560Sstevel@tonic-gate */
570Sstevel@tonic-gate
58*12633Sjohn.levon@sun.com #define ID_TO_ADDR(id) ((void *)(uintptr_t)(id + 1))
59*12633Sjohn.levon@sun.com #define ADDR_TO_ID(addr) ((id_t)((uintptr_t)addr - 1))
60*12633Sjohn.levon@sun.com
610Sstevel@tonic-gate /*
620Sstevel@tonic-gate * Create an arena to represent the range [low, high).
630Sstevel@tonic-gate * Caller must be in a context in which VM_SLEEP is legal.
640Sstevel@tonic-gate */
650Sstevel@tonic-gate id_space_t *
id_space_create(const char * name,id_t low,id_t high)660Sstevel@tonic-gate id_space_create(const char *name, id_t low, id_t high)
670Sstevel@tonic-gate {
680Sstevel@tonic-gate ASSERT(low >= 0);
690Sstevel@tonic-gate ASSERT(low < high);
700Sstevel@tonic-gate
71*12633Sjohn.levon@sun.com return (vmem_create(name, ID_TO_ADDR(low), high - low, 1,
720Sstevel@tonic-gate NULL, NULL, NULL, 0, VM_SLEEP | VMC_IDENTIFIER));
730Sstevel@tonic-gate }
740Sstevel@tonic-gate
750Sstevel@tonic-gate /*
760Sstevel@tonic-gate * Destroy a previously created ID space.
770Sstevel@tonic-gate * No restrictions on caller's context.
780Sstevel@tonic-gate */
790Sstevel@tonic-gate void
id_space_destroy(id_space_t * isp)800Sstevel@tonic-gate id_space_destroy(id_space_t *isp)
810Sstevel@tonic-gate {
820Sstevel@tonic-gate vmem_destroy(isp);
830Sstevel@tonic-gate }
840Sstevel@tonic-gate
850Sstevel@tonic-gate void
id_space_extend(id_space_t * isp,id_t low,id_t high)860Sstevel@tonic-gate id_space_extend(id_space_t *isp, id_t low, id_t high)
870Sstevel@tonic-gate {
88*12633Sjohn.levon@sun.com (void) vmem_add(isp, ID_TO_ADDR(low), high - low, VM_SLEEP);
890Sstevel@tonic-gate }
900Sstevel@tonic-gate
910Sstevel@tonic-gate /*
920Sstevel@tonic-gate * Allocate an id_t from specified ID space.
930Sstevel@tonic-gate * Caller must be in a context in which VM_SLEEP is legal.
940Sstevel@tonic-gate */
950Sstevel@tonic-gate id_t
id_alloc(id_space_t * isp)960Sstevel@tonic-gate id_alloc(id_space_t *isp)
970Sstevel@tonic-gate {
98*12633Sjohn.levon@sun.com return (ADDR_TO_ID(vmem_alloc(isp, 1, VM_SLEEP | VM_NEXTFIT)));
990Sstevel@tonic-gate }
1000Sstevel@tonic-gate
1010Sstevel@tonic-gate /*
1020Sstevel@tonic-gate * Allocate an id_t from specified ID space.
1030Sstevel@tonic-gate * Returns -1 on failure (see module block comments for more information on
1040Sstevel@tonic-gate * failure modes).
1050Sstevel@tonic-gate */
1060Sstevel@tonic-gate id_t
id_alloc_nosleep(id_space_t * isp)1070Sstevel@tonic-gate id_alloc_nosleep(id_space_t *isp)
1080Sstevel@tonic-gate {
109*12633Sjohn.levon@sun.com return (ADDR_TO_ID(vmem_alloc(isp, 1, VM_NOSLEEP | VM_NEXTFIT)));
1100Sstevel@tonic-gate }
1110Sstevel@tonic-gate
1120Sstevel@tonic-gate /*
11310696SDavid.Hollister@Sun.COM * Allocate an id_t from specified ID space using FIRSTFIT.
11410696SDavid.Hollister@Sun.COM * Caller must be in a context in which VM_SLEEP is legal.
11510696SDavid.Hollister@Sun.COM */
11610696SDavid.Hollister@Sun.COM id_t
id_allocff(id_space_t * isp)11710696SDavid.Hollister@Sun.COM id_allocff(id_space_t *isp)
11810696SDavid.Hollister@Sun.COM {
119*12633Sjohn.levon@sun.com return (ADDR_TO_ID(vmem_alloc(isp, 1, VM_SLEEP | VM_FIRSTFIT)));
12010696SDavid.Hollister@Sun.COM }
12110696SDavid.Hollister@Sun.COM
12210696SDavid.Hollister@Sun.COM /*
12310696SDavid.Hollister@Sun.COM * Allocate an id_t from specified ID space using FIRSTFIT
12410696SDavid.Hollister@Sun.COM * Returns -1 on failure (see module block comments for more information on
12510696SDavid.Hollister@Sun.COM * failure modes).
12610696SDavid.Hollister@Sun.COM */
12710696SDavid.Hollister@Sun.COM id_t
id_allocff_nosleep(id_space_t * isp)12810696SDavid.Hollister@Sun.COM id_allocff_nosleep(id_space_t *isp)
12910696SDavid.Hollister@Sun.COM {
130*12633Sjohn.levon@sun.com return (ADDR_TO_ID(vmem_alloc(isp, 1, VM_NOSLEEP | VM_FIRSTFIT)));
131*12633Sjohn.levon@sun.com }
132*12633Sjohn.levon@sun.com
133*12633Sjohn.levon@sun.com /*
134*12633Sjohn.levon@sun.com * Allocate a specific identifier if possible, returning the id if
135*12633Sjohn.levon@sun.com * successful, or -1 on failure.
136*12633Sjohn.levon@sun.com */
137*12633Sjohn.levon@sun.com id_t
id_alloc_specific_nosleep(id_space_t * isp,id_t id)138*12633Sjohn.levon@sun.com id_alloc_specific_nosleep(id_space_t *isp, id_t id)
139*12633Sjohn.levon@sun.com {
140*12633Sjohn.levon@sun.com void *minaddr = ID_TO_ADDR(id);
141*12633Sjohn.levon@sun.com void *maxaddr = ID_TO_ADDR(id + 1);
142*12633Sjohn.levon@sun.com
143*12633Sjohn.levon@sun.com /*
144*12633Sjohn.levon@sun.com * Note that even though we're vmem_free()ing this later, it
145*12633Sjohn.levon@sun.com * should be OK, since there's no quantum cache.
146*12633Sjohn.levon@sun.com */
147*12633Sjohn.levon@sun.com return (ADDR_TO_ID(vmem_xalloc(isp, 1, 1, 0, 0,
148*12633Sjohn.levon@sun.com minaddr, maxaddr, VM_NOSLEEP)));
14910696SDavid.Hollister@Sun.COM }
15010696SDavid.Hollister@Sun.COM
15110696SDavid.Hollister@Sun.COM /*
1520Sstevel@tonic-gate * Free a previously allocated ID.
1530Sstevel@tonic-gate * No restrictions on caller's context.
1540Sstevel@tonic-gate */
1550Sstevel@tonic-gate void
id_free(id_space_t * isp,id_t id)1560Sstevel@tonic-gate id_free(id_space_t *isp, id_t id)
1570Sstevel@tonic-gate {
158*12633Sjohn.levon@sun.com vmem_free(isp, ID_TO_ADDR(id), 1);
1590Sstevel@tonic-gate }
160