TDengine/include/util/tRealloc.h

68 lines
1.7 KiB
C
Raw Normal View History

2022-07-04 11:24:54 +00:00
/*
* Copyright (c) 2019 TAOS Data, Inc. <jhtao@taosdata.com>
*
* 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 <http://www.gnu.org/licenses/>.
*/
#ifndef _TD_UTIL_TREALLOC_H_
#define _TD_UTIL_TREALLOC_H_
#include "os.h"
#ifdef __cplusplus
extern "C" {
#endif
static FORCE_INLINE int32_t tRealloc(uint8_t **ppBuf, int64_t size) {
int32_t code = 0;
int64_t bsize = 0;
2022-07-10 06:01:30 +00:00
uint8_t *pBuf = NULL;
2022-07-04 11:24:54 +00:00
if (*ppBuf) {
2022-07-10 06:01:30 +00:00
pBuf = (*ppBuf) - sizeof(int64_t);
bsize = *(int64_t *)pBuf;
2022-07-04 11:24:54 +00:00
}
if (bsize >= size) goto _exit;
if (bsize == 0) bsize = 64;
while (bsize < size) {
bsize *= 2;
}
2022-07-10 06:01:30 +00:00
pBuf = (uint8_t *)taosMemoryRealloc(pBuf, bsize + sizeof(int64_t));
2022-07-04 11:24:54 +00:00
if (pBuf == NULL) {
2024-07-24 05:58:17 +00:00
code = terrno;
2022-07-04 11:24:54 +00:00
goto _exit;
}
*(int64_t *)pBuf = bsize;
*ppBuf = pBuf + sizeof(int64_t);
_exit:
return code;
}
2022-12-01 07:36:10 +00:00
#define tFree(BUF) \
do { \
if (BUF) { \
taosMemoryFree((uint8_t *)(BUF) - sizeof(int64_t)); \
(BUF) = NULL; \
} \
} while (0)
2022-07-04 11:24:54 +00:00
#ifdef __cplusplus
}
#endif
2022-07-05 03:03:09 +00:00
#endif /*_TD_UTIL_TREALLOC_H_*/