1 #ifndef KVM__BITMAP_H
2 #define KVM__BITMAP_H
3
4 #include <stdbool.h>
5 #include <string.h>
6
7 #include "linux/bitops.h"
8
9 #define DECLARE_BITMAP(name,bits) \
10 unsigned long name[BITS_TO_LONGS(bits)]
11
12 #define BITMAP_FIRST_WORD_MASK(start) (~0UL << ((start) & (BITS_PER_LONG - 1)))
13 #define BITMAP_LAST_WORD_MASK(nbits) (~0UL >> (-(nbits) & (BITS_PER_LONG - 1)))
14
bitmap_zero(unsigned long * dst,unsigned int nbits)15 static inline void bitmap_zero(unsigned long *dst, unsigned int nbits)
16 {
17 unsigned int len = BITS_TO_LONGS(nbits) * sizeof(unsigned long);
18 memset(dst, 0, len);
19 }
20
21 #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
22 #define BITMAP_MEM_ALIGNMENT 8
23 #else
24 #define BITMAP_MEM_ALIGNMENT (8 * sizeof(unsigned long))
25 #endif
26 #define BITMAP_MEM_MASK (BITMAP_MEM_ALIGNMENT - 1)
27
28 void __bitmap_set(unsigned long *map, unsigned int start, int len);
29
bitmap_set(unsigned long * map,unsigned int start,unsigned int nbits)30 static inline void bitmap_set(unsigned long *map, unsigned int start,
31 unsigned int nbits)
32 {
33 if (__builtin_constant_p(nbits) && nbits == 1)
34 set_bit(start, map);
35 else if (__builtin_constant_p(start & BITMAP_MEM_MASK) &&
36 IS_ALIGNED(start, BITMAP_MEM_ALIGNMENT) &&
37 __builtin_constant_p(nbits & BITMAP_MEM_MASK) &&
38 IS_ALIGNED(nbits, BITMAP_MEM_ALIGNMENT))
39 memset((char *)map + start / 8, 0xff, nbits / 8);
40 else
41 __bitmap_set(map, start, nbits);
42 }
43
44 bool __bitmap_and(unsigned long *dst, const unsigned long *src1,
45 const unsigned long *src2, unsigned int nbits);
46
bitmap_and(unsigned long * dst,const unsigned long * src1,const unsigned long * src2,unsigned int nbits)47 static inline bool bitmap_and(unsigned long *dst, const unsigned long *src1,
48 const unsigned long *src2, unsigned int nbits)
49 {
50 if (nbits >= 0 && nbits <= BITS_PER_LONG)
51 return (*dst = *src1 & *src2 & BITMAP_LAST_WORD_MASK(nbits)) != 0;
52
53 return __bitmap_and(dst, src1, src2, nbits);
54 }
55
56 int bitmap_parselist(const char *buf, unsigned long *maskp, int nmaskbits);
57
58 bool __bitmap_subset(const unsigned long *bitmap1, const unsigned long *bitmap2,
59 unsigned int nbits);
60
bitmap_subset(const unsigned long * src1,const unsigned long * src2,unsigned int nbits)61 static inline bool bitmap_subset(const unsigned long *src1,
62 const unsigned long *src2, unsigned int nbits)
63 {
64 if (nbits >= 0 && nbits <= BITS_PER_LONG)
65 return !((*src1 & ~(*src2)) & BITMAP_LAST_WORD_MASK(nbits));
66
67 return __bitmap_subset(src1, src2, nbits);
68 }
69
70
71 #endif /* KVM__BITMAP_H */
72