1 #ifndef __XEN_BITMAP_H
2 #define __XEN_BITMAP_H
3
4 #ifndef __ASSEMBLY__
5
6 #include <xen/lib.h>
7 #include <xen/types.h>
8 #include <xen/bitops.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 *
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_scnlistprintf(buf, len, src, nbits) Print bitmap src as list to buf
45 */
46
47 /*
48 * Also the following operations in asm/bitops.h apply to bitmaps.
49 *
50 * set_bit(bit, addr) *addr |= bit
51 * clear_bit(bit, addr) *addr &= ~bit
52 * change_bit(bit, addr) *addr ^= bit
53 * test_bit(bit, addr) Is bit set in *addr?
54 * test_and_set_bit(bit, addr) Set bit and return old value
55 * test_and_clear_bit(bit, addr) Clear bit and return old value
56 * test_and_change_bit(bit, addr) Change bit and return old value
57 * find_first_zero_bit(addr, nbits) Position first zero bit in *addr
58 * find_first_bit(addr, nbits) Position first set bit in *addr
59 * find_next_zero_bit(addr, nbits, bit) Position next zero bit in *addr >= bit
60 * find_next_bit(addr, nbits, bit) Position next set bit in *addr >= bit
61 */
62
63 /*
64 * The DECLARE_BITMAP(name,bits) macro, in xen/types.h, can be used
65 * to declare an array named 'name' of just enough unsigned longs to
66 * contain all bit positions from 0 to 'bits' - 1.
67 */
68
69 /*
70 * lib/bitmap.c provides these functions:
71 */
72
73 extern int __bitmap_empty(const unsigned long *bitmap, int bits);
74 extern int __bitmap_full(const unsigned long *bitmap, int bits);
75 extern int __bitmap_equal(const unsigned long *bitmap1,
76 const unsigned long *bitmap2, int bits);
77 extern void __bitmap_complement(unsigned long *dst, const unsigned long *src,
78 int bits);
79 extern void __bitmap_shift_right(unsigned long *dst,
80 const unsigned long *src, int shift, int bits);
81 extern void __bitmap_shift_left(unsigned long *dst,
82 const unsigned long *src, int shift, int bits);
83 extern void __bitmap_and(unsigned long *dst, const unsigned long *bitmap1,
84 const unsigned long *bitmap2, int bits);
85 extern void __bitmap_or(unsigned long *dst, const unsigned long *bitmap1,
86 const unsigned long *bitmap2, int bits);
87 extern void __bitmap_xor(unsigned long *dst, const unsigned long *bitmap1,
88 const unsigned long *bitmap2, int bits);
89 extern void __bitmap_andnot(unsigned long *dst, const unsigned long *bitmap1,
90 const unsigned long *bitmap2, int bits);
91 extern int __bitmap_intersects(const unsigned long *bitmap1,
92 const unsigned long *bitmap2, int bits);
93 extern int __bitmap_subset(const unsigned long *bitmap1,
94 const unsigned long *bitmap2, int bits);
95 extern int __bitmap_weight(const unsigned long *bitmap, int bits);
96
97 extern int bitmap_scnprintf(char *buf, unsigned int len,
98 const unsigned long *src, int nbits);
99 extern int bitmap_scnlistprintf(char *buf, unsigned int len,
100 const unsigned long *src, int nbits);
101 extern int bitmap_find_free_region(unsigned long *bitmap, int bits, int order);
102 extern void bitmap_release_region(unsigned long *bitmap, int pos, int order);
103 extern 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
111 #define bitmap_bytes(nbits) (BITS_TO_LONGS(nbits) * sizeof(unsigned long))
112
113 #define bitmap_switch(nbits, zero, small, large) \
114 unsigned int n__ = (nbits); \
115 if (__builtin_constant_p(nbits) && !n__) { \
116 zero; \
117 } else if (__builtin_constant_p(nbits) && n__ <= BITS_PER_LONG) { \
118 small; \
119 } else { \
120 large; \
121 }
122
bitmap_zero(unsigned long * dst,int nbits)123 static inline void bitmap_zero(unsigned long *dst, int nbits)
124 {
125 bitmap_switch(nbits,,
126 *dst = 0UL,
127 memset(dst, 0, bitmap_bytes(nbits)));
128 }
129
bitmap_fill(unsigned long * dst,int nbits)130 static inline void bitmap_fill(unsigned long *dst, int nbits)
131 {
132 size_t nlongs = BITS_TO_LONGS(nbits);
133
134 switch (nlongs) {
135 default:
136 memset(dst, -1, (nlongs - 1) * sizeof(unsigned long));
137 /* fall through */
138 case 1:
139 dst[nlongs - 1] = BITMAP_LAST_WORD_MASK(nbits);
140 break;
141 }
142 }
143
bitmap_copy(unsigned long * dst,const unsigned long * src,int nbits)144 static inline void bitmap_copy(unsigned long *dst, const unsigned long *src,
145 int nbits)
146 {
147 bitmap_switch(nbits,,
148 *dst = *src,
149 memcpy(dst, src, bitmap_bytes(nbits)));
150 }
151
bitmap_and(unsigned long * dst,const unsigned long * src1,const unsigned long * src2,int nbits)152 static inline void bitmap_and(unsigned long *dst, const unsigned long *src1,
153 const unsigned long *src2, int nbits)
154 {
155 bitmap_switch(nbits,,
156 *dst = *src1 & *src2,
157 __bitmap_and(dst, src1, src2, nbits));
158 }
159
bitmap_or(unsigned long * dst,const unsigned long * src1,const unsigned long * src2,int nbits)160 static inline void bitmap_or(unsigned long *dst, const unsigned long *src1,
161 const unsigned long *src2, int nbits)
162 {
163 bitmap_switch(nbits,,
164 *dst = *src1 | *src2,
165 __bitmap_or(dst, src1, src2, nbits));
166 }
167
bitmap_xor(unsigned long * dst,const unsigned long * src1,const unsigned long * src2,int nbits)168 static inline void bitmap_xor(unsigned long *dst, const unsigned long *src1,
169 const unsigned long *src2, int nbits)
170 {
171 bitmap_switch(nbits,,
172 *dst = *src1 ^ *src2,
173 __bitmap_xor(dst, src1, src2, nbits));
174 }
175
bitmap_andnot(unsigned long * dst,const unsigned long * src1,const unsigned long * src2,int nbits)176 static inline void bitmap_andnot(unsigned long *dst, const unsigned long *src1,
177 const unsigned long *src2, int nbits)
178 {
179 bitmap_switch(nbits,,
180 *dst = *src1 & ~*src2,
181 __bitmap_andnot(dst, src1, src2, nbits));
182 }
183
bitmap_complement(unsigned long * dst,const unsigned long * src,int nbits)184 static inline void bitmap_complement(unsigned long *dst, const unsigned long *src,
185 int nbits)
186 {
187 bitmap_switch(nbits,,
188 *dst = ~*src & BITMAP_LAST_WORD_MASK(nbits),
189 __bitmap_complement(dst, src, nbits));
190 }
191
bitmap_equal(const unsigned long * src1,const unsigned long * src2,int nbits)192 static inline int bitmap_equal(const unsigned long *src1,
193 const unsigned long *src2, int nbits)
194 {
195 bitmap_switch(nbits,
196 return -1,
197 return !((*src1 ^ *src2) & BITMAP_LAST_WORD_MASK(nbits)),
198 return __bitmap_equal(src1, src2, nbits));
199 }
200
bitmap_intersects(const unsigned long * src1,const unsigned long * src2,int nbits)201 static inline int bitmap_intersects(const unsigned long *src1,
202 const unsigned long *src2, int nbits)
203 {
204 bitmap_switch(nbits,
205 return -1,
206 return ((*src1 & *src2) & BITMAP_LAST_WORD_MASK(nbits)) != 0,
207 return __bitmap_intersects(src1, src2, nbits));
208 }
209
bitmap_subset(const unsigned long * src1,const unsigned long * src2,int nbits)210 static inline int bitmap_subset(const unsigned long *src1,
211 const unsigned long *src2, int nbits)
212 {
213 bitmap_switch(nbits,
214 return -1,
215 return !((*src1 & ~*src2) & BITMAP_LAST_WORD_MASK(nbits)),
216 return __bitmap_subset(src1, src2, nbits));
217 }
218
bitmap_empty(const unsigned long * src,int nbits)219 static inline int bitmap_empty(const unsigned long *src, int nbits)
220 {
221 bitmap_switch(nbits,
222 return -1,
223 return !(*src & BITMAP_LAST_WORD_MASK(nbits)),
224 return __bitmap_empty(src, nbits));
225 }
226
bitmap_full(const unsigned long * src,int nbits)227 static inline int bitmap_full(const unsigned long *src, int nbits)
228 {
229 bitmap_switch(nbits,
230 return -1,
231 return !(~*src & BITMAP_LAST_WORD_MASK(nbits)),
232 return __bitmap_full(src, nbits));
233 }
234
bitmap_weight(const unsigned long * src,int nbits)235 static inline int bitmap_weight(const unsigned long *src, int nbits)
236 {
237 return __bitmap_weight(src, nbits);
238 }
239
bitmap_shift_right(unsigned long * dst,const unsigned long * src,int n,int nbits)240 static inline void bitmap_shift_right(unsigned long *dst,
241 const unsigned long *src, int n, int nbits)
242 {
243 bitmap_switch(nbits,,
244 *dst = *src >> n,
245 __bitmap_shift_right(dst, src, n, nbits));
246 }
247
bitmap_shift_left(unsigned long * dst,const unsigned long * src,int n,int nbits)248 static inline void bitmap_shift_left(unsigned long *dst,
249 const unsigned long *src, int n, int nbits)
250 {
251 bitmap_switch(nbits,,
252 *dst = (*src << n) & BITMAP_LAST_WORD_MASK(nbits),
253 __bitmap_shift_left(dst, src, n, nbits));
254 }
255
256 #undef bitmap_switch
257 #undef bitmap_bytes
258
259 void bitmap_long_to_byte(uint8_t *bp, const unsigned long *lp, int nbits);
260 void bitmap_byte_to_long(unsigned long *lp, const uint8_t *bp, int nbits);
261
262 #endif /* __ASSEMBLY__ */
263
264 #endif /* __XEN_BITMAP_H */
265