Nicholas Flintham | 1e3d311 | 2013-04-10 10:48:38 +0100 | [diff] [blame^] | 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 | #include <linux/kernel.h> |
| 10 | |
| 11 | |
| 12 | |
| 13 | |
| 14 | |
| 15 | |
| 16 | extern int __bitmap_empty(const unsigned long *bitmap, int bits); |
| 17 | extern int __bitmap_full(const unsigned long *bitmap, int bits); |
| 18 | extern int __bitmap_equal(const unsigned long *bitmap1, |
| 19 | const unsigned long *bitmap2, int bits); |
| 20 | extern void __bitmap_complement(unsigned long *dst, const unsigned long *src, |
| 21 | int bits); |
| 22 | extern void __bitmap_shift_right(unsigned long *dst, |
| 23 | const unsigned long *src, int shift, int bits); |
| 24 | extern void __bitmap_shift_left(unsigned long *dst, |
| 25 | const unsigned long *src, int shift, int bits); |
| 26 | extern int __bitmap_and(unsigned long *dst, const unsigned long *bitmap1, |
| 27 | const unsigned long *bitmap2, int bits); |
| 28 | extern void __bitmap_or(unsigned long *dst, const unsigned long *bitmap1, |
| 29 | const unsigned long *bitmap2, int bits); |
| 30 | extern void __bitmap_xor(unsigned long *dst, const unsigned long *bitmap1, |
| 31 | const unsigned long *bitmap2, int bits); |
| 32 | extern int __bitmap_andnot(unsigned long *dst, const unsigned long *bitmap1, |
| 33 | const unsigned long *bitmap2, int bits); |
| 34 | extern int __bitmap_intersects(const unsigned long *bitmap1, |
| 35 | const unsigned long *bitmap2, int bits); |
| 36 | extern int __bitmap_subset(const unsigned long *bitmap1, |
| 37 | const unsigned long *bitmap2, int bits); |
| 38 | extern int __bitmap_weight(const unsigned long *bitmap, int bits); |
| 39 | |
| 40 | extern void bitmap_set(unsigned long *map, int i, int len); |
| 41 | extern void bitmap_clear(unsigned long *map, int start, int nr); |
| 42 | |
| 43 | extern 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 | |
| 50 | static inline unsigned long |
| 51 | bitmap_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 | |
| 61 | extern int bitmap_scnprintf(char *buf, unsigned int len, |
| 62 | const unsigned long *src, int nbits); |
| 63 | extern int __bitmap_parse(const char *buf, unsigned int buflen, int is_user, |
| 64 | unsigned long *dst, int nbits); |
| 65 | extern int bitmap_parse_user(const char __user *ubuf, unsigned int ulen, |
| 66 | unsigned long *dst, int nbits); |
| 67 | extern int bitmap_scnlistprintf(char *buf, unsigned int len, |
| 68 | const unsigned long *src, int nbits); |
| 69 | extern int bitmap_parselist(const char *buf, unsigned long *maskp, |
| 70 | int nmaskbits); |
| 71 | extern int bitmap_parselist_user(const char __user *ubuf, unsigned int ulen, |
| 72 | unsigned long *dst, int nbits); |
| 73 | extern void bitmap_remap(unsigned long *dst, const unsigned long *src, |
| 74 | const unsigned long *old, const unsigned long *new, int bits); |
| 75 | extern int bitmap_bitremap(int oldbit, |
| 76 | const unsigned long *old, const unsigned long *new, int bits); |
| 77 | extern void bitmap_onto(unsigned long *dst, const unsigned long *orig, |
| 78 | const unsigned long *relmap, int bits); |
| 79 | extern void bitmap_fold(unsigned long *dst, const unsigned long *orig, |
| 80 | int sz, int bits); |
| 81 | extern int bitmap_find_free_region(unsigned long *bitmap, int bits, int order); |
| 82 | extern void bitmap_release_region(unsigned long *bitmap, int pos, int order); |
| 83 | extern int bitmap_allocate_region(unsigned long *bitmap, int pos, int order); |
| 84 | extern void bitmap_copy_le(void *dst, const unsigned long *src, int nbits); |
| 85 | extern 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 | |
| 97 | static 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 | |
| 107 | static 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 | |
| 117 | static 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 | |
| 128 | static 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 | |
| 136 | static 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 | |
| 145 | static 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 | |
| 154 | static 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 | |
| 162 | static 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 | |
| 171 | static 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 | |
| 180 | static 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 | |
| 189 | static 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 | |
| 198 | static 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 | |
| 206 | static 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 | |
| 214 | static 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 | |
| 221 | static 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 | |
| 230 | static 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 | |
| 239 | static 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 |