make test run

This commit is contained in:
Hongze Cheng 2021-12-16 16:20:46 +08:00
parent 835c3030c5
commit d1e0a9a780
1 changed files with 46 additions and 177 deletions

View File

@ -28,6 +28,9 @@ struct SVBufPool {
SMemAllocatorFactory *pMAF; SMemAllocatorFactory *pMAF;
}; };
static SMemAllocator *vBufPoolCreateMA(SMemAllocatorFactory *pMAF);
static void vBufPoolDestroyMA(SMemAllocatorFactory *pMAF, SMemAllocator *pMA);
int vnodeOpenBufPool(SVnode *pVnode) { int vnodeOpenBufPool(SVnode *pVnode) {
uint64_t capacity; uint64_t capacity;
@ -54,6 +57,15 @@ int vnodeOpenBufPool(SVnode *pVnode) {
tDListAppend(&(pVnode->pBufPool->free), pVMA); tDListAppend(&(pVnode->pBufPool->free), pVMA);
} }
pVnode->pBufPool->pMAF = (SMemAllocatorFactory *)malloc(sizeof(SMemAllocatorFactory));
if (pVnode->pBufPool->pMAF == NULL) {
// TODO: handle error
return -1;
}
pVnode->pBufPool->pMAF->impl = pVnode;
pVnode->pBufPool->pMAF->create = vBufPoolCreateMA;
pVnode->pBufPool->pMAF->destroy = vBufPoolDestroyMA;
return 0; return 0;
} }
@ -127,195 +139,52 @@ bool vnodeBufPoolIsFull(SVnode *pVnode) {
SMemAllocatorFactory *vBufPoolGetMAF(SVnode *pVnode) { return pVnode->pBufPool->pMAF; } SMemAllocatorFactory *vBufPoolGetMAF(SVnode *pVnode) { return pVnode->pBufPool->pMAF; }
#if 0 /* ------------------------ STATIC METHODS ------------------------ */
typedef enum {
// Heap allocator
E_V_HEAP_ALLOCATOR = 0,
// Arena allocator
E_V_ARENA_ALLOCATOR
} EVMemAllocatorT;
typedef struct { typedef struct {
/* TODO */ SVnode * pVnode;
} SVHeapAllocator; SVMemAllocator *pVMA;
typedef struct SVArenaNode {
struct SVArenaNode *prev;
uint64_t size;
void * ptr;
char data[];
} SVArenaNode;
typedef struct {
uint64_t ssize; // step size
uint64_t lsize; // limit size
SVArenaNode *inuse;
SVArenaNode node;
} SVArenaAllocator;
typedef struct {
SVnode * pVnode;
SListNode *pNode;
} SVMAWrapper; } SVMAWrapper;
static FORCE_INLINE void *vmaMaloocCb(SMemAllocator *pMA, uint64_t size) {
SVMAWrapper *pWrapper = (SVMAWrapper *)(pMA->impl);
static SListNode * vBufPoolNewNode(uint64_t capacity, EVMemAllocatorT type); return vmaMalloc(pWrapper->pVMA, size);
static void vBufPoolFreeNode(SListNode *pNode); }
static SMemAllocator *vBufPoolCreateMA(SMemAllocatorFactory *pmaf);
static void vBufPoolDestroyMA(SMemAllocatorFactory *pmaf, SMemAllocator *pma);
static void * vBufPoolMalloc(SVMemAllocator *pvma, uint64_t size);
/* ------------------------ STATIC METHODS ------------------------ */ // TODO: Add atomic operations here
static SListNode *vBufPoolNewNode(uint64_t capacity, EVMemAllocatorT type) { static SMemAllocator *vBufPoolCreateMA(SMemAllocatorFactory *pMAF) {
SListNode * pNode; SMemAllocator *pMA;
SVMemAllocator *pvma; SVnode * pVnode = (SVnode *)(pMAF->impl);
uint64_t msize; SVMAWrapper * pWrapper;
uint64_t ssize = 4096; // TODO
uint64_t lsize = 1024; // TODO
msize = sizeof(SListNode) + sizeof(SVMemAllocator); pMA = (SMemAllocator *)calloc(1, sizeof(*pMA) + sizeof(SVMAWrapper));
if (type == E_V_ARENA_ALLOCATOR) { if (pMA == NULL) {
msize += capacity;
}
pNode = (SListNode *)calloc(1, msize);
if (pNode == NULL) {
// TODO: handle error
return NULL; return NULL;
} }
pvma = (SVMemAllocator *)(pNode->data); pVnode->pBufPool->inuse->_ref.val++;
pvma->capacity = capacity; pWrapper = POINTER_SHIFT(pMA, sizeof(*pMA));
pvma->type = type; pWrapper->pVnode = pVnode;
pWrapper->pVMA = pVnode->pBufPool->inuse;
switch (type) { pMA->impl = pWrapper;
case E_V_ARENA_ALLOCATOR: pMA->malloc = vmaMaloocCb;
vArenaAllocatorInit(&(pvma->vaa), capacity, ssize, lsize); pMA->calloc = NULL;
break; pMA->realloc = NULL;
case E_V_HEAP_ALLOCATOR: pMA->free = NULL;
// vHeapAllocatorInit(&(pvma->vha)); pMA->usage = NULL;
break;
default:
ASSERT(0);
}
return pNode; return pMA;
} }
static void vBufPoolFreeNode(SListNode *pNode) { static void vBufPoolDestroyMA(SMemAllocatorFactory *pMAF, SMemAllocator *pMA) {
SVMemAllocator *pvma = (SVMemAllocator *)(pNode->data); SVMAWrapper * pWrapper = (SVMAWrapper *)(pMA->impl);
SVnode * pVnode = pWrapper->pVnode;
SVMemAllocator *pVMA = pWrapper->pVMA;
switch (pvma->type) { free(pMA);
case E_V_ARENA_ALLOCATOR: if (--pVMA->_ref.val == 0) {
vArenaAllocatorClear(&(pvma->vaa)); tDListPop(&(pVnode->pBufPool->incycle), pVMA);
break; tDListAppend(&(pVnode->pBufPool->free), pVMA);
case E_V_HEAP_ALLOCATOR:
// vHeapAllocatorClear(&(pvma->vha));
break;
default:
break;
} }
}
free(pNode);
}
static void *vBufPoolMalloc(SVMemAllocator *pvma, uint64_t size) {
void *ptr = NULL;
if (pvma->type == E_V_ARENA_ALLOCATOR) {
SVArenaAllocator *pvaa = &(pvma->vaa);
if (POINTER_DISTANCE(pvaa->inuse->ptr, pvaa->inuse->data) + size > pvaa->inuse->size) {
SVArenaNode *pNode = (SVArenaNode *)malloc(sizeof(*pNode) + MAX(size, pvaa->ssize));
if (pNode == NULL) {
// TODO: handle error
return NULL;
}
pNode->prev = pvaa->inuse;
pNode->size = MAX(size, pvaa->ssize);
pNode->ptr = pNode->data;
pvaa->inuse = pNode;
}
ptr = pvaa->inuse->ptr;
pvaa->inuse->ptr = POINTER_SHIFT(ptr, size);
} else if (pvma->type == E_V_HEAP_ALLOCATOR) {
/* TODO */
}
return ptr;
}
static SMemAllocator *vBufPoolCreateMA(SMemAllocatorFactory *pmaf) {
SVnode * pVnode;
SMemAllocator * pma;
SVMemAllocator *pvma;
SVMAWrapper * pvmaw;
pVnode = (SVnode *)(pmaf->impl);
pma = (SMemAllocator *)calloc(1, sizeof(*pma) + sizeof(SVMAWrapper));
if (pma == NULL) {
// TODO: handle error
return NULL;
}
pvmaw = (SVMAWrapper *)POINTER_SHIFT(pma, sizeof(*pma));
// No allocator used currently
if (pVnode->pBufPool->inuse == NULL) {
while (listNEles(&(pVnode->pBufPool->free)) == 0) {
// TODO: wait until all released ro kill query
// tsem_wait();
ASSERT(0);
}
pVnode->pBufPool->inuse = tdListPopHead(&(pVnode->pBufPool->free));
pvma = (SVMemAllocator *)(pVnode->pBufPool->inuse->data);
T_REF_INIT_VAL(pvma, 1);
} else {
pvma = (SVMemAllocator *)(pVnode->pBufPool->inuse->data);
}
T_REF_INC(pvma);
pvmaw->pVnode = pVnode;
pvmaw->pNode = pVnode->pBufPool->inuse;
pma->impl = pvmaw;
pma->malloc = NULL;
pma->calloc = NULL; /* TODO */
pma->realloc = NULL; /* TODO */
pma->free = NULL; /* TODO */
pma->usage = NULL; /* TODO */
return pma;
}
static void vBufPoolDestroyMA(SMemAllocatorFactory *pmaf, SMemAllocator *pma) { /* TODO */
SVnode * pVnode = (SVnode *)(pmaf->impl);
SListNode * pNode = ((SVMAWrapper *)(pma->impl))->pNode;
SVMemAllocator *pvma = (SVMemAllocator *)(pNode->data);
if (T_REF_DEC(pvma) == 0) {
if (pvma->type == E_V_ARENA_ALLOCATOR) {
SVArenaAllocator *pvaa = &(pvma->vaa);
while (pvaa->inuse != &(pvaa->node)) {
SVArenaNode *pNode = pvaa->inuse;
pvaa->inuse = pNode->prev;
/* code */
}
pvaa->inuse->ptr = pvaa->inuse->data;
} else if (pvma->type == E_V_HEAP_ALLOCATOR) {
} else {
ASSERT(0);
}
// Move node from incycle to free
tdListAppendNode(&(pVnode->pBufPool->free), tdListPopNode(&(pVnode->pBufPool->incycle), pNode));
// tsem_post(); todo: sem_post
}
}
#endif