1#ifndef __LINUX_BITMAP_H
2#define __LINUX_BITMAP_H
3
4#ifndef __ASSEMBLY__
5
6#include <linux/types.h>
7#include <linux/bitops.h>
8#include <linux/string.h>
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73extern int __bitmap_empty(const unsigned long *bitmap, int bits);
74extern int __bitmap_full(const unsigned long *bitmap, int bits);
75extern int __bitmap_equal(const unsigned long *bitmap1,
76 const unsigned long *bitmap2, int bits);
77extern void __bitmap_complement(unsigned long *dst, const unsigned long *src,
78 int bits);
79extern void __bitmap_shift_right(unsigned long *dst,
80 const unsigned long *src, int shift, int bits);
81extern void __bitmap_shift_left(unsigned long *dst,
82 const unsigned long *src, int shift, int bits);
83extern void __bitmap_and(unsigned long *dst, const unsigned long *bitmap1,
84 const unsigned long *bitmap2, int bits);
85extern void __bitmap_or(unsigned long *dst, const unsigned long *bitmap1,
86 const unsigned long *bitmap2, int bits);
87extern void __bitmap_xor(unsigned long *dst, const unsigned long *bitmap1,
88 const unsigned long *bitmap2, int bits);
89extern void __bitmap_andnot(unsigned long *dst, const unsigned long *bitmap1,
90 const unsigned long *bitmap2, int bits);
91extern int __bitmap_intersects(const unsigned long *bitmap1,
92 const unsigned long *bitmap2, int bits);
93extern int __bitmap_subset(const unsigned long *bitmap1,
94 const unsigned long *bitmap2, int bits);
95extern int __bitmap_weight(const unsigned long *bitmap, int bits);
96
97extern int bitmap_scnprintf(char *buf, unsigned int len,
98 const unsigned long *src, int nbits);
99extern int bitmap_parse(const char __user *ubuf, unsigned int ulen,
100 unsigned long *dst, int nbits);
101extern int bitmap_find_free_region(unsigned long *bitmap, int bits, int order);
102extern void bitmap_release_region(unsigned long *bitmap, int pos, int order);
103extern int bitmap_allocate_region(unsigned long *bitmap, int pos, int order);
104
105#define BITMAP_LAST_WORD_MASK(nbits) \
106( \
107 ((nbits) % BITS_PER_LONG) ? \
108 (1UL<<((nbits) % BITS_PER_LONG))-1 : ~0UL \
109)
110
111static inline void bitmap_zero(unsigned long *dst, int nbits)
112{
113 if (nbits <= BITS_PER_LONG)
114 *dst = 0UL;
115 else {
116 int len = BITS_TO_LONGS(nbits) * sizeof(unsigned long);
117 memset(dst, 0, len);
118 }
119}
120
121static inline void bitmap_fill(unsigned long *dst, int nbits)
122{
123 size_t nlongs = BITS_TO_LONGS(nbits);
124 if (nlongs > 1) {
125 int len = (nlongs - 1) * sizeof(unsigned long);
126 memset(dst, 0xff, len);
127 }
128 dst[nlongs - 1] = BITMAP_LAST_WORD_MASK(nbits);
129}
130
131static inline void bitmap_copy(unsigned long *dst, const unsigned long *src,
132 int nbits)
133{
134 if (nbits <= BITS_PER_LONG)
135 *dst = *src;
136 else {
137 int len = BITS_TO_LONGS(nbits) * sizeof(unsigned long);
138 memcpy(dst, src, len);
139 }
140}
141
142static inline void bitmap_and(unsigned long *dst, const unsigned long *src1,
143 const unsigned long *src2, int nbits)
144{
145 if (nbits <= BITS_PER_LONG)
146 *dst = *src1 & *src2;
147 else
148 __bitmap_and(dst, src1, src2, nbits);
149}
150
151static inline void bitmap_or(unsigned long *dst, const unsigned long *src1,
152 const unsigned long *src2, int nbits)
153{
154 if (nbits <= BITS_PER_LONG)
155 *dst = *src1 | *src2;
156 else
157 __bitmap_or(dst, src1, src2, nbits);
158}
159
160static inline void bitmap_xor(unsigned long *dst, const unsigned long *src1,
161 const unsigned long *src2, int nbits)
162{
163 if (nbits <= BITS_PER_LONG)
164 *dst = *src1 ^ *src2;
165 else
166 __bitmap_xor(dst, src1, src2, nbits);
167}
168
169static inline void bitmap_andnot(unsigned long *dst, const unsigned long *src1,
170 const unsigned long *src2, int nbits)
171{
172 if (nbits <= BITS_PER_LONG)
173 *dst = *src1 & ~(*src2);
174 else
175 __bitmap_andnot(dst, src1, src2, nbits);
176}
177
178static inline void bitmap_complement(unsigned long *dst, const unsigned long *src,
179 int nbits)
180{
181 if (nbits <= BITS_PER_LONG)
182 *dst = ~(*src) & BITMAP_LAST_WORD_MASK(nbits);
183 else
184 __bitmap_complement(dst, src, nbits);
185}
186
187static inline int bitmap_equal(const unsigned long *src1,
188 const unsigned long *src2, int nbits)
189{
190 if (nbits <= BITS_PER_LONG)
191 return ! ((*src1 ^ *src2) & BITMAP_LAST_WORD_MASK(nbits));
192 else
193 return __bitmap_equal(src1, src2, nbits);
194}
195
196static inline int bitmap_intersects(const unsigned long *src1,
197 const unsigned long *src2, int nbits)
198{
199 if (nbits <= BITS_PER_LONG)
200 return ((*src1 & *src2) & BITMAP_LAST_WORD_MASK(nbits)) != 0;
201 else
202 return __bitmap_intersects(src1, src2, nbits);
203}
204
205static inline int bitmap_subset(const unsigned long *src1,
206 const unsigned long *src2, int nbits)
207{
208 if (nbits <= BITS_PER_LONG)
209 return ! ((*src1 & ~(*src2)) & BITMAP_LAST_WORD_MASK(nbits));
210 else
211 return __bitmap_subset(src1, src2, nbits);
212}
213
214static inline int bitmap_empty(const unsigned long *src, int nbits)
215{
216 if (nbits <= BITS_PER_LONG)
217 return ! (*src & BITMAP_LAST_WORD_MASK(nbits));
218 else
219 return __bitmap_empty(src, nbits);
220}
221
222static inline int bitmap_full(const unsigned long *src, int nbits)
223{
224 if (nbits <= BITS_PER_LONG)
225 return ! (~(*src) & BITMAP_LAST_WORD_MASK(nbits));
226 else
227 return __bitmap_full(src, nbits);
228}
229
230static inline int bitmap_weight(const unsigned long *src, int nbits)
231{
232 return __bitmap_weight(src, nbits);
233}
234
235static inline void bitmap_shift_right(unsigned long *dst,
236 const unsigned long *src, int n, int nbits)
237{
238 if (nbits <= BITS_PER_LONG)
239 *dst = *src >> n;
240 else
241 __bitmap_shift_right(dst, src, n, nbits);
242}
243
244static inline void bitmap_shift_left(unsigned long *dst,
245 const unsigned long *src, int n, int nbits)
246{
247 if (nbits <= BITS_PER_LONG)
248 *dst = (*src << n) & BITMAP_LAST_WORD_MASK(nbits);
249 else
250 __bitmap_shift_left(dst, src, n, nbits);
251}
252
253#endif
254
255#endif
256