1 #ifndef __LINUX_BITMAP_H
2 #define __LINUX_BITMAP_H
6 #include <linux/types.h>
7 #include <linux/bitops.h>
8 #include <linux/string.h>
11 * bitmaps provide bit arrays that consume one or more unsigned
12 * longs. The bitmap interface and available operations are listed
15 * Function implementations generic to all architectures are in
16 * lib/bitmap.c. Functions implementations that are architecture
17 * specific are in various include/asm-<arch>/bitops.h headers
18 * and other arch/<arch> specific files.
20 * See lib/bitmap.c for more details.
24 * The available bitmap operations and their rough meaning in the
25 * case that the bitmap is a single unsigned long are thus:
27 * bitmap_zero(dst, nbits) *dst = 0UL
28 * bitmap_fill(dst, nbits) *dst = ~0UL
29 * bitmap_copy(dst, src, nbits) *dst = *src
30 * bitmap_and(dst, src1, src2, nbits) *dst = *src1 & *src2
31 * bitmap_or(dst, src1, src2, nbits) *dst = *src1 | *src2
32 * bitmap_xor(dst, src1, src2, nbits) *dst = *src1 ^ *src2
33 * bitmap_andnot(dst, src1, src2, nbits) *dst = *src1 & ~(*src2)
34 * bitmap_complement(dst, src, nbits) *dst = ~(*src)
35 * bitmap_equal(src1, src2, nbits) Are *src1 and *src2 equal?
36 * bitmap_intersects(src1, src2, nbits) Do *src1 and *src2 overlap?
37 * bitmap_subset(src1, src2, nbits) Is *src1 a subset of *src2?
38 * bitmap_empty(src, nbits) Are all bits zero in *src?
39 * bitmap_full(src, nbits) Are all bits set in *src?
40 * bitmap_weight(src, nbits) Hamming Weight: number set bits
41 * bitmap_shift_right(dst, src, n, nbits) *dst = *src >> n
42 * bitmap_shift_left(dst, src, n, nbits) *dst = *src << n
43 * bitmap_scnprintf(buf, len, src, nbits) Print bitmap src to buf
44 * bitmap_parse(ubuf, ulen, dst, nbits) Parse bitmap dst from user buf
45 * bitmap_scnlistprintf(buf, len, src, nbits) Print bitmap src as list to buf
46 * bitmap_parselist(buf, dst, nbits) Parse bitmap dst from list
50 * Also the following operations in asm/bitops.h apply to bitmaps.
52 * set_bit(bit, addr) *addr |= bit
53 * clear_bit(bit, addr) *addr &= ~bit
54 * change_bit(bit, addr) *addr ^= bit
55 * test_bit(bit, addr) Is bit set in *addr?
56 * test_and_set_bit(bit, addr) Set bit and return old value
57 * test_and_clear_bit(bit, addr) Clear bit and return old value
58 * test_and_change_bit(bit, addr) Change bit and return old value
59 * find_first_zero_bit(addr, nbits) Position first zero bit in *addr
60 * find_first_bit(addr, nbits) Position first set bit in *addr
61 * find_next_zero_bit(addr, nbits, bit) Position next zero bit in *addr >= bit
62 * find_next_bit(addr, nbits, bit) Position next set bit in *addr >= bit
66 * The DECLARE_BITMAP(name,bits) macro, in linux/types.h, can be used
67 * to declare an array named 'name' of just enough unsigned longs to
68 * contain all bit positions from 0 to 'bits' - 1.
72 * lib/bitmap.c provides these functions:
75 extern int __bitmap_empty(const unsigned long *bitmap, int bits);
76 extern int __bitmap_full(const unsigned long *bitmap, int bits);
77 extern int __bitmap_equal(const unsigned long *bitmap1,
78 const unsigned long *bitmap2, int bits);
79 extern void __bitmap_complement(unsigned long *dst, const unsigned long *src,
81 extern void __bitmap_shift_right(unsigned long *dst,
82 const unsigned long *src, int shift, int bits);
83 extern void __bitmap_shift_left(unsigned long *dst,
84 const unsigned long *src, int shift, int bits);
85 extern void __bitmap_and(unsigned long *dst, const unsigned long *bitmap1,
86 const unsigned long *bitmap2, int bits);
87 extern void __bitmap_or(unsigned long *dst, const unsigned long *bitmap1,
88 const unsigned long *bitmap2, int bits);
89 extern void __bitmap_xor(unsigned long *dst, const unsigned long *bitmap1,
90 const unsigned long *bitmap2, int bits);
91 extern void __bitmap_andnot(unsigned long *dst, const unsigned long *bitmap1,
92 const unsigned long *bitmap2, int bits);
93 extern int __bitmap_intersects(const unsigned long *bitmap1,
94 const unsigned long *bitmap2, int bits);
95 extern int __bitmap_subset(const unsigned long *bitmap1,
96 const unsigned long *bitmap2, int bits);
97 extern int __bitmap_weight(const unsigned long *bitmap, int bits);
99 extern int bitmap_scnprintf(char *buf, unsigned int len,
100 const unsigned long *src, int nbits);
101 extern int bitmap_parse(const char __user *ubuf, unsigned int ulen,
102 unsigned long *dst, int nbits);
103 extern int bitmap_scnlistprintf(char *buf, unsigned int len,
104 const unsigned long *src, int nbits);
105 extern int bitmap_parselist(const char *buf, unsigned long *maskp,
107 extern int bitmap_find_free_region(unsigned long *bitmap, int bits, int order);
108 extern void bitmap_release_region(unsigned long *bitmap, int pos, int order);
109 extern int bitmap_allocate_region(unsigned long *bitmap, int pos, int order);
111 #define BITMAP_LAST_WORD_MASK(nbits) \
113 ((nbits) % BITS_PER_LONG) ? \
114 (1UL<<((nbits) % BITS_PER_LONG))-1 : ~0UL \
117 static inline void bitmap_zero(unsigned long *dst, int nbits)
119 if (nbits <= BITS_PER_LONG)
122 int len = BITS_TO_LONGS(nbits) * sizeof(unsigned long);
127 static inline void bitmap_fill(unsigned long *dst, int nbits)
129 size_t nlongs = BITS_TO_LONGS(nbits);
131 int len = (nlongs - 1) * sizeof(unsigned long);
132 memset(dst, 0xff, len);
134 dst[nlongs - 1] = BITMAP_LAST_WORD_MASK(nbits);
137 static inline void bitmap_copy(unsigned long *dst, const unsigned long *src,
140 if (nbits <= BITS_PER_LONG)
143 int len = BITS_TO_LONGS(nbits) * sizeof(unsigned long);
144 memcpy(dst, src, len);
148 static inline void bitmap_and(unsigned long *dst, const unsigned long *src1,
149 const unsigned long *src2, int nbits)
151 if (nbits <= BITS_PER_LONG)
152 *dst = *src1 & *src2;
154 __bitmap_and(dst, src1, src2, nbits);
157 static inline void bitmap_or(unsigned long *dst, const unsigned long *src1,
158 const unsigned long *src2, int nbits)
160 if (nbits <= BITS_PER_LONG)
161 *dst = *src1 | *src2;
163 __bitmap_or(dst, src1, src2, nbits);
166 static inline void bitmap_xor(unsigned long *dst, const unsigned long *src1,
167 const unsigned long *src2, int nbits)
169 if (nbits <= BITS_PER_LONG)
170 *dst = *src1 ^ *src2;
172 __bitmap_xor(dst, src1, src2, nbits);
175 static inline void bitmap_andnot(unsigned long *dst, const unsigned long *src1,
176 const unsigned long *src2, int nbits)
178 if (nbits <= BITS_PER_LONG)
179 *dst = *src1 & ~(*src2);
181 __bitmap_andnot(dst, src1, src2, nbits);
184 static inline void bitmap_complement(unsigned long *dst, const unsigned long *src,
187 if (nbits <= BITS_PER_LONG)
188 *dst = ~(*src) & BITMAP_LAST_WORD_MASK(nbits);
190 __bitmap_complement(dst, src, nbits);
193 static inline int bitmap_equal(const unsigned long *src1,
194 const unsigned long *src2, int nbits)
196 if (nbits <= BITS_PER_LONG)
197 return ! ((*src1 ^ *src2) & BITMAP_LAST_WORD_MASK(nbits));
199 return __bitmap_equal(src1, src2, nbits);
202 static inline int bitmap_intersects(const unsigned long *src1,
203 const unsigned long *src2, int nbits)
205 if (nbits <= BITS_PER_LONG)
206 return ((*src1 & *src2) & BITMAP_LAST_WORD_MASK(nbits)) != 0;
208 return __bitmap_intersects(src1, src2, nbits);
211 static inline int bitmap_subset(const unsigned long *src1,
212 const unsigned long *src2, int nbits)
214 if (nbits <= BITS_PER_LONG)
215 return ! ((*src1 & ~(*src2)) & BITMAP_LAST_WORD_MASK(nbits));
217 return __bitmap_subset(src1, src2, nbits);
220 static inline int bitmap_empty(const unsigned long *src, int nbits)
222 if (nbits <= BITS_PER_LONG)
223 return ! (*src & BITMAP_LAST_WORD_MASK(nbits));
225 return __bitmap_empty(src, nbits);
228 static inline int bitmap_full(const unsigned long *src, int nbits)
230 if (nbits <= BITS_PER_LONG)
231 return ! (~(*src) & BITMAP_LAST_WORD_MASK(nbits));
233 return __bitmap_full(src, nbits);
236 static inline int bitmap_weight(const unsigned long *src, int nbits)
238 return __bitmap_weight(src, nbits);
241 static inline void bitmap_shift_right(unsigned long *dst,
242 const unsigned long *src, int n, int nbits)
244 if (nbits <= BITS_PER_LONG)
247 __bitmap_shift_right(dst, src, n, nbits);
250 static inline void bitmap_shift_left(unsigned long *dst,
251 const unsigned long *src, int n, int nbits)
253 if (nbits <= BITS_PER_LONG)
254 *dst = (*src << n) & BITMAP_LAST_WORD_MASK(nbits);
256 __bitmap_shift_left(dst, src, n, nbits);
259 #endif /* __ASSEMBLY__ */
261 #endif /* __LINUX_BITMAP_H */