Skip to content

Commit 416c419

Browse files
olsajiriacmel
authored andcommitted
perf tools: Add test_and_set_bit function
Set a bit and return its old value. Stolen from kernel sources, will be used in next patches. Signed-off-by: Jiri Olsa <jolsa@kernel.org> Acked-by: Namhyung Kim <namhyung@gmail.com> Cc: Adrian Hunter <adrian.hunter@intel.com> Cc: David Ahern <dsahern@gmail.com> Cc: Frederic Weisbecker <fweisbec@gmail.com> Cc: Ingo Molnar <mingo@redhat.com> Cc: Namhyung Kim <namhyung@gmail.com> Cc: Paul Mackerras <paulus@samba.org> Cc: Peter Zijlstra <peterz@infradead.org> Link: http://lkml.kernel.org/r/1414363445-22370-1-git-send-email-jolsa@kernel.org Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
1 parent 96d7805 commit 416c419

2 files changed

Lines changed: 19 additions & 0 deletions

File tree

‎tools/perf/util/include/linux/bitmap.h‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,4 +46,21 @@ static inline void bitmap_or(unsigned long *dst, const unsigned long *src1,
4646
__bitmap_or(dst, src1, src2, nbits);
4747
}
4848

49+
/**
50+
* test_and_set_bit - Set a bit and return its old value
51+
* @nr: Bit to set
52+
* @addr: Address to count from
53+
*/
54+
static inline int test_and_set_bit(int nr, unsigned long *addr)
55+
{
56+
unsigned long mask = BIT_MASK(nr);
57+
unsigned long *p = ((unsigned long *)addr) + BIT_WORD(nr);
58+
unsigned long old;
59+
60+
old = *p;
61+
*p = old | mask;
62+
63+
return (old & mask) != 0;
64+
}
65+
4966
#endif /* _PERF_BITOPS_H */

‎tools/perf/util/include/linux/bitops.h‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
#define BITS_TO_U64(nr) DIV_ROUND_UP(nr, BITS_PER_BYTE * sizeof(u64))
1616
#define BITS_TO_U32(nr) DIV_ROUND_UP(nr, BITS_PER_BYTE * sizeof(u32))
1717
#define BITS_TO_BYTES(nr) DIV_ROUND_UP(nr, BITS_PER_BYTE)
18+
#define BIT_WORD(nr) ((nr) / BITS_PER_LONG)
19+
#define BIT_MASK(nr) (1UL << ((nr) % BITS_PER_LONG))
1820

1921
#define for_each_set_bit(bit, addr, size) \
2022
for ((bit) = find_first_bit((addr), (size)); \

0 commit comments

Comments
 (0)