diff options
| author | liuyu <[email protected]> | 2024-07-02 07:15:28 -0400 |
|---|---|---|
| committer | liuyu <[email protected]> | 2024-07-02 07:15:28 -0400 |
| commit | 462c024c8abb494efa0491af1a5cff559be06453 (patch) | |
| tree | d0e6eeac544972dc22b62b65a6bd3f293c243e71 | |
| parent | 87000030a885847c6dd71c9a692f03d0918fe396 (diff) | |
调整内存池结构体位置
| -rw-r--r-- | bbq/include/bbq.h | 41 | ||||
| -rw-r--r-- | bbq/src/bbq.c | 12 |
2 files changed, 28 insertions, 25 deletions
diff --git a/bbq/include/bbq.h b/bbq/include/bbq.h index 5353b21..ec102e5 100644 --- a/bbq/include/bbq.h +++ b/bbq/include/bbq.h @@ -1,6 +1,6 @@ /* * @Author: [email protected] - * @LastEditTime: 2024-07-02 05:51:15 + * @LastEditTime: 2024-07-02 11:12:50 * @Describe: bbq(Block-based Bounded Queue)头文件 * 参考:https://www.usenix.org/system/files/atc22-wang-jiawei.pdf */ @@ -49,31 +49,34 @@ struct bbq_block { typedef void *(*bbq_malloc_f)(int32_t socket_id, size_t size); typedef void (*bbq_free_f)(void *ptr, size_t size); -struct bbq { - char name[BBQ_SYMBOL_MAX] __BBQ_CACHE_ALIGNED; +struct bbq_mempool { + char *ptr; // 内存池起始地址 + size_t off; // 已使用的偏移大小 + size_t size; // 内存池总大小 - int32_t socket_id; // 用于libnuma分配内存,socket_id小于0将使用malloc分配 - uint32_t bn; // blocks的个数 - uint32_t bs; // blocks.entries的个数 - uint32_t flags; // 标记:retry new 模式,还是drop old模式 - uint32_t idx_bits; // bbq_head里idx所占的位数 - uint32_t off_bits; // bbq_cursor里offset所占的位数 - uint64_t idx_mask; // idx_bits偏移后的掩码 - uint64_t off_mask; // off_bits偏移后的掩码 - uint64_t entry_size; // blocks.entries里每个entry的大小 bbq_malloc_f malloc_f; // 申请内存的函数,默认为malloc bbq_free_f free_f; // 申请内存的函数,默认为free +} __BBQ_CACHE_ALIGNED; + +struct bbq { + char name[BBQ_SYMBOL_MAX] __BBQ_CACHE_ALIGNED; + int32_t socket_id; // 用于libnuma分配内存,socket_id小于0将使用malloc分配 + uint32_t bn; // blocks的个数 + uint32_t bs; // blocks.entries的个数 + uint32_t flags; // 标记:retry new 模式,还是drop old模式 + uint32_t idx_bits; // bbq_head里idx所占的位数 + uint32_t off_bits; // bbq_cursor里offset所占的位数 + uint64_t idx_mask; // idx_bits偏移后的掩码 + uint64_t off_mask; // off_bits偏移后的掩码 + uint64_t entry_size; // blocks.entries里每个entry的大小 + uint64_t pad0; + uint64_t pad1; struct bbq_head phead; // 生产者头,指向块的索引,分为两部分:version|idx struct bbq_head chead; // 消费者头,指向块的索引,分为两部分:version|idx - struct bbq_block *blocks; // bn大小的数组 - - struct { - char *ptr; // 内存池起始地址 - size_t off; // 已使用的偏移大小 - size_t size; // 内存池总大小 - } memory_pool; // 仅在初始化和调试时会读写 + struct bbq_mempool memory_pool; // 仅在初始化和调试时会读写 + struct bbq_block *blocks; // bn大小的数组 } __BBQ_CACHE_ALIGNED; #define BBQ_F_DEFAULT 0x0 diff --git a/bbq/src/bbq.c b/bbq/src/bbq.c index accea4d..3c14bb8 100644 --- a/bbq/src/bbq.c +++ b/bbq/src/bbq.c @@ -1,6 +1,6 @@ /* * @Author: liuyu - * @LastEditTime: 2024-07-02 05:52:32 + * @LastEditTime: 2024-07-02 07:15:00 * @Email: [email protected] * @Describe: bbq(Block-based Bounded Queue)实现 * 参考:https://www.usenix.org/system/files/atc22-wang-jiawei.pdf @@ -255,9 +255,9 @@ int block_init(struct bbq *q, struct bbq_block *block, bool cursor_init, uint32_ void block_destory(struct bbq *q, struct bbq_block *block) { if (block->entries) { #ifdef BBQ_MEMORY - q->free_f(block->entries, (q->bs + 1) * q->entry_size); + q->memory_pool.free_f(block->entries, (q->bs + 1) * q->entry_size); #else - q->free_f(block->entries, q->bs * q->entry_size); + q->memory_pool.free_f(block->entries, q->bs * q->entry_size); #endif block->entries = NULL; } @@ -339,6 +339,8 @@ static struct bbq *__bbq_create_bnbs(const char *name, uint32_t bn, uint32_t bs, q->memory_pool.size = all_size; q->memory_pool.ptr = (char *)q; q->memory_pool.off += sizeof(struct bbq); + q->memory_pool.malloc_f = malloc_f; + q->memory_pool.free_f = free_f; ret = snprintf(q->name, sizeof(q->name), "%s", name); q->bn = bn; @@ -352,8 +354,6 @@ static struct bbq *__bbq_create_bnbs(const char *name, uint32_t bn, uint32_t bs, q->chead.value.single = true; } q->flags = flags; - q->malloc_f = malloc_f; - q->free_f = free_f; size = bn * sizeof(*q->blocks); q->blocks = bbq_malloc_from_pool(q, size); @@ -437,7 +437,7 @@ void bbq_destory(struct bbq *q) { return; } - q->free_f(q->memory_pool.ptr, q->memory_pool.size); + q->memory_pool.free_f(q->memory_pool.ptr, q->memory_pool.size); } #define BBQ_DATA_TYPE_SINGLE 0x0 |
