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

more

上级 094ce843
...@@ -16,15 +16,23 @@ ...@@ -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 { ...@@ -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 { ...@@ -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) { ...@@ -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) { ...@@ -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) { ...@@ -102,67 +106,49 @@ void vnodeCloseBufPool(SVnode *pVnode) {
} }
// Free inuse node // Free inuse node
vBufPoolFreeNode(pVnode->pBufPool->inuse); if (pVnode->pBufPool->inuse) {
vBufPoolFreeNode(pVnode->pBufPool->inuse);
}
free(pVnode->pBufPool); free(pVnode->pBufPool);
pVnode->pBufPool = NULL; pVnode->pBufPool = NULL;
} }
} }
SMemAllocator *vnodeCreateMemAllocator(SVnode *pVnode) { /* ------------------------ STATIC METHODS ------------------------ */
SMemAllocator *pma; static void vArenaAllocatorInit(SVArenaAllocator *pvaa, uint64_t capacity, uint64_t ssize, uint64_t lsize) { /* TODO */
pvaa->ssize = ssize;
pma = (SMemAllocator *)calloc(1, sizeof(*pma)); pvaa->lsize = lsize;
if (pma == NULL) { pvaa->inuse = &pvaa->node;
/* TODO */
return NULL; pvaa->node.prev = NULL;
} pvaa->node.size = capacity;
pvaa->node.ptr = pvaa->node.data;
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) { static void vArenaAllocatorClear(SVArenaAllocator *pvaa) { /* TODO */
SVnode * pVnode = (SVnode *)pma->impl; while (pvaa->inuse != &(pvaa->node)) {
SVMemAllocator *pvma = (SVMemAllocator *)(pVnode->pBufPool->inuse->data); SVArenaNode *pANode = pvaa->inuse;
pvaa->inuse = pANode->prev;
if (T_REF_DEC(pvma) == 0) { free(pANode);
/* TODO */
} }
} }
/* ------------------------ STATIC METHODS ------------------------ */
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) { ...@@ -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) { ...@@ -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);
}
}
// --------------- For arena allocator
static int vArenaAllocatorInit(SVArenaAllocator *pvaa) {
// TODO
return 0;
}
static void vArenaAllocatorClear(SVArenaAllocator *pvaa) { switch (pvma->type) {
// TODO case E_V_ARENA_ALLOCATOR:
} vArenaAllocatorClear(&(pvma->vaa));
break;
// --------------- For heap allocator case E_V_HEAP_ALLOCATOR:
static int vHeapAllocatorInit(SVHeapAllocator *pvha) { // vHeapAllocatorClear(&(pvma->vha));
// TODO break;
return 0; default:
} break;
}
static void vHeapAllocatorClear(SVHeapAllocator *pvha) { free(pNode);
// TODO
} }
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册