xref: /titanic_44/usr/src/uts/common/sys/bitmap.h (revision 8eea8e29cc4374d1ee24c25a07f45af132db3499)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
28 /*	  All Rights Reserved  	*/
29 
30 
31 #ifndef _SYS_BITMAP_H
32 #define	_SYS_BITMAP_H
33 
34 #pragma ident	"%Z%%M%	%I%	%E% SMI"	/* SVr4.0 1.6	*/
35 
36 #ifdef	__cplusplus
37 extern "C" {
38 #endif
39 
40 #include <sys/feature_tests.h>
41 #if defined(__GNUC__) && defined(_ASM_INLINES)
42 #include <asm/bitmap.h>
43 #endif
44 
45 /*
46  * Operations on bitmaps of arbitrary size
47  * A bitmap is a vector of 1 or more ulong_t's.
48  * The user of the package is responsible for range checks and keeping
49  * track of sizes.
50  */
51 
52 #ifdef _LP64
53 #define	BT_ULSHIFT	6 /* log base 2 of BT_NBIPUL, to extract word index */
54 #define	BT_ULSHIFT32	5 /* log base 2 of BT_NBIPUL, to extract word index */
55 #else
56 #define	BT_ULSHIFT	5 /* log base 2 of BT_NBIPUL, to extract word index */
57 #endif
58 
59 #define	BT_NBIPUL	(1 << BT_ULSHIFT)	/* n bits per ulong_t */
60 #define	BT_ULMASK	(BT_NBIPUL - 1)		/* to extract bit index */
61 
62 #ifdef _LP64
63 #define	BT_NBIPUL32	(1 << BT_ULSHIFT32)	/* n bits per ulong_t */
64 #define	BT_ULMASK32	(BT_NBIPUL32 - 1)	/* to extract bit index */
65 #define	BT_ULMAXMASK	0xffffffffffffffff	/* used by bt_getlowbit */
66 #else
67 #define	BT_ULMAXMASK	0xffffffff
68 #endif
69 
70 /*
71  * bitmap is a ulong_t *, bitindex an index_t
72  *
73  * The macros BT_WIM and BT_BIW internal; there is no need
74  * for users of this package to use them.
75  */
76 
77 /*
78  * word in map
79  */
80 #define	BT_WIM(bitmap, bitindex) \
81 	((bitmap)[(bitindex) >> BT_ULSHIFT])
82 /*
83  * bit in word
84  */
85 #define	BT_BIW(bitindex) \
86 	(1UL << ((bitindex) & BT_ULMASK))
87 
88 #ifdef _LP64
89 #define	BT_WIM32(bitmap, bitindex) \
90 	((bitmap)[(bitindex) >> BT_ULSHIFT32])
91 
92 #define	BT_BIW32(bitindex) \
93 	(1UL << ((bitindex) & BT_ULMASK32))
94 #endif
95 
96 /*
97  * These are public macros
98  *
99  * BT_BITOUL == n bits to n ulong_t's
100  */
101 #define	BT_BITOUL(nbits) \
102 	(((nbits) + BT_NBIPUL - 1l) / BT_NBIPUL)
103 #define	BT_SIZEOFMAP(nbits) \
104 	(BT_BITOUL(nbits) * sizeof (ulong_t))
105 #define	BT_TEST(bitmap, bitindex) \
106 	((BT_WIM((bitmap), (bitindex)) & BT_BIW(bitindex)) ? 1 : 0)
107 #define	BT_SET(bitmap, bitindex) \
108 	{ BT_WIM((bitmap), (bitindex)) |= BT_BIW(bitindex); }
109 #define	BT_CLEAR(bitmap, bitindex) \
110 	{ BT_WIM((bitmap), (bitindex)) &= ~BT_BIW(bitindex); }
111 
112 #ifdef _LP64
113 #define	BT_BITOUL32(nbits) \
114 	(((nbits) + BT_NBIPUL32 - 1l) / BT_NBIPUL32)
115 #define	BT_SIZEOFMAP32(nbits) \
116 	(BT_BITOUL32(nbits) * sizeof (uint_t))
117 #define	BT_TEST32(bitmap, bitindex) \
118 	((BT_WIM32((bitmap), (bitindex)) & BT_BIW32(bitindex)) ? 1 : 0)
119 #define	BT_SET32(bitmap, bitindex) \
120 	{ BT_WIM32((bitmap), (bitindex)) |= BT_BIW32(bitindex); }
121 #define	BT_CLEAR32(bitmap, bitindex) \
122 	{ BT_WIM32((bitmap), (bitindex)) &= ~BT_BIW32(bitindex); }
123 #endif /* _LP64 */
124 
125 
126 #if defined(_KERNEL) && !defined(_ASM)
127 #include <sys/atomic.h>
128 
129 /*
130  * return next available bit index from map with specified number of bits
131  */
132 extern index_t	bt_availbit(ulong_t *bitmap, size_t nbits);
133 /*
134  * find the highest order bit that is on, and is within or below
135  * the word specified by wx
136  */
137 extern int	bt_gethighbit(ulong_t *mapp, int wx);
138 extern int	bt_range(ulong_t *bitmap, size_t *pos1, size_t *pos2,
139 			size_t end_pos);
140 /*
141  * Find highest and lowest one bit set.
142  *	Returns bit number + 1 of bit that is set, otherwise returns 0.
143  * Low order bit is 0, high order bit is 31.
144  */
145 extern int	highbit(ulong_t);
146 extern int	lowbit(ulong_t);
147 extern int	bt_getlowbit(ulong_t *bitmap, size_t start, size_t stop);
148 extern void	bt_copy(ulong_t *, ulong_t *, ulong_t);
149 
150 /*
151  * find the parity
152  */
153 extern int	odd_parity(ulong_t);
154 
155 /*
156  * Atomically set/clear bits
157  * Atomic exclusive operations will set "result" to "-1"
158  * if the bit is already set/cleared. "result" will be set
159  * to 0 otherwise.
160  */
161 #define	BT_ATOMIC_SET(bitmap, bitindex) \
162 	{ atomic_or_long(&(BT_WIM(bitmap, bitindex)), BT_BIW(bitindex)); }
163 #define	BT_ATOMIC_CLEAR(bitmap, bitindex) \
164 	{ atomic_and_long(&(BT_WIM(bitmap, bitindex)), ~BT_BIW(bitindex)); }
165 
166 #define	BT_ATOMIC_SET_EXCL(bitmap, bitindex, result) \
167 	{ result = atomic_set_long_excl(&(BT_WIM(bitmap, bitindex)),	\
168 	    (bitindex) % BT_NBIPUL); }
169 #define	BT_ATOMIC_CLEAR_EXCL(bitmap, bitindex, result) \
170 	{ result = atomic_clear_long_excl(&(BT_WIM(bitmap, bitindex)),	\
171 	    (bitindex) % BT_NBIPUL); }
172 
173 /*
174  * Extracts bits between index h (high, inclusive) and l (low, exclusive) from
175  * u, which must be an unsigned integer.
176  */
177 #define	BITX(u, h, l)	(((u) >> (l)) & ((1LU << ((h) - (l) + 1LU)) - 1LU))
178 
179 #endif	/* _KERNEL && !_ASM */
180 
181 #ifdef	__cplusplus
182 }
183 #endif
184 
185 #endif	/* _SYS_BITMAP_H */
186