From 1da0f92d841fbeb76ded3be11753bbb8e1bf1eb3 Mon Sep 17 00:00:00 2001 From: hjxilinx Date: Fri, 10 Apr 2020 15:06:20 +0800 Subject: [PATCH] [td-98] add clone function for array --- src/util/inc/tarray.h | 6 ++++++ src/util/src/tarray.c | 15 +++++++++++++++ 2 files changed, 21 insertions(+) diff --git a/src/util/inc/tarray.h b/src/util/inc/tarray.h index 6b70780d10..28ebb18235 100644 --- a/src/util/inc/tarray.h +++ b/src/util/inc/tarray.h @@ -100,6 +100,12 @@ void taosArrayRemove(SArray* pArray, size_t index); */ void taosArrayCopy(SArray* pDst, SArray* pSrc); +/** + * clone a new array + * @param pSrc + */ +SArray* taosArrayClone(SArray* pSrc); + /** * destroy array list * @param pArray diff --git a/src/util/src/tarray.c b/src/util/src/tarray.c index eb1d213317..d97b220a40 100755 --- a/src/util/src/tarray.c +++ b/src/util/src/tarray.c @@ -160,6 +160,21 @@ void taosArrayCopy(SArray* pDst, SArray* pSrc) { pDst->size = pSrc->size; } +SArray* taosArrayClone(SArray* pSrc) { + assert(pSrc != NULL); + + if (pSrc->size == 0) { // empty array list + return taosArrayInit(8, pSrc->elemSize); + } + + SArray* dst = taosArrayInit(pSrc->size, pSrc->elemSize); + + memcpy(dst->pData, pSrc->pData, pSrc->elemSize * pSrc->size); + dst->size = pSrc->size; + return dst; +} + + void taosArrayDestroy(SArray* pArray) { if (pArray == NULL) { return; -- GitLab