/src/tdengine/source/util/src/mpDirect.c
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 | | #define _DEFAULT_SOURCE |
17 | | #include "osMemPool.h" |
18 | | #include "tmempoolInt.h" |
19 | | #include "tlog.h" |
20 | | #include "tutil.h" |
21 | | |
22 | 0 | void* mpDirectAlloc(SMemPool* pPool, SMPJob* pJob, int64_t size) { |
23 | 0 | MP_CHECK_QUOTA(pPool, pJob, size); |
24 | | |
25 | 0 | return taosMemMalloc(size); |
26 | 0 | } |
27 | | |
28 | | |
29 | 0 | void* mpDirectAlignAlloc(SMemPool* pPool, SMPJob* pJob, uint32_t alignment, int64_t size) { |
30 | 0 | MP_CHECK_QUOTA(pPool, pJob, size); |
31 | | |
32 | 0 | return taosMemMallocAlign(alignment, size); |
33 | 0 | } |
34 | | |
35 | | |
36 | 0 | void* mpDirectCalloc(SMemPool* pPool, SMPJob* pJob, int64_t num, int64_t size) { |
37 | 0 | int64_t tSize = num * size; |
38 | 0 | MP_CHECK_QUOTA(pPool, pJob, tSize); |
39 | | |
40 | 0 | return taosMemCalloc(num, size); |
41 | 0 | } |
42 | | |
43 | 0 | void mpDirectFree(SMemPool* pPool, SMPJob* pJob, void *ptr) { |
44 | 0 | if (*pPool->cfg.jobQuota > 0) { |
45 | 0 | (void)atomic_sub_fetch_64(&pJob->job.allocMemSize, taosMemSize(ptr)); |
46 | 0 | } |
47 | 0 | taosMemFree(ptr); |
48 | 0 | } |
49 | | |
50 | | |
51 | 0 | void* mpDirectRealloc(SMemPool* pPool, SMPJob* pJob, void* ptr, int64_t size) { |
52 | 0 | int32_t code = TSDB_CODE_SUCCESS; |
53 | |
|
54 | 0 | if (NULL == ptr) { |
55 | 0 | return mpDirectAlloc(pPool, pJob, size); |
56 | 0 | } |
57 | | |
58 | 0 | if (0 == size) { |
59 | 0 | mpDirectFree(pPool, pJob, ptr); |
60 | 0 | return NULL; |
61 | 0 | } |
62 | | |
63 | 0 | int64_t oSize = taosMemSize(ptr); |
64 | |
|
65 | 0 | MP_CHECK_QUOTA(pPool, pJob, size - oSize); |
66 | | |
67 | 0 | return taosMemRealloc(ptr, size); |
68 | 0 | } |
69 | | |
70 | 0 | void* mpDirectStrdup(SMemPool* pPool, SMPJob* pJob, const void* ptr) { |
71 | 0 | if (NULL == ptr) { |
72 | 0 | return NULL; |
73 | 0 | } |
74 | | |
75 | 0 | int64_t oSize = strlen(ptr); |
76 | 0 | MP_CHECK_QUOTA(pPool, pJob, oSize); |
77 | | |
78 | 0 | return taosStrdupi(ptr); |
79 | 0 | } |
80 | | |
81 | 0 | void* mpDirectStrndup(SMemPool* pPool, SMPJob* pJob, const void* ptr, int64_t size) { |
82 | 0 | if (NULL == ptr) { |
83 | 0 | return NULL; |
84 | 0 | } |
85 | | |
86 | 0 | int64_t oSize = strlen(ptr); |
87 | 0 | MP_CHECK_QUOTA(pPool, pJob, TMIN(oSize, size) + 1); |
88 | | |
89 | 0 | return taosStrndupi(ptr, size); |
90 | 0 | } |
91 | | |
92 | | |
93 | | |
94 | | |
95 | 0 | int64_t mpDirectGetMemSize(SMemPool* pPool, SMPSession* pSession, void *ptr) { |
96 | 0 | return taosMemSize(ptr); |
97 | 0 | } |
98 | | |
99 | 0 | void mpDirectFullFree(SMemPool* pPool, SMPSession* pSession, void *ptr, int64_t* origSize) { |
100 | 0 | int64_t oSize = taosMemSize(ptr); |
101 | 0 | if (origSize) { |
102 | 0 | *origSize = oSize; |
103 | 0 | } |
104 | | |
105 | 0 | MP_LOCK(MP_READ, &pPool->cfgLock); // tmp test |
106 | |
|
107 | 0 | taosMemFree(ptr); |
108 | |
|
109 | 0 | if (NULL != pSession) { |
110 | 0 | (void)atomic_sub_fetch_64(&pSession->allocMemSize, oSize); |
111 | 0 | (void)atomic_sub_fetch_64(&pSession->pJob->job.allocMemSize, oSize); |
112 | 0 | } |
113 | | |
114 | 0 | (void)atomic_sub_fetch_64(&pPool->allocMemSize, oSize); |
115 | |
|
116 | 0 | MP_UNLOCK(MP_READ, &pPool->cfgLock); |
117 | 0 | } |
118 | | |
119 | | |
120 | | |
121 | 0 | int32_t mpDirectFullAlloc(SMemPool* pPool, SMPSession* pSession, int64_t* size, uint32_t alignment, void** ppRes) { |
122 | 0 | int32_t code = TSDB_CODE_SUCCESS; |
123 | 0 | void* res = NULL; |
124 | 0 | int64_t nSize = *size; |
125 | | |
126 | 0 | MP_LOCK(MP_READ, &pPool->cfgLock); |
127 | |
|
128 | 0 | MP_ERR_JRET(mpChkFullQuota(pPool, pSession, *size)); |
129 | | |
130 | 0 | res = alignment ? taosMemMallocAlign(alignment, *size) : taosMemMalloc(*size); |
131 | 0 | if (NULL != res) { |
132 | 0 | nSize = taosMemSize(res); |
133 | 0 | mpUpdateAllocSize(pPool, pSession, nSize, nSize - *size); |
134 | 0 | } else { |
135 | 0 | if (NULL != pSession) { |
136 | 0 | (void)atomic_sub_fetch_64(&pSession->pJob->job.allocMemSize, *size); |
137 | 0 | } |
138 | | |
139 | 0 | (void)atomic_sub_fetch_64(&pPool->allocMemSize, *size); |
140 | | |
141 | 0 | uError("malloc %" PRId64 " alignment %d failed, code: 0x%x", *size, alignment, terrno); |
142 | |
|
143 | 0 | code = terrno; |
144 | 0 | } |
145 | |
|
146 | 0 | _return: |
147 | |
|
148 | 0 | MP_UNLOCK(MP_READ, &pPool->cfgLock); |
149 | | |
150 | 0 | *ppRes = res; |
151 | 0 | *size = nSize; |
152 | | |
153 | 0 | MP_RET(code); |
154 | 0 | } |
155 | | |
156 | 0 | int32_t mpDirectFullRealloc(SMemPool* pPool, SMPSession* pSession, void **pPtr, int64_t* size, int64_t* origSize) { |
157 | 0 | int32_t code = TSDB_CODE_SUCCESS; |
158 | 0 | int64_t nSize = *size; |
159 | |
|
160 | 0 | MP_LOCK(MP_READ, &pPool->cfgLock); |
161 | |
|
162 | 0 | MP_ERR_JRET(mpChkFullQuota(pPool, pSession, *size - *origSize)); |
163 | | |
164 | 0 | *pPtr = taosMemRealloc(*pPtr, *size); |
165 | 0 | if (NULL != *pPtr) { |
166 | 0 | nSize = taosMemSize(*pPtr); |
167 | 0 | mpUpdateAllocSize(pPool, pSession, nSize - *origSize, nSize - *size + *origSize); |
168 | 0 | } else { |
169 | 0 | MP_ERR_JRET(terrno); |
170 | 0 | } |
171 | | |
172 | 0 | _return: |
173 | |
|
174 | 0 | MP_UNLOCK(MP_READ, &pPool->cfgLock); |
175 | |
|
176 | 0 | if (code) { |
177 | 0 | mpDirectFullFree(pPool, pSession, *pPtr, origSize); |
178 | 0 | *pPtr = NULL; |
179 | 0 | } |
180 | |
|
181 | 0 | *size = nSize; |
182 | | |
183 | 0 | return TSDB_CODE_SUCCESS; |
184 | 0 | } |
185 | | |
186 | 0 | int32_t mpDirectTrim(SMemPool* pPool, SMPSession* pSession, int32_t size, bool* trimed) { |
187 | 0 | return taosMemTrim(size, trimed); |
188 | 0 | } |
189 | | |
190 | | |