]> git.ipfire.org Git - thirdparty/xfsprogs-dev.git/commitdiff
libxfs: add xfs_bit.c
authorDave Chinner <dchinner@redhat.com>
Tue, 16 Jun 2015 00:57:23 +0000 (10:57 +1000)
committerDave Chinner <david@fromorbit.com>
Tue, 23 Jun 2015 05:06:09 +0000 (15:06 +1000)
The header side of xfs_bit.c is already in libxfs, and the sparse
inode code requires the xfs_next_bit() function so pull in the
xfs_bit.c file

Signed-off-by: Dave Chinner <dchinner@redhat.com>
include/bitops.h
libxfs/Makefile
libxfs/xfs_bit.c [new file with mode: 0644]

index e914f8f763040bfaf3926e5f0e60ca0bb5c11f61..930f64a3258abb18dee27aecf786beaca89f57fa 100644 (file)
@@ -48,4 +48,10 @@ static inline unsigned fls_long(unsigned long l)
         return fls64(l);
 }
 
+/*
+ * ffz: find first zero bit.
+ * Result is undefined if no zero bit exists.
+ */
+#define ffz(x) ffs(~(x))
+
 #endif
index 981cb0bbd9172bb557718b59e1677574d6561fed..33d73f06e75361582dc983638fdafaa4046bcfda 100644 (file)
@@ -51,12 +51,19 @@ QAHFILES = xfs_alloc.h \
 
 CFILES = cache.c \
        crc32.c \
-       init.c kmem.c logitem.c radix-tree.c rdwr.c trans.c util.c \
+       init.c \
+       kmem.c \
+       logitem.c \
+       radix-tree.c \
+       rdwr.c \
+       trans.c \
+       util.c \
        xfs_alloc.c \
        xfs_alloc_btree.c \
        xfs_attr.c \
        xfs_attr_leaf.c \
        xfs_attr_remote.c \
+       xfs_bit.c \
        xfs_bmap.c \
        xfs_bmap_btree.c \
        xfs_btree.c \
diff --git a/libxfs/xfs_bit.c b/libxfs/xfs_bit.c
new file mode 100644 (file)
index 0000000..8b5b81c
--- /dev/null
@@ -0,0 +1,118 @@
+/*
+ * Copyright (c) 2000-2005 Silicon Graphics, Inc.
+ * All Rights Reserved.
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License as
+ * published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it would be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write the Free Software Foundation,
+ * Inc.,  51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
+ */
+#include "libxfs_priv.h"
+#include "xfs_log_format.h"
+#include "xfs_bit.h"
+
+/*
+ * XFS bit manipulation routines, used in non-realtime code.
+ */
+
+/*
+ * Return whether bitmap is empty.
+ * Size is number of words in the bitmap, which is padded to word boundary
+ * Returns 1 for empty, 0 for non-empty.
+ */
+int
+xfs_bitmap_empty(uint *map, uint size)
+{
+       uint i;
+       uint ret = 0;
+
+       for (i = 0; i < size; i++) {
+               ret |= map[i];
+       }
+
+       return (ret == 0);
+}
+
+/*
+ * Count the number of contiguous bits set in the bitmap starting with bit
+ * start_bit.  Size is the size of the bitmap in words.
+ */
+int
+xfs_contig_bits(uint *map, uint        size, uint start_bit)
+{
+       uint * p = ((unsigned int *) map) + (start_bit >> BIT_TO_WORD_SHIFT);
+       uint result = 0;
+       uint tmp;
+
+       size <<= BIT_TO_WORD_SHIFT;
+
+       ASSERT(start_bit < size);
+       size -= start_bit & ~(NBWORD - 1);
+       start_bit &= (NBWORD - 1);
+       if (start_bit) {
+               tmp = *p++;
+               /* set to one first offset bits prior to start */
+               tmp |= (~0U >> (NBWORD-start_bit));
+               if (tmp != ~0U)
+                       goto found;
+               result += NBWORD;
+               size -= NBWORD;
+       }
+       while (size) {
+               if ((tmp = *p++) != ~0U)
+                       goto found;
+               result += NBWORD;
+               size -= NBWORD;
+       }
+       return result - start_bit;
+found:
+       return result + ffz(tmp) - start_bit;
+}
+
+/*
+ * This takes the bit number to start looking from and
+ * returns the next set bit from there.  It returns -1
+ * if there are no more bits set or the start bit is
+ * beyond the end of the bitmap.
+ *
+ * Size is the number of words, not bytes, in the bitmap.
+ */
+int xfs_next_bit(uint *map, uint size, uint start_bit)
+{
+       uint * p = ((unsigned int *) map) + (start_bit >> BIT_TO_WORD_SHIFT);
+       uint result = start_bit & ~(NBWORD - 1);
+       uint tmp;
+
+       size <<= BIT_TO_WORD_SHIFT;
+
+       if (start_bit >= size)
+               return -1;
+       size -= result;
+       start_bit &= (NBWORD - 1);
+       if (start_bit) {
+               tmp = *p++;
+               /* set to zero first offset bits prior to start */
+               tmp &= (~0U << start_bit);
+               if (tmp != 0U)
+                       goto found;
+               result += NBWORD;
+               size -= NBWORD;
+       }
+       while (size) {
+               if ((tmp = *p++) != 0U)
+                       goto found;
+               result += NBWORD;
+               size -= NBWORD;
+       }
+       return -1;
+found:
+       return result + ffs(tmp) - 1;
+}