This commit is contained in:
Hongze Cheng 2021-11-16 11:41:53 +08:00
parent 094ce84321
commit e12b2d043e
1 changed files with 61 additions and 84 deletions

View File

@ -16,15 +16,23 @@
#include "vnodeDef.h" #include "vnodeDef.h"
/* ------------------------ STRUCTURES ------------------------ */ /* ------------------------ STRUCTURES ------------------------ */
#define VNODE_BUF_POOL_SHARDS 3
struct SVBufPool { struct SVBufPool {
SList free; SList free;
SList incycle; SList incycle;
SListNode *inuse; SListNode *inuse;
}; };
typedef enum { E_V_HEAP_ALLOCATOR = 0, E_V_ARENA_ALLOCATOR } EVMemAllocatorT; typedef enum {
// Heap allocator
E_V_HEAP_ALLOCATOR = 0,
// Arena allocator
E_V_ARENA_ALLOCATOR
} EVMemAllocatorT;
typedef struct { typedef struct {
/* TODO */
} SVHeapAllocator; } SVHeapAllocator;
typedef struct SVArenaNode { typedef struct SVArenaNode {
@ -35,14 +43,16 @@ typedef struct SVArenaNode {
} SVArenaNode; } SVArenaNode;
typedef struct { typedef struct {
uint64_t ssize; // step size
uint64_t lsize; // limit size
SVArenaNode *inuse; SVArenaNode *inuse;
SVArenaNode node; SVArenaNode node;
} SVArenaAllocator; } SVArenaAllocator;
typedef struct { typedef struct {
T_REF_DECLARE()
uint64_t capacity; uint64_t capacity;
EVMemAllocatorT type; EVMemAllocatorT type;
T_REF_DECLARE()
union { union {
SVHeapAllocator vha; SVHeapAllocator vha;
SVArenaAllocator vaa; SVArenaAllocator vaa;
@ -51,10 +61,6 @@ typedef struct {
static SListNode *vBufPoolNewNode(uint64_t capacity, EVMemAllocatorT type); static SListNode *vBufPoolNewNode(uint64_t capacity, EVMemAllocatorT type);
static void vBufPoolFreeNode(SListNode *pNode); static void vBufPoolFreeNode(SListNode *pNode);
static int vArenaAllocatorInit(SVArenaAllocator *pvaa);
static void vArenaAllocatorClear(SVArenaAllocator *pvaa);
static int vHeapAllocatorInit(SVHeapAllocator *pvha);
static void vHeapAllocatorClear(SVHeapAllocator *pvha);
int vnodeOpenBufPool(SVnode *pVnode) { int vnodeOpenBufPool(SVnode *pVnode) {
uint64_t capacity; uint64_t capacity;
@ -68,12 +74,12 @@ int vnodeOpenBufPool(SVnode *pVnode) {
tdListInit(&(pVnode->pBufPool->free), 0); tdListInit(&(pVnode->pBufPool->free), 0);
tdListInit(&(pVnode->pBufPool->incycle), 0); tdListInit(&(pVnode->pBufPool->incycle), 0);
capacity = pVnode->options.wsize / 3; capacity = pVnode->options.wsize / VNODE_BUF_POOL_SHARDS;
if (pVnode->options.isHeapAllocator) { if (pVnode->options.isHeapAllocator) {
type = E_V_HEAP_ALLOCATOR; type = E_V_HEAP_ALLOCATOR;
} }
for (int i = 0; i < 3; i++) { for (int i = 0; i < VNODE_BUF_POOL_SHARDS; i++) {
SListNode *pNode = vBufPoolNewNode(capacity, type); SListNode *pNode = vBufPoolNewNode(capacity, type);
if (pNode == NULL) { if (pNode == NULL) {
vnodeCloseBufPool(pVnode); vnodeCloseBufPool(pVnode);
@ -83,8 +89,6 @@ int vnodeOpenBufPool(SVnode *pVnode) {
tdListAppendNode(&(pVnode->pBufPool->free), pNode); tdListAppendNode(&(pVnode->pBufPool->free), pNode);
} }
pVnode->pBufPool->inuse = tdListPopHead(&(pVnode->pBufPool->free));
return 0; return 0;
} }
@ -102,67 +106,49 @@ void vnodeCloseBufPool(SVnode *pVnode) {
} }
// Free inuse node // Free inuse node
if (pVnode->pBufPool->inuse) {
vBufPoolFreeNode(pVnode->pBufPool->inuse); vBufPoolFreeNode(pVnode->pBufPool->inuse);
}
free(pVnode->pBufPool); free(pVnode->pBufPool);
pVnode->pBufPool = NULL; pVnode->pBufPool = NULL;
} }
} }
SMemAllocator *vnodeCreateMemAllocator(SVnode *pVnode) {
SMemAllocator *pma;
pma = (SMemAllocator *)calloc(1, sizeof(*pma));
if (pma == NULL) {
/* TODO */
return NULL;
}
pma->impl = pVnode;
if (pVnode->options.isHeapAllocator) {
/* TODO */
pma->malloc = NULL;
pma->calloc = NULL;
pma->realloc = NULL;
pma->free = NULL;
pma->usage = NULL;
} else {
/* TODO */
pma->malloc = NULL;
pma->calloc = NULL;
pma->realloc = NULL;
pma->free = NULL;
pma->usage = NULL;
}
return pma;
}
void vnodeDestroyMemAllocator(SMemAllocator *pma) { tfree(pma); }
void vnodeRefMemAllocator(SMemAllocator *pma) {
SVnode * pVnode = (SVnode *)pma->impl;
SVMemAllocator *pvma = (SVMemAllocator *)(pVnode->pBufPool->inuse->data);
T_REF_INC(pvma);
}
void vnodeUnrefMemAllocator(SMemAllocator *pma) {
SVnode * pVnode = (SVnode *)pma->impl;
SVMemAllocator *pvma = (SVMemAllocator *)(pVnode->pBufPool->inuse->data);
if (T_REF_DEC(pvma) == 0) {
/* TODO */
}
}
/* ------------------------ STATIC METHODS ------------------------ */ /* ------------------------ STATIC METHODS ------------------------ */
static void vArenaAllocatorInit(SVArenaAllocator *pvaa, uint64_t capacity, uint64_t ssize, uint64_t lsize) { /* TODO */
pvaa->ssize = ssize;
pvaa->lsize = lsize;
pvaa->inuse = &pvaa->node;
pvaa->node.prev = NULL;
pvaa->node.size = capacity;
pvaa->node.ptr = pvaa->node.data;
}
static void vArenaAllocatorClear(SVArenaAllocator *pvaa) { /* TODO */
while (pvaa->inuse != &(pvaa->node)) {
SVArenaNode *pANode = pvaa->inuse;
pvaa->inuse = pANode->prev;
free(pANode);
}
}
static SListNode *vBufPoolNewNode(uint64_t capacity, EVMemAllocatorT type) { static SListNode *vBufPoolNewNode(uint64_t capacity, EVMemAllocatorT type) {
SListNode * pNode; SListNode * pNode;
SVMemAllocator *pvma; SVMemAllocator *pvma;
uint64_t msize;
uint64_t ssize = 0; // TODO
uint64_t lsize = 0; // TODO
pNode = (SListNode *)calloc(1, sizeof(*pNode) + sizeof(SVMemAllocator)); msize = sizeof(SListNode) + sizeof(SVMemAllocator);
if (type == E_V_ARENA_ALLOCATOR) {
msize += capacity;
}
pNode = (SListNode *)calloc(1, msize);
if (pNode == NULL) { if (pNode == NULL) {
// TODO: handle error
return NULL; return NULL;
} }
@ -171,11 +157,11 @@ static SListNode *vBufPoolNewNode(uint64_t capacity, EVMemAllocatorT type) {
pvma->type = type; pvma->type = type;
switch (type) { switch (type) {
case E_V_HEAP_ALLOCATOR:
vHeapAllocatorInit(&(pvma->vha));
break;
case E_V_ARENA_ALLOCATOR: case E_V_ARENA_ALLOCATOR:
vArenaAllocatorInit(&(pvma->vaa)); vArenaAllocatorInit(&(pvma->vaa), capacity, ssize, lsize);
break;
case E_V_HEAP_ALLOCATOR:
// vHeapAllocatorInit(&(pvma->vha));
break; break;
default: default:
ASSERT(0); ASSERT(0);
@ -185,27 +171,18 @@ static SListNode *vBufPoolNewNode(uint64_t capacity, EVMemAllocatorT type) {
} }
static void vBufPoolFreeNode(SListNode *pNode) { static void vBufPoolFreeNode(SListNode *pNode) {
if (pNode) { SVMemAllocator *pvma = (SVMemAllocator *)(pNode->data);
free(pNode);
switch (pvma->type) {
case E_V_ARENA_ALLOCATOR:
vArenaAllocatorClear(&(pvma->vaa));
break;
case E_V_HEAP_ALLOCATOR:
// vHeapAllocatorClear(&(pvma->vha));
break;
default:
break;
} }
}
// --------------- For arena allocator free(pNode);
static int vArenaAllocatorInit(SVArenaAllocator *pvaa) {
// TODO
return 0;
}
static void vArenaAllocatorClear(SVArenaAllocator *pvaa) {
// TODO
}
// --------------- For heap allocator
static int vHeapAllocatorInit(SVHeapAllocator *pvha) {
// TODO
return 0;
}
static void vHeapAllocatorClear(SVHeapAllocator *pvha) {
// TODO
} }