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