/src/tdengine/include/util/tRealloc.h
Line | Count | Source |
1 | | /* |
2 | | * Copyright (c) 2019 TAOS Data, Inc. <jhtao@taosdata.com> |
3 | | * |
4 | | * This program is free software: you can use, redistribute, and/or modify |
5 | | * it under the terms of the GNU Affero General Public License, version 3 |
6 | | * or later ("AGPL"), as published by the Free Software Foundation. |
7 | | * |
8 | | * This program is distributed in the hope that it will be useful, but WITHOUT |
9 | | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
10 | | * FITNESS FOR A PARTICULAR PURPOSE. |
11 | | * |
12 | | * You should have received a copy of the GNU Affero General Public License |
13 | | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
14 | | */ |
15 | | |
16 | | #ifndef _TD_UTIL_TREALLOC_H_ |
17 | | #define _TD_UTIL_TREALLOC_H_ |
18 | | |
19 | | #include "os.h" |
20 | | |
21 | | #ifdef __cplusplus |
22 | | extern "C" { |
23 | | #endif |
24 | | |
25 | 0 | static FORCE_INLINE int32_t tRealloc(uint8_t **ppBuf, int64_t size) { |
26 | 0 | int32_t code = 0; |
27 | 0 | int64_t bsize = 0; |
28 | 0 | uint8_t *pBuf = NULL; |
29 | |
|
30 | 0 | if (*ppBuf) { |
31 | 0 | pBuf = (*ppBuf) - sizeof(int64_t); |
32 | 0 | bsize = *(int64_t *)pBuf; |
33 | 0 | } |
34 | |
|
35 | 0 | if (bsize >= size) goto _exit; |
36 | | |
37 | 0 | if (bsize == 0) bsize = 64; |
38 | 0 | while (bsize < size) { |
39 | 0 | bsize *= 2; |
40 | 0 | } |
41 | |
|
42 | 0 | pBuf = (uint8_t *)taosMemoryRealloc(pBuf, bsize + sizeof(int64_t)); |
43 | 0 | if (pBuf == NULL) { |
44 | 0 | code = terrno; |
45 | 0 | goto _exit; |
46 | 0 | } |
47 | | |
48 | 0 | *(int64_t *)pBuf = bsize; |
49 | 0 | *ppBuf = pBuf + sizeof(int64_t); |
50 | |
|
51 | 0 | _exit: |
52 | 0 | return code; |
53 | 0 | } Unexecuted instantiation: parInsertUtil.c:tRealloc Unexecuted instantiation: tdataformat.c:tRealloc Unexecuted instantiation: tmsg.c:tRealloc |
54 | | |
55 | | #define tFree(BUF) \ |
56 | 0 | do { \ |
57 | 0 | if (BUF) { \ |
58 | 0 | taosMemoryFree((uint8_t *)(BUF) - sizeof(int64_t)); \ |
59 | 0 | (BUF) = NULL; \ |
60 | 0 | } \ |
61 | 0 | } while (0) |
62 | | |
63 | | #ifdef __cplusplus |
64 | | } |
65 | | #endif |
66 | | |
67 | | #endif /*_TD_UTIL_TREALLOC_H_*/ |