]> git.kernelconcepts.de Git - karo-tx-linux.git/blobdiff - lib/bitmap.c
Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/jikos/hid
[karo-tx-linux.git] / lib / bitmap.c
index f69435c23f9c8278e99e774c8f0410a10a75f457..1e031f2c9aba1ba4f536147ef5a2adbc6954addb 100644 (file)
@@ -185,11 +185,14 @@ int __bitmap_and(unsigned long *dst, const unsigned long *bitmap1,
                                const unsigned long *bitmap2, unsigned int bits)
 {
        unsigned int k;
-       unsigned int nr = BITS_TO_LONGS(bits);
+       unsigned int lim = bits/BITS_PER_LONG;
        unsigned long result = 0;
 
-       for (k = 0; k < nr; k++)
+       for (k = 0; k < lim; k++)
                result |= (dst[k] = bitmap1[k] & bitmap2[k]);
+       if (bits % BITS_PER_LONG)
+               result |= (dst[k] = bitmap1[k] & bitmap2[k] &
+                          BITMAP_LAST_WORD_MASK(bits));
        return result != 0;
 }
 EXPORT_SYMBOL(__bitmap_and);
@@ -220,11 +223,14 @@ int __bitmap_andnot(unsigned long *dst, const unsigned long *bitmap1,
                                const unsigned long *bitmap2, unsigned int bits)
 {
        unsigned int k;
-       unsigned int nr = BITS_TO_LONGS(bits);
+       unsigned int lim = bits/BITS_PER_LONG;
        unsigned long result = 0;
 
-       for (k = 0; k < nr; k++)
+       for (k = 0; k < lim; k++)
                result |= (dst[k] = bitmap1[k] & ~bitmap2[k]);
+       if (bits % BITS_PER_LONG)
+               result |= (dst[k] = bitmap1[k] & ~bitmap2[k] &
+                          BITMAP_LAST_WORD_MASK(bits));
        return result != 0;
 }
 EXPORT_SYMBOL(__bitmap_andnot);
@@ -274,42 +280,42 @@ int __bitmap_weight(const unsigned long *bitmap, unsigned int bits)
 }
 EXPORT_SYMBOL(__bitmap_weight);
 
-void bitmap_set(unsigned long *map, int start, int nr)
+void bitmap_set(unsigned long *map, unsigned int start, int len)
 {
        unsigned long *p = map + BIT_WORD(start);
-       const int size = start + nr;
+       const unsigned int size = start + len;
        int bits_to_set = BITS_PER_LONG - (start % BITS_PER_LONG);
        unsigned long mask_to_set = BITMAP_FIRST_WORD_MASK(start);
 
-       while (nr - bits_to_set >= 0) {
+       while (len - bits_to_set >= 0) {
                *p |= mask_to_set;
-               nr -= bits_to_set;
+               len -= bits_to_set;
                bits_to_set = BITS_PER_LONG;
                mask_to_set = ~0UL;
                p++;
        }
-       if (nr) {
+       if (len) {
                mask_to_set &= BITMAP_LAST_WORD_MASK(size);
                *p |= mask_to_set;
        }
 }
 EXPORT_SYMBOL(bitmap_set);
 
-void bitmap_clear(unsigned long *map, int start, int nr)
+void bitmap_clear(unsigned long *map, unsigned int start, int len)
 {
        unsigned long *p = map + BIT_WORD(start);
-       const int size = start + nr;
+       const unsigned int size = start + len;
        int bits_to_clear = BITS_PER_LONG - (start % BITS_PER_LONG);
        unsigned long mask_to_clear = BITMAP_FIRST_WORD_MASK(start);
 
-       while (nr - bits_to_clear >= 0) {
+       while (len - bits_to_clear >= 0) {
                *p &= ~mask_to_clear;
-               nr -= bits_to_clear;
+               len -= bits_to_clear;
                bits_to_clear = BITS_PER_LONG;
                mask_to_clear = ~0UL;
                p++;
        }
-       if (nr) {
+       if (len) {
                mask_to_clear &= BITMAP_LAST_WORD_MASK(size);
                *p &= ~mask_to_clear;
        }
@@ -665,13 +671,8 @@ static int __bitmap_parselist(const char *buf, unsigned int buflen,
 
 int bitmap_parselist(const char *bp, unsigned long *maskp, int nmaskbits)
 {
-       char *nl  = strchr(bp, '\n');
-       int len;
-
-       if (nl)
-               len = nl - bp;
-       else
-               len = strlen(bp);
+       char *nl  = strchrnul(bp, '\n');
+       int len = nl - bp;
 
        return __bitmap_parselist(bp, len, 0, maskp, nmaskbits);
 }
@@ -717,7 +718,7 @@ EXPORT_SYMBOL(bitmap_parselist_user);
  *
  * If for example, just bits 4 through 7 are set in @buf, then @pos
  * values 4 through 7 will get mapped to 0 through 3, respectively,
- * and other @pos values will get mapped to 0.  When @pos value 7
+ * and other @pos values will get mapped to -1.  When @pos value 7
  * gets mapped to (returns) @ord value 3 in this example, that means
  * that bit 7 is the 3rd (starting with 0th) set bit in @buf.
  *
@@ -1047,7 +1048,7 @@ enum {
        REG_OP_RELEASE,         /* clear all bits in region */
 };
 
-static int __reg_op(unsigned long *bitmap, int pos, int order, int reg_op)
+static int __reg_op(unsigned long *bitmap, unsigned int pos, int order, int reg_op)
 {
        int nbits_reg;          /* number of bits in region */
        int index;              /* index first long of region in bitmap */
@@ -1113,11 +1114,11 @@ done:
  * Return the bit offset in bitmap of the allocated region,
  * or -errno on failure.
  */
-int bitmap_find_free_region(unsigned long *bitmap, int bits, int order)
+int bitmap_find_free_region(unsigned long *bitmap, unsigned int bits, int order)
 {
-       int pos, end;           /* scans bitmap by regions of size order */
+       unsigned int pos, end;          /* scans bitmap by regions of size order */
 
-       for (pos = 0 ; (end = pos + (1 << order)) <= bits; pos = end) {
+       for (pos = 0 ; (end = pos + (1U << order)) <= bits; pos = end) {
                if (!__reg_op(bitmap, pos, order, REG_OP_ISFREE))
                        continue;
                __reg_op(bitmap, pos, order, REG_OP_ALLOC);
@@ -1138,7 +1139,7 @@ EXPORT_SYMBOL(bitmap_find_free_region);
  *
  * No return value.
  */
-void bitmap_release_region(unsigned long *bitmap, int pos, int order)
+void bitmap_release_region(unsigned long *bitmap, unsigned int pos, int order)
 {
        __reg_op(bitmap, pos, order, REG_OP_RELEASE);
 }
@@ -1155,12 +1156,11 @@ EXPORT_SYMBOL(bitmap_release_region);
  * Return 0 on success, or %-EBUSY if specified region wasn't
  * free (not all bits were zero).
  */
-int bitmap_allocate_region(unsigned long *bitmap, int pos, int order)
+int bitmap_allocate_region(unsigned long *bitmap, unsigned int pos, int order)
 {
        if (!__reg_op(bitmap, pos, order, REG_OP_ISFREE))
                return -EBUSY;
-       __reg_op(bitmap, pos, order, REG_OP_ALLOC);
-       return 0;
+       return __reg_op(bitmap, pos, order, REG_OP_ALLOC);
 }
 EXPORT_SYMBOL(bitmap_allocate_region);