diff options
author | Matthew Wilcox <willy@linux.intel.com> | 2016-03-17 17:21:45 -0400 |
---|---|---|
committer | Linus Torvalds <torvalds@linux-foundation.org> | 2016-03-17 18:09:34 -0400 |
commit | 1366c37ed84b166a0fffe201154b0d3b78a3976b (patch) | |
tree | 3a688edaf09069694db90421f200568b886a4295 /tools/testing/radix-tree/linux/bitops.h | |
parent | f67c07f07fca95a7f330b8bb928eabaf9fcce75d (diff) |
radix tree test harness
This code is mostly from Andrew Morton and Nick Piggin; tarball downloaded
from http://ozlabs.org/~akpm/rtth.tar.gz with sha1sum
0ce679db9ec047296b5d1ff7a1dfaa03a7bef1bd
Some small modifications were necessary to the test harness to fix the
build with the current Linux source code.
I also made minor modifications to automatically test the radix-tree.c
and radix-tree.h files that are in the current source tree, as opposed
to a copied and slightly modified version. I am sure more could be done
to tidy up the harness, as well as adding more tests.
[koct9i@gmail.com: fix compilation]
Signed-off-by: Matthew Wilcox <willy@linux.intel.com>
Cc: Shuah Khan <shuahkh@osg.samsung.com>
Cc: Johannes Weiner <hannes@cmpxchg.org>
Cc: Matthew Wilcox <willy@linux.intel.com>
Cc: "Kirill A. Shutemov" <kirill.shutemov@linux.intel.com>
Cc: Ross Zwisler <ross.zwisler@linux.intel.com>
Cc: Hugh Dickins <hughd@google.com>
Signed-off-by: Konstantin Khlebnikov <koct9i@gmail.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
Diffstat (limited to 'tools/testing/radix-tree/linux/bitops.h')
-rw-r--r-- | tools/testing/radix-tree/linux/bitops.h | 150 |
1 files changed, 150 insertions, 0 deletions
diff --git a/tools/testing/radix-tree/linux/bitops.h b/tools/testing/radix-tree/linux/bitops.h new file mode 100644 index 000000000000..71d58427ab60 --- /dev/null +++ b/tools/testing/radix-tree/linux/bitops.h | |||
@@ -0,0 +1,150 @@ | |||
1 | #ifndef _ASM_GENERIC_BITOPS_NON_ATOMIC_H_ | ||
2 | #define _ASM_GENERIC_BITOPS_NON_ATOMIC_H_ | ||
3 | |||
4 | #include <linux/types.h> | ||
5 | |||
6 | #define BITOP_MASK(nr) (1UL << ((nr) % BITS_PER_LONG)) | ||
7 | #define BITOP_WORD(nr) ((nr) / BITS_PER_LONG) | ||
8 | |||
9 | /** | ||
10 | * __set_bit - Set a bit in memory | ||
11 | * @nr: the bit to set | ||
12 | * @addr: the address to start counting from | ||
13 | * | ||
14 | * Unlike set_bit(), this function is non-atomic and may be reordered. | ||
15 | * If it's called on the same region of memory simultaneously, the effect | ||
16 | * may be that only one operation succeeds. | ||
17 | */ | ||
18 | static inline void __set_bit(int nr, volatile unsigned long *addr) | ||
19 | { | ||
20 | unsigned long mask = BITOP_MASK(nr); | ||
21 | unsigned long *p = ((unsigned long *)addr) + BITOP_WORD(nr); | ||
22 | |||
23 | *p |= mask; | ||
24 | } | ||
25 | |||
26 | static inline void __clear_bit(int nr, volatile unsigned long *addr) | ||
27 | { | ||
28 | unsigned long mask = BITOP_MASK(nr); | ||
29 | unsigned long *p = ((unsigned long *)addr) + BITOP_WORD(nr); | ||
30 | |||
31 | *p &= ~mask; | ||
32 | } | ||
33 | |||
34 | /** | ||
35 | * __change_bit - Toggle a bit in memory | ||
36 | * @nr: the bit to change | ||
37 | * @addr: the address to start counting from | ||
38 | * | ||
39 | * Unlike change_bit(), this function is non-atomic and may be reordered. | ||
40 | * If it's called on the same region of memory simultaneously, the effect | ||
41 | * may be that only one operation succeeds. | ||
42 | */ | ||
43 | static inline void __change_bit(int nr, volatile unsigned long *addr) | ||
44 | { | ||
45 | unsigned long mask = BITOP_MASK(nr); | ||
46 | unsigned long *p = ((unsigned long *)addr) + BITOP_WORD(nr); | ||
47 | |||
48 | *p ^= mask; | ||
49 | } | ||
50 | |||
51 | /** | ||
52 | * __test_and_set_bit - Set a bit and return its old value | ||
53 | * @nr: Bit to set | ||
54 | * @addr: Address to count from | ||
55 | * | ||
56 | * This operation is non-atomic and can be reordered. | ||
57 | * If two examples of this operation race, one can appear to succeed | ||
58 | * but actually fail. You must protect multiple accesses with a lock. | ||
59 | */ | ||
60 | static inline int __test_and_set_bit(int nr, volatile unsigned long *addr) | ||
61 | { | ||
62 | unsigned long mask = BITOP_MASK(nr); | ||
63 | unsigned long *p = ((unsigned long *)addr) + BITOP_WORD(nr); | ||
64 | unsigned long old = *p; | ||
65 | |||
66 | *p = old | mask; | ||
67 | return (old & mask) != 0; | ||
68 | } | ||
69 | |||
70 | /** | ||
71 | * __test_and_clear_bit - Clear a bit and return its old value | ||
72 | * @nr: Bit to clear | ||
73 | * @addr: Address to count from | ||
74 | * | ||
75 | * This operation is non-atomic and can be reordered. | ||
76 | * If two examples of this operation race, one can appear to succeed | ||
77 | * but actually fail. You must protect multiple accesses with a lock. | ||
78 | */ | ||
79 | static inline int __test_and_clear_bit(int nr, volatile unsigned long *addr) | ||
80 | { | ||
81 | unsigned long mask = BITOP_MASK(nr); | ||
82 | unsigned long *p = ((unsigned long *)addr) + BITOP_WORD(nr); | ||
83 | unsigned long old = *p; | ||
84 | |||
85 | *p = old & ~mask; | ||
86 | return (old & mask) != 0; | ||
87 | } | ||
88 | |||
89 | /* WARNING: non atomic and it can be reordered! */ | ||
90 | static inline int __test_and_change_bit(int nr, | ||
91 | volatile unsigned long *addr) | ||
92 | { | ||
93 | unsigned long mask = BITOP_MASK(nr); | ||
94 | unsigned long *p = ((unsigned long *)addr) + BITOP_WORD(nr); | ||
95 | unsigned long old = *p; | ||
96 | |||
97 | *p = old ^ mask; | ||
98 | return (old & mask) != 0; | ||
99 | } | ||
100 | |||
101 | /** | ||
102 | * test_bit - Determine whether a bit is set | ||
103 | * @nr: bit number to test | ||
104 | * @addr: Address to start counting from | ||
105 | */ | ||
106 | static inline int test_bit(int nr, const volatile unsigned long *addr) | ||
107 | { | ||
108 | return 1UL & (addr[BITOP_WORD(nr)] >> (nr & (BITS_PER_LONG-1))); | ||
109 | } | ||
110 | |||
111 | /** | ||
112 | * __ffs - find first bit in word. | ||
113 | * @word: The word to search | ||
114 | * | ||
115 | * Undefined if no bit exists, so code should check against 0 first. | ||
116 | */ | ||
117 | static inline unsigned long __ffs(unsigned long word) | ||
118 | { | ||
119 | int num = 0; | ||
120 | |||
121 | if ((word & 0xffffffff) == 0) { | ||
122 | num += 32; | ||
123 | word >>= 32; | ||
124 | } | ||
125 | if ((word & 0xffff) == 0) { | ||
126 | num += 16; | ||
127 | word >>= 16; | ||
128 | } | ||
129 | if ((word & 0xff) == 0) { | ||
130 | num += 8; | ||
131 | word >>= 8; | ||
132 | } | ||
133 | if ((word & 0xf) == 0) { | ||
134 | num += 4; | ||
135 | word >>= 4; | ||
136 | } | ||
137 | if ((word & 0x3) == 0) { | ||
138 | num += 2; | ||
139 | word >>= 2; | ||
140 | } | ||
141 | if ((word & 0x1) == 0) | ||
142 | num += 1; | ||
143 | return num; | ||
144 | } | ||
145 | |||
146 | unsigned long find_next_bit(const unsigned long *addr, | ||
147 | unsigned long size, | ||
148 | unsigned long offset); | ||
149 | |||
150 | #endif /* _ASM_GENERIC_BITOPS_NON_ATOMIC_H_ */ | ||