From f65a1339d65fb5733bb693e0ada66804afd65914 Mon Sep 17 00:00:00 2001 From: Hongze Cheng Date: Fri, 17 Dec 2021 14:12:01 +0800 Subject: [PATCH 1/6] merge tlist.h and tdlist.h --- include/util/tdlist.h | 169 ------------------------- include/util/tlist.h | 107 ++++++++++++++++ source/dnode/vnode/impl/inc/vnodeDef.h | 2 +- source/dnode/vnode/tsdb/inc/tsdbDef.h | 2 +- 4 files changed, 109 insertions(+), 171 deletions(-) delete mode 100644 include/util/tdlist.h diff --git a/include/util/tdlist.h b/include/util/tdlist.h deleted file mode 100644 index d047a57770..0000000000 --- a/include/util/tdlist.h +++ /dev/null @@ -1,169 +0,0 @@ -/* - * Copyright (c) 2019 TAOS Data, Inc. - * - * This program is free software: you can use, redistribute, and/or modify - * it under the terms of the GNU Affero General Public License, version 3 - * or later ("AGPL"), as published by the Free Software Foundation. - * - * This program is distributed in the hope that it will be useful, but WITHOUT - * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - * FITNESS FOR A PARTICULAR PURPOSE. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see . - */ - -#ifndef _TD_UTIL_TDLIST_H_ -#define _TD_UTIL_TDLIST_H_ - -#ifdef __cplusplus -extern "C" { -#endif - -// Single linked list -#define TD_SLIST_NODE(TYPE) \ - struct { \ - struct TYPE *sl_next_; \ - } - -#define TD_SLIST(TYPE) \ - struct { \ - struct TYPE *sl_head_; \ - int sl_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_head_ = NULL; \ - (sl)->sl_neles_ = 0; \ - } while (0) - -#define tSListPush(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) \ - 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 { \ - struct TYPE *dl_prev_; \ - struct TYPE *dl_next_; \ - } - -#define TD_DLIST(TYPE) \ - struct { \ - struct TYPE *dl_head_; \ - struct TYPE *dl_tail_; \ - int dl_neles_; \ - } - -#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 tDListInit(dl) \ - do { \ - TD_DLIST_HEAD(dl) = TD_DLIST_TAIL(dl) = NULL; \ - TD_DLIST_NELES(dl) = 0; \ - } while (0) - -#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 -#define TD_LIST_ITER(S) \ - struct { \ - int it_dir_; \ - S * it_next_; \ - S * it_ptr_; \ - 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)->dl_head_; \ - } else { \ - (it)->it_next_ = (l)->dl_tail_; \ - } - -#define tlistIterNext(it) \ - ({ \ - (it)->it_ptr_ = (it)->it_next_; \ - if ((it)->it_next_ != NULL) { \ - if ((it)->it_dir_ == TD_LIST_FITER) { \ - (it)->it_next_ = (it)->it_next_->next_; \ - } else { \ - (it)->it_next_ = (it)->it_next_->prev_; \ - } \ - } \ - (it)->it_ptr_; \ - }) -#endif - -#ifdef __cplusplus -} -#endif - -#endif /*_TD_UTIL_TDLIST_H_*/ \ No newline at end of file diff --git a/include/util/tlist.h b/include/util/tlist.h index e803e96605..497f88b29e 100644 --- a/include/util/tlist.h +++ b/include/util/tlist.h @@ -19,6 +19,113 @@ extern "C" { #endif +// Single linked list +#define TD_SLIST_NODE(TYPE) \ + struct { \ + struct TYPE *sl_next_; \ + } + +#define TD_SLIST(TYPE) \ + struct { \ + struct TYPE *sl_head_; \ + int sl_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_head_ = NULL; \ + (sl)->sl_neles_ = 0; \ + } while (0) + +#define tSListPush(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) \ + 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 { \ + struct TYPE *dl_prev_; \ + struct TYPE *dl_next_; \ + } + +#define TD_DLIST(TYPE) \ + struct { \ + struct TYPE *dl_head_; \ + struct TYPE *dl_tail_; \ + int dl_neles_; \ + } + +#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 tDListInit(dl) \ + do { \ + TD_DLIST_HEAD(dl) = TD_DLIST_TAIL(dl) = NULL; \ + TD_DLIST_NELES(dl) = 0; \ + } while (0) + +#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) + typedef enum { TD_LIST_FORWARD, TD_LIST_BACKWARD } TD_LIST_DIRECTION_T; typedef struct _list_node { diff --git a/source/dnode/vnode/impl/inc/vnodeDef.h b/source/dnode/vnode/impl/inc/vnodeDef.h index e6a88c7629..56e07aca10 100644 --- a/source/dnode/vnode/impl/inc/vnodeDef.h +++ b/source/dnode/vnode/impl/inc/vnodeDef.h @@ -19,7 +19,7 @@ #include "mallocator.h" #include "sync.h" #include "tcoding.h" -#include "tdlist.h" +#include "tlist.h" #include "tlockfree.h" #include "tmacro.h" #include "wal.h" diff --git a/source/dnode/vnode/tsdb/inc/tsdbDef.h b/source/dnode/vnode/tsdb/inc/tsdbDef.h index 7c593cb4c7..b1375c9477 100644 --- a/source/dnode/vnode/tsdb/inc/tsdbDef.h +++ b/source/dnode/vnode/tsdb/inc/tsdbDef.h @@ -18,7 +18,7 @@ #include "mallocator.h" #include "taosmsg.h" -#include "tdlist.h" +#include "tlist.h" #include "thash.h" #include "tskiplist.h" From 9d5e0d53fbcdbf6d145bcf450871592e23f6b8c6 Mon Sep 17 00:00:00 2001 From: Hongze Cheng Date: Fri, 17 Dec 2021 14:49:34 +0800 Subject: [PATCH 2/6] refactor list --- include/util/tlist.h | 46 +++---- .../dnode/vnode/impl/src/vnodeArenaMAImpl.c | 10 +- source/dnode/vnode/impl/src/vnodeBufferPool.c | 22 +-- source/dnode/vnode/impl/src/vnodeMgr.c | 6 +- source/dnode/vnode/tsdb/src/tsdbMemTable.c | 4 +- source/util/src/tlist.c | 125 +++++------------- 6 files changed, 75 insertions(+), 138 deletions(-) diff --git a/include/util/tlist.h b/include/util/tlist.h index 497f88b29e..06d0abc797 100644 --- a/include/util/tlist.h +++ b/include/util/tlist.h @@ -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); diff --git a/source/dnode/vnode/impl/src/vnodeArenaMAImpl.c b/source/dnode/vnode/impl/src/vnodeArenaMAImpl.c index 99d4781df9..5999b08a7d 100644 --- a/source/dnode/vnode/impl/src/vnodeArenaMAImpl.c +++ b/source/dnode/vnode/impl/src/vnodeArenaMAImpl.c @@ -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; diff --git a/source/dnode/vnode/impl/src/vnodeBufferPool.c b/source/dnode/vnode/impl/src/vnodeBufferPool.c index 6c1ededfc9..763cf3e3b3 100644 --- a/source/dnode/vnode/impl/src/vnodeBufferPool.c +++ b/source/dnode/vnode/impl/src/vnodeBufferPool.c @@ -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 diff --git a/source/dnode/vnode/impl/src/vnodeMgr.c b/source/dnode/vnode/impl/src/vnodeMgr.c index 964cbe77da..fae96ae22c 100644 --- a/source/dnode/vnode/impl/src/vnodeMgr.c +++ b/source/dnode/vnode/impl/src/vnodeMgr.c @@ -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; } } diff --git a/source/dnode/vnode/tsdb/src/tsdbMemTable.c b/source/dnode/vnode/tsdb/src/tsdbMemTable.c index e3d1f8673e..8a320129b0 100644 --- a/source/dnode/vnode/tsdb/src/tsdbMemTable.c +++ b/source/dnode/vnode/tsdb/src/tsdbMemTable.c @@ -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; } diff --git a/source/util/src/tlist.c b/source/util/src/tlist.c index 6756af226f..f79bca1e4b 100644 --- a/source/util/src/tlist.c +++ b/source/util/src/tlist.c @@ -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 tdListPrependNode(SList *list, SListNode *node) { TD_DLIST_PREPEND(list, node); } -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; - } - - 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; + SListNode *node; + + node = TD_DLIST_TAIL(list); + if (node) { + TD_DLIST_POP(list, node); } - list->numOfEles--; - node->next = node->prev = NULL; + return node; } -SListNode *tdListGetHead(SList *list) { - if (list == NULL || list->numOfEles == 0) { - return NULL; - } +SListNode *tdListGetHead(SList *list) { return TD_DLIST_HEAD(list); } - return list->head; -} - -SListNode *tsListGetTail(SList *list) { - if (list == NULL || list->numOfEles == 0) { - return NULL; - } - - 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; From 53cbb261ae00f5a2549eddc6173d03b5db788578 Mon Sep 17 00:00:00 2001 From: Hongze Cheng Date: Fri, 17 Dec 2021 15:05:19 +0800 Subject: [PATCH 3/6] configure build --- source/util/CMakeLists.txt | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/source/util/CMakeLists.txt b/source/util/CMakeLists.txt index 7c42afcc51..6fa6e5f82f 100644 --- a/source/util/CMakeLists.txt +++ b/source/util/CMakeLists.txt @@ -1,3 +1,4 @@ +configure_file(src/version.c.in src/version.c) aux_source_directory(src UTIL_SRC) add_library(util STATIC ${UTIL_SRC}) target_include_directories( @@ -11,6 +12,4 @@ target_link_libraries( PUBLIC zlib PUBLIC lz4_static PUBLIC api -) - -CONFIGURE_FILE("${CMAKE_CURRENT_SOURCE_DIR}/src/version.c.in" "${CMAKE_CURRENT_SOURCE_DIR}/src/version.c") +) \ No newline at end of file From 7f7de09d4e7a08219be2005bcac5577fdfe786c8 Mon Sep 17 00:00:00 2001 From: Hongze Cheng Date: Fri, 17 Dec 2021 15:30:11 +0800 Subject: [PATCH 4/6] make it compile --- source/util/CMakeLists.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/source/util/CMakeLists.txt b/source/util/CMakeLists.txt index 6fa6e5f82f..d343945a80 100644 --- a/source/util/CMakeLists.txt +++ b/source/util/CMakeLists.txt @@ -1,4 +1,4 @@ -configure_file(src/version.c.in src/version.c) +configure_file("${CMAKE_CURRENT_SOURCE_DIR}/src/version.c.in" "${CMAKE_CURRENT_SOURCE_DIR}/src/version.c") aux_source_directory(src UTIL_SRC) add_library(util STATIC ${UTIL_SRC}) target_include_directories( From 91a36a51cf20672a28d236984a20a150f23dfc12 Mon Sep 17 00:00:00 2001 From: Hongze Cheng Date: Fri, 17 Dec 2021 15:49:15 +0800 Subject: [PATCH 5/6] make compile faster --- Jenkinsfile | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/Jenkinsfile b/Jenkinsfile index 398ce0f869..2558df777b 100644 --- a/Jenkinsfile +++ b/Jenkinsfile @@ -83,8 +83,7 @@ def pre_test(){ mkdir debug cd debug cmake .. > /dev/null - make > /dev/null - make install > /dev/null + make -j4> /dev/null ''' return 1 From a3360ea0942d9f9a894867818d0ba22052107c37 Mon Sep 17 00:00:00 2001 From: Hongze Cheng Date: Fri, 17 Dec 2021 16:12:49 +0800 Subject: [PATCH 6/6] refact --- include/util/mallocator.h | 33 +++++++++++-------- source/dnode/vnode/impl/src/vnodeBufferPool.c | 6 +--- source/dnode/vnode/tsdb/src/tsdbMemTable.c | 6 ++-- source/util/src/mallocator.c | 4 ++- 4 files changed, 26 insertions(+), 23 deletions(-) diff --git a/include/util/mallocator.h b/include/util/mallocator.h index ffe242017e..49a9327353 100644 --- a/include/util/mallocator.h +++ b/include/util/mallocator.h @@ -22,23 +22,28 @@ extern "C" { #endif -typedef struct SMemAllocator SMemAllocator; -typedef struct SMemAllocatorFactory SMemAllocatorFactory; +// Memory allocator +#define TD_MEM_ALCT(TYPE) \ + struct { \ + void *(*malloc_)(struct TYPE *, uint64_t size); \ + void (*free_)(struct TYPE *, void *ptr); \ + } +#define TD_MA_MALLOC_FUNC(TMA) (TMA)->malloc_ +#define TD_MA_FREE_FUNC(TMA) (TMA)->free_ -struct SMemAllocator { - void *impl; - void *(*malloc)(SMemAllocator *, uint64_t size); - void *(*calloc)(SMemAllocator *, uint64_t nmemb, uint64_t size); - void *(*realloc)(SMemAllocator *, void *ptr, uint64_t size); - void (*free)(SMemAllocator *, void *ptr); - uint64_t (*usage)(SMemAllocator *); -}; +#define TD_MA_MALLOC(TMA, SIZE) (*((TMA)->malloc_))(TMA, (SIZE)) +#define TD_MA_FREE(TMA, PTR) (*((TMA)->free_))(TMA, (PTR)) -struct SMemAllocatorFactory { +typedef struct SMemAllocator { void *impl; - SMemAllocator *(*create)(SMemAllocatorFactory *); - void (*destroy)(SMemAllocatorFactory *, SMemAllocator *); -}; + TD_MEM_ALCT(SMemAllocator); +} SMemAllocator; + +typedef struct SMemAllocatorFactory { + void *impl; + SMemAllocator *(*create)(struct SMemAllocatorFactory *); + void (*destroy)(struct SMemAllocatorFactory *, SMemAllocator *); +} SMemAllocatorFactory; #ifdef __cplusplus } diff --git a/source/dnode/vnode/impl/src/vnodeBufferPool.c b/source/dnode/vnode/impl/src/vnodeBufferPool.c index 763cf3e3b3..228df6c0a4 100644 --- a/source/dnode/vnode/impl/src/vnodeBufferPool.c +++ b/source/dnode/vnode/impl/src/vnodeBufferPool.c @@ -168,11 +168,7 @@ static SMemAllocator *vBufPoolCreateMA(SMemAllocatorFactory *pMAF) { pWrapper->pVMA = pVnode->pBufPool->inuse; pMA->impl = pWrapper; - pMA->malloc = vmaMaloocCb; - pMA->calloc = NULL; - pMA->realloc = NULL; - pMA->free = NULL; - pMA->usage = NULL; + TD_MA_MALLOC_FUNC(pMA) = vmaMaloocCb; return pMA; } diff --git a/source/dnode/vnode/tsdb/src/tsdbMemTable.c b/source/dnode/vnode/tsdb/src/tsdbMemTable.c index 8a320129b0..7b0df18f5a 100644 --- a/source/dnode/vnode/tsdb/src/tsdbMemTable.c +++ b/source/dnode/vnode/tsdb/src/tsdbMemTable.c @@ -50,7 +50,7 @@ STsdbMemTable *tsdbNewMemTable(SMemAllocatorFactory *pMAF) { pMA = (*pMAF->create)(pMAF); ASSERT(pMA != NULL); - pMemTable = (STsdbMemTable *)((*pMA->malloc)(pMA, sizeof(*pMemTable))); + pMemTable = (STsdbMemTable *)TD_MA_MALLOC(pMA, sizeof(*pMemTable)); if (pMemTable == NULL) { (*pMAF->destroy)(pMAF, pMA); return NULL; @@ -71,7 +71,7 @@ STsdbMemTable *tsdbNewMemTable(SMemAllocatorFactory *pMAF) { void tsdbFreeMemTable(SMemAllocatorFactory *pMAF, STsdbMemTable *pMemTable) { SMemAllocator *pMA = pMemTable->pMA; - if (pMA->free) { + if (TD_MA_FREE_FUNC(pMA) != NULL) { // TODO ASSERT(0); } @@ -81,7 +81,7 @@ void tsdbFreeMemTable(SMemAllocatorFactory *pMAF, STsdbMemTable *pMemTable) { int tsdbInsertDataToMemTable(STsdbMemTable *pMemTable, SSubmitMsg *pMsg) { SMemAllocator *pMA = pMemTable->pMA; - STbData * pTbData = (STbData *)((*pMA->malloc)(pMA, sizeof(*pTbData))); + STbData * pTbData = (STbData *)TD_MA_MALLOC(pMA, sizeof(*pTbData)); if (pTbData == NULL) { // TODO } diff --git a/source/util/src/mallocator.c b/source/util/src/mallocator.c index 1819396ccd..a56fbfa597 100644 --- a/source/util/src/mallocator.c +++ b/source/util/src/mallocator.c @@ -16,6 +16,7 @@ #include "mallocator.h" /* ------------------------ HEAP ALLOCATOR ------------------------ */ +#if 0 typedef struct { size_t tusage; } SHeapAllocator; @@ -104,4 +105,5 @@ static size_t haUsage(SMemAllocator *pma) { return ((SHeapAllocator *)(pma->impl /* ------------------------ ARENA ALLOCATOR ------------------------ */ typedef struct { size_t usage; -} SArenaAllocator; \ No newline at end of file +} SArenaAllocator; +#endif \ No newline at end of file