From cfada1cc8ae2ee8e03baf6cf0b99ffa384336578 Mon Sep 17 00:00:00 2001 From: Hongze Cheng Date: Tue, 3 May 2022 14:08:30 +0000 Subject: [PATCH] more sl refact --- include/util/tskiplist2.h | 6 ++++ source/util/src/tskiplist2.c | 57 +++++++++++++++++++++++++++++------- 2 files changed, 53 insertions(+), 10 deletions(-) diff --git a/include/util/tskiplist2.h b/include/util/tskiplist2.h index 95fbee5a23..f3366dd574 100644 --- a/include/util/tskiplist2.h +++ b/include/util/tskiplist2.h @@ -32,6 +32,7 @@ typedef int32_t (*tslCmprFn)(const void *pKey1, int32_t nKey1, const void *pKey2 // SSkipList2 int32_t slOpen(const SSLCfg *pCfg, SSkipList2 **ppSl); int32_t slClose(SSkipList2 *pSl); +int32_t slClear(SSkipList2 *pSl); // SSLCursor int32_t slcOpen(SSkipList2 *pSl, SSLCursor *pSlc); @@ -56,6 +57,11 @@ struct SSLCfg { void (*xFree)(void *, void *); }; +struct SSLCursor { + SSkipList2 *pSl; + SSLNode **forwards[SL_MAX_LEVEL]; +}; + #ifdef __cplusplus } #endif diff --git a/source/util/src/tskiplist2.c b/source/util/src/tskiplist2.c index 6657896131..e7391da285 100644 --- a/source/util/src/tskiplist2.c +++ b/source/util/src/tskiplist2.c @@ -27,11 +27,7 @@ struct SSkipList2 { uint32_t seed; int32_t size; const SSLCfg *pCfg; -}; - -struct SSLCursor { - SSkipList2 *pSl; - SSLNode *forwards[SL_MAX_LEVEL]; + SSLNode *pHead[]; }; static void *slMalloc(void *pPool, int32_t size); @@ -48,25 +44,66 @@ const SSLCfg slDefaultCfg = {.maxLevel = SL_MAX_LEVEL, int32_t slOpen(const SSLCfg *pCfg, SSkipList2 **ppSl) { SSkipList2 *pSl = NULL; + int32_t size; *ppSl = NULL; if (pCfg == NULL) pCfg = &slDefaultCfg; - // check cfg (TODO) + // check config (TODO) + + // malloc handle + size = sizeof(*pSl) + sizeof(SSLNode *) * pCfg->maxLevel * 2; + pSl = pCfg->xMalloc(pCfg->pPool, size); + if (pSl == NULL) { + return -1; + } + + pSl->level = 0; + pSl->seed = taosRand(); + pSl->size = 0; + pSl->pCfg = pCfg; + + // init an empty skiplist + for (int32_t i = 0; i < pCfg->maxLevel * 2; i++) { + pSl->pHead[i] = NULL; + } - // create handle - pSl = pCfg->xMalloc(pCfg->pPool, sizeof(*pSl)); - // (TODO) *ppSl = pSl; return 0; } int32_t slClose(SSkipList2 *pSl) { - // TODO + if (pSl) { + slClear(pSl); + if (pSl->pCfg->xFree) { + pSl->pCfg->xFree(pSl->pCfg->pPool, pSl); + } + } + + return 0; +} + +int32_t slClear(SSkipList2 *pSl) { + // loop to clear sl + for (;;) { + // (TODO) + } + + // init sl (TODO) + return 0; } int32_t slcOpen(SSkipList2 *pSl, SSLCursor *pSlc) { + pSlc->pSl = pSl; + + for (int i = 0; i < SL_MAX_LEVEL; i++) { + if (i < pSl->pCfg->maxLevel) { + } else { + pSlc->forwards[i] = NULL; + } + } + // TODO return 0; } -- GitLab