5 #ifndef __INCLUDE_RTE_BITMAP_H__
6 #define __INCLUDE_RTE_BITMAP_H__
40 #include <rte_config.h>
46 #ifndef RTE_BITMAP_OPTIMIZATIONS
47 #define RTE_BITMAP_OPTIMIZATIONS 1
51 #define RTE_BITMAP_SLAB_BIT_SIZE 64
52 #define RTE_BITMAP_SLAB_BIT_SIZE_LOG2 6
53 #define RTE_BITMAP_SLAB_BIT_MASK (RTE_BITMAP_SLAB_BIT_SIZE - 1)
56 #define RTE_BITMAP_CL_BIT_SIZE (RTE_CACHE_LINE_SIZE * 8)
57 #define RTE_BITMAP_CL_BIT_SIZE_LOG2 (RTE_CACHE_LINE_SIZE_LOG2 + 3)
58 #define RTE_BITMAP_CL_BIT_MASK (RTE_BITMAP_CL_BIT_SIZE - 1)
60 #define RTE_BITMAP_CL_SLAB_SIZE (RTE_BITMAP_CL_BIT_SIZE / RTE_BITMAP_SLAB_BIT_SIZE)
61 #define RTE_BITMAP_CL_SLAB_SIZE_LOG2 (RTE_BITMAP_CL_BIT_SIZE_LOG2 - RTE_BITMAP_SLAB_BIT_SIZE_LOG2)
62 #define RTE_BITMAP_CL_SLAB_MASK (RTE_BITMAP_CL_SLAB_SIZE - 1)
83 __rte_bitmap_index1_inc(
struct rte_bitmap *bmp)
88 static inline uint64_t
95 __rte_bitmap_index2_set(
struct rte_bitmap *bmp)
97 bmp->
index2 = (((bmp->
index1 << RTE_BITMAP_SLAB_BIT_SIZE_LOG2) + bmp->
offset1) << RTE_BITMAP_CL_SLAB_SIZE_LOG2);
100 #if RTE_BITMAP_OPTIMIZATIONS
103 rte_bsf64(uint64_t slab, uint32_t *pos)
109 *pos = __builtin_ctzll(slab);
116 rte_bsf64(uint64_t slab, uint32_t *pos)
125 for (i = 0, mask = 1; i < RTE_BITMAP_SLAB_BIT_SIZE; i ++, mask <<= 1) {
137 static inline uint32_t
138 __rte_bitmap_get_memory_footprint(uint32_t n_bits,
139 uint32_t *array1_byte_offset, uint32_t *array1_slabs,
140 uint32_t *array2_byte_offset, uint32_t *array2_slabs)
142 uint32_t n_slabs_context, n_slabs_array1, n_cache_lines_context_and_array1;
143 uint32_t n_cache_lines_array2;
144 uint32_t n_bytes_total;
146 n_cache_lines_array2 = (n_bits + RTE_BITMAP_CL_BIT_SIZE - 1) / RTE_BITMAP_CL_BIT_SIZE;
147 n_slabs_array1 = (n_cache_lines_array2 + RTE_BITMAP_SLAB_BIT_SIZE - 1) / RTE_BITMAP_SLAB_BIT_SIZE;
149 n_slabs_context = (
sizeof(
struct rte_bitmap) + (RTE_BITMAP_SLAB_BIT_SIZE / 8) - 1) / (RTE_BITMAP_SLAB_BIT_SIZE / 8);
150 n_cache_lines_context_and_array1 = (n_slabs_context + n_slabs_array1 + RTE_BITMAP_CL_SLAB_SIZE - 1) / RTE_BITMAP_CL_SLAB_SIZE;
151 n_bytes_total = (n_cache_lines_context_and_array1 + n_cache_lines_array2) * RTE_CACHE_LINE_SIZE;
153 if (array1_byte_offset) {
154 *array1_byte_offset = n_slabs_context * (RTE_BITMAP_SLAB_BIT_SIZE / 8);
157 *array1_slabs = n_slabs_array1;
159 if (array2_byte_offset) {
160 *array2_byte_offset = n_cache_lines_context_and_array1 * RTE_CACHE_LINE_SIZE;
163 *array2_slabs = n_cache_lines_array2 * RTE_BITMAP_CL_SLAB_SIZE;
166 return n_bytes_total;
170 __rte_bitmap_scan_init(
struct rte_bitmap *bmp)
173 bmp->
offset1 = RTE_BITMAP_SLAB_BIT_SIZE - 1;
174 __rte_bitmap_index2_set(bmp);
175 bmp->
index2 += RTE_BITMAP_CL_SLAB_SIZE;
188 static inline uint32_t
195 return __rte_bitmap_get_memory_footprint(n_bits, NULL, NULL, NULL, NULL);
214 uint32_t array1_byte_offset, array1_slabs, array2_byte_offset, array2_slabs;
226 size = __rte_bitmap_get_memory_footprint(n_bits,
227 &array1_byte_offset, &array1_slabs,
228 &array2_byte_offset, &array2_slabs);
229 if (size < mem_size) {
234 memset(mem, 0, size);
237 bmp->
array1 = (uint64_t *) &mem[array1_byte_offset];
239 bmp->
array2 = (uint64_t *) &mem[array2_byte_offset];
242 __rte_bitmap_scan_init(bmp);
277 __rte_bitmap_scan_init(bmp);
296 index2 = pos >> RTE_BITMAP_SLAB_BIT_SIZE_LOG2;
311 static inline uint64_t
317 index2 = pos >> RTE_BITMAP_SLAB_BIT_SIZE_LOG2;
318 offset2 = pos & RTE_BITMAP_SLAB_BIT_MASK;
320 return (*slab2) & (1lu << offset2);
334 uint64_t *slab1, *slab2;
338 index2 = pos >> RTE_BITMAP_SLAB_BIT_SIZE_LOG2;
339 offset2 = pos & RTE_BITMAP_SLAB_BIT_MASK;
340 index1 = pos >> (RTE_BITMAP_SLAB_BIT_SIZE_LOG2 + RTE_BITMAP_CL_BIT_SIZE_LOG2);
341 offset1 = (pos >> RTE_BITMAP_CL_BIT_SIZE_LOG2) & RTE_BITMAP_SLAB_BIT_MASK;
345 *slab2 |= 1lu << offset2;
362 uint64_t *slab1, *slab2;
366 index2 = pos >> RTE_BITMAP_SLAB_BIT_SIZE_LOG2;
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;
376 static inline uint64_t
377 __rte_bitmap_line_not_empty(uint64_t *slab2)
379 uint64_t v1, v2, v3, v4;
381 v1 = slab2[0] | slab2[1];
382 v2 = slab2[2] | slab2[3];
383 v3 = slab2[4] | slab2[5];
384 v4 = slab2[6] | slab2[7];
402 uint64_t *slab1, *slab2;
406 index2 = pos >> RTE_BITMAP_SLAB_BIT_SIZE_LOG2;
407 offset2 = pos & RTE_BITMAP_SLAB_BIT_MASK;
411 *slab2 &= ~(1lu << offset2);
417 index2 &= ~ RTE_BITMAP_CL_SLAB_MASK;
419 if (__rte_bitmap_line_not_empty(slab2)) {
424 index1 = pos >> (RTE_BITMAP_SLAB_BIT_SIZE_LOG2 + RTE_BITMAP_CL_BIT_SIZE_LOG2);
425 offset1 = (pos >> RTE_BITMAP_CL_BIT_SIZE_LOG2) & RTE_BITMAP_SLAB_BIT_MASK;
433 __rte_bitmap_scan_search(
struct rte_bitmap *bmp)
440 value1 &= __rte_bitmap_mask1_get(bmp);
442 if (rte_bsf64(value1, &bmp->
offset1)) {
446 __rte_bitmap_index1_inc(bmp);
450 for (i = 0; i < bmp->
array1_size; i ++, __rte_bitmap_index1_inc(bmp)) {
453 if (rte_bsf64(value1, &bmp->
offset1)) {
462 __rte_bitmap_scan_read_init(
struct rte_bitmap *bmp)
464 __rte_bitmap_index2_set(bmp);
470 __rte_bitmap_scan_read(
struct rte_bitmap *bmp, uint32_t *pos, uint64_t *slab)
475 for ( ; bmp->
go2 ; bmp->
index2 ++, slab2 ++, bmp->
go2 = bmp->
index2 & RTE_BITMAP_CL_SLAB_MASK) {
477 *pos = bmp->
index2 << RTE_BITMAP_SLAB_BIT_SIZE_LOG2;
482 bmp->
go2 = bmp->
index2 & RTE_BITMAP_CL_SLAB_MASK;
514 if (__rte_bitmap_scan_read(bmp, pos, slab)) {
519 if (__rte_bitmap_scan_search(bmp)) {
520 __rte_bitmap_scan_read_init(bmp);
521 __rte_bitmap_scan_read(bmp, pos, slab);