DPDK 25.11.0-rc3
rte_bitmap.h
Go to the documentation of this file.
1/* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2010-2014 Intel Corporation
3 */
4
5#ifndef __INCLUDE_RTE_BITMAP_H__
6#define __INCLUDE_RTE_BITMAP_H__
7
33#include <string.h>
34
35#include <rte_common.h>
36#include <rte_config.h>
37#include <rte_debug.h>
38#include <rte_memory.h>
40#include <rte_prefetch.h>
41
42#ifdef __cplusplus
43extern "C" {
44#endif
45
46/* Slab */
47#define RTE_BITMAP_SLAB_BIT_SIZE 64
48#define RTE_BITMAP_SLAB_BIT_SIZE_LOG2 6
49#define RTE_BITMAP_SLAB_BIT_MASK (RTE_BITMAP_SLAB_BIT_SIZE - 1)
50
51/* Cache line (CL) */
52#define RTE_BITMAP_CL_BIT_SIZE (RTE_CACHE_LINE_SIZE * 8)
53#define RTE_BITMAP_CL_BIT_SIZE_LOG2 (RTE_CACHE_LINE_SIZE_LOG2 + 3)
54#define RTE_BITMAP_CL_BIT_MASK (RTE_BITMAP_CL_BIT_SIZE - 1)
55
56#define RTE_BITMAP_CL_SLAB_SIZE (RTE_BITMAP_CL_BIT_SIZE / RTE_BITMAP_SLAB_BIT_SIZE)
57#define RTE_BITMAP_CL_SLAB_SIZE_LOG2 (RTE_BITMAP_CL_BIT_SIZE_LOG2 - RTE_BITMAP_SLAB_BIT_SIZE_LOG2)
58#define RTE_BITMAP_CL_SLAB_MASK (RTE_BITMAP_CL_SLAB_SIZE - 1)
59
61struct rte_bitmap {
62 /* Context for array1 and array2 */
63 uint64_t *array1;
64 uint64_t *array2;
65 uint32_t array1_size;
66 uint32_t array2_size;
68 /* Context for the "scan next" operation */
69 uint32_t index1;
70 uint32_t offset1;
71 uint32_t index2;
72 uint32_t go2;
74 /* Storage space for array1 and array2 */
75 uint8_t memory[];
76};
77
78static inline void
79__rte_bitmap_index1_inc(struct rte_bitmap *bmp)
80{
81 bmp->index1 = (bmp->index1 + 1) & (bmp->array1_size - 1);
82}
83
84static inline uint64_t
85__rte_bitmap_mask1_get(struct rte_bitmap *bmp)
86{
87 return (~1llu) << bmp->offset1;
88}
89
90static inline void
91__rte_bitmap_index2_set(struct rte_bitmap *bmp)
92{
93 bmp->index2 = (((bmp->index1 << RTE_BITMAP_SLAB_BIT_SIZE_LOG2) + bmp->offset1) << RTE_BITMAP_CL_SLAB_SIZE_LOG2);
94}
95
96static inline uint32_t
97__rte_bitmap_get_memory_footprint(uint32_t n_bits,
98 uint32_t *array1_byte_offset, uint32_t *array1_slabs,
99 uint32_t *array2_byte_offset, uint32_t *array2_slabs)
100{
101 uint32_t n_slabs_context, n_slabs_array1, n_cache_lines_context_and_array1;
102 uint32_t n_cache_lines_array2;
103 uint32_t n_bytes_total;
104
105 n_cache_lines_array2 = (n_bits + RTE_BITMAP_CL_BIT_SIZE - 1) / RTE_BITMAP_CL_BIT_SIZE;
106 n_slabs_array1 = (n_cache_lines_array2 + RTE_BITMAP_SLAB_BIT_SIZE - 1) / RTE_BITMAP_SLAB_BIT_SIZE;
107 n_slabs_array1 = rte_align32pow2(n_slabs_array1);
108 n_slabs_context = (sizeof(struct rte_bitmap) + (RTE_BITMAP_SLAB_BIT_SIZE / 8) - 1) / (RTE_BITMAP_SLAB_BIT_SIZE / 8);
109 n_cache_lines_context_and_array1 = (n_slabs_context + n_slabs_array1 + RTE_BITMAP_CL_SLAB_SIZE - 1) / RTE_BITMAP_CL_SLAB_SIZE;
110 n_bytes_total = (n_cache_lines_context_and_array1 + n_cache_lines_array2) * RTE_CACHE_LINE_SIZE;
111
112 if (array1_byte_offset) {
113 *array1_byte_offset = n_slabs_context * (RTE_BITMAP_SLAB_BIT_SIZE / 8);
114 }
115 if (array1_slabs) {
116 *array1_slabs = n_slabs_array1;
117 }
118 if (array2_byte_offset) {
119 *array2_byte_offset = n_cache_lines_context_and_array1 * RTE_CACHE_LINE_SIZE;
120 }
121 if (array2_slabs) {
122 *array2_slabs = n_cache_lines_array2 * RTE_BITMAP_CL_SLAB_SIZE;
123 }
124
125 return n_bytes_total;
126}
127
128static inline void
129__rte_bitmap_scan_init(struct rte_bitmap *bmp)
130{
131 bmp->index1 = bmp->array1_size - 1;
132 bmp->offset1 = RTE_BITMAP_SLAB_BIT_SIZE - 1;
133 __rte_bitmap_index2_set(bmp);
134 bmp->index2 += RTE_BITMAP_CL_SLAB_SIZE;
135
136 bmp->go2 = 0;
137}
138
147static inline uint32_t
149 /* Check input arguments */
150 if (n_bits == 0) {
151 return 0;
152 }
153
154 return __rte_bitmap_get_memory_footprint(n_bits, NULL, NULL, NULL, NULL);
155}
156
169static inline struct rte_bitmap *
170rte_bitmap_init(uint32_t n_bits, uint8_t *mem, uint32_t mem_size)
171{
172 struct rte_bitmap *bmp;
173 uint32_t array1_byte_offset, array1_slabs, array2_byte_offset, array2_slabs;
174 uint32_t size;
175
176 /* Check input arguments */
177 if (n_bits == 0) {
178 return NULL;
179 }
180
181 if ((mem == NULL) || (((uintptr_t) mem) & RTE_CACHE_LINE_MASK)) {
182 return NULL;
183 }
184
185 size = __rte_bitmap_get_memory_footprint(n_bits,
186 &array1_byte_offset, &array1_slabs,
187 &array2_byte_offset, &array2_slabs);
188 if (size > mem_size)
189 return NULL;
190
191 /* Setup bitmap */
192 memset(mem, 0, size);
193 bmp = (struct rte_bitmap *) mem;
194
195 bmp->array1 = (uint64_t *) &mem[array1_byte_offset];
196 bmp->array1_size = array1_slabs;
197 bmp->array2 = (uint64_t *) &mem[array2_byte_offset];
198 bmp->array2_size = array2_slabs;
199
200 __rte_bitmap_scan_init(bmp);
201
202 return bmp;
203}
204
215static inline void
216__rte_bitmap_clear_slab_overhead_bits(uint64_t *slabs, uint32_t slab_size,
217 uint32_t pos)
218{
219 uint32_t i;
220 uint32_t index = pos / RTE_BITMAP_SLAB_BIT_SIZE;
221 uint32_t offset = pos & RTE_BITMAP_SLAB_BIT_MASK;
222
223 if (offset) {
224 for (i = offset; i < RTE_BITMAP_SLAB_BIT_SIZE; i++)
225 slabs[index] &= ~(1llu << i);
226 index++;
227 }
228 if (index < slab_size)
229 memset(&slabs[index], 0, sizeof(slabs[0]) *
230 (slab_size - index));
231}
232
245static inline struct rte_bitmap *
246rte_bitmap_init_with_all_set(uint32_t n_bits, uint8_t *mem, uint32_t mem_size)
247{
248 struct rte_bitmap *bmp;
249 uint32_t array1_byte_offset, array1_slabs;
250 uint32_t array2_byte_offset, array2_slabs;
251 uint32_t size;
252
253 /* Check input arguments */
254 if (!n_bits || !mem || (((uintptr_t) mem) & RTE_CACHE_LINE_MASK))
255 return NULL;
256
257 size = __rte_bitmap_get_memory_footprint(n_bits,
258 &array1_byte_offset, &array1_slabs,
259 &array2_byte_offset, &array2_slabs);
260 if (size < mem_size)
261 return NULL;
262
263 /* Setup bitmap */
264 bmp = (struct rte_bitmap *) mem;
265 bmp->array1 = (uint64_t *) &mem[array1_byte_offset];
266 bmp->array1_size = array1_slabs;
267 bmp->array2 = (uint64_t *) &mem[array2_byte_offset];
268 bmp->array2_size = array2_slabs;
269
270 __rte_bitmap_scan_init(bmp);
271
272 memset(bmp->array1, 0xff, bmp->array1_size * sizeof(bmp->array1[0]));
273 memset(bmp->array2, 0xff, bmp->array2_size * sizeof(bmp->array2[0]));
274 /* Clear overhead bits. */
276 bmp->array2_size >> RTE_BITMAP_CL_SLAB_SIZE_LOG2);
278 n_bits);
279 return bmp;
280}
281
290static inline void
292{
293 RTE_SET_USED(bmp);
294}
295
302static inline void
304{
305 memset(bmp->array1, 0, bmp->array1_size * sizeof(uint64_t));
306 memset(bmp->array2, 0, bmp->array2_size * sizeof(uint64_t));
307 __rte_bitmap_scan_init(bmp);
308}
309
318static inline void
319rte_bitmap_prefetch0(struct rte_bitmap *bmp, uint32_t pos)
320{
321 uint64_t *slab2;
322 uint32_t index2;
323
324 index2 = pos >> RTE_BITMAP_SLAB_BIT_SIZE_LOG2;
325 slab2 = bmp->array2 + index2;
326 rte_prefetch0((void *) slab2);
327}
328
339static inline uint64_t
340rte_bitmap_get(struct rte_bitmap *bmp, uint32_t pos)
341{
342 uint64_t *slab2;
343 uint32_t index2, offset2;
344
345 index2 = pos >> RTE_BITMAP_SLAB_BIT_SIZE_LOG2;
346 offset2 = pos & RTE_BITMAP_SLAB_BIT_MASK;
347 slab2 = bmp->array2 + index2;
348 return (*slab2) & (1llu << offset2);
349}
350
359static inline void
360rte_bitmap_set(struct rte_bitmap *bmp, uint32_t pos)
361{
362 uint64_t *slab1, *slab2;
363 uint32_t index1, index2, offset1, offset2;
364
365 /* Set bit in array2 slab and set bit in array1 slab */
366 index2 = pos >> RTE_BITMAP_SLAB_BIT_SIZE_LOG2;
367 offset2 = pos & RTE_BITMAP_SLAB_BIT_MASK;
368 index1 = pos >> (RTE_BITMAP_SLAB_BIT_SIZE_LOG2 + RTE_BITMAP_CL_BIT_SIZE_LOG2);
369 offset1 = (pos >> RTE_BITMAP_CL_BIT_SIZE_LOG2) & RTE_BITMAP_SLAB_BIT_MASK;
370 slab2 = bmp->array2 + index2;
371 slab1 = bmp->array1 + index1;
372
373 *slab2 |= 1llu << offset2;
374 *slab1 |= 1llu << offset1;
375}
376
387static inline void
388rte_bitmap_set_slab(struct rte_bitmap *bmp, uint32_t pos, uint64_t slab)
389{
390 uint64_t *slab1, *slab2;
391 uint32_t index1, index2, offset1;
392
393 /* Set bits in array2 slab and set bit in array1 slab */
394 index2 = pos >> RTE_BITMAP_SLAB_BIT_SIZE_LOG2;
395 index1 = pos >> (RTE_BITMAP_SLAB_BIT_SIZE_LOG2 + RTE_BITMAP_CL_BIT_SIZE_LOG2);
396 offset1 = (pos >> RTE_BITMAP_CL_BIT_SIZE_LOG2) & RTE_BITMAP_SLAB_BIT_MASK;
397 slab2 = bmp->array2 + index2;
398 slab1 = bmp->array1 + index1;
399
400 *slab2 |= slab;
401 *slab1 |= 1llu << offset1;
402}
403
404#if RTE_BITMAP_CL_SLAB_SIZE == 8
405static inline uint64_t
406__rte_bitmap_line_not_empty(uint64_t *slab2)
407{
408 uint64_t v1, v2, v3, v4;
409
410 v1 = slab2[0] | slab2[1];
411 v2 = slab2[2] | slab2[3];
412 v3 = slab2[4] | slab2[5];
413 v4 = slab2[6] | slab2[7];
414 v1 |= v2;
415 v3 |= v4;
416
417 return v1 | v3;
418}
419
420#elif RTE_BITMAP_CL_SLAB_SIZE == 16
421static inline uint64_t
422__rte_bitmap_line_not_empty(uint64_t *slab2)
423{
424 uint64_t v1, v2, v3, v4, v5, v6, v7, v8;
425
426 v1 = slab2[0] | slab2[1];
427 v2 = slab2[2] | slab2[3];
428 v3 = slab2[4] | slab2[5];
429 v4 = slab2[6] | slab2[7];
430 v5 = slab2[8] | slab2[9];
431 v6 = slab2[10] | slab2[11];
432 v7 = slab2[12] | slab2[13];
433 v8 = slab2[14] | slab2[15];
434 v1 |= v2;
435 v3 |= v4;
436 v5 |= v6;
437 v7 |= v8;
438
439 return v1 | v3 | v5 | v7;
440}
441
442#endif /* RTE_BITMAP_CL_SLAB_SIZE */
443
452static inline void
453rte_bitmap_clear(struct rte_bitmap *bmp, uint32_t pos)
454{
455 uint64_t *slab1, *slab2;
456 uint32_t index1, index2, offset1, offset2;
457
458 /* Clear bit in array2 slab */
459 index2 = pos >> RTE_BITMAP_SLAB_BIT_SIZE_LOG2;
460 offset2 = pos & RTE_BITMAP_SLAB_BIT_MASK;
461 slab2 = bmp->array2 + index2;
462
463 /* Return if array2 slab is not all-zeros */
464 *slab2 &= ~(1llu << offset2);
465 if (*slab2){
466 return;
467 }
468
469 /* Check the entire cache line of array2 for all-zeros */
470 index2 &= ~ RTE_BITMAP_CL_SLAB_MASK;
471 slab2 = bmp->array2 + index2;
472 if (__rte_bitmap_line_not_empty(slab2)) {
473 return;
474 }
475
476 /* The array2 cache line is all-zeros, so clear bit in array1 slab */
477 index1 = pos >> (RTE_BITMAP_SLAB_BIT_SIZE_LOG2 + RTE_BITMAP_CL_BIT_SIZE_LOG2);
478 offset1 = (pos >> RTE_BITMAP_CL_BIT_SIZE_LOG2) & RTE_BITMAP_SLAB_BIT_MASK;
479 slab1 = bmp->array1 + index1;
480 *slab1 &= ~(1llu << offset1);
481
482 return;
483}
484
485static inline int
486__rte_bitmap_scan_search(struct rte_bitmap *bmp)
487{
488 uint64_t value1;
489 uint32_t i;
490
491 /* Check current array1 slab */
492 value1 = bmp->array1[bmp->index1];
493 value1 &= __rte_bitmap_mask1_get(bmp);
494
495 if (rte_bsf64_safe(value1, &bmp->offset1))
496 return 1;
497
498 __rte_bitmap_index1_inc(bmp);
499 bmp->offset1 = 0;
500
501 /* Look for another array1 slab */
502 for (i = 0; i < bmp->array1_size; i ++, __rte_bitmap_index1_inc(bmp)) {
503 value1 = bmp->array1[bmp->index1];
504
505 if (rte_bsf64_safe(value1, &bmp->offset1))
506 return 1;
507 }
508
509 return 0;
510}
511
512static inline void
513__rte_bitmap_scan_read_init(struct rte_bitmap *bmp)
514{
515 __rte_bitmap_index2_set(bmp);
516 bmp->go2 = 1;
517 rte_prefetch1((void *)(bmp->array2 + bmp->index2 + 8));
518}
519
520static inline int
521__rte_bitmap_scan_read(struct rte_bitmap *bmp, uint32_t *pos, uint64_t *slab)
522{
523 uint64_t *slab2;
524
525 slab2 = bmp->array2 + bmp->index2;
526 for ( ; bmp->go2 ; bmp->index2 ++, slab2 ++, bmp->go2 = bmp->index2 & RTE_BITMAP_CL_SLAB_MASK) {
527 if (*slab2) {
528 *pos = bmp->index2 << RTE_BITMAP_SLAB_BIT_SIZE_LOG2;
529 *slab = *slab2;
530
531 bmp->index2 ++;
532 slab2 ++;
533 bmp->go2 = bmp->index2 & RTE_BITMAP_CL_SLAB_MASK;
534 return 1;
535 }
536 }
537
538 return 0;
539}
540
561static inline int
562rte_bitmap_scan(struct rte_bitmap *bmp, uint32_t *pos, uint64_t *slab)
563{
564 /* Return data from current array2 line if available */
565 if (__rte_bitmap_scan_read(bmp, pos, slab)) {
566 return 1;
567 }
568
569 /* Look for non-empty array2 line */
570 if (__rte_bitmap_scan_search(bmp)) {
571 __rte_bitmap_scan_read_init(bmp);
572 __rte_bitmap_scan_read(bmp, pos, slab);
573 return 1;
574 }
575
576 /* Empty bitmap */
577 return 0;
578}
579
580#ifdef __cplusplus
581}
582#endif
583
584#endif /* __INCLUDE_RTE_BITMAP_H__ */
static void rte_bitmap_prefetch0(struct rte_bitmap *bmp, uint32_t pos)
Definition: rte_bitmap.h:319
static void rte_bitmap_clear(struct rte_bitmap *bmp, uint32_t pos)
Definition: rte_bitmap.h:453
static uint64_t rte_bitmap_get(struct rte_bitmap *bmp, uint32_t pos)
Definition: rte_bitmap.h:340
static uint32_t rte_bitmap_get_memory_footprint(uint32_t n_bits)
Definition: rte_bitmap.h:148
static void rte_bitmap_set_slab(struct rte_bitmap *bmp, uint32_t pos, uint64_t slab)
Definition: rte_bitmap.h:388
static int rte_bitmap_scan(struct rte_bitmap *bmp, uint32_t *pos, uint64_t *slab)
Definition: rte_bitmap.h:562
static void __rte_bitmap_clear_slab_overhead_bits(uint64_t *slabs, uint32_t slab_size, uint32_t pos)
Definition: rte_bitmap.h:216
static struct rte_bitmap * rte_bitmap_init(uint32_t n_bits, uint8_t *mem, uint32_t mem_size)
Definition: rte_bitmap.h:170
static void rte_bitmap_reset(struct rte_bitmap *bmp)
Definition: rte_bitmap.h:303
static struct rte_bitmap * rte_bitmap_init_with_all_set(uint32_t n_bits, uint8_t *mem, uint32_t mem_size)
Definition: rte_bitmap.h:246
static void rte_bitmap_free(struct rte_bitmap *bmp)
Definition: rte_bitmap.h:291
static void rte_bitmap_set(struct rte_bitmap *bmp, uint32_t pos)
Definition: rte_bitmap.h:360
static uint32_t rte_align32pow2(uint32_t x)
Definition: rte_bitops.h:1326
static int rte_bsf64_safe(uint64_t v, uint32_t *pos)
Definition: rte_bitops.h:1253
#define RTE_CACHE_LINE_MASK
Definition: rte_common.h:721
#define RTE_SET_USED(x)
Definition: rte_common.h:264
static void rte_prefetch1(const volatile void *p)
static void rte_prefetch0(const volatile void *p)
uint32_t array2_size
Definition: rte_bitmap.h:66
uint32_t index2
Definition: rte_bitmap.h:71
uint32_t array1_size
Definition: rte_bitmap.h:65
uint64_t * array1
Definition: rte_bitmap.h:63
uint32_t index1
Definition: rte_bitmap.h:69
uint32_t go2
Definition: rte_bitmap.h:72
uint64_t * array2
Definition: rte_bitmap.h:64
uint32_t offset1
Definition: rte_bitmap.h:70