]>
git.proxmox.com Git - mirror_ubuntu-zesty-kernel.git/blob - zfs/module/icp/include/sys/bitmap.h
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
23 * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
27 /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
28 /* All Rights Reserved */
38 #if defined(__GNUC__) && defined(_ASM_INLINES) && \
39 (defined(__i386) || defined(__amd64))
40 #include <asm/bitmap.h>
44 * Operations on bitmaps of arbitrary size
45 * A bitmap is a vector of 1 or more ulong_t's.
46 * The user of the package is responsible for range checks and keeping
51 #define BT_ULSHIFT 6 /* log base 2 of BT_NBIPUL, to extract word index */
52 #define BT_ULSHIFT32 5 /* log base 2 of BT_NBIPUL, to extract word index */
54 #define BT_ULSHIFT 5 /* log base 2 of BT_NBIPUL, to extract word index */
57 #define BT_NBIPUL (1 << BT_ULSHIFT) /* n bits per ulong_t */
58 #define BT_ULMASK (BT_NBIPUL - 1) /* to extract bit index */
61 #define BT_NBIPUL32 (1 << BT_ULSHIFT32) /* n bits per ulong_t */
62 #define BT_ULMASK32 (BT_NBIPUL32 - 1) /* to extract bit index */
63 #define BT_ULMAXMASK 0xffffffffffffffff /* used by bt_getlowbit */
65 #define BT_ULMAXMASK 0xffffffff
69 * bitmap is a ulong_t *, bitindex an index_t
71 * The macros BT_WIM and BT_BIW internal; there is no need
72 * for users of this package to use them.
78 #define BT_WIM(bitmap, bitindex) \
79 ((bitmap)[(bitindex) >> BT_ULSHIFT])
83 #define BT_BIW(bitindex) \
84 (1UL << ((bitindex) & BT_ULMASK))
87 #define BT_WIM32(bitmap, bitindex) \
88 ((bitmap)[(bitindex) >> BT_ULSHIFT32])
90 #define BT_BIW32(bitindex) \
91 (1UL << ((bitindex) & BT_ULMASK32))
95 * These are public macros
97 * BT_BITOUL == n bits to n ulong_t's
99 #define BT_BITOUL(nbits) \
100 (((nbits) + BT_NBIPUL - 1l) / BT_NBIPUL)
101 #define BT_SIZEOFMAP(nbits) \
102 (BT_BITOUL(nbits) * sizeof (ulong_t))
103 #define BT_TEST(bitmap, bitindex) \
104 ((BT_WIM((bitmap), (bitindex)) & BT_BIW(bitindex)) ? 1 : 0)
105 #define BT_SET(bitmap, bitindex) \
106 { BT_WIM((bitmap), (bitindex)) |= BT_BIW(bitindex); }
107 #define BT_CLEAR(bitmap, bitindex) \
108 { BT_WIM((bitmap), (bitindex)) &= ~BT_BIW(bitindex); }
111 #define BT_BITOUL32(nbits) \
112 (((nbits) + BT_NBIPUL32 - 1l) / BT_NBIPUL32)
113 #define BT_SIZEOFMAP32(nbits) \
114 (BT_BITOUL32(nbits) * sizeof (uint_t))
115 #define BT_TEST32(bitmap, bitindex) \
116 ((BT_WIM32((bitmap), (bitindex)) & BT_BIW32(bitindex)) ? 1 : 0)
117 #define BT_SET32(bitmap, bitindex) \
118 { BT_WIM32((bitmap), (bitindex)) |= BT_BIW32(bitindex); }
119 #define BT_CLEAR32(bitmap, bitindex) \
120 { BT_WIM32((bitmap), (bitindex)) &= ~BT_BIW32(bitindex); }
125 * BIT_ONLYONESET is a private macro not designed for bitmaps of
126 * arbitrary size. u must be an unsigned integer/long. It returns
127 * true if one and only one bit is set in u.
129 #define BIT_ONLYONESET(u) \
130 ((((u) == 0) ? 0 : ((u) & ((u) - 1)) == 0))
135 * return next available bit index from map with specified number of bits
137 extern index_t
bt_availbit(ulong_t
*bitmap
, size_t nbits
);
139 * find the highest order bit that is on, and is within or below
140 * the word specified by wx
142 extern int bt_gethighbit(ulong_t
*mapp
, int wx
);
143 extern int bt_range(ulong_t
*bitmap
, size_t *pos1
, size_t *pos2
,
145 extern int bt_getlowbit(ulong_t
*bitmap
, size_t start
, size_t stop
);
146 extern void bt_copy(ulong_t
*, ulong_t
*, ulong_t
);
151 extern int odd_parity(ulong_t
);
154 * Atomically set/clear bits
155 * Atomic exclusive operations will set "result" to "-1"
156 * if the bit is already set/cleared. "result" will be set
159 #define BT_ATOMIC_SET(bitmap, bitindex) \
160 { atomic_or_long(&(BT_WIM(bitmap, bitindex)), BT_BIW(bitindex)); }
161 #define BT_ATOMIC_CLEAR(bitmap, bitindex) \
162 { atomic_and_long(&(BT_WIM(bitmap, bitindex)), ~BT_BIW(bitindex)); }
164 #define BT_ATOMIC_SET_EXCL(bitmap, bitindex, result) \
165 { result = atomic_set_long_excl(&(BT_WIM(bitmap, bitindex)), \
166 (bitindex) % BT_NBIPUL); }
167 #define BT_ATOMIC_CLEAR_EXCL(bitmap, bitindex, result) \
168 { result = atomic_clear_long_excl(&(BT_WIM(bitmap, bitindex)), \
169 (bitindex) % BT_NBIPUL); }
172 * Extracts bits between index h (high, inclusive) and l (low, exclusive) from
173 * u, which must be an unsigned integer.
175 #define BITX(u, h, l) (((u) >> (l)) & ((1LU << ((h) - (l) + 1LU)) - 1LU))
183 #endif /* _SYS_BITMAP_H */