]>
Commit | Line | Data |
---|---|---|
1da177e4 LT |
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 | * bitmaps provide bit arrays that consume one or more unsigned | |
12 | * longs. The bitmap interface and available operations are listed | |
13 | * here, in bitmap.h | |
14 | * | |
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. | |
19 | * | |
20 | * See lib/bitmap.c for more details. | |
21 | */ | |
22 | ||
23 | /* | |
24 | * The available bitmap operations and their rough meaning in the | |
25 | * case that the bitmap is a single unsigned long are thus: | |
26 | * | |
08cd3657 AK |
27 | * Note that nbits should be always a compile time evaluable constant. |
28 | * Otherwise many inlines will generate horrible code. | |
29 | * | |
1da177e4 LT |
30 | * bitmap_zero(dst, nbits) *dst = 0UL |
31 | * bitmap_fill(dst, nbits) *dst = ~0UL | |
32 | * bitmap_copy(dst, src, nbits) *dst = *src | |
33 | * bitmap_and(dst, src1, src2, nbits) *dst = *src1 & *src2 | |
34 | * bitmap_or(dst, src1, src2, nbits) *dst = *src1 | *src2 | |
35 | * bitmap_xor(dst, src1, src2, nbits) *dst = *src1 ^ *src2 | |
36 | * bitmap_andnot(dst, src1, src2, nbits) *dst = *src1 & ~(*src2) | |
37 | * bitmap_complement(dst, src, nbits) *dst = ~(*src) | |
38 | * bitmap_equal(src1, src2, nbits) Are *src1 and *src2 equal? | |
39 | * bitmap_intersects(src1, src2, nbits) Do *src1 and *src2 overlap? | |
40 | * bitmap_subset(src1, src2, nbits) Is *src1 a subset of *src2? | |
41 | * bitmap_empty(src, nbits) Are all bits zero in *src? | |
42 | * bitmap_full(src, nbits) Are all bits set in *src? | |
43 | * bitmap_weight(src, nbits) Hamming Weight: number set bits | |
44 | * bitmap_shift_right(dst, src, n, nbits) *dst = *src >> n | |
45 | * bitmap_shift_left(dst, src, n, nbits) *dst = *src << n | |
fb5eeeee PJ |
46 | * bitmap_remap(dst, src, old, new, nbits) *dst = map(old, new)(src) |
47 | * bitmap_bitremap(oldbit, old, new, nbits) newbit = map(old, new)(oldbit) | |
1da177e4 | 48 | * bitmap_scnprintf(buf, len, src, nbits) Print bitmap src to buf |
01a3ee2b RC |
49 | * bitmap_parse(buf, buflen, dst, nbits) Parse bitmap dst from kernel buf |
50 | * bitmap_parse_user(ubuf, ulen, dst, nbits) Parse bitmap dst from user buf | |
1da177e4 LT |
51 | * bitmap_scnlistprintf(buf, len, src, nbits) Print bitmap src as list to buf |
52 | * bitmap_parselist(buf, dst, nbits) Parse bitmap dst from list | |
87e24802 PJ |
53 | * bitmap_find_free_region(bitmap, bits, order) Find and allocate bit region |
54 | * bitmap_release_region(bitmap, pos, order) Free specified bit region | |
55 | * bitmap_allocate_region(bitmap, pos, order) Allocate specified bit region | |
1da177e4 LT |
56 | */ |
57 | ||
58 | /* | |
59 | * Also the following operations in asm/bitops.h apply to bitmaps. | |
60 | * | |
61 | * set_bit(bit, addr) *addr |= bit | |
62 | * clear_bit(bit, addr) *addr &= ~bit | |
63 | * change_bit(bit, addr) *addr ^= bit | |
64 | * test_bit(bit, addr) Is bit set in *addr? | |
65 | * test_and_set_bit(bit, addr) Set bit and return old value | |
66 | * test_and_clear_bit(bit, addr) Clear bit and return old value | |
67 | * test_and_change_bit(bit, addr) Change bit and return old value | |
68 | * find_first_zero_bit(addr, nbits) Position first zero bit in *addr | |
69 | * find_first_bit(addr, nbits) Position first set bit in *addr | |
70 | * find_next_zero_bit(addr, nbits, bit) Position next zero bit in *addr >= bit | |
71 | * find_next_bit(addr, nbits, bit) Position next set bit in *addr >= bit | |
72 | */ | |
73 | ||
74 | /* | |
75 | * The DECLARE_BITMAP(name,bits) macro, in linux/types.h, can be used | |
76 | * to declare an array named 'name' of just enough unsigned longs to | |
77 | * contain all bit positions from 0 to 'bits' - 1. | |
78 | */ | |
79 | ||
80 | /* | |
81 | * lib/bitmap.c provides these functions: | |
82 | */ | |
83 | ||
84 | extern int __bitmap_empty(const unsigned long *bitmap, int bits); | |
85 | extern int __bitmap_full(const unsigned long *bitmap, int bits); | |
86 | extern int __bitmap_equal(const unsigned long *bitmap1, | |
87 | const unsigned long *bitmap2, int bits); | |
88 | extern void __bitmap_complement(unsigned long *dst, const unsigned long *src, | |
89 | int bits); | |
90 | extern void __bitmap_shift_right(unsigned long *dst, | |
91 | const unsigned long *src, int shift, int bits); | |
92 | extern void __bitmap_shift_left(unsigned long *dst, | |
93 | const unsigned long *src, int shift, int bits); | |
94 | extern void __bitmap_and(unsigned long *dst, const unsigned long *bitmap1, | |
95 | const unsigned long *bitmap2, int bits); | |
96 | extern void __bitmap_or(unsigned long *dst, const unsigned long *bitmap1, | |
97 | const unsigned long *bitmap2, int bits); | |
98 | extern void __bitmap_xor(unsigned long *dst, const unsigned long *bitmap1, | |
99 | const unsigned long *bitmap2, int bits); | |
100 | extern void __bitmap_andnot(unsigned long *dst, const unsigned long *bitmap1, | |
101 | const unsigned long *bitmap2, int bits); | |
102 | extern int __bitmap_intersects(const unsigned long *bitmap1, | |
103 | const unsigned long *bitmap2, int bits); | |
104 | extern int __bitmap_subset(const unsigned long *bitmap1, | |
105 | const unsigned long *bitmap2, int bits); | |
106 | extern int __bitmap_weight(const unsigned long *bitmap, int bits); | |
107 | ||
108 | extern int bitmap_scnprintf(char *buf, unsigned int len, | |
109 | const unsigned long *src, int nbits); | |
01a3ee2b RC |
110 | extern int __bitmap_parse(const char *buf, unsigned int buflen, int is_user, |
111 | unsigned long *dst, int nbits); | |
112 | extern int bitmap_parse_user(const char __user *ubuf, unsigned int ulen, | |
1da177e4 LT |
113 | unsigned long *dst, int nbits); |
114 | extern int bitmap_scnlistprintf(char *buf, unsigned int len, | |
115 | const unsigned long *src, int nbits); | |
116 | extern int bitmap_parselist(const char *buf, unsigned long *maskp, | |
117 | int nmaskbits); | |
fb5eeeee PJ |
118 | extern void bitmap_remap(unsigned long *dst, const unsigned long *src, |
119 | const unsigned long *old, const unsigned long *new, int bits); | |
120 | extern int bitmap_bitremap(int oldbit, | |
121 | const unsigned long *old, const unsigned long *new, int bits); | |
1da177e4 LT |
122 | extern int bitmap_find_free_region(unsigned long *bitmap, int bits, int order); |
123 | extern void bitmap_release_region(unsigned long *bitmap, int pos, int order); | |
124 | extern int bitmap_allocate_region(unsigned long *bitmap, int pos, int order); | |
125 | ||
126 | #define BITMAP_LAST_WORD_MASK(nbits) \ | |
127 | ( \ | |
128 | ((nbits) % BITS_PER_LONG) ? \ | |
129 | (1UL<<((nbits) % BITS_PER_LONG))-1 : ~0UL \ | |
130 | ) | |
131 | ||
132 | static inline void bitmap_zero(unsigned long *dst, int nbits) | |
133 | { | |
134 | if (nbits <= BITS_PER_LONG) | |
135 | *dst = 0UL; | |
136 | else { | |
137 | int len = BITS_TO_LONGS(nbits) * sizeof(unsigned long); | |
138 | memset(dst, 0, len); | |
139 | } | |
140 | } | |
141 | ||
142 | static inline void bitmap_fill(unsigned long *dst, int nbits) | |
143 | { | |
144 | size_t nlongs = BITS_TO_LONGS(nbits); | |
145 | if (nlongs > 1) { | |
146 | int len = (nlongs - 1) * sizeof(unsigned long); | |
147 | memset(dst, 0xff, len); | |
148 | } | |
149 | dst[nlongs - 1] = BITMAP_LAST_WORD_MASK(nbits); | |
150 | } | |
151 | ||
152 | static inline void bitmap_copy(unsigned long *dst, const unsigned long *src, | |
153 | int nbits) | |
154 | { | |
155 | if (nbits <= BITS_PER_LONG) | |
156 | *dst = *src; | |
157 | else { | |
158 | int len = BITS_TO_LONGS(nbits) * sizeof(unsigned long); | |
159 | memcpy(dst, src, len); | |
160 | } | |
161 | } | |
162 | ||
163 | static inline void bitmap_and(unsigned long *dst, const unsigned long *src1, | |
164 | const unsigned long *src2, int nbits) | |
165 | { | |
166 | if (nbits <= BITS_PER_LONG) | |
167 | *dst = *src1 & *src2; | |
168 | else | |
169 | __bitmap_and(dst, src1, src2, nbits); | |
170 | } | |
171 | ||
172 | static inline void bitmap_or(unsigned long *dst, const unsigned long *src1, | |
173 | const unsigned long *src2, int nbits) | |
174 | { | |
175 | if (nbits <= BITS_PER_LONG) | |
176 | *dst = *src1 | *src2; | |
177 | else | |
178 | __bitmap_or(dst, src1, src2, nbits); | |
179 | } | |
180 | ||
181 | static inline void bitmap_xor(unsigned long *dst, const unsigned long *src1, | |
182 | const unsigned long *src2, int nbits) | |
183 | { | |
184 | if (nbits <= BITS_PER_LONG) | |
185 | *dst = *src1 ^ *src2; | |
186 | else | |
187 | __bitmap_xor(dst, src1, src2, nbits); | |
188 | } | |
189 | ||
190 | static inline void bitmap_andnot(unsigned long *dst, const unsigned long *src1, | |
191 | const unsigned long *src2, int nbits) | |
192 | { | |
193 | if (nbits <= BITS_PER_LONG) | |
194 | *dst = *src1 & ~(*src2); | |
195 | else | |
196 | __bitmap_andnot(dst, src1, src2, nbits); | |
197 | } | |
198 | ||
199 | static inline void bitmap_complement(unsigned long *dst, const unsigned long *src, | |
200 | int nbits) | |
201 | { | |
202 | if (nbits <= BITS_PER_LONG) | |
203 | *dst = ~(*src) & BITMAP_LAST_WORD_MASK(nbits); | |
204 | else | |
205 | __bitmap_complement(dst, src, nbits); | |
206 | } | |
207 | ||
208 | static inline int bitmap_equal(const unsigned long *src1, | |
209 | const unsigned long *src2, int nbits) | |
210 | { | |
211 | if (nbits <= BITS_PER_LONG) | |
212 | return ! ((*src1 ^ *src2) & BITMAP_LAST_WORD_MASK(nbits)); | |
213 | else | |
214 | return __bitmap_equal(src1, src2, nbits); | |
215 | } | |
216 | ||
217 | static inline int bitmap_intersects(const unsigned long *src1, | |
218 | const unsigned long *src2, int nbits) | |
219 | { | |
220 | if (nbits <= BITS_PER_LONG) | |
221 | return ((*src1 & *src2) & BITMAP_LAST_WORD_MASK(nbits)) != 0; | |
222 | else | |
223 | return __bitmap_intersects(src1, src2, nbits); | |
224 | } | |
225 | ||
226 | static inline int bitmap_subset(const unsigned long *src1, | |
227 | const unsigned long *src2, int nbits) | |
228 | { | |
229 | if (nbits <= BITS_PER_LONG) | |
230 | return ! ((*src1 & ~(*src2)) & BITMAP_LAST_WORD_MASK(nbits)); | |
231 | else | |
232 | return __bitmap_subset(src1, src2, nbits); | |
233 | } | |
234 | ||
235 | static inline int bitmap_empty(const unsigned long *src, int nbits) | |
236 | { | |
237 | if (nbits <= BITS_PER_LONG) | |
238 | return ! (*src & BITMAP_LAST_WORD_MASK(nbits)); | |
239 | else | |
240 | return __bitmap_empty(src, nbits); | |
241 | } | |
242 | ||
243 | static inline int bitmap_full(const unsigned long *src, int nbits) | |
244 | { | |
245 | if (nbits <= BITS_PER_LONG) | |
246 | return ! (~(*src) & BITMAP_LAST_WORD_MASK(nbits)); | |
247 | else | |
248 | return __bitmap_full(src, nbits); | |
249 | } | |
250 | ||
251 | static inline int bitmap_weight(const unsigned long *src, int nbits) | |
252 | { | |
08cd3657 AK |
253 | if (nbits <= BITS_PER_LONG) |
254 | return hweight_long(*src & BITMAP_LAST_WORD_MASK(nbits)); | |
1da177e4 LT |
255 | return __bitmap_weight(src, nbits); |
256 | } | |
257 | ||
258 | static inline void bitmap_shift_right(unsigned long *dst, | |
259 | const unsigned long *src, int n, int nbits) | |
260 | { | |
261 | if (nbits <= BITS_PER_LONG) | |
262 | *dst = *src >> n; | |
263 | else | |
264 | __bitmap_shift_right(dst, src, n, nbits); | |
265 | } | |
266 | ||
267 | static inline void bitmap_shift_left(unsigned long *dst, | |
268 | const unsigned long *src, int n, int nbits) | |
269 | { | |
270 | if (nbits <= BITS_PER_LONG) | |
271 | *dst = (*src << n) & BITMAP_LAST_WORD_MASK(nbits); | |
272 | else | |
273 | __bitmap_shift_left(dst, src, n, nbits); | |
274 | } | |
275 | ||
01a3ee2b RC |
276 | static inline int bitmap_parse(const char *buf, unsigned int buflen, |
277 | unsigned long *maskp, int nmaskbits) | |
278 | { | |
279 | return __bitmap_parse(buf, buflen, 0, maskp, nmaskbits); | |
280 | } | |
281 | ||
1da177e4 LT |
282 | #endif /* __ASSEMBLY__ */ |
283 | ||
284 | #endif /* __LINUX_BITMAP_H */ |