Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

bitmap: move bitmap_*_region() functions to bitmap.h

Now that bitmap_*_region() functions are implemented as thin wrappers
around others, it's worth to move them to the header, as it opens room
for compile-time optimizations.

CC: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
CC: Rasmus Villemoes <linux@rasmusvillemoes.dk>
CC: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Signed-off-by: Yury Norov <yury.norov@gmail.com>

+61 -67
+61 -3
include/linux/bitmap.h
··· 6 6 7 7 #include <linux/align.h> 8 8 #include <linux/bitops.h> 9 + #include <linux/errno.h> 9 10 #include <linux/find.h> 10 11 #include <linux/limits.h> 11 12 #include <linux/string.h> ··· 210 209 const unsigned long *relmap, unsigned int bits); 211 210 void bitmap_fold(unsigned long *dst, const unsigned long *orig, 212 211 unsigned int sz, unsigned int nbits); 213 - int bitmap_find_free_region(unsigned long *bitmap, unsigned int bits, int order); 214 - void bitmap_release_region(unsigned long *bitmap, unsigned int pos, int order); 215 - int bitmap_allocate_region(unsigned long *bitmap, unsigned int pos, int order); 216 212 217 213 #define BITMAP_FIRST_WORD_MASK(start) (~0UL << ((start) & (BITS_PER_LONG - 1))) 218 214 #define BITMAP_LAST_WORD_MASK(nbits) (~0UL >> (-(nbits) & (BITS_PER_LONG - 1))) ··· 493 495 { 494 496 *rs = find_next_bit(bitmap, end, *rs); 495 497 *re = find_next_zero_bit(bitmap, end, *rs + 1); 498 + } 499 + 500 + /** 501 + * bitmap_release_region - release allocated bitmap region 502 + * @bitmap: array of unsigned longs corresponding to the bitmap 503 + * @pos: beginning of bit region to release 504 + * @order: region size (log base 2 of number of bits) to release 505 + * 506 + * This is the complement to __bitmap_find_free_region() and releases 507 + * the found region (by clearing it in the bitmap). 508 + */ 509 + static inline void bitmap_release_region(unsigned long *bitmap, unsigned int pos, int order) 510 + { 511 + bitmap_clear(bitmap, pos, BIT(order)); 512 + } 513 + 514 + /** 515 + * bitmap_allocate_region - allocate bitmap region 516 + * @bitmap: array of unsigned longs corresponding to the bitmap 517 + * @pos: beginning of bit region to allocate 518 + * @order: region size (log base 2 of number of bits) to allocate 519 + * 520 + * Allocate (set bits in) a specified region of a bitmap. 521 + * 522 + * Returns: 0 on success, or %-EBUSY if specified region wasn't 523 + * free (not all bits were zero). 524 + */ 525 + static inline int bitmap_allocate_region(unsigned long *bitmap, unsigned int pos, int order) 526 + { 527 + unsigned int len = BIT(order); 528 + 529 + if (find_next_bit(bitmap, pos + len, pos) < pos + len) 530 + return -EBUSY; 531 + bitmap_set(bitmap, pos, len); 532 + return 0; 533 + } 534 + 535 + /** 536 + * bitmap_find_free_region - find a contiguous aligned mem region 537 + * @bitmap: array of unsigned longs corresponding to the bitmap 538 + * @bits: number of bits in the bitmap 539 + * @order: region size (log base 2 of number of bits) to find 540 + * 541 + * Find a region of free (zero) bits in a @bitmap of @bits bits and 542 + * allocate them (set them to one). Only consider regions of length 543 + * a power (@order) of two, aligned to that power of two, which 544 + * makes the search algorithm much faster. 545 + * 546 + * Returns: the bit offset in bitmap of the allocated region, 547 + * or -errno on failure. 548 + */ 549 + static inline int bitmap_find_free_region(unsigned long *bitmap, unsigned int bits, int order) 550 + { 551 + unsigned int pos, end; /* scans bitmap by regions of size order */ 552 + 553 + for (pos = 0; (end = pos + BIT(order)) <= bits; pos = end) { 554 + if (!bitmap_allocate_region(bitmap, pos, order)) 555 + return pos; 556 + } 557 + return -ENOMEM; 496 558 } 497 559 498 560 /**
-64
lib/bitmap.c
··· 8 8 #include <linux/bitops.h> 9 9 #include <linux/ctype.h> 10 10 #include <linux/device.h> 11 - #include <linux/errno.h> 12 11 #include <linux/export.h> 13 12 #include <linux/slab.h> 14 13 ··· 706 707 set_bit(oldbit % sz, dst); 707 708 } 708 709 #endif /* CONFIG_NUMA */ 709 - 710 - /** 711 - * bitmap_find_free_region - find a contiguous aligned mem region 712 - * @bitmap: array of unsigned longs corresponding to the bitmap 713 - * @bits: number of bits in the bitmap 714 - * @order: region size (log base 2 of number of bits) to find 715 - * 716 - * Find a region of free (zero) bits in a @bitmap of @bits bits and 717 - * allocate them (set them to one). Only consider regions of length 718 - * a power (@order) of two, aligned to that power of two, which 719 - * makes the search algorithm much faster. 720 - * 721 - * Return: the bit offset in bitmap of the allocated region, 722 - * or -errno on failure. 723 - */ 724 - int bitmap_find_free_region(unsigned long *bitmap, unsigned int bits, int order) 725 - { 726 - unsigned int pos, end; /* scans bitmap by regions of size order */ 727 - 728 - for (pos = 0; (end = pos + BIT(order)) <= bits; pos = end) { 729 - if (!bitmap_allocate_region(bitmap, pos, order)) 730 - return pos; 731 - } 732 - return -ENOMEM; 733 - } 734 - EXPORT_SYMBOL(bitmap_find_free_region); 735 - 736 - /** 737 - * bitmap_release_region - release allocated bitmap region 738 - * @bitmap: array of unsigned longs corresponding to the bitmap 739 - * @pos: beginning of bit region to release 740 - * @order: region size (log base 2 of number of bits) to release 741 - * 742 - * This is the complement to __bitmap_find_free_region() and releases 743 - * the found region (by clearing it in the bitmap). 744 - */ 745 - void bitmap_release_region(unsigned long *bitmap, unsigned int pos, int order) 746 - { 747 - bitmap_clear(bitmap, pos, BIT(order)); 748 - } 749 - EXPORT_SYMBOL(bitmap_release_region); 750 - 751 - /** 752 - * bitmap_allocate_region - allocate bitmap region 753 - * @bitmap: array of unsigned longs corresponding to the bitmap 754 - * @pos: beginning of bit region to allocate 755 - * @order: region size (log base 2 of number of bits) to allocate 756 - * 757 - * Allocate (set bits in) a specified region of a bitmap. 758 - * 759 - * Return: 0 on success, or %-EBUSY if specified region wasn't 760 - * free (not all bits were zero). 761 - */ 762 - int bitmap_allocate_region(unsigned long *bitmap, unsigned int pos, int order) 763 - { 764 - unsigned int len = BIT(order); 765 - 766 - if (find_next_bit(bitmap, pos + len, pos) < pos + len) 767 - return -EBUSY; 768 - bitmap_set(bitmap, pos, len); 769 - return 0; 770 - } 771 - EXPORT_SYMBOL(bitmap_allocate_region); 772 710 773 711 unsigned long *bitmap_alloc(unsigned int nbits, gfp_t flags) 774 712 {