1
2#ifndef __LINUX_BITMAP_H
3#define __LINUX_BITMAP_H
4
5#ifndef __ASSEMBLY__
6
7#include <linux/types.h>
8#include <linux/bitops.h>
9#include <linux/string.h>
10#include <linux/kernel.h>
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111extern unsigned long *bitmap_alloc(unsigned int nbits, gfp_t flags);
112extern unsigned long *bitmap_zalloc(unsigned int nbits, gfp_t flags);
113extern void bitmap_free(const unsigned long *bitmap);
114
115
116
117
118
119extern int __bitmap_empty(const unsigned long *bitmap, unsigned int nbits);
120extern int __bitmap_full(const unsigned long *bitmap, unsigned int nbits);
121extern int __bitmap_equal(const unsigned long *bitmap1,
122 const unsigned long *bitmap2, unsigned int nbits);
123extern void __bitmap_complement(unsigned long *dst, const unsigned long *src,
124 unsigned int nbits);
125extern void __bitmap_shift_right(unsigned long *dst, const unsigned long *src,
126 unsigned int shift, unsigned int nbits);
127extern void __bitmap_shift_left(unsigned long *dst, const unsigned long *src,
128 unsigned int shift, unsigned int nbits);
129extern int __bitmap_and(unsigned long *dst, const unsigned long *bitmap1,
130 const unsigned long *bitmap2, unsigned int nbits);
131extern void __bitmap_or(unsigned long *dst, const unsigned long *bitmap1,
132 const unsigned long *bitmap2, unsigned int nbits);
133extern void __bitmap_xor(unsigned long *dst, const unsigned long *bitmap1,
134 const unsigned long *bitmap2, unsigned int nbits);
135extern int __bitmap_andnot(unsigned long *dst, const unsigned long *bitmap1,
136 const unsigned long *bitmap2, unsigned int nbits);
137extern int __bitmap_intersects(const unsigned long *bitmap1,
138 const unsigned long *bitmap2, unsigned int nbits);
139extern int __bitmap_subset(const unsigned long *bitmap1,
140 const unsigned long *bitmap2, unsigned int nbits);
141extern int __bitmap_weight(const unsigned long *bitmap, unsigned int nbits);
142extern void __bitmap_set(unsigned long *map, unsigned int start, int len);
143extern void __bitmap_clear(unsigned long *map, unsigned int start, int len);
144
145extern unsigned long bitmap_find_next_zero_area_off(unsigned long *map,
146 unsigned long size,
147 unsigned long start,
148 unsigned int nr,
149 unsigned long align_mask,
150 unsigned long align_offset);
151
152
153
154
155
156
157
158
159
160
161
162
163
164static inline unsigned long
165bitmap_find_next_zero_area(unsigned long *map,
166 unsigned long size,
167 unsigned long start,
168 unsigned int nr,
169 unsigned long align_mask)
170{
171 return bitmap_find_next_zero_area_off(map, size, start, nr,
172 align_mask, 0);
173}
174
175extern int __bitmap_parse(const char *buf, unsigned int buflen, int is_user,
176 unsigned long *dst, int nbits);
177extern int bitmap_parse_user(const char __user *ubuf, unsigned int ulen,
178 unsigned long *dst, int nbits);
179extern int bitmap_parselist(const char *buf, unsigned long *maskp,
180 int nmaskbits);
181extern int bitmap_parselist_user(const char __user *ubuf, unsigned int ulen,
182 unsigned long *dst, int nbits);
183extern void bitmap_remap(unsigned long *dst, const unsigned long *src,
184 const unsigned long *old, const unsigned long *new, unsigned int nbits);
185extern int bitmap_bitremap(int oldbit,
186 const unsigned long *old, const unsigned long *new, int bits);
187extern void bitmap_onto(unsigned long *dst, const unsigned long *orig,
188 const unsigned long *relmap, unsigned int bits);
189extern void bitmap_fold(unsigned long *dst, const unsigned long *orig,
190 unsigned int sz, unsigned int nbits);
191extern int bitmap_find_free_region(unsigned long *bitmap, unsigned int bits, int order);
192extern void bitmap_release_region(unsigned long *bitmap, unsigned int pos, int order);
193extern int bitmap_allocate_region(unsigned long *bitmap, unsigned int pos, int order);
194
195#ifdef __BIG_ENDIAN
196extern void bitmap_copy_le(unsigned long *dst, const unsigned long *src, unsigned int nbits);
197#else
198#define bitmap_copy_le bitmap_copy
199#endif
200extern unsigned int bitmap_ord_to_pos(const unsigned long *bitmap, unsigned int ord, unsigned int nbits);
201extern int bitmap_print_to_pagebuf(bool list, char *buf,
202 const unsigned long *maskp, int nmaskbits);
203
204#define BITMAP_FIRST_WORD_MASK(start) (~0UL << ((start) & (BITS_PER_LONG - 1)))
205#define BITMAP_LAST_WORD_MASK(nbits) (~0UL >> (-(nbits) & (BITS_PER_LONG - 1)))
206
207#define small_const_nbits(nbits) \
208 (__builtin_constant_p(nbits) && (nbits) <= BITS_PER_LONG)
209
210static inline void bitmap_zero(unsigned long *dst, unsigned int nbits)
211{
212 if (small_const_nbits(nbits))
213 *dst = 0UL;
214 else {
215 unsigned int len = BITS_TO_LONGS(nbits) * sizeof(unsigned long);
216 memset(dst, 0, len);
217 }
218}
219
220static inline void bitmap_fill(unsigned long *dst, unsigned int nbits)
221{
222 if (small_const_nbits(nbits))
223 *dst = ~0UL;
224 else {
225 unsigned int len = BITS_TO_LONGS(nbits) * sizeof(unsigned long);
226 memset(dst, 0xff, len);
227 }
228}
229
230static inline void bitmap_copy(unsigned long *dst, const unsigned long *src,
231 unsigned int nbits)
232{
233 if (small_const_nbits(nbits))
234 *dst = *src;
235 else {
236 unsigned int len = BITS_TO_LONGS(nbits) * sizeof(unsigned long);
237 memcpy(dst, src, len);
238 }
239}
240
241
242
243
244static inline void bitmap_copy_clear_tail(unsigned long *dst,
245 const unsigned long *src, unsigned int nbits)
246{
247 bitmap_copy(dst, src, nbits);
248 if (nbits % BITS_PER_LONG)
249 dst[nbits / BITS_PER_LONG] &= BITMAP_LAST_WORD_MASK(nbits);
250}
251
252
253
254
255
256#if BITS_PER_LONG == 64
257extern void bitmap_from_arr32(unsigned long *bitmap, const u32 *buf,
258 unsigned int nbits);
259extern void bitmap_to_arr32(u32 *buf, const unsigned long *bitmap,
260 unsigned int nbits);
261#else
262#define bitmap_from_arr32(bitmap, buf, nbits) \
263 bitmap_copy_clear_tail((unsigned long *) (bitmap), \
264 (const unsigned long *) (buf), (nbits))
265#define bitmap_to_arr32(buf, bitmap, nbits) \
266 bitmap_copy_clear_tail((unsigned long *) (buf), \
267 (const unsigned long *) (bitmap), (nbits))
268#endif
269
270static inline int bitmap_and(unsigned long *dst, const unsigned long *src1,
271 const unsigned long *src2, unsigned int nbits)
272{
273 if (small_const_nbits(nbits))
274 return (*dst = *src1 & *src2 & BITMAP_LAST_WORD_MASK(nbits)) != 0;
275 return __bitmap_and(dst, src1, src2, nbits);
276}
277
278static inline void bitmap_or(unsigned long *dst, const unsigned long *src1,
279 const unsigned long *src2, unsigned int nbits)
280{
281 if (small_const_nbits(nbits))
282 *dst = *src1 | *src2;
283 else
284 __bitmap_or(dst, src1, src2, nbits);
285}
286
287static inline void bitmap_xor(unsigned long *dst, const unsigned long *src1,
288 const unsigned long *src2, unsigned int nbits)
289{
290 if (small_const_nbits(nbits))
291 *dst = *src1 ^ *src2;
292 else
293 __bitmap_xor(dst, src1, src2, nbits);
294}
295
296static inline int bitmap_andnot(unsigned long *dst, const unsigned long *src1,
297 const unsigned long *src2, unsigned int nbits)
298{
299 if (small_const_nbits(nbits))
300 return (*dst = *src1 & ~(*src2) & BITMAP_LAST_WORD_MASK(nbits)) != 0;
301 return __bitmap_andnot(dst, src1, src2, nbits);
302}
303
304static inline void bitmap_complement(unsigned long *dst, const unsigned long *src,
305 unsigned int nbits)
306{
307 if (small_const_nbits(nbits))
308 *dst = ~(*src);
309 else
310 __bitmap_complement(dst, src, nbits);
311}
312
313#ifdef __LITTLE_ENDIAN
314#define BITMAP_MEM_ALIGNMENT 8
315#else
316#define BITMAP_MEM_ALIGNMENT (8 * sizeof(unsigned long))
317#endif
318#define BITMAP_MEM_MASK (BITMAP_MEM_ALIGNMENT - 1)
319
320static inline int bitmap_equal(const unsigned long *src1,
321 const unsigned long *src2, unsigned int nbits)
322{
323 if (small_const_nbits(nbits))
324 return !((*src1 ^ *src2) & BITMAP_LAST_WORD_MASK(nbits));
325 if (__builtin_constant_p(nbits & BITMAP_MEM_MASK) &&
326 IS_ALIGNED(nbits, BITMAP_MEM_ALIGNMENT))
327 return !memcmp(src1, src2, nbits / 8);
328 return __bitmap_equal(src1, src2, nbits);
329}
330
331static inline int bitmap_intersects(const unsigned long *src1,
332 const unsigned long *src2, unsigned int nbits)
333{
334 if (small_const_nbits(nbits))
335 return ((*src1 & *src2) & BITMAP_LAST_WORD_MASK(nbits)) != 0;
336 else
337 return __bitmap_intersects(src1, src2, nbits);
338}
339
340static inline int bitmap_subset(const unsigned long *src1,
341 const unsigned long *src2, unsigned int nbits)
342{
343 if (small_const_nbits(nbits))
344 return ! ((*src1 & ~(*src2)) & BITMAP_LAST_WORD_MASK(nbits));
345 else
346 return __bitmap_subset(src1, src2, nbits);
347}
348
349static inline int bitmap_empty(const unsigned long *src, unsigned nbits)
350{
351 if (small_const_nbits(nbits))
352 return ! (*src & BITMAP_LAST_WORD_MASK(nbits));
353
354 return find_first_bit(src, nbits) == nbits;
355}
356
357static inline int bitmap_full(const unsigned long *src, unsigned int nbits)
358{
359 if (small_const_nbits(nbits))
360 return ! (~(*src) & BITMAP_LAST_WORD_MASK(nbits));
361
362 return find_first_zero_bit(src, nbits) == nbits;
363}
364
365static __always_inline int bitmap_weight(const unsigned long *src, unsigned int nbits)
366{
367 if (small_const_nbits(nbits))
368 return hweight_long(*src & BITMAP_LAST_WORD_MASK(nbits));
369 return __bitmap_weight(src, nbits);
370}
371
372static __always_inline void bitmap_set(unsigned long *map, unsigned int start,
373 unsigned int nbits)
374{
375 if (__builtin_constant_p(nbits) && nbits == 1)
376 __set_bit(start, map);
377 else if (__builtin_constant_p(start & BITMAP_MEM_MASK) &&
378 IS_ALIGNED(start, BITMAP_MEM_ALIGNMENT) &&
379 __builtin_constant_p(nbits & BITMAP_MEM_MASK) &&
380 IS_ALIGNED(nbits, BITMAP_MEM_ALIGNMENT))
381 memset((char *)map + start / 8, 0xff, nbits / 8);
382 else
383 __bitmap_set(map, start, nbits);
384}
385
386static __always_inline void bitmap_clear(unsigned long *map, unsigned int start,
387 unsigned int nbits)
388{
389 if (__builtin_constant_p(nbits) && nbits == 1)
390 __clear_bit(start, map);
391 else if (__builtin_constant_p(start & BITMAP_MEM_MASK) &&
392 IS_ALIGNED(start, BITMAP_MEM_ALIGNMENT) &&
393 __builtin_constant_p(nbits & BITMAP_MEM_MASK) &&
394 IS_ALIGNED(nbits, BITMAP_MEM_ALIGNMENT))
395 memset((char *)map + start / 8, 0, nbits / 8);
396 else
397 __bitmap_clear(map, start, nbits);
398}
399
400static inline void bitmap_shift_right(unsigned long *dst, const unsigned long *src,
401 unsigned int shift, int nbits)
402{
403 if (small_const_nbits(nbits))
404 *dst = (*src & BITMAP_LAST_WORD_MASK(nbits)) >> shift;
405 else
406 __bitmap_shift_right(dst, src, shift, nbits);
407}
408
409static inline void bitmap_shift_left(unsigned long *dst, const unsigned long *src,
410 unsigned int shift, unsigned int nbits)
411{
412 if (small_const_nbits(nbits))
413 *dst = (*src << shift) & BITMAP_LAST_WORD_MASK(nbits);
414 else
415 __bitmap_shift_left(dst, src, shift, nbits);
416}
417
418static inline int bitmap_parse(const char *buf, unsigned int buflen,
419 unsigned long *maskp, int nmaskbits)
420{
421 return __bitmap_parse(buf, buflen, 0, maskp, nmaskbits);
422}
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450#if __BITS_PER_LONG == 64
451#define BITMAP_FROM_U64(n) (n)
452#else
453#define BITMAP_FROM_U64(n) ((unsigned long) ((u64)(n) & ULONG_MAX)), \
454 ((unsigned long) ((u64)(n) >> 32))
455#endif
456
457
458
459
460
461
462
463
464
465
466
467static inline void bitmap_from_u64(unsigned long *dst, u64 mask)
468{
469 dst[0] = mask & ULONG_MAX;
470
471 if (sizeof(mask) > sizeof(unsigned long))
472 dst[1] = mask >> 32;
473}
474
475#endif
476
477#endif
478