1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2017 Cavium, Inc
8 #include <rte_common.h>
9 #include <rte_bitmap.h>
10 #include <rte_malloc.h>
17 test_bitmap_scan_operations(struct rte_bitmap *bmp)
20 uint64_t slab1_magic = 0xBADC0FFEEBADF00D;
21 uint64_t slab2_magic = 0xFEEDDEADDEADF00D;
22 uint64_t out_slab = 0;
24 rte_bitmap_reset(bmp);
26 rte_bitmap_set_slab(bmp, pos, slab1_magic);
27 rte_bitmap_set_slab(bmp, pos + RTE_BITMAP_SLAB_BIT_SIZE, slab2_magic);
29 if (!rte_bitmap_scan(bmp, &pos, &out_slab)) {
30 printf("Failed to get slab from bitmap.\n");
34 if (slab1_magic != out_slab) {
35 printf("Scan operation sanity failed.\n");
39 if (!rte_bitmap_scan(bmp, &pos, &out_slab)) {
40 printf("Failed to get slab from bitmap.\n");
44 if (slab2_magic != out_slab) {
45 printf("Scan operation sanity failed.\n");
50 if (!rte_bitmap_scan(bmp, &pos, &out_slab)) {
51 printf("Failed to get slab from bitmap.\n");
55 if (slab1_magic != out_slab) {
56 printf("Scan operation wrap around failed.\n");
60 /* Scan reset check. */
61 __rte_bitmap_scan_init(bmp);
63 if (!rte_bitmap_scan(bmp, &pos, &out_slab)) {
64 printf("Failed to get slab from bitmap.\n");
68 if (slab1_magic != out_slab) {
69 printf("Scan reset operation failed.\n");
77 test_bitmap_slab_set_get(struct rte_bitmap *bmp)
80 uint64_t slab_magic = 0xBADC0FFEEBADF00D;
81 uint64_t out_slab = 0;
83 rte_bitmap_reset(bmp);
84 rte_bitmap_set_slab(bmp, pos, slab_magic);
86 if (!rte_bitmap_scan(bmp, &pos, &out_slab)) {
87 printf("Failed to get slab from bitmap.\n");
92 if (slab_magic != out_slab) {
93 printf("Invalid slab in bitmap.\n");
102 test_bitmap_set_get_clear(struct rte_bitmap *bmp)
107 rte_bitmap_reset(bmp);
108 for (i = 0; i < MAX_BITS; i++)
109 rte_bitmap_set(bmp, i);
111 for (i = 0; i < MAX_BITS; i++) {
112 if (!rte_bitmap_get(bmp, i)) {
113 printf("Failed to get set bit.\n");
118 for (i = 0; i < MAX_BITS; i++)
119 rte_bitmap_clear(bmp, i);
121 for (i = 0; i < MAX_BITS; i++) {
122 if (rte_bitmap_get(bmp, i)) {
123 printf("Failed to clear set bit.\n");
128 rte_bitmap_reset(bmp);
130 /* Alternate slab set test */
131 for (i = 0; i < MAX_BITS; i++) {
132 if (i % RTE_BITMAP_SLAB_BIT_SIZE)
133 rte_bitmap_set(bmp, i);
136 for (i = 0; i < MAX_BITS; i++) {
137 val = rte_bitmap_get(bmp, i);
138 if (((i % RTE_BITMAP_SLAB_BIT_SIZE) && !val) ||
139 (!(i % RTE_BITMAP_SLAB_BIT_SIZE) && val)) {
140 printf("Failed to get set bit.\n");
149 test_bitmap_all_clear(void)
153 struct rte_bitmap *bmp;
156 rte_bitmap_get_memory_footprint(MAX_BITS);
158 mem = rte_zmalloc("test_bmap", bmp_size, RTE_CACHE_LINE_SIZE);
160 printf("Failed to allocate memory for bitmap\n");
164 bmp = rte_bitmap_init(MAX_BITS, mem, bmp_size);
166 printf("Failed to init bitmap\n");
170 if (test_bitmap_set_get_clear(bmp) < 0)
173 if (test_bitmap_slab_set_get(bmp) < 0)
176 if (test_bitmap_scan_operations(bmp) < 0)
179 rte_bitmap_free(bmp);
186 test_bitmap_all_set(void)
193 struct rte_bitmap *bmp;
196 rte_bitmap_get_memory_footprint(MAX_BITS);
198 mem = rte_zmalloc("test_bmap", bmp_size, RTE_CACHE_LINE_SIZE);
200 printf("Failed to allocate memory for bitmap\n");
204 bmp = rte_bitmap_init_with_all_set(MAX_BITS, mem, bmp_size);
206 printf("Failed to init bitmap\n");
210 for (i = 0; i < MAX_BITS; i++) {
212 if (!rte_bitmap_scan(bmp, &pos, &slab)) {
213 printf("Failed with init bitmap.\n");
216 pos += (slab ? __builtin_ctzll(slab) : 0);
217 rte_bitmap_clear(bmp, pos);
220 if (rte_bitmap_scan(bmp, &pos, &slab)) {
221 printf("Too much bits set.\n");
225 rte_bitmap_free(bmp);
235 if (test_bitmap_all_clear() != TEST_SUCCESS)
237 return test_bitmap_all_set();
240 REGISTER_TEST_COMMAND(bitmap_test, test_bitmap);