0001
0002 #ifndef __LINUX_BITMAP_H
0003 #define __LINUX_BITMAP_H
0004
0005 #ifndef __ASSEMBLY__
0006
0007 #include <linux/align.h>
0008 #include <linux/bitops.h>
0009 #include <linux/find.h>
0010 #include <linux/limits.h>
0011 #include <linux/string.h>
0012 #include <linux/types.h>
0013
0014 struct device;
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027
0028
0029
0030
0031
0032
0033
0034
0035
0036
0037
0038
0039
0040
0041
0042
0043
0044
0045
0046
0047
0048
0049
0050
0051
0052
0053
0054
0055
0056
0057
0058
0059
0060
0061
0062
0063
0064
0065
0066
0067
0068
0069
0070
0071
0072
0073
0074
0075
0076
0077
0078
0079
0080
0081
0082
0083
0084
0085
0086
0087
0088
0089
0090
0091
0092
0093
0094
0095
0096
0097
0098
0099
0100
0101
0102
0103
0104
0105
0106
0107
0108
0109
0110
0111
0112
0113
0114
0115
0116
0117
0118
0119
0120
0121 unsigned long *bitmap_alloc(unsigned int nbits, gfp_t flags);
0122 unsigned long *bitmap_zalloc(unsigned int nbits, gfp_t flags);
0123 unsigned long *bitmap_alloc_node(unsigned int nbits, gfp_t flags, int node);
0124 unsigned long *bitmap_zalloc_node(unsigned int nbits, gfp_t flags, int node);
0125 void bitmap_free(const unsigned long *bitmap);
0126
0127
0128 unsigned long *devm_bitmap_alloc(struct device *dev,
0129 unsigned int nbits, gfp_t flags);
0130 unsigned long *devm_bitmap_zalloc(struct device *dev,
0131 unsigned int nbits, gfp_t flags);
0132
0133
0134
0135
0136
0137 bool __bitmap_equal(const unsigned long *bitmap1,
0138 const unsigned long *bitmap2, unsigned int nbits);
0139 bool __pure __bitmap_or_equal(const unsigned long *src1,
0140 const unsigned long *src2,
0141 const unsigned long *src3,
0142 unsigned int nbits);
0143 void __bitmap_complement(unsigned long *dst, const unsigned long *src,
0144 unsigned int nbits);
0145 void __bitmap_shift_right(unsigned long *dst, const unsigned long *src,
0146 unsigned int shift, unsigned int nbits);
0147 void __bitmap_shift_left(unsigned long *dst, const unsigned long *src,
0148 unsigned int shift, unsigned int nbits);
0149 void bitmap_cut(unsigned long *dst, const unsigned long *src,
0150 unsigned int first, unsigned int cut, unsigned int nbits);
0151 bool __bitmap_and(unsigned long *dst, const unsigned long *bitmap1,
0152 const unsigned long *bitmap2, unsigned int nbits);
0153 void __bitmap_or(unsigned long *dst, const unsigned long *bitmap1,
0154 const unsigned long *bitmap2, unsigned int nbits);
0155 void __bitmap_xor(unsigned long *dst, const unsigned long *bitmap1,
0156 const unsigned long *bitmap2, unsigned int nbits);
0157 bool __bitmap_andnot(unsigned long *dst, const unsigned long *bitmap1,
0158 const unsigned long *bitmap2, unsigned int nbits);
0159 void __bitmap_replace(unsigned long *dst,
0160 const unsigned long *old, const unsigned long *new,
0161 const unsigned long *mask, unsigned int nbits);
0162 bool __bitmap_intersects(const unsigned long *bitmap1,
0163 const unsigned long *bitmap2, unsigned int nbits);
0164 bool __bitmap_subset(const unsigned long *bitmap1,
0165 const unsigned long *bitmap2, unsigned int nbits);
0166 unsigned int __bitmap_weight(const unsigned long *bitmap, unsigned int nbits);
0167 void __bitmap_set(unsigned long *map, unsigned int start, int len);
0168 void __bitmap_clear(unsigned long *map, unsigned int start, int len);
0169
0170 unsigned long bitmap_find_next_zero_area_off(unsigned long *map,
0171 unsigned long size,
0172 unsigned long start,
0173 unsigned int nr,
0174 unsigned long align_mask,
0175 unsigned long align_offset);
0176
0177
0178
0179
0180
0181
0182
0183
0184
0185
0186
0187
0188
0189 static inline unsigned long
0190 bitmap_find_next_zero_area(unsigned long *map,
0191 unsigned long size,
0192 unsigned long start,
0193 unsigned int nr,
0194 unsigned long align_mask)
0195 {
0196 return bitmap_find_next_zero_area_off(map, size, start, nr,
0197 align_mask, 0);
0198 }
0199
0200 int bitmap_parse(const char *buf, unsigned int buflen,
0201 unsigned long *dst, int nbits);
0202 int bitmap_parse_user(const char __user *ubuf, unsigned int ulen,
0203 unsigned long *dst, int nbits);
0204 int bitmap_parselist(const char *buf, unsigned long *maskp,
0205 int nmaskbits);
0206 int bitmap_parselist_user(const char __user *ubuf, unsigned int ulen,
0207 unsigned long *dst, int nbits);
0208 void bitmap_remap(unsigned long *dst, const unsigned long *src,
0209 const unsigned long *old, const unsigned long *new, unsigned int nbits);
0210 int bitmap_bitremap(int oldbit,
0211 const unsigned long *old, const unsigned long *new, int bits);
0212 void bitmap_onto(unsigned long *dst, const unsigned long *orig,
0213 const unsigned long *relmap, unsigned int bits);
0214 void bitmap_fold(unsigned long *dst, const unsigned long *orig,
0215 unsigned int sz, unsigned int nbits);
0216 int bitmap_find_free_region(unsigned long *bitmap, unsigned int bits, int order);
0217 void bitmap_release_region(unsigned long *bitmap, unsigned int pos, int order);
0218 int bitmap_allocate_region(unsigned long *bitmap, unsigned int pos, int order);
0219
0220 #ifdef __BIG_ENDIAN
0221 void bitmap_copy_le(unsigned long *dst, const unsigned long *src, unsigned int nbits);
0222 #else
0223 #define bitmap_copy_le bitmap_copy
0224 #endif
0225 unsigned int bitmap_ord_to_pos(const unsigned long *bitmap, unsigned int ord, unsigned int nbits);
0226 int bitmap_print_to_pagebuf(bool list, char *buf,
0227 const unsigned long *maskp, int nmaskbits);
0228
0229 extern int bitmap_print_bitmask_to_buf(char *buf, const unsigned long *maskp,
0230 int nmaskbits, loff_t off, size_t count);
0231
0232 extern int bitmap_print_list_to_buf(char *buf, const unsigned long *maskp,
0233 int nmaskbits, loff_t off, size_t count);
0234
0235 #define BITMAP_FIRST_WORD_MASK(start) (~0UL << ((start) & (BITS_PER_LONG - 1)))
0236 #define BITMAP_LAST_WORD_MASK(nbits) (~0UL >> (-(nbits) & (BITS_PER_LONG - 1)))
0237
0238 static inline void bitmap_zero(unsigned long *dst, unsigned int nbits)
0239 {
0240 unsigned int len = BITS_TO_LONGS(nbits) * sizeof(unsigned long);
0241
0242 if (small_const_nbits(nbits))
0243 *dst = 0;
0244 else
0245 memset(dst, 0, len);
0246 }
0247
0248 static inline void bitmap_fill(unsigned long *dst, unsigned int nbits)
0249 {
0250 unsigned int len = BITS_TO_LONGS(nbits) * sizeof(unsigned long);
0251
0252 if (small_const_nbits(nbits))
0253 *dst = ~0UL;
0254 else
0255 memset(dst, 0xff, len);
0256 }
0257
0258 static inline void bitmap_copy(unsigned long *dst, const unsigned long *src,
0259 unsigned int nbits)
0260 {
0261 unsigned int len = BITS_TO_LONGS(nbits) * sizeof(unsigned long);
0262
0263 if (small_const_nbits(nbits))
0264 *dst = *src;
0265 else
0266 memcpy(dst, src, len);
0267 }
0268
0269
0270
0271
0272 static inline void bitmap_copy_clear_tail(unsigned long *dst,
0273 const unsigned long *src, unsigned int nbits)
0274 {
0275 bitmap_copy(dst, src, nbits);
0276 if (nbits % BITS_PER_LONG)
0277 dst[nbits / BITS_PER_LONG] &= BITMAP_LAST_WORD_MASK(nbits);
0278 }
0279
0280
0281
0282
0283
0284
0285
0286
0287
0288 #if BITS_PER_LONG == 64
0289 void bitmap_from_arr32(unsigned long *bitmap, const u32 *buf,
0290 unsigned int nbits);
0291 void bitmap_to_arr32(u32 *buf, const unsigned long *bitmap,
0292 unsigned int nbits);
0293 #else
0294 #define bitmap_from_arr32(bitmap, buf, nbits) \
0295 bitmap_copy_clear_tail((unsigned long *) (bitmap), \
0296 (const unsigned long *) (buf), (nbits))
0297 #define bitmap_to_arr32(buf, bitmap, nbits) \
0298 bitmap_copy_clear_tail((unsigned long *) (buf), \
0299 (const unsigned long *) (bitmap), (nbits))
0300 #endif
0301
0302
0303
0304
0305
0306
0307
0308 #if (BITS_PER_LONG == 32) && defined(__BIG_ENDIAN)
0309 void bitmap_from_arr64(unsigned long *bitmap, const u64 *buf, unsigned int nbits);
0310 void bitmap_to_arr64(u64 *buf, const unsigned long *bitmap, unsigned int nbits);
0311 #else
0312 #define bitmap_from_arr64(bitmap, buf, nbits) \
0313 bitmap_copy_clear_tail((unsigned long *)(bitmap), (const unsigned long *)(buf), (nbits))
0314 #define bitmap_to_arr64(buf, bitmap, nbits) \
0315 bitmap_copy_clear_tail((unsigned long *)(buf), (const unsigned long *)(bitmap), (nbits))
0316 #endif
0317
0318 static inline bool bitmap_and(unsigned long *dst, const unsigned long *src1,
0319 const unsigned long *src2, unsigned int nbits)
0320 {
0321 if (small_const_nbits(nbits))
0322 return (*dst = *src1 & *src2 & BITMAP_LAST_WORD_MASK(nbits)) != 0;
0323 return __bitmap_and(dst, src1, src2, nbits);
0324 }
0325
0326 static inline void bitmap_or(unsigned long *dst, const unsigned long *src1,
0327 const unsigned long *src2, unsigned int nbits)
0328 {
0329 if (small_const_nbits(nbits))
0330 *dst = *src1 | *src2;
0331 else
0332 __bitmap_or(dst, src1, src2, nbits);
0333 }
0334
0335 static inline void bitmap_xor(unsigned long *dst, const unsigned long *src1,
0336 const unsigned long *src2, unsigned int nbits)
0337 {
0338 if (small_const_nbits(nbits))
0339 *dst = *src1 ^ *src2;
0340 else
0341 __bitmap_xor(dst, src1, src2, nbits);
0342 }
0343
0344 static inline bool bitmap_andnot(unsigned long *dst, const unsigned long *src1,
0345 const unsigned long *src2, unsigned int nbits)
0346 {
0347 if (small_const_nbits(nbits))
0348 return (*dst = *src1 & ~(*src2) & BITMAP_LAST_WORD_MASK(nbits)) != 0;
0349 return __bitmap_andnot(dst, src1, src2, nbits);
0350 }
0351
0352 static inline void bitmap_complement(unsigned long *dst, const unsigned long *src,
0353 unsigned int nbits)
0354 {
0355 if (small_const_nbits(nbits))
0356 *dst = ~(*src);
0357 else
0358 __bitmap_complement(dst, src, nbits);
0359 }
0360
0361 #ifdef __LITTLE_ENDIAN
0362 #define BITMAP_MEM_ALIGNMENT 8
0363 #else
0364 #define BITMAP_MEM_ALIGNMENT (8 * sizeof(unsigned long))
0365 #endif
0366 #define BITMAP_MEM_MASK (BITMAP_MEM_ALIGNMENT - 1)
0367
0368 static inline bool bitmap_equal(const unsigned long *src1,
0369 const unsigned long *src2, unsigned int nbits)
0370 {
0371 if (small_const_nbits(nbits))
0372 return !((*src1 ^ *src2) & BITMAP_LAST_WORD_MASK(nbits));
0373 if (__builtin_constant_p(nbits & BITMAP_MEM_MASK) &&
0374 IS_ALIGNED(nbits, BITMAP_MEM_ALIGNMENT))
0375 return !memcmp(src1, src2, nbits / 8);
0376 return __bitmap_equal(src1, src2, nbits);
0377 }
0378
0379
0380
0381
0382
0383
0384
0385
0386
0387
0388 static inline bool bitmap_or_equal(const unsigned long *src1,
0389 const unsigned long *src2,
0390 const unsigned long *src3,
0391 unsigned int nbits)
0392 {
0393 if (!small_const_nbits(nbits))
0394 return __bitmap_or_equal(src1, src2, src3, nbits);
0395
0396 return !(((*src1 | *src2) ^ *src3) & BITMAP_LAST_WORD_MASK(nbits));
0397 }
0398
0399 static inline bool bitmap_intersects(const unsigned long *src1,
0400 const unsigned long *src2,
0401 unsigned int nbits)
0402 {
0403 if (small_const_nbits(nbits))
0404 return ((*src1 & *src2) & BITMAP_LAST_WORD_MASK(nbits)) != 0;
0405 else
0406 return __bitmap_intersects(src1, src2, nbits);
0407 }
0408
0409 static inline bool bitmap_subset(const unsigned long *src1,
0410 const unsigned long *src2, unsigned int nbits)
0411 {
0412 if (small_const_nbits(nbits))
0413 return ! ((*src1 & ~(*src2)) & BITMAP_LAST_WORD_MASK(nbits));
0414 else
0415 return __bitmap_subset(src1, src2, nbits);
0416 }
0417
0418 static inline bool bitmap_empty(const unsigned long *src, unsigned nbits)
0419 {
0420 if (small_const_nbits(nbits))
0421 return ! (*src & BITMAP_LAST_WORD_MASK(nbits));
0422
0423 return find_first_bit(src, nbits) == nbits;
0424 }
0425
0426 static inline bool bitmap_full(const unsigned long *src, unsigned int nbits)
0427 {
0428 if (small_const_nbits(nbits))
0429 return ! (~(*src) & BITMAP_LAST_WORD_MASK(nbits));
0430
0431 return find_first_zero_bit(src, nbits) == nbits;
0432 }
0433
0434 static __always_inline
0435 unsigned int bitmap_weight(const unsigned long *src, unsigned int nbits)
0436 {
0437 if (small_const_nbits(nbits))
0438 return hweight_long(*src & BITMAP_LAST_WORD_MASK(nbits));
0439 return __bitmap_weight(src, nbits);
0440 }
0441
0442 static __always_inline void bitmap_set(unsigned long *map, unsigned int start,
0443 unsigned int nbits)
0444 {
0445 if (__builtin_constant_p(nbits) && nbits == 1)
0446 __set_bit(start, map);
0447 else if (small_const_nbits(start + nbits))
0448 *map |= GENMASK(start + nbits - 1, start);
0449 else if (__builtin_constant_p(start & BITMAP_MEM_MASK) &&
0450 IS_ALIGNED(start, BITMAP_MEM_ALIGNMENT) &&
0451 __builtin_constant_p(nbits & BITMAP_MEM_MASK) &&
0452 IS_ALIGNED(nbits, BITMAP_MEM_ALIGNMENT))
0453 memset((char *)map + start / 8, 0xff, nbits / 8);
0454 else
0455 __bitmap_set(map, start, nbits);
0456 }
0457
0458 static __always_inline void bitmap_clear(unsigned long *map, unsigned int start,
0459 unsigned int nbits)
0460 {
0461 if (__builtin_constant_p(nbits) && nbits == 1)
0462 __clear_bit(start, map);
0463 else if (small_const_nbits(start + nbits))
0464 *map &= ~GENMASK(start + nbits - 1, start);
0465 else if (__builtin_constant_p(start & BITMAP_MEM_MASK) &&
0466 IS_ALIGNED(start, BITMAP_MEM_ALIGNMENT) &&
0467 __builtin_constant_p(nbits & BITMAP_MEM_MASK) &&
0468 IS_ALIGNED(nbits, BITMAP_MEM_ALIGNMENT))
0469 memset((char *)map + start / 8, 0, nbits / 8);
0470 else
0471 __bitmap_clear(map, start, nbits);
0472 }
0473
0474 static inline void bitmap_shift_right(unsigned long *dst, const unsigned long *src,
0475 unsigned int shift, unsigned int nbits)
0476 {
0477 if (small_const_nbits(nbits))
0478 *dst = (*src & BITMAP_LAST_WORD_MASK(nbits)) >> shift;
0479 else
0480 __bitmap_shift_right(dst, src, shift, nbits);
0481 }
0482
0483 static inline void bitmap_shift_left(unsigned long *dst, const unsigned long *src,
0484 unsigned int shift, unsigned int nbits)
0485 {
0486 if (small_const_nbits(nbits))
0487 *dst = (*src << shift) & BITMAP_LAST_WORD_MASK(nbits);
0488 else
0489 __bitmap_shift_left(dst, src, shift, nbits);
0490 }
0491
0492 static inline void bitmap_replace(unsigned long *dst,
0493 const unsigned long *old,
0494 const unsigned long *new,
0495 const unsigned long *mask,
0496 unsigned int nbits)
0497 {
0498 if (small_const_nbits(nbits))
0499 *dst = (*old & ~(*mask)) | (*new & *mask);
0500 else
0501 __bitmap_replace(dst, old, new, mask, nbits);
0502 }
0503
0504 static inline void bitmap_next_set_region(unsigned long *bitmap,
0505 unsigned int *rs, unsigned int *re,
0506 unsigned int end)
0507 {
0508 *rs = find_next_bit(bitmap, end, *rs);
0509 *re = find_next_zero_bit(bitmap, end, *rs + 1);
0510 }
0511
0512
0513
0514
0515
0516
0517
0518
0519
0520
0521
0522
0523
0524
0525
0526
0527
0528
0529
0530
0531
0532
0533
0534
0535
0536
0537
0538 #if __BITS_PER_LONG == 64
0539 #define BITMAP_FROM_U64(n) (n)
0540 #else
0541 #define BITMAP_FROM_U64(n) ((unsigned long) ((u64)(n) & ULONG_MAX)), \
0542 ((unsigned long) ((u64)(n) >> 32))
0543 #endif
0544
0545
0546
0547
0548
0549
0550
0551
0552
0553
0554
0555 static inline void bitmap_from_u64(unsigned long *dst, u64 mask)
0556 {
0557 bitmap_from_arr64(dst, &mask, 64);
0558 }
0559
0560
0561
0562
0563
0564
0565
0566
0567
0568 static inline unsigned long bitmap_get_value8(const unsigned long *map,
0569 unsigned long start)
0570 {
0571 const size_t index = BIT_WORD(start);
0572 const unsigned long offset = start % BITS_PER_LONG;
0573
0574 return (map[index] >> offset) & 0xFF;
0575 }
0576
0577
0578
0579
0580
0581
0582
0583 static inline void bitmap_set_value8(unsigned long *map, unsigned long value,
0584 unsigned long start)
0585 {
0586 const size_t index = BIT_WORD(start);
0587 const unsigned long offset = start % BITS_PER_LONG;
0588
0589 map[index] &= ~(0xFFUL << offset);
0590 map[index] |= value << offset;
0591 }
0592
0593 #endif
0594
0595 #endif