Bitmaps are fairly popular for their space efficiency, but we don't have generic iterators available. Make percpu's bitmap region iterators available to everyone.
Showing the changes to include/linux (1 of 2 files). Show all
Unified · Split
| @@ -456,6 +456,41 @@ static inline int bitmap_parse(const char *buf, unsigned int buflen, · 455 unmodified lines | ||
| 456 | 456 | return __bitmap_parse(buf, buflen, 0, maskp, nmaskbits); |
| 457 | 457 | } |
| 458 | 458 | |
| 459 | +static inline void bitmap_next_clear_region(unsigned long *bitmap, | |
| 460 | + unsigned int *rs, unsigned int *re, | |
| 461 | + unsigned int end) | |
| 462 | +{ | |
| 463 | + *rs = find_next_zero_bit(bitmap, end, *rs); | |
| 464 | + *re = find_next_bit(bitmap, end, *rs + 1); | |
| 465 | +} | |
| 466 | + | |
| 467 | +static inline void bitmap_next_set_region(unsigned long *bitmap, | |
| 468 | + unsigned int *rs, unsigned int *re, | |
| 469 | + unsigned int end) | |
| 470 | +{ | |
| 471 | + *rs = find_next_bit(bitmap, end, *rs); | |
| 472 | + *re = find_next_zero_bit(bitmap, end, *rs + 1); | |
| 473 | +} | |
| 474 | + | |
| 475 | +/* | |
| 476 | + * Bitmap region iterators. Iterates over the bitmap between [@start, @end). | |
| 477 | + * @rs and @re should be integer variables and will be set to start and end | |
| 478 | + * index of the current clear or set region. | |
| 479 | + */ | |
| 480 | +#define bitmap_for_each_clear_region(bitmap, rs, re, start, end) \ | |
| 481 | + for ((rs) = (start), \ | |
| 482 | + bitmap_next_clear_region((bitmap), &(rs), &(re), (end)); \ | |
| 483 | + (rs) < (re); \ | |
| 484 | + (rs) = (re) + 1, \ | |
| 485 | + bitmap_next_clear_region((bitmap), &(rs), &(re), (end))) | |
| 486 | + | |
| 487 | +#define bitmap_for_each_set_region(bitmap, rs, re, start, end) \ | |
| 488 | + for ((rs) = (start), \ | |
| 489 | + bitmap_next_set_region((bitmap), &(rs), &(re), (end)); \ | |
| 490 | + (rs) < (re); \ | |
| 491 | + (rs) = (re) + 1, \ | |
| 492 | + bitmap_next_set_region((bitmap), &(rs), &(re), (end))) | |
| 493 | + | |
| 459 | 494 | /** |
| 460 | 495 | * BITMAP_FROM_U64() - Represent u64 value in the format suitable for bitmap. |
| 461 | 496 | * @n: u64 value |