/** * Copyright (c) Huawei Technologies Co., Ltd. 2023. All rights reserved. * 内部基础模块,所有参数合法性由调用者保证 */ #ifndef MEM_POOL_H_ #define MEM_POOL_H_ #include #include "single_list.h" #include "mmpa_api.h" #ifdef __cplusplus extern "C" { #endif #define MEM_POOL_NODE_SEQ_BITNUM 32 #define MEM_POOL_NODE_IDLE_FLAG 0xCADAEA00UL #define MEM_POOL_NODE_USED_FLAG 0xCADAEA01UL typedef struct { CSingleList idleList; size_t memSize; mmAtomicType64 updating; uint32_t seq; } MemPool; typedef struct { mmAtomicType64 flag; CSingleListNode node; uint8_t data[0]; } MemNode; #define MEM_POOL_INIT(memSize) {CSINGLE_LIST_INIT, (memSize), 0, 0} void InitMemPool(MemPool *pool, size_t memSize); void DeInitMemPool(MemPool *pool); void* MemPoolAlloc(MemPool *pool); void MemPoolFree(MemPool *pool, void *mem, FnDestroy pfnDestroy); static inline bool MemPoolMemUsed(void *mem) { MemNode *memNode = GET_MAIN_BY_MEMBER(mem, MemNode, data); return ((memNode->flag & MEM_POOL_NODE_USED_FLAG) == MEM_POOL_NODE_USED_FLAG); } static inline uint32_t GetMemPoolMemSeq(void *mem) { MemNode *memNode = GET_MAIN_BY_MEMBER(mem, MemNode, data); return (uint32_t)(memNode->flag >> MEM_POOL_NODE_SEQ_BITNUM); } static inline bool MemPoolMemMatchSeq(void *mem, uint32_t seq) { MemNode *memNode = GET_MAIN_BY_MEMBER(mem, MemNode, data); return (memNode->flag == (MEM_POOL_NODE_USED_FLAG | (((uint64_t)seq) << MEM_POOL_NODE_SEQ_BITNUM))); } #ifdef __cplusplus } #endif #endif