Skip to content

Commit f6a1f5d

Browse files
Villemoestorvalds
authored andcommitted
lib/bitmap.c: simplify bitmap_ord_to_pos
Make the return value and the ord and nbits parameters of bitmap_ord_to_pos unsigned. Also, simplify the implementation and as a side effect make the result fully defined, returning nbits for ord >= weight, in analogy with what find_{first,next}_bit does. This is a better sentinel than the former ("unofficial") 0. No current users are affected by this change. Signed-off-by: Rasmus Villemoes <linux@rasmusvillemoes.dk> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent df1d80a commit f6a1f5d

2 files changed

Lines changed: 12 additions & 18 deletions

File tree

‎include/linux/bitmap.h‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ extern int bitmap_find_free_region(unsigned long *bitmap, unsigned int bits, int
171171
extern void bitmap_release_region(unsigned long *bitmap, unsigned int pos, int order);
172172
extern int bitmap_allocate_region(unsigned long *bitmap, unsigned int pos, int order);
173173
extern void bitmap_copy_le(void *dst, const unsigned long *src, int nbits);
174-
extern int bitmap_ord_to_pos(const unsigned long *bitmap, int n, int bits);
174+
extern unsigned int bitmap_ord_to_pos(const unsigned long *bitmap, unsigned int ord, unsigned int nbits);
175175
extern int bitmap_print_to_pagebuf(bool list, char *buf,
176176
const unsigned long *maskp, int nmaskbits);
177177

‎lib/bitmap.c‎

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -771,34 +771,28 @@ static int bitmap_pos_to_ord(const unsigned long *buf, unsigned int pos, unsigne
771771
* bitmap_ord_to_pos - find position of n-th set bit in bitmap
772772
* @buf: pointer to bitmap
773773
* @ord: ordinal bit position (n-th set bit, n >= 0)
774-
* @bits: number of valid bit positions in @buf
774+
* @nbits: number of valid bit positions in @buf
775775
*
776776
* Map the ordinal offset of bit @ord in @buf to its position in @buf.
777-
* Value of @ord should be in range 0 <= @ord < weight(buf), else
778-
* results are undefined.
777+
* Value of @ord should be in range 0 <= @ord < weight(buf). If @ord
778+
* >= weight(buf), returns @nbits.
779779
*
780780
* If for example, just bits 4 through 7 are set in @buf, then @ord
781781
* values 0 through 3 will get mapped to 4 through 7, respectively,
782-
* and all other @ord values return undefined values. When @ord value 3
782+
* and all other @ord values returns @nbits. When @ord value 3
783783
* gets mapped to (returns) @pos value 7 in this example, that means
784784
* that the 3rd set bit (starting with 0th) is at position 7 in @buf.
785785
*
786-
* The bit positions 0 through @bits are valid positions in @buf.
786+
* The bit positions 0 through @nbits-1 are valid positions in @buf.
787787
*/
788-
int bitmap_ord_to_pos(const unsigned long *buf, int ord, int bits)
788+
unsigned int bitmap_ord_to_pos(const unsigned long *buf, unsigned int ord, unsigned int nbits)
789789
{
790-
int pos = 0;
790+
unsigned int pos;
791791

792-
if (ord >= 0 && ord < bits) {
793-
int i;
794-
795-
for (i = find_first_bit(buf, bits);
796-
i < bits && ord > 0;
797-
i = find_next_bit(buf, bits, i + 1))
798-
ord--;
799-
if (i < bits && ord == 0)
800-
pos = i;
801-
}
792+
for (pos = find_first_bit(buf, nbits);
793+
pos < nbits && ord;
794+
pos = find_next_bit(buf, nbits, pos + 1))
795+
ord--;
802796

803797
return pos;
804798
}

0 commit comments

Comments
 (0)