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

more

上级 aaf9ca18
......@@ -26,87 +26,108 @@ extern "C" {
struct type *sl_next_; \
}
#define TD_SLIST(TYPE) \
struct { \
struct TYPE *sl_head_; \
#define TD_SLIST(TYPE) \
struct { \
struct TYPE *sl_dl_head_; \
int sl_dl_neles_; \
}
#define TD_SLIST_HEAD(sl) ((sl)->sl_head_)
#define TD_SLIST_HEAD(sl) ((sl)->sl_dl_head_)
#define TD_SLIST_NELES(sl) ((sl)->sl_dl_neles_)
#define TD_SLIST_NODE_NEXT(sln) ((sln)->sl_next_)
#define tSListInit(sl) \
do { \
(sl)->sl_head_ = NULL; \
#define tSListInit(sl) \
do { \
(sl)->sl_dl_head_ = NULL; \
(sl)->sl_dl_neles_ = 0; \
} while (0)
#define tSListPrepend(sl, sln) \
#define tSListPush(sl, sln) \
do { \
TD_SLIST_NODE_NEXT(sln) = TD_SLIST_HEAD(sl); \
TD_SLIST_HEAD(sl) = (sln); \
} while (0);
TD_SLIST_NELES(sl) += 1; \
} while (0)
#define tSListPop(sl) \
do { \
TD_SLIST_HEAD(sl) = TD_SLIST_NODE_NEXT(TD_SLIST_HEAD(sl)); \
TD_SLIST_NELES(sl) -= 1; \
} while (0)
// Double linked list
#define TD_DLIST_NODE(TYPE) \
struct { \
TYPE *prev_; \
TYPE *next_; \
TYPE *dl_prev_; \
TYPE *dl_next_; \
}
#define TD_DLIST(TYPE) \
struct { \
TYPE *head_; \
TYPE *tail_; \
int neles_; \
#define TD_DLIST(TYPE) \
struct { \
struct TYPE *dl_head_; \
struct TYPE *dl_tail_; \
int dl_neles_; \
}
#define tDListInit(l) \
(l)->head_ = (l)->tail_ = NULL; \
(l)->neles_ = 0;
#define TD_DLIST_NODE_PREV(dln) ((dln)->dl_prev_)
#define TD_DLIST_NODE_NEXT(dln) ((dln)->dl_next_)
#define TD_DLIST_HEAD(dl) ((dl)->dl_head_)
#define TD_DLIST_TAIL(dl) ((dl)->dl_tail_)
#define TD_DLIST_NELES(dl) ((dl)->dl_neles_)
#define tlistHead(l) (l)->head_
#define tlistTail(l) (l)->tail_
#define tlistNEles(l) (l)->neles_
#define tDListInit(dl) \
do { \
TD_DLIST_HEAD(dl) = TD_DLIST_TAIL(dl) = NULL; \
TD_DLIST_NELES(dl) = 0; \
} while (0)
#define tlistAppend(l, n) \
if ((l)->head_ == NULL) { \
(n)->prev_ = (n)->next_ = NULL; \
(l)->head_ = (l)->tail_ = (n); \
} else { \
(n)->prev_ = (l)->tail_; \
(n)->next_ = NULL; \
(l)->tail_->next_ = (n); \
(l)->tail_ = (n); \
} \
(l)->neles_ += 1;
#define tlistPrepend(l, n) \
if ((l)->head_ == NULL) { \
(n)->prev_ = (n)->next_ = NULL; \
(l)->head_ = (l)->tail_ = (n); \
} else { \
(n)->prev_ = NULL; \
(n)->next_ = (l)->head_; \
(l)->head_->prev_ = (n); \
(l)->head_ = (n); \
} \
(l)->neles_ += 1;
#define tlistPop(l, n) \
if ((l)->head_ == (n)) { \
(l)->head_ = (n)->next_; \
} \
if ((l)->tail_ == (n)) { \
(l)->tail_ = (n)->prev_; \
} \
if ((n)->prev_ != NULL) { \
(n)->prev_->next_ = (n)->next_; \
} \
if ((n)->next_ != NULL) { \
(n)->next_->prev_ = (n)->prev_; \
} \
(l)->neles_ -= 1; \
(n)->prev_ = (n)->next_ = NULL;
#define tDListAppend(dl, dln) \
do { \
if (TD_DLIST_HEAD(dl) == NULL) { \
TD_DLIST_NODE_PREV(dln) = TD_DLIST_NODE_NEXT(dln) = NULL; \
TD_DLIST_HEAD(dl) = TD_DLIST_TAIL(dl) = (dln); \
} else { \
TD_DLIST_NODE_PREV(dln) = TD_DLIST_TAIL(dl); \
TD_DLIST_NODE_NEXT(dln) = NULL; \
TD_DLIST_NODE_NEXT(TD_DLIST_TAIL(dl)) = (dln); \
TD_DLIST_TAIL(dl) = (dln); \
} \
TD_DLIST_NELES(dl) += 1; \
} while (0)
#define tDListPrepend(dl, dln) \
do { \
if (TD_DLIST_HEAD(dl) == NULL) { \
TD_DLIST_NODE_PREV(dln) = TD_DLIST_NODE_NEXT(dln) = NULL; \
TD_DLIST_HEAD(dl) = TD_DLIST_TAIL(dl) = (dln); \
} else { \
TD_DLIST_NODE_PREV(dln) = NULL; \
TD_DLIST_NODE_NEXT(dln) = TD_DLIST_HEAD(dl); \
TD_DLIST_NODE_PREV(TD_DLIST_HEAD(dl)) = (dln); \
TD_DLIST_HEAD(dl) = (dln); \
} \
TD_DLIST_NELES(dl) += 1; \
} while (0)
#define tDListPop(dl, dln) \
do { \
if (TD_DLIST_HEAD(dl) == (dln)) { \
TD_DLIST_HEAD(dl) = TD_DLIST_NODE_NEXT(dln); \
} \
if (TD_DLIST_TAIL(dl) == (dln)) { \
TD_DLIST_TAIL(dl) = TD_DLIST_NODE_PREV(dln); \
} \
if (TD_DLIST_NODE_PREV(dln) != NULL) { \
TD_DLIST_NODE_NEXT(TD_DLIST_NODE_PREV(dln)) = TD_DLIST_NODE_NEXT(dln); \
} \
if (TD_DLIST_NODE_NEXT(dln) != NULL) { \
TD_DLIST_NODE_PREV(TD_DLIST_NODE_NEXT(dln)) = TD_DLIST_NODE_PREV(dln); \
} \
TD_DLIST_NELES(dl) -= 1; \
TD_DLIST_NODE_PREV(dln) = TD_DLIST_NODE_NEXT(dln) = NULL; \
} while (0)
#if 0
// List iterator
#define TD_LIST_FITER 0
#define TD_LIST_BITER 1
......@@ -118,13 +139,13 @@ extern "C" {
TD_DLIST(S) * it_list_; \
}
#define tlistIterInit(it, l, dir) \
(it)->it_dir_ = (dir); \
(it)->it_list_ = l; \
if ((dir) == TD_LIST_FITER) { \
(it)->it_next_ = (l)->head_; \
} else { \
(it)->it_next_ = (l)->tail_; \
#define tlistIterInit(it, l, dir) \
(it)->it_dir_ = (dir); \
(it)->it_list_ = l; \
if ((dir) == TD_LIST_FITER) { \
(it)->it_next_ = (l)->dl_head_; \
} else { \
(it)->it_next_ = (l)->dl_tail_; \
}
#define tlistIterNext(it) \
......@@ -139,6 +160,7 @@ extern "C" {
} \
(it)->it_ptr_; \
})
#endif
#ifdef __cplusplus
}
......
......@@ -35,7 +35,7 @@ SVMemAllocator *vmaCreate(uint64_t capacity, uint64_t ssize, uint64_t lsize) {
return NULL;
}
tlistAppend(&(pVMA->nlist), pNode);
tDListAppend(&(pVMA->nlist), pNode);
return pVMA;
}
......@@ -43,10 +43,10 @@ SVMemAllocator *vmaCreate(uint64_t capacity, uint64_t ssize, uint64_t lsize) {
void vmaDestroy(SVMemAllocator *pVMA) {
if (pVMA) {
while (true) {
SVArenaNode *pNode = tlistTail(&(pVMA->nlist));
SVArenaNode *pNode = TD_DLIST_TAIL(&(pVMA->nlist));
if (pNode) {
tlistPop(&(pVMA->nlist), pNode);
tDListPop(&(pVMA->nlist), pNode);
vArenaNodeFree(pNode);
} else {
break;
......@@ -58,18 +58,18 @@ void vmaDestroy(SVMemAllocator *pVMA) {
}
void vmaReset(SVMemAllocator *pVMA) {
while (tlistNEles(&(pVMA->nlist)) > 1) {
SVArenaNode *pNode = tlistTail(&(pVMA->nlist));
tlistPop(&(pVMA->nlist), pNode);
while (TD_DLIST_NELES(&(pVMA->nlist)) > 1) {
SVArenaNode *pNode = TD_DLIST_TAIL(&(pVMA->nlist));
tDListPop(&(pVMA->nlist), pNode);
vArenaNodeFree(pNode);
}
SVArenaNode *pNode = tlistHead(&(pVMA->nlist));
SVArenaNode *pNode = TD_DLIST_HEAD(&(pVMA->nlist));
pNode->ptr = pNode->data;
}
void *vmaMalloc(SVMemAllocator *pVMA, uint64_t size) {
SVArenaNode *pNode = tlistTail(&(pVMA->nlist));
SVArenaNode *pNode = TD_DLIST_TAIL(&(pVMA->nlist));
void * ptr;
if (pNode->size < POINTER_DISTANCE(pNode->ptr, pNode->data) + size) {
......@@ -80,7 +80,7 @@ void *vmaMalloc(SVMemAllocator *pVMA, uint64_t size) {
return NULL;
}
tlistAppend(&(pVMA->nlist), pNode);
tDListAppend(&(pVMA->nlist), pNode);
}
ptr = pNode->ptr;
......@@ -94,9 +94,10 @@ void vmaFree(SVMemAllocator *pVMA, void *ptr) {
}
bool vmaIsFull(SVMemAllocator *pVMA) {
SVArenaNode *pNode = tlistTail(&(pVMA->nlist));
SVArenaNode *pNode = TD_DLIST_TAIL(&(pVMA->nlist));
return (tlistNEles(&(pVMA->nlist)) > 1) || (pNode->size < POINTER_DISTANCE(pNode->ptr, pNode->data) + pVMA->lsize);
return (TD_DLIST_NELES(&(pVMA->nlist)) > 1) ||
(pNode->size < POINTER_DISTANCE(pNode->ptr, pNode->data) + pVMA->lsize);
}
/* ------------------------ STATIC METHODS ------------------------ */
......
......@@ -50,7 +50,7 @@ int vnodeOpenBufPool(SVnode *pVnode) {
return -1;
}
tlistAppend(&(pVnode->pBufPool->free), pVMA);
tDListAppend(&(pVnode->pBufPool->free), pVMA);
}
return 0;
......@@ -61,16 +61,16 @@ void vnodeCloseBufPool(SVnode *pVnode) {
vmaDestroy(pVnode->pBufPool->inuse);
while (true) {
SVMemAllocator *pVMA = tlistHead(&(pVnode->pBufPool->incycle));
SVMemAllocator *pVMA = TD_DLIST_HEAD(&(pVnode->pBufPool->incycle));
if (pVMA == NULL) break;
tlistPop(&(pVnode->pBufPool->incycle), pVMA);
tDListPop(&(pVnode->pBufPool->incycle), pVMA);
vmaDestroy(pVMA);
}
while (true) {
SVMemAllocator *pVMA = tlistHead(&(pVnode->pBufPool->free));
SVMemAllocator *pVMA = TD_DLIST_HEAD(&(pVnode->pBufPool->free));
if (pVMA == NULL) break;
tlistPop(&(pVnode->pBufPool->free), pVMA);
tDListPop(&(pVnode->pBufPool->free), pVMA);
vmaDestroy(pVMA);
}
......@@ -85,9 +85,9 @@ void *vnodeMalloc(SVnode *pVnode, uint64_t size) {
if (pBufPool->inuse == NULL) {
while (true) {
// TODO: add sem_wait and sem_post
pBufPool->inuse = tlistHead(&(pBufPool->free));
pBufPool->inuse = TD_DLIST_HEAD(&(pBufPool->free));
if (pBufPool->inuse) {
tlistPop(&(pBufPool->free), pBufPool->inuse);
tDListPop(&(pBufPool->free), pBufPool->inuse);
break;
}
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册