提交 9d5e0d53 编写于 作者: H Hongze Cheng

refactor list

上级 50749639
......@@ -19,7 +19,7 @@
extern "C" {
#endif
// Single linked list
// Single linked list ================
#define TD_SLIST_NODE(TYPE) \
struct { \
struct TYPE *sl_next_; \
......@@ -35,30 +35,30 @@ extern "C" {
#define TD_SLIST_NELES(sl) ((sl)->sl_neles_)
#define TD_SLIST_NODE_NEXT(sln) ((sln)->sl_next_)
#define tSListInit(sl) \
#define TD_SLIST_INIT(sl) \
do { \
(sl)->sl_head_ = NULL; \
(sl)->sl_neles_ = 0; \
} while (0)
#define tSListPush(sl, sln) \
#define TD_SLIST_PUSH(sl, sln) \
do { \
TD_SLIST_NODE_NEXT(sln) = TD_SLIST_HEAD(sl); \
TD_SLIST_HEAD(sl) = (sln); \
TD_SLIST_NELES(sl) += 1; \
} while (0)
#define tSListPop(sl) \
#define TD_SLIST_POP(sl) \
do { \
TD_SLIST_HEAD(sl) = TD_SLIST_NODE_NEXT(TD_SLIST_HEAD(sl)); \
TD_SLIST_NELES(sl) -= 1; \
} while (0)
// Double linked list
// Double linked list ================
#define TD_DLIST_NODE(TYPE) \
struct { \
struct TYPE *dl_prev_; \
struct TYPE *dl_next_; \
struct TYPE *dl_prev_; \
struct TYPE *dl_next_; \
}
#define TD_DLIST(TYPE) \
......@@ -74,13 +74,13 @@ extern "C" {
#define TD_DLIST_TAIL(dl) ((dl)->dl_tail_)
#define TD_DLIST_NELES(dl) ((dl)->dl_neles_)
#define tDListInit(dl) \
#define TD_DLIST_INIT(dl) \
do { \
TD_DLIST_HEAD(dl) = TD_DLIST_TAIL(dl) = NULL; \
TD_DLIST_NELES(dl) = 0; \
} while (0)
#define tDListAppend(dl, dln) \
#define TD_DLIST_APPEND(dl, dln) \
do { \
if (TD_DLIST_HEAD(dl) == NULL) { \
TD_DLIST_NODE_PREV(dln) = TD_DLIST_NODE_NEXT(dln) = NULL; \
......@@ -94,7 +94,7 @@ extern "C" {
TD_DLIST_NELES(dl) += 1; \
} while (0)
#define tDListPrepend(dl, dln) \
#define TD_DLIST_PREPEND(dl, dln) \
do { \
if (TD_DLIST_HEAD(dl) == NULL) { \
TD_DLIST_NODE_PREV(dln) = TD_DLIST_NODE_NEXT(dln) = NULL; \
......@@ -108,7 +108,7 @@ extern "C" {
TD_DLIST_NELES(dl) += 1; \
} while (0)
#define tDListPop(dl, dln) \
#define TD_DLIST_POP(dl, dln) \
do { \
if (TD_DLIST_HEAD(dl) == (dln)) { \
TD_DLIST_HEAD(dl) = TD_DLIST_NODE_NEXT(dln); \
......@@ -126,19 +126,17 @@ extern "C" {
TD_DLIST_NODE_PREV(dln) = TD_DLIST_NODE_NEXT(dln) = NULL; \
} while (0)
// General double linked list
typedef enum { TD_LIST_FORWARD, TD_LIST_BACKWARD } TD_LIST_DIRECTION_T;
typedef struct _list_node {
struct _list_node *next;
struct _list_node *prev;
char data[];
typedef struct SListNode {
TD_DLIST_NODE(SListNode);
char data[];
} SListNode;
typedef struct {
struct _list_node *head;
struct _list_node *tail;
int numOfEles;
int eleSize;
TD_DLIST(SListNode);
int eleSize;
} SList;
typedef struct {
......@@ -146,11 +144,11 @@ typedef struct {
TD_LIST_DIRECTION_T direction;
} SListIter;
#define listHead(l) (l)->head
#define listTail(l) (l)->tail
#define listNEles(l) (l)->numOfEles
#define listEleSize(l) (l)->eleSize
#define isListEmpty(l) ((l)->numOfEles == 0)
#define listHead(l) TD_DLIST_HEAD(l)
#define listTail(l) TD_DLIST_TAIL(l)
#define listNEles(l) TD_DLIST_NELES(l)
#define listEleSize(l) ((l)->eleSize)
#define isListEmpty(l) (TD_DLIST_NELES(l) == 0)
#define listNodeFree(n) free(n)
void tdListInit(SList *list, int eleSize);
......
......@@ -27,7 +27,7 @@ SVMemAllocator *vmaCreate(uint64_t capacity, uint64_t ssize, uint64_t lsize) {
pVMA->capacity = capacity;
pVMA->ssize = ssize;
pVMA->lsize = lsize;
tSListInit(&(pVMA->nlist));
TD_SLIST_INIT(&(pVMA->nlist));
pVMA->pNode = vArenaNodeNew(capacity);
if (pVMA->pNode == NULL) {
......@@ -35,7 +35,7 @@ SVMemAllocator *vmaCreate(uint64_t capacity, uint64_t ssize, uint64_t lsize) {
return NULL;
}
tSListPush(&(pVMA->nlist), pVMA->pNode);
TD_SLIST_PUSH(&(pVMA->nlist), pVMA->pNode);
return pVMA;
}
......@@ -44,7 +44,7 @@ void vmaDestroy(SVMemAllocator *pVMA) {
if (pVMA) {
while (TD_SLIST_NELES(&(pVMA->nlist)) > 1) {
SVArenaNode *pNode = TD_SLIST_HEAD(&(pVMA->nlist));
tSListPop(&(pVMA->nlist));
TD_SLIST_POP(&(pVMA->nlist));
vArenaNodeFree(pNode);
}
......@@ -55,7 +55,7 @@ void vmaDestroy(SVMemAllocator *pVMA) {
void vmaReset(SVMemAllocator *pVMA) {
while (TD_SLIST_NELES(&(pVMA->nlist)) > 1) {
SVArenaNode *pNode = TD_SLIST_HEAD(&(pVMA->nlist));
tSListPop(&(pVMA->nlist));
TD_SLIST_POP(&(pVMA->nlist));
vArenaNodeFree(pNode);
}
......@@ -75,7 +75,7 @@ void *vmaMalloc(SVMemAllocator *pVMA, uint64_t size) {
return NULL;
}
tSListPush(&(pVMA->nlist), pNode);
TD_SLIST_PUSH(&(pVMA->nlist), pNode);
}
ptr = pNode->ptr;
......
......@@ -39,8 +39,8 @@ int vnodeOpenBufPool(SVnode *pVnode) {
return -1;
}
tDListInit(&(pVnode->pBufPool->free));
tDListInit(&(pVnode->pBufPool->incycle));
TD_DLIST_INIT(&(pVnode->pBufPool->free));
TD_DLIST_INIT(&(pVnode->pBufPool->incycle));
pVnode->pBufPool->inuse = NULL;
......@@ -54,7 +54,7 @@ int vnodeOpenBufPool(SVnode *pVnode) {
return -1;
}
tDListAppend(&(pVnode->pBufPool->free), pVMA);
TD_DLIST_APPEND(&(pVnode->pBufPool->free), pVMA);
}
pVnode->pBufPool->pMAF = (SMemAllocatorFactory *)malloc(sizeof(SMemAllocatorFactory));
......@@ -76,14 +76,14 @@ void vnodeCloseBufPool(SVnode *pVnode) {
while (true) {
SVMemAllocator *pVMA = TD_DLIST_HEAD(&(pVnode->pBufPool->incycle));
if (pVMA == NULL) break;
tDListPop(&(pVnode->pBufPool->incycle), pVMA);
TD_DLIST_POP(&(pVnode->pBufPool->incycle), pVMA);
vmaDestroy(pVMA);
}
while (true) {
SVMemAllocator *pVMA = TD_DLIST_HEAD(&(pVnode->pBufPool->free));
if (pVMA == NULL) break;
tDListPop(&(pVnode->pBufPool->free), pVMA);
TD_DLIST_POP(&(pVnode->pBufPool->free), pVMA);
vmaDestroy(pVMA);
}
......@@ -97,7 +97,7 @@ int vnodeBufPoolSwitch(SVnode *pVnode) {
pVnode->pBufPool->inuse = NULL;
tDListAppend(&(pVnode->pBufPool->incycle), pvma);
TD_DLIST_APPEND(&(pVnode->pBufPool->incycle), pvma);
return 0;
}
......@@ -106,9 +106,9 @@ int vnodeBufPoolRecycle(SVnode *pVnode) {
SVMemAllocator *pvma = TD_DLIST_HEAD(&(pBufPool->incycle));
ASSERT(pvma != NULL);
tDListPop(&(pBufPool->incycle), pvma);
TD_DLIST_POP(&(pBufPool->incycle), pvma);
vmaReset(pvma);
tDListAppend(&(pBufPool->free), pvma);
TD_DLIST_APPEND(&(pBufPool->free), pvma);
return 0;
}
......@@ -121,7 +121,7 @@ void *vnodeMalloc(SVnode *pVnode, uint64_t size) {
// TODO: add sem_wait and sem_post
pBufPool->inuse = TD_DLIST_HEAD(&(pBufPool->free));
if (pBufPool->inuse) {
tDListPop(&(pBufPool->free), pBufPool->inuse);
TD_DLIST_POP(&(pBufPool->free), pBufPool->inuse);
break;
} else {
// tsem_wait(&(pBufPool->hasFree));
......@@ -184,7 +184,7 @@ static void vBufPoolDestroyMA(SMemAllocatorFactory *pMAF, SMemAllocator *pMA) {
free(pMA);
if (--pVMA->_ref.val == 0) {
tDListPop(&(pVnode->pBufPool->incycle), pVMA);
tDListAppend(&(pVnode->pBufPool->free), pVMA);
TD_DLIST_POP(&(pVnode->pBufPool->incycle), pVMA);
TD_DLIST_APPEND(&(pVnode->pBufPool->free), pVMA);
}
}
\ No newline at end of file
......@@ -34,7 +34,7 @@ int vnodeInit(uint16_t nthreads) {
pthread_mutex_init(&(vnodeMgr.mutex), NULL);
pthread_cond_init(&(vnodeMgr.hasTask), NULL);
tDListInit(&(vnodeMgr.queue));
TD_DLIST_INIT(&(vnodeMgr.queue));
for (uint16_t i = 0; i < nthreads; i++) {
pthread_create(&(vnodeMgr.threads[i]), NULL, loop, NULL);
......@@ -77,7 +77,7 @@ void vnodeClear() {
int vnodeScheduleTask(SVnodeTask* pTask) {
pthread_mutex_lock(&(vnodeMgr.mutex));
tDListAppend(&(vnodeMgr.queue), pTask);
TD_DLIST_APPEND(&(vnodeMgr.queue), pTask);
pthread_cond_signal(&(vnodeMgr.hasTask));
......@@ -101,7 +101,7 @@ static void* loop(void* arg) {
pthread_cond_wait(&(vnodeMgr.hasTask), &(vnodeMgr.mutex));
}
} else {
tDListPop(&(vnodeMgr.queue), pTask);
TD_DLIST_POP(&(vnodeMgr.queue), pTask);
break;
}
}
......
......@@ -62,7 +62,7 @@ STsdbMemTable *tsdbNewMemTable(SMemAllocatorFactory *pMAF) {
pMemTable->keyMax = TSKEY_MIN;
pMemTable->nRow = 0;
pMemTable->pMA = pMA;
tSListInit(&(pMemTable->list));
TD_SLIST_INIT(&(pMemTable->list));
// TODO
return pMemTable;
......@@ -86,7 +86,7 @@ int tsdbInsertDataToMemTable(STsdbMemTable *pMemTable, SSubmitMsg *pMsg) {
// TODO
}
tSListPush(&(pMemTable->list), pTbData);
TD_SLIST_PUSH(&(pMemTable->list), pTbData);
return 0;
}
......
......@@ -17,9 +17,8 @@
#include "os.h"
void tdListInit(SList *list, int eleSize) {
list->eleSize = eleSize;
list->numOfEles = 0;
list->head = list->tail = NULL;
TD_DLIST_INIT(list);
listEleSize(list) = eleSize;
}
SList *tdListNew(int eleSize) {
......@@ -31,14 +30,11 @@ SList *tdListNew(int eleSize) {
}
void tdListEmpty(SList *list) {
SListNode *node = list->head;
while (node) {
list->head = node->next;
SListNode *node;
while ((node = TD_DLIST_HEAD(list)) != NULL) {
TD_DLIST_POP(list, node);
free(node);
node = list->head;
}
list->head = list->tail = 0;
list->numOfEles = 0;
}
void *tdListFree(SList *list) {
......@@ -50,40 +46,16 @@ void *tdListFree(SList *list) {
return NULL;
}
void tdListPrependNode(SList *list, SListNode *node) {
if (list->head == NULL) {
list->head = node;
list->tail = node;
} else {
node->next = list->head;
node->prev = NULL;
list->head->prev = node;
list->head = node;
}
list->numOfEles++;
}
void tdListAppendNode(SList *list, SListNode *node) {
if (list->head == NULL) {
list->head = node;
list->tail = node;
} else {
node->prev = list->tail;
node->next = NULL;
list->tail->next = node;
list->tail = node;
}
void tdListPrependNode(SList *list, SListNode *node) { TD_DLIST_PREPEND(list, node); }
list->numOfEles++;
}
void tdListAppendNode(SList *list, SListNode *node) { TD_DLIST_APPEND(list, node); }
int tdListPrepend(SList *list, void *data) {
SListNode *node = (SListNode *)malloc(sizeof(SListNode) + list->eleSize);
if (node == NULL) return -1;
node->next = node->prev = NULL;
memcpy((void *)(node->data), data, list->eleSize);
tdListPrependNode(list, node);
TD_DLIST_PREPEND(list, node);
return 0;
}
......@@ -93,73 +65,40 @@ int tdListAppend(SList *list, void *data) {
if (node == NULL) return -1;
memcpy((void *)(node->data), data, list->eleSize);
tdListAppendNode(list, node);
TD_DLIST_APPEND(list, node);
return 0;
}
SListNode *tdListPopHead(SList *list) {
if (list->head == NULL) return NULL;
SListNode *node = list->head;
if (node->next == NULL) {
list->head = NULL;
list->tail = NULL;
} else {
list->head = node->next;
SListNode *node;
node = TD_DLIST_HEAD(list);
if (node) {
TD_DLIST_POP(list, node);
}
list->numOfEles--;
node->next = NULL;
node->prev = NULL;
return node;
}
SListNode *tdListPopTail(SList *list) {
if (list->tail == NULL) return NULL;
SListNode *node = list->tail;
if (node->prev == NULL) {
list->head = NULL;
list->tail = NULL;
} else {
list->tail = node->prev;
}
list->numOfEles--;
node->next = node->prev = NULL;
return node;
}
SListNode *node;
SListNode *tdListGetHead(SList *list) {
if (list == NULL || list->numOfEles == 0) {
return NULL;
node = TD_DLIST_TAIL(list);
if (node) {
TD_DLIST_POP(list, node);
}
return list->head;
return node;
}
SListNode *tsListGetTail(SList *list) {
if (list == NULL || list->numOfEles == 0) {
return NULL;
}
SListNode *tdListGetHead(SList *list) { return TD_DLIST_HEAD(list); }
return list->tail;
}
SListNode *tsListGetTail(SList *list) { return TD_DLIST_TAIL(list); }
SListNode *tdListPopNode(SList *list, SListNode *node) {
if (list->head == node) {
list->head = node->next;
}
if (list->tail == node) {
list->tail = node->prev;
}
if (node->prev != NULL) {
node->prev->next = node->next;
}
if (node->next != NULL) {
node->next->prev = node->prev;
}
list->numOfEles--;
node->next = node->prev = NULL;
TD_DLIST_POP(list, node);
return node;
}
......@@ -174,19 +113,19 @@ void tdListMove(SList *src, SList *dst) {
void tdListDiscard(SList *list) {
if (list) {
list->head = list->tail = NULL;
list->numOfEles = 0;
listHead(list) = listTail(list) = NULL;
listNEles(list) = 0;
}
}
void tdListNodeGetData(SList *list, SListNode *node, void *target) { memcpy(target, node->data, list->eleSize); }
void tdListNodeGetData(SList *list, SListNode *node, void *target) { memcpy(target, node->data, listEleSize(list)); }
void tdListInitIter(SList *list, SListIter *pIter, TD_LIST_DIRECTION_T direction) {
pIter->direction = direction;
if (direction == TD_LIST_FORWARD) {
pIter->next = list->head;
pIter->next = listHead(list);
} else {
pIter->next = list->tail;
pIter->next = listTail(list);
}
}
......@@ -194,9 +133,9 @@ SListNode *tdListNext(SListIter *pIter) {
SListNode *node = pIter->next;
if (node == NULL) return NULL;
if (pIter->direction == TD_LIST_FORWARD) {
pIter->next = node->next;
pIter->next = TD_DLIST_NODE_NEXT(node);
} else {
pIter->next = node->prev;
pIter->next = TD_DLIST_NODE_PREV(node);
}
return node;
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册