34 #ifndef __INCLUDE_RTE_BITMAP_H__
35 #define __INCLUDE_RTE_BITMAP_H__
69 #include <rte_config.h>
75 #ifndef RTE_BITMAP_OPTIMIZATIONS
76 #define RTE_BITMAP_OPTIMIZATIONS 1
80 #define RTE_BITMAP_SLAB_BIT_SIZE 64
81 #define RTE_BITMAP_SLAB_BIT_SIZE_LOG2 6
82 #define RTE_BITMAP_SLAB_BIT_MASK (RTE_BITMAP_SLAB_BIT_SIZE - 1)
85 #define RTE_BITMAP_CL_BIT_SIZE (RTE_CACHE_LINE_SIZE * 8)
86 #define RTE_BITMAP_CL_BIT_SIZE_LOG2 (RTE_CACHE_LINE_SIZE_LOG2 + 3)
87 #define RTE_BITMAP_CL_BIT_MASK (RTE_BITMAP_CL_BIT_SIZE - 1)
89 #define RTE_BITMAP_CL_SLAB_SIZE (RTE_BITMAP_CL_BIT_SIZE / RTE_BITMAP_SLAB_BIT_SIZE)
90 #define RTE_BITMAP_CL_SLAB_SIZE_LOG2 (RTE_BITMAP_CL_BIT_SIZE_LOG2 - RTE_BITMAP_SLAB_BIT_SIZE_LOG2)
91 #define RTE_BITMAP_CL_SLAB_MASK (RTE_BITMAP_CL_SLAB_SIZE - 1)
112 __rte_bitmap_index1_inc(
struct rte_bitmap *bmp)
117 static inline uint64_t
118 __rte_bitmap_mask1_get(
struct rte_bitmap *bmp)
120 return (~1llu) << bmp->
offset1;
124 __rte_bitmap_index2_set(
struct rte_bitmap *bmp)
126 bmp->
index2 = (((bmp->
index1 << RTE_BITMAP_SLAB_BIT_SIZE_LOG2) + bmp->
offset1) << RTE_BITMAP_CL_SLAB_SIZE_LOG2);
129 #if RTE_BITMAP_OPTIMIZATIONS
132 rte_bsf64(uint64_t slab, uint32_t *pos)
138 *pos = __builtin_ctzll(slab);
145 rte_bsf64(uint64_t slab, uint32_t *pos)
154 for (i = 0, mask = 1; i < RTE_BITMAP_SLAB_BIT_SIZE; i ++, mask <<= 1) {
166 static inline uint32_t
167 __rte_bitmap_get_memory_footprint(uint32_t n_bits,
168 uint32_t *array1_byte_offset, uint32_t *array1_slabs,
169 uint32_t *array2_byte_offset, uint32_t *array2_slabs)
171 uint32_t n_slabs_context, n_slabs_array1, n_cache_lines_context_and_array1;
172 uint32_t n_cache_lines_array2;
173 uint32_t n_bytes_total;
175 n_cache_lines_array2 = (n_bits + RTE_BITMAP_CL_BIT_SIZE - 1) / RTE_BITMAP_CL_BIT_SIZE;
176 n_slabs_array1 = (n_cache_lines_array2 + RTE_BITMAP_SLAB_BIT_SIZE - 1) / RTE_BITMAP_SLAB_BIT_SIZE;
178 n_slabs_context = (
sizeof(
struct rte_bitmap) + (RTE_BITMAP_SLAB_BIT_SIZE / 8) - 1) / (RTE_BITMAP_SLAB_BIT_SIZE / 8);
179 n_cache_lines_context_and_array1 = (n_slabs_context + n_slabs_array1 + RTE_BITMAP_CL_SLAB_SIZE - 1) / RTE_BITMAP_CL_SLAB_SIZE;
180 n_bytes_total = (n_cache_lines_context_and_array1 + n_cache_lines_array2) * RTE_CACHE_LINE_SIZE;
182 if (array1_byte_offset) {
183 *array1_byte_offset = n_slabs_context * (RTE_BITMAP_SLAB_BIT_SIZE / 8);
186 *array1_slabs = n_slabs_array1;
188 if (array2_byte_offset) {
189 *array2_byte_offset = n_cache_lines_context_and_array1 * RTE_CACHE_LINE_SIZE;
192 *array2_slabs = n_cache_lines_array2 * RTE_BITMAP_CL_SLAB_SIZE;
195 return n_bytes_total;
199 __rte_bitmap_scan_init(
struct rte_bitmap *bmp)
202 bmp->
offset1 = RTE_BITMAP_SLAB_BIT_SIZE - 1;
203 __rte_bitmap_index2_set(bmp);
204 bmp->
index2 += RTE_BITMAP_CL_SLAB_SIZE;
217 static inline uint32_t
224 return __rte_bitmap_get_memory_footprint(n_bits, NULL, NULL, NULL, NULL);
243 uint32_t array1_byte_offset, array1_slabs, array2_byte_offset, array2_slabs;
255 size = __rte_bitmap_get_memory_footprint(n_bits,
256 &array1_byte_offset, &array1_slabs,
257 &array2_byte_offset, &array2_slabs);
258 if (size < mem_size) {
263 memset(mem, 0, size);
266 bmp->
array1 = (uint64_t *) &mem[array1_byte_offset];
268 bmp->
array2 = (uint64_t *) &mem[array2_byte_offset];
271 __rte_bitmap_scan_init(bmp);
306 __rte_bitmap_scan_init(bmp);
325 index2 = pos >> RTE_BITMAP_SLAB_BIT_SIZE_LOG2;
340 static inline uint64_t
346 index2 = pos >> RTE_BITMAP_SLAB_BIT_SIZE_LOG2;
347 offset2 = pos & RTE_BITMAP_SLAB_BIT_MASK;
349 return (*slab2) & (1llu << offset2);
363 uint64_t *slab1, *slab2;
367 index2 = pos >> RTE_BITMAP_SLAB_BIT_SIZE_LOG2;
368 offset2 = pos & RTE_BITMAP_SLAB_BIT_MASK;
369 index1 = pos >> (RTE_BITMAP_SLAB_BIT_SIZE_LOG2 + RTE_BITMAP_CL_BIT_SIZE_LOG2);
370 offset1 = (pos >> RTE_BITMAP_CL_BIT_SIZE_LOG2) & RTE_BITMAP_SLAB_BIT_MASK;
374 *slab2 |= 1llu << offset2;
391 uint64_t *slab1, *slab2;
395 index2 = pos >> RTE_BITMAP_SLAB_BIT_SIZE_LOG2;
396 index1 = pos >> (RTE_BITMAP_SLAB_BIT_SIZE_LOG2 + RTE_BITMAP_CL_BIT_SIZE_LOG2);
397 offset1 = (pos >> RTE_BITMAP_CL_BIT_SIZE_LOG2) & RTE_BITMAP_SLAB_BIT_MASK;
405 static inline uint64_t
406 __rte_bitmap_line_not_empty(uint64_t *slab2)
408 uint64_t v1, v2, v3, v4;
410 v1 = slab2[0] | slab2[1];
411 v2 = slab2[2] | slab2[3];
412 v3 = slab2[4] | slab2[5];
413 v4 = slab2[6] | slab2[7];
431 uint64_t *slab1, *slab2;
435 index2 = pos >> RTE_BITMAP_SLAB_BIT_SIZE_LOG2;
436 offset2 = pos & RTE_BITMAP_SLAB_BIT_MASK;
440 *slab2 &= ~(1llu << offset2);
446 index2 &= ~ RTE_BITMAP_CL_SLAB_MASK;
448 if (__rte_bitmap_line_not_empty(slab2)) {
453 index1 = pos >> (RTE_BITMAP_SLAB_BIT_SIZE_LOG2 + RTE_BITMAP_CL_BIT_SIZE_LOG2);
454 offset1 = (pos >> RTE_BITMAP_CL_BIT_SIZE_LOG2) & RTE_BITMAP_SLAB_BIT_MASK;
462 __rte_bitmap_scan_search(
struct rte_bitmap *bmp)
469 value1 &= __rte_bitmap_mask1_get(bmp);
471 if (rte_bsf64(value1, &bmp->
offset1)) {
475 __rte_bitmap_index1_inc(bmp);
479 for (i = 0; i < bmp->
array1_size; i ++, __rte_bitmap_index1_inc(bmp)) {
482 if (rte_bsf64(value1, &bmp->
offset1)) {
491 __rte_bitmap_scan_read_init(
struct rte_bitmap *bmp)
493 __rte_bitmap_index2_set(bmp);
499 __rte_bitmap_scan_read(
struct rte_bitmap *bmp, uint32_t *pos, uint64_t *slab)
504 for ( ; bmp->
go2 ; bmp->
index2 ++, slab2 ++, bmp->
go2 = bmp->
index2 & RTE_BITMAP_CL_SLAB_MASK) {
506 *pos = bmp->
index2 << RTE_BITMAP_SLAB_BIT_SIZE_LOG2;
511 bmp->
go2 = bmp->
index2 & RTE_BITMAP_CL_SLAB_MASK;
543 if (__rte_bitmap_scan_read(bmp, pos, slab)) {
548 if (__rte_bitmap_scan_search(bmp)) {
549 __rte_bitmap_scan_read_init(bmp);
550 __rte_bitmap_scan_read(bmp, pos, slab);
static int rte_bitmap_free(struct rte_bitmap *bmp)
static void rte_bitmap_set_slab(struct rte_bitmap *bmp, uint32_t pos, uint64_t slab)
static void rte_bitmap_set(struct rte_bitmap *bmp, uint32_t pos)
static void rte_bitmap_reset(struct rte_bitmap *bmp)
static void rte_bitmap_clear(struct rte_bitmap *bmp, uint32_t pos)
static struct rte_bitmap * rte_bitmap_init(uint32_t n_bits, uint8_t *mem, uint32_t mem_size)
#define RTE_CACHE_LINE_MASK
static uint32_t rte_bitmap_get_memory_footprint(uint32_t n_bits)
static uint32_t rte_align32pow2(uint32_t x)
static uint64_t rte_bitmap_get(struct rte_bitmap *bmp, uint32_t pos)
static void rte_bitmap_prefetch0(struct rte_bitmap *bmp, uint32_t pos)
static void rte_prefetch1(const volatile void *p)
static int rte_bitmap_scan(struct rte_bitmap *bmp, uint32_t *pos, uint64_t *slab)
static void rte_prefetch0(const volatile void *p)