34 #ifndef __INCLUDE_RTE_BITMAP_H__
35 #define __INCLUDE_RTE_BITMAP_H__
73 #ifndef RTE_BITMAP_OPTIMIZATIONS
74 #define RTE_BITMAP_OPTIMIZATIONS 1
78 #define RTE_BITMAP_SLAB_BIT_SIZE 64
79 #define RTE_BITMAP_SLAB_BIT_SIZE_LOG2 6
80 #define RTE_BITMAP_SLAB_BIT_MASK (RTE_BITMAP_SLAB_BIT_SIZE - 1)
83 #define RTE_BITMAP_CL_BIT_SIZE (RTE_CACHE_LINE_SIZE * 8)
84 #define RTE_BITMAP_CL_BIT_SIZE_LOG2 (RTE_CACHE_LINE_SIZE_LOG2 + 3)
85 #define RTE_BITMAP_CL_BIT_MASK (RTE_BITMAP_CL_BIT_SIZE - 1)
87 #define RTE_BITMAP_CL_SLAB_SIZE (RTE_BITMAP_CL_BIT_SIZE / RTE_BITMAP_SLAB_BIT_SIZE)
88 #define RTE_BITMAP_CL_SLAB_SIZE_LOG2 (RTE_BITMAP_CL_BIT_SIZE_LOG2 - RTE_BITMAP_SLAB_BIT_SIZE_LOG2)
89 #define RTE_BITMAP_CL_SLAB_MASK (RTE_BITMAP_CL_SLAB_SIZE - 1)
110 __rte_bitmap_index1_inc(
struct rte_bitmap *bmp)
115 static inline uint64_t
116 __rte_bitmap_mask1_get(
struct rte_bitmap *bmp)
122 __rte_bitmap_index2_set(
struct rte_bitmap *bmp)
124 bmp->
index2 = (((bmp->
index1 << RTE_BITMAP_SLAB_BIT_SIZE_LOG2) + bmp->
offset1) << RTE_BITMAP_CL_SLAB_SIZE_LOG2);
127 #if RTE_BITMAP_OPTIMIZATIONS
130 rte_bsf64(uint64_t slab, uint32_t *pos)
136 *pos = __builtin_ctzll(slab);
143 rte_bsf64(uint64_t slab, uint32_t *pos)
152 for (i = 0, mask = 1; i < RTE_BITMAP_SLAB_BIT_SIZE; i ++, mask <<= 1) {
164 static inline uint32_t
165 __rte_bitmap_get_memory_footprint(uint32_t n_bits,
166 uint32_t *array1_byte_offset, uint32_t *array1_slabs,
167 uint32_t *array2_byte_offset, uint32_t *array2_slabs)
169 uint32_t n_slabs_context, n_slabs_array1, n_cache_lines_context_and_array1;
170 uint32_t n_cache_lines_array2;
171 uint32_t n_bytes_total;
173 n_cache_lines_array2 = (n_bits + RTE_BITMAP_CL_BIT_SIZE - 1) / RTE_BITMAP_CL_BIT_SIZE;
174 n_slabs_array1 = (n_cache_lines_array2 + RTE_BITMAP_SLAB_BIT_SIZE - 1) / RTE_BITMAP_SLAB_BIT_SIZE;
176 n_slabs_context = (
sizeof(
struct rte_bitmap) + (RTE_BITMAP_SLAB_BIT_SIZE / 8) - 1) / (RTE_BITMAP_SLAB_BIT_SIZE / 8);
177 n_cache_lines_context_and_array1 = (n_slabs_context + n_slabs_array1 + RTE_BITMAP_CL_SLAB_SIZE - 1) / RTE_BITMAP_CL_SLAB_SIZE;
178 n_bytes_total = (n_cache_lines_context_and_array1 + n_cache_lines_array2) * RTE_CACHE_LINE_SIZE;
180 if (array1_byte_offset) {
181 *array1_byte_offset = n_slabs_context * (RTE_BITMAP_SLAB_BIT_SIZE / 8);
184 *array1_slabs = n_slabs_array1;
186 if (array2_byte_offset) {
187 *array2_byte_offset = n_cache_lines_context_and_array1 * RTE_CACHE_LINE_SIZE;
190 *array2_slabs = n_cache_lines_array2 * RTE_BITMAP_CL_SLAB_SIZE;
193 return n_bytes_total;
197 __rte_bitmap_scan_init(
struct rte_bitmap *bmp)
200 bmp->
offset1 = RTE_BITMAP_SLAB_BIT_SIZE - 1;
201 __rte_bitmap_index2_set(bmp);
202 bmp->
index2 += RTE_BITMAP_CL_SLAB_SIZE;
215 static inline uint32_t
222 return __rte_bitmap_get_memory_footprint(n_bits, NULL, NULL, NULL, NULL);
241 uint32_t array1_byte_offset, array1_slabs, array2_byte_offset, array2_slabs;
253 size = __rte_bitmap_get_memory_footprint(n_bits,
254 &array1_byte_offset, &array1_slabs,
255 &array2_byte_offset, &array2_slabs);
256 if (size < mem_size) {
261 memset(mem, 0, size);
264 bmp->
array1 = (uint64_t *) &mem[array1_byte_offset];
266 bmp->
array2 = (uint64_t *) &mem[array2_byte_offset];
269 __rte_bitmap_scan_init(bmp);
304 __rte_bitmap_scan_init(bmp);
323 index2 = pos >> RTE_BITMAP_SLAB_BIT_SIZE_LOG2;
338 static inline uint64_t
344 index2 = pos >> RTE_BITMAP_SLAB_BIT_SIZE_LOG2;
345 offset2 = pos & RTE_BITMAP_SLAB_BIT_MASK;
347 return (*slab2) & (1lu << offset2);
361 uint64_t *slab1, *slab2;
365 index2 = pos >> RTE_BITMAP_SLAB_BIT_SIZE_LOG2;
366 offset2 = pos & RTE_BITMAP_SLAB_BIT_MASK;
367 index1 = pos >> (RTE_BITMAP_SLAB_BIT_SIZE_LOG2 + RTE_BITMAP_CL_BIT_SIZE_LOG2);
368 offset1 = (pos >> RTE_BITMAP_CL_BIT_SIZE_LOG2) & RTE_BITMAP_SLAB_BIT_MASK;
372 *slab2 |= 1lu << offset2;
389 uint64_t *slab1, *slab2;
393 index2 = pos >> RTE_BITMAP_SLAB_BIT_SIZE_LOG2;
394 index1 = pos >> (RTE_BITMAP_SLAB_BIT_SIZE_LOG2 + RTE_BITMAP_CL_BIT_SIZE_LOG2);
395 offset1 = (pos >> RTE_BITMAP_CL_BIT_SIZE_LOG2) & RTE_BITMAP_SLAB_BIT_MASK;
403 static inline uint64_t
404 __rte_bitmap_line_not_empty(uint64_t *slab2)
406 uint64_t v1, v2, v3, v4;
408 v1 = slab2[0] | slab2[1];
409 v2 = slab2[2] | slab2[3];
410 v3 = slab2[4] | slab2[5];
411 v4 = slab2[6] | slab2[7];
429 uint64_t *slab1, *slab2;
433 index2 = pos >> RTE_BITMAP_SLAB_BIT_SIZE_LOG2;
434 offset2 = pos & RTE_BITMAP_SLAB_BIT_MASK;
438 *slab2 &= ~(1lu << offset2);
444 index2 &= ~ RTE_BITMAP_CL_SLAB_MASK;
446 if (__rte_bitmap_line_not_empty(slab2)) {
451 index1 = pos >> (RTE_BITMAP_SLAB_BIT_SIZE_LOG2 + RTE_BITMAP_CL_BIT_SIZE_LOG2);
452 offset1 = (pos >> RTE_BITMAP_CL_BIT_SIZE_LOG2) & RTE_BITMAP_SLAB_BIT_MASK;
460 __rte_bitmap_scan_search(
struct rte_bitmap *bmp)
467 value1 &= __rte_bitmap_mask1_get(bmp);
469 if (rte_bsf64(value1, &bmp->
offset1)) {
473 __rte_bitmap_index1_inc(bmp);
477 for (i = 0; i < bmp->
array1_size; i ++, __rte_bitmap_index1_inc(bmp)) {
480 if (rte_bsf64(value1, &bmp->
offset1)) {
489 __rte_bitmap_scan_read_init(
struct rte_bitmap *bmp)
491 __rte_bitmap_index2_set(bmp);
497 __rte_bitmap_scan_read(
struct rte_bitmap *bmp, uint32_t *pos, uint64_t *slab)
502 for ( ; bmp->
go2 ; bmp->
index2 ++, slab2 ++, bmp->
go2 = bmp->
index2 & RTE_BITMAP_CL_SLAB_MASK) {
504 *pos = bmp->
index2 << RTE_BITMAP_SLAB_BIT_SIZE_LOG2;
509 bmp->
go2 = bmp->
index2 & RTE_BITMAP_CL_SLAB_MASK;
541 if (__rte_bitmap_scan_read(bmp, pos, slab)) {
546 if (__rte_bitmap_scan_search(bmp)) {
547 __rte_bitmap_scan_read_init(bmp);
548 __rte_bitmap_scan_read(bmp, pos, slab);