提交 a3360ea0 编写于 作者: H Hongze Cheng

refact

上级 91a36a51
......@@ -22,23 +22,28 @@
extern "C" {
#endif
typedef struct SMemAllocator SMemAllocator;
typedef struct SMemAllocatorFactory SMemAllocatorFactory;
struct SMemAllocator {
// 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_
#define TD_MA_MALLOC(TMA, SIZE) (*((TMA)->malloc_))(TMA, (SIZE))
#define TD_MA_FREE(TMA, PTR) (*((TMA)->free_))(TMA, (PTR))
typedef 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 *);
};
struct SMemAllocatorFactory {
TD_MEM_ALCT(SMemAllocator);
} SMemAllocator;
typedef struct SMemAllocatorFactory {
void *impl;
SMemAllocator *(*create)(SMemAllocatorFactory *);
void (*destroy)(SMemAllocatorFactory *, SMemAllocator *);
};
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;
\ No newline at end of file
} SArenaAllocator;
#endif
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册