tools: introduce test_and_clear_bit
authorPeter Xu <peterx@redhat.com>
Wed, 22 Aug 2018 07:19:56 +0000 (15:19 +0800)
committerPaolo Bonzini <pbonzini@redhat.com>
Wed, 22 Aug 2018 14:48:36 +0000 (16:48 +0200)
We have test_and_set_bit but not test_and_clear_bit.  Add it.

Signed-off-by: Peter Xu <peterx@redhat.com>
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
tools/include/linux/bitmap.h

index 63440cc..e63662d 100644 (file)
@@ -96,6 +96,23 @@ static inline int test_and_set_bit(int nr, unsigned long *addr)
        return (old & mask) != 0;
 }
 
+/**
+ * test_and_clear_bit - Clear a bit and return its old value
+ * @nr: Bit to clear
+ * @addr: Address to count from
+ */
+static inline int test_and_clear_bit(int nr, unsigned long *addr)
+{
+       unsigned long mask = BIT_MASK(nr);
+       unsigned long *p = ((unsigned long *)addr) + BIT_WORD(nr);
+       unsigned long old;
+
+       old = *p;
+       *p = old & ~mask;
+
+       return (old & mask) != 0;
+}
+
 /**
  * bitmap_alloc - Allocate bitmap
  * @nbits: Number of bits