提交 b41f8e17 编写于 作者: S slguan

Merge branch 'refact/slguan' of https://github.com/taosdata/TDengine into refact/slguan

......@@ -56,7 +56,7 @@ void* taosArrayPush(SArray* pArray, void* pData);
void taosArrayPop(SArray* pArray);
/**
*
* get the data from array
* @param pArray
* @param index
* @return
......@@ -64,25 +64,33 @@ void taosArrayPop(SArray* pArray);
void* taosArrayGet(SArray* pArray, size_t index);
/**
*
* get the pointer data from the array
* @param pArray
* @param index
* @return
*/
void* taosArrayGetP(SArray* pArray, size_t index);
/**
* return the size of array
* @param pArray
* @return
*/
size_t taosArrayGetSize(SArray* pArray);
/**
*
* insert data into array
* @param pArray
* @param index
* @param pData
*/
void taosArrayInsert(SArray* pArray, int32_t index, void* pData);
void* taosArrayInsert(SArray* pArray, size_t index, void* pData);
/**
*
* @param pArray
*/
void taosArrayDestory(SArray* pArray);
void taosArrayDestroy(SArray* pArray);
#ifdef __cplusplus
}
......
......@@ -38,19 +38,21 @@ void* taosArrayInit(size_t size, size_t elemSize) {
return pArray;
}
static void taosArrayResize(SArray* pArray) {
static int32_t taosArrayResize(SArray* pArray) {
assert(pArray->size >= pArray->capacity);
size_t size = pArray->capacity;
size = (size << 1u);
void* tmp = realloc(pArray->pData, size * pArray->elemSize);
if (tmp == NULL) {
// todo
if (tmp == NULL) { // reallocate failed, the original buffer remains
return -1;
}
pArray->pData = tmp;
pArray->capacity = size;
return 0;
}
void* taosArrayPush(SArray* pArray, void* pData) {
......@@ -59,7 +61,12 @@ void* taosArrayPush(SArray* pArray, void* pData) {
}
if (pArray->size >= pArray->capacity) {
taosArrayResize(pArray);
int32_t ret = taosArrayResize(pArray);
// failed to push data into buffer due to the failure of memory allocation
if (ret != 0) {
return NULL;
}
}
void* dst = TARRAY_GET_ELEM(pArray, pArray->size);
......@@ -82,20 +89,32 @@ void* taosArrayGet(SArray* pArray, size_t index) {
return TARRAY_GET_ELEM(pArray, index);
}
void* taosArrayGetP(SArray* pArray, size_t index) {
void* ret = taosArrayGet(pArray, index);
if (ret == NULL) {
return NULL;
}
return *(void**)ret;
}
size_t taosArrayGetSize(SArray* pArray) { return pArray->size; }
void taosArrayInsert(SArray* pArray, int32_t index, void* pData) {
void* taosArrayInsert(SArray* pArray, size_t index, void* pData) {
if (pArray == NULL || pData == NULL) {
return;
return NULL;
}
if (index >= pArray->size) {
taosArrayPush(pArray, pData);
return;
return taosArrayPush(pArray, pData);
}
if (pArray->size >= pArray->capacity) {
taosArrayResize(pArray);
int32_t ret = taosArrayResize(pArray);
if (ret < 0) {
return NULL;
}
}
void* dst = TARRAY_GET_ELEM(pArray, index);
......@@ -105,9 +124,11 @@ void taosArrayInsert(SArray* pArray, int32_t index, void* pData) {
memcpy(dst, pData, pArray->elemSize);
pArray->size += 1;
return dst;
}
void taosArrayDestory(SArray* pArray) {
void taosArrayDestroy(SArray* pArray) {
if (pArray == NULL) {
return;
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册