/**
 * @Author: jankincai
 * @Date:   2024-04-01
 * @Last Modified by:   jankincai
 * @Last Modified time: 2024-04-01
 */
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <rte_bitmap.h>
#include <rte_malloc.h>


#define MAX_BITS 8


int main(int argc, char **argv)
{
    int ret = rte_eal_init(argc, argv);
    if (ret < 0)
        rte_panic("Cannot init EAL\n");
    /* >8 End of initialization of Environment Abstraction Layer */

    void *mem;
    uint32_t bmp_size;
    struct rte_bitmap *bmp;

    // bitmap内存占用计算, 返回字节数量
    bmp_size = rte_bitmap_get_memory_footprint(MAX_BITS);
    //        MAX_BITS <= 512  (64 * 8),        bmp_size = 64 * 2 = 128 (bytes)
    //  512 < MAX_BITS <= 1024 (64 * 2 * 8),    bmp_size = 64 * 3 = 192 (bytes)
    // 1024 < MAX_BITS <= 1356 (64 * 3 * 8),    bmp_size = 64 * 4 = 256 (bytes)
    // 1536 < MAX_BITS <= 2048 (64 * 4 * 8),    bmp_size = 64 * 5 = 320 (bytes)
    printf(">>> bmp_size=%d\n", bmp_size);

    // 申请对应字节数量内存, 并填充0
    mem = rte_zmalloc("test_bmap", bmp_size, RTE_CACHE_LINE_SIZE /* align 64 */);
    if (mem == NULL) {
        printf("Failed to allocate memory for bitmap\n");
        exit(EXIT_FAILURE);
    }

    // 初始化bitmap
    bmp = rte_bitmap_init(MAX_BITS, mem, bmp_size);
    if (bmp == NULL) {
        printf("Failed to init bitmap\n");
        exit(EXIT_FAILURE);
    }

    rte_bitmap_set(bmp, 1 /* index */);                             // 0000 00[0]0  => 0000 00[1]0
    printf(">>> [0]: %ld\n", rte_bitmap_get(bmp, 1 /* index */));   // 0000 00[1]0
    rte_bitmap_clear(bmp, 1 /* index */);                           // 0000 00[1]0  => 0000 00[0]0
    printf(">>> [0]: %ld\n", rte_bitmap_get(bmp, 1 /* index */));   // 0000 00[0]0

    rte_bitmap_set(bmp, 0 /* index */);                             // 0000 000[0]  =>  0000 000[1]
    rte_bitmap_set(bmp, 1 /* index */);                             // 0000 00[0]1  =>  0000 00[1]1
    printf(">>> [0]: %ld\n", rte_bitmap_get(bmp, 0 /* index */));   // 0000 000[1]
    printf(">>> [1]: %ld\n", rte_bitmap_get(bmp, 1 /* index */));   // 0000 00[1]0
    rte_bitmap_reset(bmp);                                          // 0000 0011    => 0000 0000
    printf(">>> [0]: %ld\n", rte_bitmap_get(bmp, 0 /* index */));   // 0000 000[0]
    printf(">>> [1]: %ld\n", rte_bitmap_get(bmp, 1 /* index */));   // 0000 00[0]0

    rte_bitmap_free(bmp);
    rte_free(mem);

    return 0;
}

Jankin Cai
1 声望0 粉丝