5#ifndef __INCLUDE_RTE_BITMAP_H__
6#define __INCLUDE_RTE_BITMAP_H__
39#include <rte_compat.h>
41#include <rte_config.h>
48#define RTE_BITMAP_SLAB_BIT_SIZE 64
49#define RTE_BITMAP_SLAB_BIT_SIZE_LOG2 6
50#define RTE_BITMAP_SLAB_BIT_MASK (RTE_BITMAP_SLAB_BIT_SIZE - 1)
53#define RTE_BITMAP_CL_BIT_SIZE (RTE_CACHE_LINE_SIZE * 8)
54#define RTE_BITMAP_CL_BIT_SIZE_LOG2 (RTE_CACHE_LINE_SIZE_LOG2 + 3)
55#define RTE_BITMAP_CL_BIT_MASK (RTE_BITMAP_CL_BIT_SIZE - 1)
57#define RTE_BITMAP_CL_SLAB_SIZE (RTE_BITMAP_CL_BIT_SIZE / RTE_BITMAP_SLAB_BIT_SIZE)
58#define RTE_BITMAP_CL_SLAB_SIZE_LOG2 (RTE_BITMAP_CL_BIT_SIZE_LOG2 - RTE_BITMAP_SLAB_BIT_SIZE_LOG2)
59#define RTE_BITMAP_CL_SLAB_MASK (RTE_BITMAP_CL_SLAB_SIZE - 1)
94 bmp->
index2 = (((bmp->
index1 << RTE_BITMAP_SLAB_BIT_SIZE_LOG2) + bmp->
offset1) << RTE_BITMAP_CL_SLAB_SIZE_LOG2);
98__rte_bitmap_get_memory_footprint(uint32_t n_bits,
99 uint32_t *array1_byte_offset, uint32_t *array1_slabs,
100 uint32_t *array2_byte_offset, uint32_t *array2_slabs)
102 uint32_t n_slabs_context, n_slabs_array1, n_cache_lines_context_and_array1;
103 uint32_t n_cache_lines_array2;
104 uint32_t n_bytes_total;
106 n_cache_lines_array2 = (n_bits + RTE_BITMAP_CL_BIT_SIZE - 1) / RTE_BITMAP_CL_BIT_SIZE;
107 n_slabs_array1 = (n_cache_lines_array2 + RTE_BITMAP_SLAB_BIT_SIZE - 1) / RTE_BITMAP_SLAB_BIT_SIZE;
109 n_slabs_context = (
sizeof(
struct rte_bitmap) + (RTE_BITMAP_SLAB_BIT_SIZE / 8) - 1) / (RTE_BITMAP_SLAB_BIT_SIZE / 8);
110 n_cache_lines_context_and_array1 = (n_slabs_context + n_slabs_array1 + RTE_BITMAP_CL_SLAB_SIZE - 1) / RTE_BITMAP_CL_SLAB_SIZE;
111 n_bytes_total = (n_cache_lines_context_and_array1 + n_cache_lines_array2) * RTE_CACHE_LINE_SIZE;
113 if (array1_byte_offset) {
114 *array1_byte_offset = n_slabs_context * (RTE_BITMAP_SLAB_BIT_SIZE / 8);
117 *array1_slabs = n_slabs_array1;
119 if (array2_byte_offset) {
120 *array2_byte_offset = n_cache_lines_context_and_array1 * RTE_CACHE_LINE_SIZE;
123 *array2_slabs = n_cache_lines_array2 * RTE_BITMAP_CL_SLAB_SIZE;
126 return n_bytes_total;
133 bmp->
offset1 = RTE_BITMAP_SLAB_BIT_SIZE - 1;
134 __rte_bitmap_index2_set(bmp);
135 bmp->
index2 += RTE_BITMAP_CL_SLAB_SIZE;
148static inline uint32_t
155 return __rte_bitmap_get_memory_footprint(n_bits, NULL, NULL, NULL, NULL);
174 uint32_t array1_byte_offset, array1_slabs, array2_byte_offset, array2_slabs;
186 size = __rte_bitmap_get_memory_footprint(n_bits,
187 &array1_byte_offset, &array1_slabs,
188 &array2_byte_offset, &array2_slabs);
193 memset(mem, 0, size);
196 bmp->
array1 = (uint64_t *) &mem[array1_byte_offset];
198 bmp->
array2 = (uint64_t *) &mem[array2_byte_offset];
201 __rte_bitmap_scan_init(bmp);
225 uint32_t index = pos / RTE_BITMAP_SLAB_BIT_SIZE;
226 uint32_t offset = pos & RTE_BITMAP_SLAB_BIT_MASK;
229 for (i = offset; i < RTE_BITMAP_SLAB_BIT_SIZE; i++)
230 slabs[index] &= ~(1llu << i);
233 if (index < slab_size)
234 memset(&slabs[index], 0,
sizeof(slabs[0]) *
235 (slab_size - index));
258 uint32_t array1_byte_offset, array1_slabs;
259 uint32_t array2_byte_offset, array2_slabs;
266 size = __rte_bitmap_get_memory_footprint(n_bits,
267 &array1_byte_offset, &array1_slabs,
268 &array2_byte_offset, &array2_slabs);
274 bmp->
array1 = (uint64_t *) &mem[array1_byte_offset];
276 bmp->
array2 = (uint64_t *) &mem[array2_byte_offset];
279 __rte_bitmap_scan_init(bmp);
321 __rte_bitmap_scan_init(bmp);
338 index2 = pos >> RTE_BITMAP_SLAB_BIT_SIZE_LOG2;
353static inline uint64_t
359 index2 = pos >> RTE_BITMAP_SLAB_BIT_SIZE_LOG2;
360 offset2 = pos & RTE_BITMAP_SLAB_BIT_MASK;
362 return (*slab2) & (1llu << offset2);
376 uint64_t *slab1, *slab2;
380 index2 = pos >> RTE_BITMAP_SLAB_BIT_SIZE_LOG2;
381 offset2 = pos & RTE_BITMAP_SLAB_BIT_MASK;
382 index1 = pos >> (RTE_BITMAP_SLAB_BIT_SIZE_LOG2 + RTE_BITMAP_CL_BIT_SIZE_LOG2);
383 offset1 = (pos >> RTE_BITMAP_CL_BIT_SIZE_LOG2) & RTE_BITMAP_SLAB_BIT_MASK;
387 *slab2 |= 1llu << offset2;
404 uint64_t *slab1, *slab2;
408 index2 = pos >> RTE_BITMAP_SLAB_BIT_SIZE_LOG2;
409 index1 = pos >> (RTE_BITMAP_SLAB_BIT_SIZE_LOG2 + RTE_BITMAP_CL_BIT_SIZE_LOG2);
410 offset1 = (pos >> RTE_BITMAP_CL_BIT_SIZE_LOG2) & RTE_BITMAP_SLAB_BIT_MASK;
418#if RTE_BITMAP_CL_SLAB_SIZE == 8
419static inline uint64_t
420__rte_bitmap_line_not_empty(uint64_t *slab2)
422 uint64_t v1, v2, v3, v4;
424 v1 = slab2[0] | slab2[1];
425 v2 = slab2[2] | slab2[3];
426 v3 = slab2[4] | slab2[5];
427 v4 = slab2[6] | slab2[7];
434#elif RTE_BITMAP_CL_SLAB_SIZE == 16
435static inline uint64_t
436__rte_bitmap_line_not_empty(uint64_t *slab2)
438 uint64_t v1, v2, v3, v4, v5, v6, v7, v8;
440 v1 = slab2[0] | slab2[1];
441 v2 = slab2[2] | slab2[3];
442 v3 = slab2[4] | slab2[5];
443 v4 = slab2[6] | slab2[7];
444 v5 = slab2[8] | slab2[9];
445 v6 = slab2[10] | slab2[11];
446 v7 = slab2[12] | slab2[13];
447 v8 = slab2[14] | slab2[15];
453 return v1 | v3 | v5 | v7;
469 uint64_t *slab1, *slab2;
473 index2 = pos >> RTE_BITMAP_SLAB_BIT_SIZE_LOG2;
474 offset2 = pos & RTE_BITMAP_SLAB_BIT_MASK;
478 *slab2 &= ~(1llu << offset2);
484 index2 &= ~ RTE_BITMAP_CL_SLAB_MASK;
486 if (__rte_bitmap_line_not_empty(slab2)) {
491 index1 = pos >> (RTE_BITMAP_SLAB_BIT_SIZE_LOG2 + RTE_BITMAP_CL_BIT_SIZE_LOG2);
492 offset1 = (pos >> RTE_BITMAP_CL_BIT_SIZE_LOG2) & RTE_BITMAP_SLAB_BIT_MASK;
500__rte_bitmap_scan_search(
struct rte_bitmap *bmp)
507 value1 &= __rte_bitmap_mask1_get(bmp);
512 __rte_bitmap_index1_inc(bmp);
516 for (i = 0; i < bmp->
array1_size; i ++, __rte_bitmap_index1_inc(bmp)) {
527__rte_bitmap_scan_read_init(
struct rte_bitmap *bmp)
529 __rte_bitmap_index2_set(bmp);
535__rte_bitmap_scan_read(
struct rte_bitmap *bmp, uint32_t *pos, uint64_t *slab)
540 for ( ; bmp->
go2 ; bmp->
index2 ++, slab2 ++, bmp->
go2 = bmp->
index2 & RTE_BITMAP_CL_SLAB_MASK) {
542 *pos = bmp->
index2 << RTE_BITMAP_SLAB_BIT_SIZE_LOG2;
547 bmp->
go2 = bmp->
index2 & RTE_BITMAP_CL_SLAB_MASK;
579 if (__rte_bitmap_scan_read(bmp, pos, slab)) {
584 if (__rte_bitmap_scan_search(bmp)) {
585 __rte_bitmap_scan_read_init(bmp);
586 __rte_bitmap_scan_read(bmp, pos, slab);
static __rte_experimental void __rte_bitmap_clear_slab_overhead_bits(uint64_t *slabs, uint32_t slab_size, uint32_t pos)
static void rte_bitmap_prefetch0(struct rte_bitmap *bmp, uint32_t pos)
static int rte_bitmap_free(struct rte_bitmap *bmp)
static void rte_bitmap_clear(struct rte_bitmap *bmp, uint32_t pos)
static uint64_t rte_bitmap_get(struct rte_bitmap *bmp, uint32_t pos)
static uint32_t rte_bitmap_get_memory_footprint(uint32_t n_bits)
static void rte_bitmap_set_slab(struct rte_bitmap *bmp, uint32_t pos, uint64_t slab)
static int rte_bitmap_scan(struct rte_bitmap *bmp, uint32_t *pos, uint64_t *slab)
static struct rte_bitmap * rte_bitmap_init(uint32_t n_bits, uint8_t *mem, uint32_t mem_size)
static void rte_bitmap_reset(struct rte_bitmap *bmp)
static __rte_experimental struct rte_bitmap * rte_bitmap_init_with_all_set(uint32_t n_bits, uint8_t *mem, uint32_t mem_size)
static void rte_bitmap_set(struct rte_bitmap *bmp, uint32_t pos)
static uint32_t rte_align32pow2(uint32_t x)
#define RTE_CACHE_LINE_MASK
static int rte_bsf64_safe(uint64_t v, uint32_t *pos)
static void rte_prefetch1(const volatile void *p)
static void rte_prefetch0(const volatile void *p)