From a3360ea0942d9f9a894867818d0ba22052107c37 Mon Sep 17 00:00:00 2001 From: Hongze Cheng Date: Fri, 17 Dec 2021 16:12:49 +0800 Subject: [PATCH] refact --- include/util/mallocator.h | 33 +++++++++++-------- source/dnode/vnode/impl/src/vnodeBufferPool.c | 6 +--- source/dnode/vnode/tsdb/src/tsdbMemTable.c | 6 ++-- source/util/src/mallocator.c | 4 ++- 4 files changed, 26 insertions(+), 23 deletions(-) diff --git a/include/util/mallocator.h b/include/util/mallocator.h index ffe242017e..49a9327353 100644 --- a/include/util/mallocator.h +++ b/include/util/mallocator.h @@ -22,23 +22,28 @@ extern "C" { #endif -typedef struct SMemAllocator SMemAllocator; -typedef struct SMemAllocatorFactory SMemAllocatorFactory; +// Memory allocator +#define TD_MEM_ALCT(TYPE) \ + struct { \ + void *(*malloc_)(struct TYPE *, uint64_t size); \ + void (*free_)(struct TYPE *, void *ptr); \ + } +#define TD_MA_MALLOC_FUNC(TMA) (TMA)->malloc_ +#define TD_MA_FREE_FUNC(TMA) (TMA)->free_ -struct SMemAllocator { - void *impl; - void *(*malloc)(SMemAllocator *, uint64_t size); - void *(*calloc)(SMemAllocator *, uint64_t nmemb, uint64_t size); - void *(*realloc)(SMemAllocator *, void *ptr, uint64_t size); - void (*free)(SMemAllocator *, void *ptr); - uint64_t (*usage)(SMemAllocator *); -}; +#define TD_MA_MALLOC(TMA, SIZE) (*((TMA)->malloc_))(TMA, (SIZE)) +#define TD_MA_FREE(TMA, PTR) (*((TMA)->free_))(TMA, (PTR)) -struct SMemAllocatorFactory { +typedef struct SMemAllocator { void *impl; - SMemAllocator *(*create)(SMemAllocatorFactory *); - void (*destroy)(SMemAllocatorFactory *, SMemAllocator *); -}; + TD_MEM_ALCT(SMemAllocator); +} SMemAllocator; + +typedef struct SMemAllocatorFactory { + void *impl; + SMemAllocator *(*create)(struct SMemAllocatorFactory *); + void (*destroy)(struct SMemAllocatorFactory *, SMemAllocator *); +} SMemAllocatorFactory; #ifdef __cplusplus } diff --git a/source/dnode/vnode/impl/src/vnodeBufferPool.c b/source/dnode/vnode/impl/src/vnodeBufferPool.c index 763cf3e3b3..228df6c0a4 100644 --- a/source/dnode/vnode/impl/src/vnodeBufferPool.c +++ b/source/dnode/vnode/impl/src/vnodeBufferPool.c @@ -168,11 +168,7 @@ static SMemAllocator *vBufPoolCreateMA(SMemAllocatorFactory *pMAF) { pWrapper->pVMA = pVnode->pBufPool->inuse; pMA->impl = pWrapper; - pMA->malloc = vmaMaloocCb; - pMA->calloc = NULL; - pMA->realloc = NULL; - pMA->free = NULL; - pMA->usage = NULL; + TD_MA_MALLOC_FUNC(pMA) = vmaMaloocCb; return pMA; } diff --git a/source/dnode/vnode/tsdb/src/tsdbMemTable.c b/source/dnode/vnode/tsdb/src/tsdbMemTable.c index 8a320129b0..7b0df18f5a 100644 --- a/source/dnode/vnode/tsdb/src/tsdbMemTable.c +++ b/source/dnode/vnode/tsdb/src/tsdbMemTable.c @@ -50,7 +50,7 @@ STsdbMemTable *tsdbNewMemTable(SMemAllocatorFactory *pMAF) { pMA = (*pMAF->create)(pMAF); ASSERT(pMA != NULL); - pMemTable = (STsdbMemTable *)((*pMA->malloc)(pMA, sizeof(*pMemTable))); + pMemTable = (STsdbMemTable *)TD_MA_MALLOC(pMA, sizeof(*pMemTable)); if (pMemTable == NULL) { (*pMAF->destroy)(pMAF, pMA); return NULL; @@ -71,7 +71,7 @@ STsdbMemTable *tsdbNewMemTable(SMemAllocatorFactory *pMAF) { void tsdbFreeMemTable(SMemAllocatorFactory *pMAF, STsdbMemTable *pMemTable) { SMemAllocator *pMA = pMemTable->pMA; - if (pMA->free) { + if (TD_MA_FREE_FUNC(pMA) != NULL) { // TODO ASSERT(0); } @@ -81,7 +81,7 @@ void tsdbFreeMemTable(SMemAllocatorFactory *pMAF, STsdbMemTable *pMemTable) { int tsdbInsertDataToMemTable(STsdbMemTable *pMemTable, SSubmitMsg *pMsg) { SMemAllocator *pMA = pMemTable->pMA; - STbData * pTbData = (STbData *)((*pMA->malloc)(pMA, sizeof(*pTbData))); + STbData * pTbData = (STbData *)TD_MA_MALLOC(pMA, sizeof(*pTbData)); if (pTbData == NULL) { // TODO } diff --git a/source/util/src/mallocator.c b/source/util/src/mallocator.c index 1819396ccd..a56fbfa597 100644 --- a/source/util/src/mallocator.c +++ b/source/util/src/mallocator.c @@ -16,6 +16,7 @@ #include "mallocator.h" /* ------------------------ HEAP ALLOCATOR ------------------------ */ +#if 0 typedef struct { size_t tusage; } SHeapAllocator; @@ -104,4 +105,5 @@ static size_t haUsage(SMemAllocator *pma) { return ((SHeapAllocator *)(pma->impl /* ------------------------ ARENA ALLOCATOR ------------------------ */ typedef struct { size_t usage; -} SArenaAllocator; \ No newline at end of file +} SArenaAllocator; +#endif \ No newline at end of file