From 76e393d4a8f354eef5e4ce6feb5ea300278ff856 Mon Sep 17 00:00:00 2001 From: Hongze Cheng Date: Fri, 31 Dec 2021 05:41:58 +0000 Subject: [PATCH] more --- include/common/tmsg.h | 18 +++++----- include/util/freelist.h | 59 +++++++++++++++++++++++++++++++ source/util/test/CMakeLists.txt | 8 +++++ source/util/test/freelistTest.cpp | 16 +++++++++ 4 files changed, 92 insertions(+), 9 deletions(-) create mode 100644 include/util/freelist.h create mode 100644 source/util/test/freelistTest.cpp diff --git a/include/common/tmsg.h b/include/common/tmsg.h index 80dec89611..bf7a6ea11b 100644 --- a/include/common/tmsg.h +++ b/include/common/tmsg.h @@ -355,9 +355,9 @@ typedef struct SEpSet { } SEpSet; static FORCE_INLINE int taosEncodeSEpSet(void** buf, const SEpSet* pEp) { - if(buf == NULL) return sizeof(SEpSet); + if (buf == NULL) return sizeof(SEpSet); memcpy(buf, pEp, sizeof(SEpSet)); - //TODO: endian conversion + // TODO: endian conversion return sizeof(SEpSet); } @@ -1119,10 +1119,10 @@ typedef struct STaskDropRsp { } STaskDropRsp; typedef struct { - int8_t igExists; - char* name; - char* physicalPlan; - char* logicalPlan; + int8_t igExists; + char* name; + char* physicalPlan; + char* logicalPlan; } SCMCreateTopicReq; static FORCE_INLINE int tSerializeSCMCreateTopicReq(void** buf, const SCMCreateTopicReq* pReq) { @@ -1158,8 +1158,8 @@ static FORCE_INLINE void* tDeserializeSCMCreateTopicRsp(void* buf, SCMCreateTopi } typedef struct { - char* topicName; - char* consumerGroup; + char* topicName; + char* consumerGroup; int64_t consumerId; } SCMSubscribeReq; @@ -1180,7 +1180,7 @@ static FORCE_INLINE void* tDeserializeSCMSubscribeReq(void* buf, SCMSubscribeReq typedef struct { int32_t vgId; - SEpSet pEpSet; + SEpSet pEpSet; } SCMSubscribeRsp; static FORCE_INLINE int tSerializeSCMSubscribeRsp(void** buf, const SCMSubscribeRsp* pRsp) { diff --git a/include/util/freelist.h b/include/util/freelist.h new file mode 100644 index 0000000000..497a6d58c3 --- /dev/null +++ b/include/util/freelist.h @@ -0,0 +1,59 @@ +/* + * 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_FREELIST_H_ +#define _TD_UTIL_FREELIST_H_ + +#include "os.h" +#include "tlist.h" + +#ifdef __cplusplus +extern "C" { +#endif + +struct SFreeListNode { + TD_SLIST_NODE(SFreeListNode); + char payload[]; +}; + +typedef TD_SLIST(SFreeListNode) SFreeList; + +#define TFL_MALLOC(SIZE, LIST) \ + ({ \ + void *ptr = malloc((SIZE) + sizeof(struct SFreeListNode)); \ + if (ptr) { \ + TD_SLIST_PUSH((LIST), (struct SFreeListNode *)ptr); \ + ptr = ((struct SFreeListNode *)ptr)->payload; \ + } \ + ptr; \ + }) + +#define tFreeListInit(pFL) TD_SLIST_INIT(pFL) + +static FORCE_INLINE void tFreeListClear(SFreeList *pFL) { + struct SFreeListNode *pNode; + for (;;) { + pNode = TD_SLIST_HEAD(pFL); + if (pNode == NULL) break; + TD_SLIST_POP(pFL); + free(pNode); + } +} + +#ifdef __cplusplus +} +#endif + +#endif /*_TD_UTIL_FREELIST_H_*/ \ No newline at end of file diff --git a/source/util/test/CMakeLists.txt b/source/util/test/CMakeLists.txt index 79aaa1beb0..bfc3906f79 100644 --- a/source/util/test/CMakeLists.txt +++ b/source/util/test/CMakeLists.txt @@ -33,4 +33,12 @@ ENDIF() INCLUDE_DIRECTORIES(${TD_COMMUNITY_DIR}/src/util/inc) +# freelistTest +add_executable(freelistTest "") +target_sources(freelistTest + PRIVATE + "freelistTest.cpp" +) +target_link_libraries(freelistTest os util gtest gtest_main) + diff --git a/source/util/test/freelistTest.cpp b/source/util/test/freelistTest.cpp new file mode 100644 index 0000000000..7a4e8be5b7 --- /dev/null +++ b/source/util/test/freelistTest.cpp @@ -0,0 +1,16 @@ +#include "gtest/gtest.h" + +#include "freelist.h" + +TEST(TD_UTIL_FREELIST_TEST, simple_test) { + SFreeList fl; + + tFreeListInit(&fl); + + for (size_t i = 0; i < 1000; i++) { + void *ptr = TFL_MALLOC(1024, &fl); + GTEST_ASSERT_NE(ptr, nullptr); + } + + tFreeListClear(&fl); +} \ No newline at end of file