diff --git a/include/util/tdigest.h b/include/util/tdigest.h index 836af92e78..03519b4c7b 100644 --- a/include/util/tdigest.h +++ b/include/util/tdigest.h @@ -64,10 +64,10 @@ typedef struct TDigest { } TDigest; TDigest *tdigestNewFrom(void *pBuf, int32_t compression); -void tdigestAdd(TDigest *t, double x, int64_t w); -void tdigestMerge(TDigest *t1, TDigest *t2); +int32_t tdigestAdd(TDigest *t, double x, int64_t w); +int32_t tdigestMerge(TDigest *t1, TDigest *t2); double tdigestQuantile(TDigest *t, double q); -void tdigestCompress(TDigest *t); +int32_t tdigestCompress(TDigest *t); void tdigestFreeFrom(TDigest *t); void tdigestAutoFill(TDigest *t, int32_t compression); diff --git a/source/os/src/osMemory.c b/source/os/src/osMemory.c index 3e49808dad..25567dfde0 100644 --- a/source/os/src/osMemory.c +++ b/source/os/src/osMemory.c @@ -266,7 +266,7 @@ void *taosMemoryMalloc(int64_t size) { return (char *)tmp + sizeof(TdMemoryInfo); #else - void* p = malloc(size); + void *p = malloc(size); if (NULL == p) { terrno = TSDB_CODE_OUT_OF_MEMORY; } @@ -287,7 +287,7 @@ void *taosMemoryCalloc(int64_t num, int64_t size) { return (char *)tmp + sizeof(TdMemoryInfo); #else - void* p = calloc(num, size); + void *p = calloc(num, size); if (NULL == p) { terrno = TSDB_CODE_OUT_OF_MEMORY; } @@ -317,7 +317,7 @@ void *taosMemoryRealloc(void *ptr, int64_t size) { return (char *)tmp + sizeof(TdMemoryInfo); #else - void* p = realloc(ptr, size); + void *p = realloc(ptr, size); if (size > 0 && NULL == p) { terrno = TSDB_CODE_OUT_OF_MEMORY; } @@ -342,12 +342,12 @@ char *taosStrdup(const char *ptr) { return (char *)tmp + sizeof(TdMemoryInfo); #else - char* p = tstrdup(ptr); + char *p = tstrdup(ptr); if (ptr != NULL && NULL == p) { terrno = TSDB_CODE_OUT_OF_MEMORY; } return p; - + #endif } diff --git a/source/util/src/tarray.c b/source/util/src/tarray.c index ec5e41f256..75fe5d254d 100644 --- a/source/util/src/tarray.c +++ b/source/util/src/tarray.c @@ -18,9 +18,9 @@ #include "tcoding.h" // todo refactor API -#define BOUNDARY_SIZE 1024*1024*1024 // 1G +#define BOUNDARY_SIZE 1024 * 1024 * 1024 // 1G #define BOUNDARY_SMALL_FACTOR 1.2 -#define BOUNDARY_BIG_FACTOR 2 +#define BOUNDARY_BIG_FACTOR 2 SArray* taosArrayInit(size_t size, size_t elemSize) { if (elemSize == 0) { @@ -52,14 +52,12 @@ SArray* taosArrayInit(size_t size, size_t elemSize) { SArray* taosArrayInit_s(size_t elemSize, size_t initialSize) { SArray* pArray = taosMemoryMalloc(sizeof(SArray)); if (pArray == NULL) { - terrno = TSDB_CODE_OUT_OF_MEMORY; return NULL; } pArray->size = initialSize; pArray->pData = taosMemoryCalloc(initialSize, elemSize); if (pArray->pData == NULL) { - terrno = TSDB_CODE_OUT_OF_MEMORY; taosMemoryFree(pArray); return NULL; } @@ -75,7 +73,7 @@ static int32_t taosArrayResize(SArray* pArray) { void* tmp = taosMemoryRealloc(pArray->pData, size * pArray->elemSize); if (tmp == NULL) { // reallocate failed, the original buffer remains - return -1; + return terrno; } pArray->pData = tmp; @@ -99,7 +97,7 @@ int32_t taosArrayEnsureCap(SArray* pArray, size_t newCap) { pArray->pData = taosMemoryRealloc(pArray->pData, tsize * pArray->elemSize); if (pArray->pData == NULL) { - return -1; + return terrno; } pArray->capacity = tsize; @@ -109,10 +107,13 @@ int32_t taosArrayEnsureCap(SArray* pArray, size_t newCap) { void* taosArrayAddBatch(SArray* pArray, const void* pData, int32_t nEles) { if (pData == NULL) { + terrno = TSDB_CODE_INVALID_PARA; return NULL; } - if (taosArrayEnsureCap(pArray, pArray->size + nEles) != 0) { + int32_t code = taosArrayEnsureCap(pArray, pArray->size + nEles); + if (code) { + terrno = code; return NULL; } @@ -171,7 +172,9 @@ void* taosArrayAddAll(SArray* pArray, const SArray* pInput) { } void* taosArrayReserve(SArray* pArray, int32_t num) { - if (taosArrayEnsureCap(pArray, pArray->size + num) != 0) { + int32_t code = taosArrayEnsureCap(pArray, pArray->size + num); + if (code) { + terrno = code; return NULL; } @@ -198,7 +201,7 @@ void* taosArrayGet(const SArray* pArray, size_t index) { } if (index >= pArray->size) { - uError("index is out of range, current:%" PRIzu " max:%"PRIzu, index, pArray->size); + uError("index is out of range, current:%" PRIzu " max:%" PRIzu, index, pArray->size); return NULL; } @@ -230,6 +233,7 @@ size_t taosArrayGetSize(const SArray* pArray) { void* taosArrayInsert(SArray* pArray, size_t index, const void* pData) { if (pArray == NULL || pData == NULL) { + terrno = TSDB_CODE_INVALID_PARA; return NULL; } @@ -239,7 +243,6 @@ void* taosArrayInsert(SArray* pArray, size_t index, const void* pData) { if (pArray->size >= pArray->capacity) { int32_t ret = taosArrayResize(pArray); - if (ret < 0) { return NULL; } @@ -316,8 +319,10 @@ SArray* taosArrayFromList(const void* src, size_t size, size_t elemSize) { } SArray* pDst = taosArrayInit(size, elemSize); - memcpy(pDst->pData, src, elemSize * size); - pDst->size = size; + if (pDst) { + memcpy(pDst->pData, src, elemSize * size); + pDst->size = size; + } return pDst; } @@ -333,18 +338,20 @@ SArray* taosArrayDup(const SArray* pSrc, __array_item_dup_fn_t fn) { SArray* dst = taosArrayInit(pSrc->size, pSrc->elemSize); - if (fn == NULL) { - memcpy(dst->pData, pSrc->pData, pSrc->elemSize * pSrc->size); - } else { - ASSERT(pSrc->elemSize == sizeof(void*)); + if (dst) { + if (fn == NULL) { + memcpy(dst->pData, pSrc->pData, pSrc->elemSize * pSrc->size); + } else { + ASSERT(pSrc->elemSize == sizeof(void*)); - for (int32_t i = 0; i < pSrc->size; ++i) { - void* p = fn(taosArrayGetP(pSrc, i)); - memcpy(((char*)dst->pData) + i * dst->elemSize, &p, dst->elemSize); + for (int32_t i = 0; i < pSrc->size; ++i) { + void* p = fn(taosArrayGetP(pSrc, i)); + memcpy(((char*)dst->pData) + i * dst->elemSize, &p, dst->elemSize); + } } - } - dst->size = pSrc->size; + dst->size = pSrc->size; + } return dst; } @@ -508,10 +515,21 @@ void* taosDecodeArray(const void* buf, SArray** pArray, FDecode decode, int32_t int32_t sz; buf = taosDecodeFixedI32(buf, &sz); *pArray = taosArrayInit(sz, sizeof(void*)); + if (*pArray == NULL) { + return NULL; + } for (int32_t i = 0; i < sz; i++) { void* data = taosMemoryCalloc(1, dataSz); + if (data == NULL) { + return NULL; + } + buf = decode(buf, data, sver); - taosArrayPush(*pArray, &data); + + if (taosArrayPush(*pArray, &data) == NULL) { + taosMemoryFree(data); + return NULL; + } } return (void*)buf; } diff --git a/source/util/src/tcache.c b/source/util/src/tcache.c index 0a7842194e..c0ed0b98d0 100644 --- a/source/util/src/tcache.c +++ b/source/util/src/tcache.c @@ -230,6 +230,7 @@ static FORCE_INLINE void taosCacheReleaseNode(SCacheObj *pCacheObj, SCacheNode * static FORCE_INLINE STrashElem *doRemoveElemInTrashcan(SCacheObj *pCacheObj, STrashElem *pElem) { if (pElem->pData->signature != pElem->pData) { uWarn("key:sig:0x%" PRIx64 " %p data has been released, ignore", (int64_t)pElem->pData->signature, pElem->pData); + terrno = TSDB_CODE_INVALID_PARA; return NULL; } @@ -358,6 +359,7 @@ SCacheObj *taosCacheInit(int32_t keyType, int64_t refreshTimeInMs, bool extendLi SCacheObj *pCacheObj = (SCacheObj *)taosMemoryCalloc(1, sizeof(SCacheObj)); if (pCacheObj == NULL) { uError("failed to allocate memory, reason:%s", strerror(errno)); + terrno = TSDB_CODE_OUT_OF_MEMORY; return NULL; } @@ -393,6 +395,7 @@ SCacheObj *taosCacheInit(int32_t keyType, int64_t refreshTimeInMs, bool extendLi void *taosCachePut(SCacheObj *pCacheObj, const void *key, size_t keyLen, const void *pData, size_t dataSize, int32_t durationMS) { if (pCacheObj == NULL || pCacheObj->pEntryList == NULL || pCacheObj->deleting == 1) { + terrno = TSDB_CODE_INVALID_PARA; return NULL; } @@ -501,11 +504,15 @@ void *taosCacheAcquireByData(SCacheObj *pCacheObj, void *data) { } void *taosCacheTransferData(SCacheObj *pCacheObj, void **data) { - if (pCacheObj == NULL || data == NULL || (*data) == NULL) return NULL; + if (pCacheObj == NULL || data == NULL || (*data) == NULL) { + terrno = TSDB_CODE_INVALID_PARA; + return NULL; + } SCacheNode *ptNode = (SCacheNode *)((char *)(*data) - sizeof(SCacheNode)); if (ptNode->signature != ptNode) { uError("cache:%s, key: %p the data from cache is invalid", pCacheObj->name, ptNode); + terrno = TSDB_CODE_INVALID_PARA; return NULL; } @@ -702,8 +709,8 @@ SCacheNode *taosCreateCacheNode(const char *key, size_t keyLen, const char *pDat SCacheNode *pNewNode = taosMemoryCalloc(1, sizeInBytes); if (pNewNode == NULL) { - terrno = TSDB_CODE_OUT_OF_MEMORY; uError("failed to allocate memory, reason:%s", strerror(errno)); + terrno = TSDB_CODE_OUT_OF_MEMORY; return NULL; } diff --git a/source/util/src/tdes.c b/source/util/src/tdes.c index a4da08630e..2da8cd9b55 100644 --- a/source/util/src/tdes.c +++ b/source/util/src/tdes.c @@ -76,6 +76,12 @@ char* taosDesEncode(int64_t key, char* src, int32_t len) { char* taosDesDecode(int64_t key, char* src, int32_t len) { uint8_t* keyStr = (uint8_t*)(&key); char* temp = taosMemoryCalloc(len + 8, 1); + + if (!temp) { + terrno = TSDB_CODE_OUT_OF_MEMORY; + return NULL; + } + memcpy(temp, src, len); len += 8; diff --git a/source/util/src/tdigest.c b/source/util/src/tdigest.c index b46a50b2dc..588c53537a 100644 --- a/source/util/src/tdigest.c +++ b/source/util/src/tdigest.c @@ -30,7 +30,7 @@ #include "tlog.h" #define INTERPOLATE(x, x0, x1) (((x) - (x0)) / ((x1) - (x0))) -//#define INTEGRATED_LOCATION(compression, q) ((compression) * (asin(2 * (q) - 1) + M_PI / 2) / M_PI) +// #define INTEGRATED_LOCATION(compression, q) ((compression) * (asin(2 * (q) - 1) + M_PI / 2) / M_PI) #define INTEGRATED_LOCATION(compression, q) ((compression) * (asin(2 * (double)(q)-1) / M_PI + (double)1 / 2)) #define FLOAT_EQ(f1, f2) (fabs((f1) - (f2)) <= FLT_EPSILON) @@ -99,16 +99,19 @@ static void mergeCentroid(SMergeArgs *args, SCentroid *merge) { } } -void tdigestCompress(TDigest *t) { +int32_t tdigestCompress(TDigest *t) { SCentroid *unmerged_centroids; int64_t unmerged_weight = 0; int32_t num_unmerged = t->num_buffered_pts; int32_t i, j; SMergeArgs args; - if (t->num_buffered_pts <= 0) return; + if (t->num_buffered_pts <= 0) return 0; unmerged_centroids = (SCentroid *)taosMemoryMalloc(sizeof(SCentroid) * t->num_buffered_pts); + if (unmerged_centroids == NULL) { + return terrno; + } for (i = 0; i < num_unmerged; i++) { SPt *p = t->buffered_pts + i; SCentroid *c = &unmerged_centroids[i]; @@ -122,6 +125,10 @@ void tdigestCompress(TDigest *t) { taosSort(unmerged_centroids, num_unmerged, sizeof(SCentroid), cmpCentroid); memset(&args, 0, sizeof(SMergeArgs)); args.centroids = (SCentroid *)taosMemoryMalloc((size_t)(sizeof(SCentroid) * t->size)); + if (args.centroids == NULL) { + taosMemoryFree((void *)unmerged_centroids); + return terrno; + } memset(args.centroids, 0, (size_t)(sizeof(SCentroid) * t->size)); args.t = t; @@ -167,10 +174,11 @@ void tdigestCompress(TDigest *t) { memcpy(t->centroids, args.centroids, sizeof(SCentroid) * t->num_centroids); taosMemoryFree((void *)args.centroids); + return 0; } -void tdigestAdd(TDigest *t, double x, int64_t w) { - if (w == 0) return; +int32_t tdigestAdd(TDigest *t, double x, int64_t w) { + if (w == 0) return 0; int32_t i = t->num_buffered_pts; if (i > 0 && t->buffered_pts[i - 1].value == x) { @@ -181,7 +189,10 @@ void tdigestAdd(TDigest *t, double x, int64_t w) { t->num_buffered_pts++; } - if (t->num_buffered_pts >= t->threshold) tdigestCompress(t); + if (t->num_buffered_pts >= t->threshold) { + return tdigestCompress(t); + } + return 0; } #if 0 @@ -284,16 +295,21 @@ double tdigestQuantile(TDigest *t, double q) { return t->max; } -void tdigestMerge(TDigest *t1, TDigest *t2) { +int32_t tdigestMerge(TDigest *t1, TDigest *t2) { + int32_t code = 0; // SPoints int32_t num_pts = t2->num_buffered_pts; for (int32_t i = num_pts - 1; i >= 0; i--) { SPt *p = t2->buffered_pts + i; - tdigestAdd(t1, p->value, p->weight); + code = tdigestAdd(t1, p->value, p->weight); + if (code) return code; t2->num_buffered_pts--; } // centroids for (int32_t i = 0; i < t2->num_centroids; i++) { - tdigestAdd(t1, t2->centroids[i].mean, t2->centroids[i].weight); + code = tdigestAdd(t1, t2->centroids[i].mean, t2->centroids[i].weight); + if (code) return code; } + + return 0; } diff --git a/source/util/src/tenv.c b/source/util/src/tenv.c index 7fe079ada4..539687878b 100644 --- a/source/util/src/tenv.c +++ b/source/util/src/tenv.c @@ -28,7 +28,7 @@ int32_t taosEnvNameToCfgName(const char *envNameStr, char *cfgNameStr, int32_t c // p[cfgNameMaxLen - 1] = '\0'; // return strlen(cfgNameStr); cfgNameStr[0] = '\0'; - return -1; + return TSDB_CODE_INVALID_PARA; } envNameStr += 5; if (*envNameStr != '\0') { @@ -55,7 +55,7 @@ int32_t taosEnvNameToCfgName(const char *envNameStr, char *cfgNameStr, int32_t c int32_t taosEnvToCfg(const char *envStr, char *cfgStr) { if (envStr == NULL || cfgStr == NULL) { - return -1; + return TSDB_CODE_INVALID_PARA; } if (cfgStr != envStr) strcpy(cfgStr, envStr); char *p = strchr(cfgStr, '='); @@ -74,7 +74,7 @@ int32_t taosEnvToCfg(const char *envStr, char *cfgStr) { memset(&cfgStr[cfgNameLen], ' ', p - cfgStr - cfgNameLen + 1); } else { *cfgStr = '\0'; - return -1; + return TSDB_CODE_INVALID_PARA; } } return strlen(cfgStr); diff --git a/source/util/src/tgeosctx.c b/source/util/src/tgeosctx.c index b60e9ee8ac..a05734c911 100644 --- a/source/util/src/tgeosctx.c +++ b/source/util/src/tgeosctx.c @@ -13,14 +13,12 @@ * along with this program. If not, see . */ -#include "tdef.h" #include "tgeosctx.h" +#include "tdef.h" static threadlocal SGeosContext tlGeosCtx = {0}; -SGeosContext* getThreadLocalGeosCtx() { - return &tlGeosCtx; -} +SGeosContext* getThreadLocalGeosCtx() { return &tlGeosCtx; } void destroyThreadLocalGeosCtx() { if (tlGeosCtx.WKTReader) { @@ -47,7 +45,7 @@ void destroyThreadLocalGeosCtx() { destroyRegexes(tlGeosCtx.WKTRegex, tlGeosCtx.WKTMatchData); } - if(tlGeosCtx.handle) { + if (tlGeosCtx.handle) { GEOS_finish_r(tlGeosCtx.handle); tlGeosCtx.handle = NULL; } diff --git a/source/util/src/thash.c b/source/util/src/thash.c index b509aaebc3..62a73cb38c 100644 --- a/source/util/src/thash.c +++ b/source/util/src/thash.c @@ -67,7 +67,7 @@ struct SHashObj { bool enableUpdate; // enable update SArray *pMemBlock; // memory block allocated for SHashEntry _hash_before_fn_t callbackFp; // function invoked before return the value to caller -// int64_t compTimes; + // int64_t compTimes; }; /* @@ -147,7 +147,7 @@ static FORCE_INLINE SHashNode *doSearchInEntryList(SHashObj *pHashObj, SHashEntr uint32_t hashVal) { SHashNode *pNode = pe->next; while (pNode) { - //atomic_add_fetch_64(&pHashObj->compTimes, 1); + // atomic_add_fetch_64(&pHashObj->compTimes, 1); if ((pNode->keyLen == keyLen) && ((*(pHashObj->equalFp))(GET_HASH_NODE_KEY(pNode), key, keyLen) == 0) && pNode->removed == 0) { break; @@ -243,7 +243,6 @@ SHashObj *taosHashInit(size_t capacity, _hash_fn_t fn, bool update, SHashLockTyp SHashObj *pHashObj = (SHashObj *)taosMemoryMalloc(sizeof(SHashObj)); if (pHashObj == NULL) { - terrno = TSDB_CODE_OUT_OF_MEMORY; return NULL; } @@ -262,7 +261,6 @@ SHashObj *taosHashInit(size_t capacity, _hash_fn_t fn, bool update, SHashLockTyp pHashObj->hashList = (SHashEntry **)taosMemoryMalloc(pHashObj->capacity * sizeof(void *)); if (pHashObj->hashList == NULL) { taosMemoryFree(pHashObj); - terrno = TSDB_CODE_OUT_OF_MEMORY; return NULL; } @@ -270,7 +268,6 @@ SHashObj *taosHashInit(size_t capacity, _hash_fn_t fn, bool update, SHashLockTyp if (pHashObj->pMemBlock == NULL) { taosMemoryFree(pHashObj->hashList); taosMemoryFree(pHashObj); - terrno = TSDB_CODE_OUT_OF_MEMORY; return NULL; } @@ -279,7 +276,6 @@ SHashObj *taosHashInit(size_t capacity, _hash_fn_t fn, bool update, SHashLockTyp taosArrayDestroy(pHashObj->pMemBlock); taosMemoryFree(pHashObj->hashList); taosMemoryFree(pHashObj); - terrno = TSDB_CODE_OUT_OF_MEMORY; return NULL; } @@ -290,7 +286,12 @@ SHashObj *taosHashInit(size_t capacity, _hash_fn_t fn, bool update, SHashLockTyp pHashObj->hashList[i]->next = NULL; } - taosArrayPush(pHashObj->pMemBlock, &p); + if (taosArrayPush(pHashObj->pMemBlock, &p) == NULL) { + taosArrayDestroy(pHashObj->pMemBlock); + taosMemoryFree(pHashObj->hashList); + taosMemoryFree(pHashObj); + return NULL; + } return pHashObj; } @@ -315,8 +316,7 @@ int32_t taosHashGetSize(const SHashObj *pHashObj) { int32_t taosHashPut(SHashObj *pHashObj, const void *key, size_t keyLen, const void *data, size_t size) { if (pHashObj == NULL || key == NULL || keyLen == 0) { - terrno = TSDB_CODE_INVALID_PTR; - return TSDB_CODE_INVALID_PTR; + return terrno = TSDB_CODE_INVALID_PTR; } uint32_t hashVal = (*pHashObj->hashFp)(key, (uint32_t)keyLen); @@ -331,7 +331,7 @@ int32_t taosHashPut(SHashObj *pHashObj, const void *key, size_t keyLen, const vo // disable resize taosHashRLock(pHashObj); - int32_t code = TSDB_CODE_SUCCESS; + int32_t code = TSDB_CODE_SUCCESS; uint32_t slot = HASH_INDEX(hashVal, pHashObj->capacity); SHashEntry *pe = pHashObj->hashList[slot]; @@ -440,14 +440,12 @@ void *taosHashGetImpl(SHashObj *pHashObj, const void *key, size_t keyLen, void * *size = pNode->dataLen; *d = taosMemoryCalloc(1, *size); if (*d == NULL) { - terrno = TSDB_CODE_OUT_OF_MEMORY; return NULL; } } else if (*size < pNode->dataLen) { *size = pNode->dataLen; char *tmp = taosMemoryRealloc(*d, *size); if (tmp == NULL) { - terrno = TSDB_CODE_OUT_OF_MEMORY; return NULL; } @@ -474,7 +472,7 @@ void *taosHashGetImpl(SHashObj *pHashObj, const void *key, size_t keyLen, void * int32_t taosHashRemove(SHashObj *pHashObj, const void *key, size_t keyLen) { if (pHashObj == NULL || taosHashTableEmpty(pHashObj) || key == NULL || keyLen == 0) { - return -1; + return TSDB_CODE_INVALID_PARA; } uint32_t hashVal = (*pHashObj->hashFp)(key, (uint32_t)keyLen); @@ -615,7 +613,7 @@ void taosHashTableResize(SHashObj *pHashObj) { return; } - int64_t st = taosGetTimestampUs(); + int64_t st = taosGetTimestampUs(); SHashEntry **pNewEntryList = taosMemoryRealloc(pHashObj->hashList, sizeof(SHashEntry *) * newCapacity); if (pNewEntryList == NULL) { // uDebug("cache resize failed due to out of memory, capacity remain:%zu", pHashObj->capacity); @@ -631,7 +629,10 @@ void taosHashTableResize(SHashObj *pHashObj) { pHashObj->hashList[i + pHashObj->capacity] = (void *)((char *)p + i * sizeof(SHashEntry)); } - taosArrayPush(pHashObj->pMemBlock, &p); + if (taosArrayPush(pHashObj->pMemBlock, &p) == NULL) { + taosMemoryFree(p); + return; + } pHashObj->capacity = newCapacity; for (int32_t idx = 0; idx < pHashObj->capacity; ++idx) { @@ -681,7 +682,6 @@ SHashNode *doCreateHashNode(const void *key, size_t keyLen, const void *pData, s #endif if (pNewNode == NULL) { - terrno = TSDB_CODE_OUT_OF_MEMORY; return NULL; } diff --git a/source/util/src/theap.c b/source/util/src/theap.c index 315ddf9367..40fe63b004 100644 --- a/source/util/src/theap.c +++ b/source/util/src/theap.c @@ -188,7 +188,6 @@ void heapRemove(Heap* heap, HeapNode* node) { void heapDequeue(Heap* heap) { heapRemove(heap, heap->min); } - struct PriorityQueue { SArray* container; pq_comp_fn fn; @@ -197,16 +196,23 @@ struct PriorityQueue { }; PriorityQueue* createPriorityQueue(pq_comp_fn fn, FDelete deleteFn, void* param) { PriorityQueue* pq = (PriorityQueue*)taosMemoryCalloc(1, sizeof(PriorityQueue)); + if (pq == NULL) { + return NULL; + } + pq->container = taosArrayInit(1, sizeof(PriorityQueueNode)); + if (pq->container == NULL) { + taosMemoryFree(pq); + return NULL; + } + pq->fn = fn; pq->deleteFn = deleteFn; pq->param = param; return pq; } -void taosPQSetFn(PriorityQueue* pq, pq_comp_fn fn) { - pq->fn = fn; -} +void taosPQSetFn(PriorityQueue* pq, pq_comp_fn fn) { pq->fn = fn; } void destroyPriorityQueue(PriorityQueue* pq) { if (pq->deleteFn) @@ -218,15 +224,15 @@ void destroyPriorityQueue(PriorityQueue* pq) { static size_t pqParent(size_t i) { return (--i) >> 1; /* (i - 1) / 2 */ } static size_t pqLeft(size_t i) { return (i << 1) | 1; /* i * 2 + 1 */ } -static size_t pqRight(size_t i) { return (++i) << 1; /* (i + 1) * 2 */} -static void pqSwapPQNode(PriorityQueueNode* a, PriorityQueueNode* b) { - void * tmp = a->data; +static size_t pqRight(size_t i) { return (++i) << 1; /* (i + 1) * 2 */ } +static void pqSwapPQNode(PriorityQueueNode* a, PriorityQueueNode* b) { + void* tmp = a->data; a->data = b->data; b->data = tmp; } #define pqContainerGetEle(pq, i) ((PriorityQueueNode*)taosArrayGet((pq)->container, (i))) -#define pqContainerSize(pq) (taosArrayGetSize((pq)->container)) +#define pqContainerSize(pq) (taosArrayGetSize((pq)->container)) size_t taosPQSize(PriorityQueue* pq) { return pqContainerSize(pq); } @@ -288,12 +294,12 @@ static void pqRemove(PriorityQueue* pq, size_t i) { pqUpdate(pq, i); } -PriorityQueueNode* taosPQTop(PriorityQueue* pq) { - return pqContainerGetEle(pq, 0); -} +PriorityQueueNode* taosPQTop(PriorityQueue* pq) { return pqContainerGetEle(pq, 0); } PriorityQueueNode* taosPQPush(PriorityQueue* pq, const PriorityQueueNode* node) { - taosArrayPush(pq->container, node); + if (taosArrayPush(pq->container, node) == NULL) { + return NULL; + } return pqReverseHeapify(pq, pqContainerSize(pq) - 1); } @@ -310,15 +316,28 @@ struct BoundedQueue { BoundedQueue* createBoundedQueue(uint32_t maxSize, pq_comp_fn fn, FDelete deleteFn, void* param) { BoundedQueue* q = (BoundedQueue*)taosMemoryCalloc(1, sizeof(BoundedQueue)); + if (q == NULL) { + return NULL; + } + q->queue = createPriorityQueue(fn, deleteFn, param); - taosArrayEnsureCap(q->queue->container, maxSize + 1); + if (q->queue == NULL) { + taosMemoryFree(q); + return NULL; + } + + int32_t code = taosArrayEnsureCap(q->queue->container, maxSize + 1); + if (code) { + destroyPriorityQueue(q->queue); + taosMemoryFree(q); + terrno = code; + return NULL; + } q->maxSize = maxSize; return q; } -void taosBQSetFn(BoundedQueue* q, pq_comp_fn fn) { - taosPQSetFn(q->queue, fn); -} +void taosBQSetFn(BoundedQueue* q, pq_comp_fn fn) { taosPQSetFn(q->queue, fn); } void destroyBoundedQueue(BoundedQueue* q) { if (!q) return; @@ -343,22 +362,12 @@ PriorityQueueNode* taosBQPush(BoundedQueue* q, PriorityQueueNode* n) { } } -PriorityQueueNode* taosBQTop(BoundedQueue* q) { - return taosPQTop(q->queue); -} +PriorityQueueNode* taosBQTop(BoundedQueue* q) { return taosPQTop(q->queue); } -void taosBQBuildHeap(BoundedQueue *q) { - pqBuildHeap(q->queue); -} +void taosBQBuildHeap(BoundedQueue* q) { pqBuildHeap(q->queue); } -size_t taosBQMaxSize(BoundedQueue* q) { - return q->maxSize; -} +size_t taosBQMaxSize(BoundedQueue* q) { return q->maxSize; } -size_t taosBQSize(BoundedQueue* q) { - return taosPQSize(q->queue); -} +size_t taosBQSize(BoundedQueue* q) { return taosPQSize(q->queue); } -void taosBQPop(BoundedQueue* q) { - taosPQPop(q->queue); -} +void taosBQPop(BoundedQueue* q) { taosPQPop(q->queue); } diff --git a/source/util/src/tidpool.c b/source/util/src/tidpool.c index b45113942c..cfb2be5f02 100644 --- a/source/util/src/tidpool.c +++ b/source/util/src/tidpool.c @@ -19,7 +19,9 @@ void *taosInitIdPool(int32_t maxId) { id_pool_t *pIdPool = taosMemoryCalloc(1, sizeof(id_pool_t)); - if (pIdPool == NULL) return NULL; + if (pIdPool == NULL) { + return NULL; + } pIdPool->freeList = taosMemoryCalloc(maxId, sizeof(bool)); if (pIdPool->freeList == NULL) { @@ -120,7 +122,7 @@ int32_t taosUpdateIdPool(id_pool_t *pIdPool, int32_t maxId) { bool *idList = taosMemoryCalloc(maxId, sizeof(bool)); if (idList == NULL) { - return -1; + return terrno; } taosThreadMutexLock(&pIdPool->mutex); diff --git a/source/util/src/tref.c b/source/util/src/tref.c index 1bd3099b2d..5583286280 100644 --- a/source/util/src/tref.c +++ b/source/util/src/tref.c @@ -67,15 +67,13 @@ int32_t taosOpenRef(int32_t max, RefFp fp) { nodeList = taosMemoryCalloc(sizeof(SRefNode *), (size_t)max); if (nodeList == NULL) { - terrno = TSDB_CODE_OUT_OF_MEMORY; - return -1; + return terrno = TSDB_CODE_OUT_OF_MEMORY; } lockedBy = taosMemoryCalloc(sizeof(int64_t), (size_t)max); if (lockedBy == NULL) { taosMemoryFree(nodeList); - terrno = TSDB_CODE_OUT_OF_MEMORY; - return -1; + return terrno = TSDB_CODE_OUT_OF_MEMORY; } taosThreadMutexLock(&tsRefMutex); @@ -118,8 +116,7 @@ int32_t taosCloseRef(int32_t rsetId) { if (rsetId < 0 || rsetId >= TSDB_REF_OBJECTS) { uTrace("rsetId:%d is invalid, out of range", rsetId); - terrno = TSDB_CODE_REF_INVALID_ID; - return -1; + return terrno = TSDB_CODE_REF_INVALID_ID; } pSet = tsRefSetList + rsetId; @@ -149,8 +146,7 @@ int64_t taosAddRef(int32_t rsetId, void *p) { if (rsetId < 0 || rsetId >= TSDB_REF_OBJECTS) { uTrace("rsetId:%d p:%p failed to add, rsetId not valid", rsetId, p); - terrno = TSDB_CODE_REF_INVALID_ID; - return -1; + return terrno = TSDB_CODE_REF_INVALID_ID; } pSet = tsRefSetList + rsetId; @@ -158,14 +154,12 @@ int64_t taosAddRef(int32_t rsetId, void *p) { if (pSet->state != TSDB_REF_STATE_ACTIVE) { taosDecRsetCount(pSet); uTrace("rsetId:%d p:%p failed to add, not active", rsetId, p); - terrno = TSDB_CODE_REF_ID_REMOVED; - return -1; + return terrno = TSDB_CODE_REF_ID_REMOVED; } pNode = taosMemoryCalloc(sizeof(SRefNode), 1); if (pNode == NULL) { - terrno = TSDB_CODE_OUT_OF_MEMORY; - return -1; + return terrno = TSDB_CODE_OUT_OF_MEMORY; } rid = atomic_add_fetch_64(&pSet->rid, 1); @@ -389,21 +383,18 @@ static int32_t taosDecRefCount(int32_t rsetId, int64_t rid, int32_t remove) { if (rsetId < 0 || rsetId >= TSDB_REF_OBJECTS) { uTrace("rsetId:%d rid:%" PRId64 " failed to remove, rsetId not valid", rsetId, rid); - terrno = TSDB_CODE_REF_INVALID_ID; - return -1; + return terrno = TSDB_CODE_REF_INVALID_ID; } if (rid <= 0) { uTrace("rsetId:%d rid:%" PRId64 " failed to remove, rid not valid", rsetId, rid); - terrno = TSDB_CODE_REF_NOT_EXIST; - return -1; + return terrno = TSDB_CODE_REF_NOT_EXIST; } pSet = tsRefSetList + rsetId; if (pSet->state == TSDB_REF_STATE_EMPTY) { uTrace("rsetId:%d rid:%" PRId64 " failed to remove, cleaned", rsetId, rid); - terrno = TSDB_CODE_REF_ID_REMOVED; - return -1; + return terrno = TSDB_CODE_REF_ID_REMOVED; } hash = rid % pSet->max;