This commit is contained in:
Hongze Cheng 2021-12-17 16:12:49 +08:00
parent 91a36a51cf
commit a3360ea094
4 changed files with 26 additions and 23 deletions

View File

@ -22,23 +22,28 @@
extern "C" { extern "C" {
#endif #endif
typedef struct SMemAllocator SMemAllocator; // Memory allocator
typedef struct SMemAllocatorFactory SMemAllocatorFactory; #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 { #define TD_MA_MALLOC(TMA, SIZE) (*((TMA)->malloc_))(TMA, (SIZE))
void *impl; #define TD_MA_FREE(TMA, PTR) (*((TMA)->free_))(TMA, (PTR))
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 *);
};
struct SMemAllocatorFactory { typedef struct SMemAllocator {
void *impl; void *impl;
SMemAllocator *(*create)(SMemAllocatorFactory *); TD_MEM_ALCT(SMemAllocator);
void (*destroy)(SMemAllocatorFactory *, SMemAllocator *); } SMemAllocator;
};
typedef struct SMemAllocatorFactory {
void *impl;
SMemAllocator *(*create)(struct SMemAllocatorFactory *);
void (*destroy)(struct SMemAllocatorFactory *, SMemAllocator *);
} SMemAllocatorFactory;
#ifdef __cplusplus #ifdef __cplusplus
} }

View File

@ -168,11 +168,7 @@ static SMemAllocator *vBufPoolCreateMA(SMemAllocatorFactory *pMAF) {
pWrapper->pVMA = pVnode->pBufPool->inuse; pWrapper->pVMA = pVnode->pBufPool->inuse;
pMA->impl = pWrapper; pMA->impl = pWrapper;
pMA->malloc = vmaMaloocCb; TD_MA_MALLOC_FUNC(pMA) = vmaMaloocCb;
pMA->calloc = NULL;
pMA->realloc = NULL;
pMA->free = NULL;
pMA->usage = NULL;
return pMA; return pMA;
} }

View File

@ -50,7 +50,7 @@ STsdbMemTable *tsdbNewMemTable(SMemAllocatorFactory *pMAF) {
pMA = (*pMAF->create)(pMAF); pMA = (*pMAF->create)(pMAF);
ASSERT(pMA != NULL); ASSERT(pMA != NULL);
pMemTable = (STsdbMemTable *)((*pMA->malloc)(pMA, sizeof(*pMemTable))); pMemTable = (STsdbMemTable *)TD_MA_MALLOC(pMA, sizeof(*pMemTable));
if (pMemTable == NULL) { if (pMemTable == NULL) {
(*pMAF->destroy)(pMAF, pMA); (*pMAF->destroy)(pMAF, pMA);
return NULL; return NULL;
@ -71,7 +71,7 @@ STsdbMemTable *tsdbNewMemTable(SMemAllocatorFactory *pMAF) {
void tsdbFreeMemTable(SMemAllocatorFactory *pMAF, STsdbMemTable *pMemTable) { void tsdbFreeMemTable(SMemAllocatorFactory *pMAF, STsdbMemTable *pMemTable) {
SMemAllocator *pMA = pMemTable->pMA; SMemAllocator *pMA = pMemTable->pMA;
if (pMA->free) { if (TD_MA_FREE_FUNC(pMA) != NULL) {
// TODO // TODO
ASSERT(0); ASSERT(0);
} }
@ -81,7 +81,7 @@ void tsdbFreeMemTable(SMemAllocatorFactory *pMAF, STsdbMemTable *pMemTable) {
int tsdbInsertDataToMemTable(STsdbMemTable *pMemTable, SSubmitMsg *pMsg) { int tsdbInsertDataToMemTable(STsdbMemTable *pMemTable, SSubmitMsg *pMsg) {
SMemAllocator *pMA = pMemTable->pMA; SMemAllocator *pMA = pMemTable->pMA;
STbData * pTbData = (STbData *)((*pMA->malloc)(pMA, sizeof(*pTbData))); STbData * pTbData = (STbData *)TD_MA_MALLOC(pMA, sizeof(*pTbData));
if (pTbData == NULL) { if (pTbData == NULL) {
// TODO // TODO
} }

View File

@ -16,6 +16,7 @@
#include "mallocator.h" #include "mallocator.h"
/* ------------------------ HEAP ALLOCATOR ------------------------ */ /* ------------------------ HEAP ALLOCATOR ------------------------ */
#if 0
typedef struct { typedef struct {
size_t tusage; size_t tusage;
} SHeapAllocator; } SHeapAllocator;
@ -104,4 +105,5 @@ static size_t haUsage(SMemAllocator *pma) { return ((SHeapAllocator *)(pma->impl
/* ------------------------ ARENA ALLOCATOR ------------------------ */ /* ------------------------ ARENA ALLOCATOR ------------------------ */
typedef struct { typedef struct {
size_t usage; size_t usage;
} SArenaAllocator; } SArenaAllocator;
#endif