refact
This commit is contained in:
parent
91a36a51cf
commit
a3360ea094
|
@ -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
|
||||
}
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
|
|
@ -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
|
||||
}
|
||||
|
|
|
@ -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;
|
||||
} SArenaAllocator;
|
||||
#endif
|
Loading…
Reference in New Issue