From 8b6eba666fff5d8a519e2d839148f7e55b16f9a1 Mon Sep 17 00:00:00 2001 From: Hongze Cheng Date: Mon, 13 Dec 2021 17:57:14 +0800 Subject: [PATCH] more --- include/util/tdlist.h | 2 - .../dnode/vnode/impl/inc/vnodeMemAllocator.h | 3 +- .../dnode/vnode/impl/src/vnodeArenaMAImpl.c | 65 ++++++++++++++++--- 3 files changed, 59 insertions(+), 11 deletions(-) diff --git a/include/util/tdlist.h b/include/util/tdlist.h index 7ebd8f5d09..757b404ad6 100644 --- a/include/util/tdlist.h +++ b/include/util/tdlist.h @@ -90,8 +90,6 @@ extern "C" { #define tlistPopTail(l) tlistPop(l, (l)->tail_) -#define tlistIterInit(it, l, dir) - // List iterator #define TD_LIST_FITER 0 #define TD_LIST_BITER 1 diff --git a/source/dnode/vnode/impl/inc/vnodeMemAllocator.h b/source/dnode/vnode/impl/inc/vnodeMemAllocator.h index bd014c6ff6..df8b367d25 100644 --- a/source/dnode/vnode/impl/inc/vnodeMemAllocator.h +++ b/source/dnode/vnode/impl/inc/vnodeMemAllocator.h @@ -27,7 +27,7 @@ typedef struct SVMemAllocator SVMemAllocator; struct SVArenaNode { TD_LIST_NODE(SVArenaNode); - uint64_t nsize; // current node size + uint64_t size; // current node size void * ptr; char data[]; }; @@ -45,6 +45,7 @@ void vmaDestroy(SVMemAllocator *pVMA); void vmaReset(SVMemAllocator *pVMA); void * vmaMalloc(SVMemAllocator *pVMA, uint64_t size); void vmaFree(SVMemAllocator *pVMA, void *ptr); +bool vmaIsFull(SVMemAllocator *pVMA); #ifdef __cplusplus } diff --git a/source/dnode/vnode/impl/src/vnodeArenaMAImpl.c b/source/dnode/vnode/impl/src/vnodeArenaMAImpl.c index e0c098b24b..532c2dad8e 100644 --- a/source/dnode/vnode/impl/src/vnodeArenaMAImpl.c +++ b/source/dnode/vnode/impl/src/vnodeArenaMAImpl.c @@ -21,7 +21,6 @@ static void vArenaNodeFree(SVArenaNode *pNode); SVMemAllocator *vmaCreate(uint64_t capacity, uint64_t ssize, uint64_t lsize) { SVMemAllocator *pVMA = (SVMemAllocator *)malloc(sizeof(*pVMA)); if (pVMA == NULL) { - // TODO: handle error return NULL; } @@ -32,7 +31,7 @@ SVMemAllocator *vmaCreate(uint64_t capacity, uint64_t ssize, uint64_t lsize) { SVArenaNode *pNode = vArenaNodeNew(capacity); if (pNode == NULL) { - // TODO + free(pVMA); return NULL; } @@ -42,29 +41,79 @@ SVMemAllocator *vmaCreate(uint64_t capacity, uint64_t ssize, uint64_t lsize) { } void vmaDestroy(SVMemAllocator *pVMA) { - // TODO + if (pVMA) { + while (true) { + SVArenaNode *pNode = tlistPopTail(&(pVMA->nlist)); + + if (pNode) { + vArenaNodeFree(pNode); + } else { + break; + } + } + + free(pVMA); + } } void vmaReset(SVMemAllocator *pVMA) { - // TODO + while (tlistNEles(&(pVMA->nlist)) > 1) { + SVArenaNode *pNode = tlistPopTail(&(pVMA->nlist)); + vArenaNodeFree(pNode); + } + + SVArenaNode *pNode = tlistHead(&(pVMA->nlist)); + pNode->ptr = pNode->data; } void *vmaMalloc(SVMemAllocator *pVMA, uint64_t size) { - // TODO - return NULL; + SVArenaNode *pNode = tlistTail(&(pVMA->nlist)); + void * ptr; + + if (pNode->size < POINTER_DISTANCE(pNode->ptr, pNode->data) + size) { + uint64_t capacity = MAX(pVMA->ssize, size); + pNode = vArenaNodeNew(capacity); + if (pNode == NULL) { + // TODO: handle error + return NULL; + } + + tlistAppend(&(pVMA->nlist), pNode); + } + + ptr = pNode->ptr; + pNode->ptr = POINTER_SHIFT(ptr, size); + + return ptr; } void vmaFree(SVMemAllocator *pVMA, void *ptr) { // TODO } +bool vmaIsFull(SVMemAllocator *pVMA) { + SVArenaNode *pNode = tlistTail(&(pVMA->nlist)); + + return (tlistNEles(&(pVMA->nlist)) > 1) || (pNode->size < POINTER_DISTANCE(pNode->ptr, pNode->data) + pVMA->lsize); +} + /* ------------------------ STATIC METHODS ------------------------ */ static SVArenaNode *vArenaNodeNew(uint64_t capacity) { SVArenaNode *pNode = NULL; - // TODO + + pNode = (SVArenaNode *)malloc(sizeof(*pNode) + capacity); + if (pNode == NULL) { + return NULL; + } + + pNode->size = capacity; + pNode->ptr = pNode->data; + return pNode; } static void vArenaNodeFree(SVArenaNode *pNode) { - // TODO + if (pNode) { + free(pNode); + } } \ No newline at end of file -- GitLab