提交 4503901f 编写于 作者: H Hongze Cheng

more

上级 c0e308f2
......@@ -23,23 +23,23 @@ extern "C" {
// Single linked list
#define TD_SLIST_NODE(TYPE) \
struct { \
struct type *sl_next_; \
struct TYPE *sl_next_; \
}
#define TD_SLIST(TYPE) \
struct { \
struct TYPE *sl_dl_head_; \
int sl_dl_neles_; \
#define TD_SLIST(TYPE) \
struct { \
struct TYPE *sl_head_; \
int sl_neles_; \
}
#define TD_SLIST_HEAD(sl) ((sl)->sl_dl_head_)
#define TD_SLIST_NELES(sl) ((sl)->sl_dl_neles_)
#define TD_SLIST_HEAD(sl) ((sl)->sl_head_)
#define TD_SLIST_NELES(sl) ((sl)->sl_neles_)
#define TD_SLIST_NODE_NEXT(sln) ((sln)->sl_next_)
#define tSListInit(sl) \
do { \
(sl)->sl_dl_head_ = NULL; \
(sl)->sl_dl_neles_ = 0; \
#define tSListInit(sl) \
do { \
(sl)->sl_head_ = NULL; \
(sl)->sl_neles_ = 0; \
} while (0)
#define tSListPush(sl, sln) \
......
......@@ -26,7 +26,7 @@ typedef struct SVArenaNode SVArenaNode;
typedef struct SVMemAllocator SVMemAllocator;
struct SVArenaNode {
TD_DLIST_NODE(SVArenaNode);
TD_SLIST_NODE(SVArenaNode);
uint64_t size; // current node size
void * ptr;
char data[];
......@@ -34,10 +34,11 @@ struct SVArenaNode {
struct SVMemAllocator {
TD_DLIST_NODE(SVMemAllocator);
uint64_t capacity;
uint64_t ssize;
uint64_t lsize;
TD_DLIST(SVArenaNode) nlist;
uint64_t capacity;
uint64_t ssize;
uint64_t lsize;
SVArenaNode *pNode;
TD_SLIST(SVArenaNode) nlist;
};
SVMemAllocator *vmaCreate(uint64_t capacity, uint64_t ssize, uint64_t lsize);
......
......@@ -27,30 +27,25 @@ SVMemAllocator *vmaCreate(uint64_t capacity, uint64_t ssize, uint64_t lsize) {
pVMA->capacity = capacity;
pVMA->ssize = ssize;
pVMA->lsize = lsize;
tDListInit(&(pVMA->nlist));
tSListInit(&(pVMA->nlist));
SVArenaNode *pNode = vArenaNodeNew(capacity);
if (pNode == NULL) {
pVMA->pNode = vArenaNodeNew(capacity);
if (pVMA->pNode == NULL) {
free(pVMA);
return NULL;
}
tDListAppend(&(pVMA->nlist), pNode);
tSListPush(&(pVMA->nlist), pVMA->pNode);
return pVMA;
}
void vmaDestroy(SVMemAllocator *pVMA) {
if (pVMA) {
while (true) {
SVArenaNode *pNode = TD_DLIST_TAIL(&(pVMA->nlist));
if (pNode) {
tDListPop(&(pVMA->nlist), pNode);
vArenaNodeFree(pNode);
} else {
break;
}
while (TD_SLIST_NELES(&(pVMA->nlist)) > 1) {
SVArenaNode *pNode = TD_SLIST_HEAD(&(pVMA->nlist));
tSListPop(&(pVMA->nlist));
vArenaNodeFree(pNode);
}
free(pVMA);
......@@ -58,18 +53,18 @@ void vmaDestroy(SVMemAllocator *pVMA) {
}
void vmaReset(SVMemAllocator *pVMA) {
while (TD_DLIST_NELES(&(pVMA->nlist)) > 1) {
SVArenaNode *pNode = TD_DLIST_TAIL(&(pVMA->nlist));
tDListPop(&(pVMA->nlist), pNode);
while (TD_SLIST_NELES(&(pVMA->nlist)) > 1) {
SVArenaNode *pNode = TD_SLIST_HEAD(&(pVMA->nlist));
tSListPop(&(pVMA->nlist));
vArenaNodeFree(pNode);
}
SVArenaNode *pNode = TD_DLIST_HEAD(&(pVMA->nlist));
SVArenaNode *pNode = TD_SLIST_HEAD(&(pVMA->nlist));
pNode->ptr = pNode->data;
}
void *vmaMalloc(SVMemAllocator *pVMA, uint64_t size) {
SVArenaNode *pNode = TD_DLIST_TAIL(&(pVMA->nlist));
SVArenaNode *pNode = TD_SLIST_HEAD(&(pVMA->nlist));
void * ptr;
if (pNode->size < POINTER_DISTANCE(pNode->ptr, pNode->data) + size) {
......@@ -80,7 +75,7 @@ void *vmaMalloc(SVMemAllocator *pVMA, uint64_t size) {
return NULL;
}
tDListAppend(&(pVMA->nlist), pNode);
tSListPush(&(pVMA->nlist), pNode);
}
ptr = pNode->ptr;
......@@ -94,9 +89,9 @@ void vmaFree(SVMemAllocator *pVMA, void *ptr) {
}
bool vmaIsFull(SVMemAllocator *pVMA) {
SVArenaNode *pNode = TD_DLIST_TAIL(&(pVMA->nlist));
SVArenaNode *pNode = TD_SLIST_HEAD(&(pVMA->nlist));
return (TD_DLIST_NELES(&(pVMA->nlist)) > 1) ||
return (TD_SLIST_NELES(&(pVMA->nlist)) > 1) ||
(pNode->size < POINTER_DISTANCE(pNode->ptr, pNode->data) + pVMA->lsize);
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册