[TD-13758]<fix>: add memory option.

This commit is contained in:
afwerar 2022-03-28 14:48:57 +08:00
parent 47fe8bd902
commit 6baa845db3
2 changed files with 24 additions and 0 deletions

View File

@ -13,6 +13,9 @@ find_path(IconvApiIncludes iconv.h PATHS)
if(NOT IconvApiIncludes) if(NOT IconvApiIncludes)
add_definitions(-DDISALLOW_NCHAR_WITHOUT_ICONV) add_definitions(-DDISALLOW_NCHAR_WITHOUT_ICONV)
endif () endif ()
if(USE_TD_MEMORY)
add_definitions(-DUSE_TD_MEMORY)
endif ()
target_link_libraries( target_link_libraries(
os pthread dl rt m os pthread dl rt m
) )

View File

@ -14,6 +14,7 @@
*/ */
#define ALLOW_FORBID_FUNC #define ALLOW_FORBID_FUNC
#include <malloc.h>
#include "os.h" #include "os.h"
#define TD_MEMORY_SYMBOL ('T'<<24|'A'<<16|'O'<<8|'S') #define TD_MEMORY_SYMBOL ('T'<<24|'A'<<16|'O'<<8|'S')
@ -68,6 +69,7 @@ int32_t taosBackTrace(void **buffer, int32_t size) {
// } // }
void *taosMemoryMalloc(int32_t size) { void *taosMemoryMalloc(int32_t size) {
#ifdef USE_TD_MEMORY
void *tmp = malloc(size + sizeof(TdMemoryInfo)); void *tmp = malloc(size + sizeof(TdMemoryInfo));
if (tmp == NULL) return NULL; if (tmp == NULL) return NULL;
@ -77,9 +79,13 @@ void *taosMemoryMalloc(int32_t size) {
taosBackTrace(pTdMemoryInfo->stackTrace,TD_MEMORY_STACK_TRACE_DEPTH); taosBackTrace(pTdMemoryInfo->stackTrace,TD_MEMORY_STACK_TRACE_DEPTH);
return (char*)tmp + sizeof(TdMemoryInfo); return (char*)tmp + sizeof(TdMemoryInfo);
#else
return malloc(size);
#endif
} }
void *taosMemoryCalloc(int32_t num, int32_t size) { void *taosMemoryCalloc(int32_t num, int32_t size) {
#ifdef USE_TD_MEMORY
int32_t memorySize = num * size; int32_t memorySize = num * size;
char *tmp = calloc(memorySize + sizeof(TdMemoryInfo), 1); char *tmp = calloc(memorySize + sizeof(TdMemoryInfo), 1);
if (tmp == NULL) return NULL; if (tmp == NULL) return NULL;
@ -90,9 +96,13 @@ void *taosMemoryCalloc(int32_t num, int32_t size) {
taosBackTrace(pTdMemoryInfo->stackTrace,TD_MEMORY_STACK_TRACE_DEPTH); taosBackTrace(pTdMemoryInfo->stackTrace,TD_MEMORY_STACK_TRACE_DEPTH);
return (char*)tmp + sizeof(TdMemoryInfo); return (char*)tmp + sizeof(TdMemoryInfo);
#else
return calloc(num, size);
#endif
} }
void *taosMemoryRealloc(void *ptr, int32_t size) { void *taosMemoryRealloc(void *ptr, int32_t size) {
#ifdef USE_TD_MEMORY
if (ptr == NULL) return taosMemoryMalloc(size); if (ptr == NULL) return taosMemoryMalloc(size);
TdMemoryInfoPtr pTdMemoryInfo = (TdMemoryInfoPtr)((char*)ptr - sizeof(TdMemoryInfo)); TdMemoryInfoPtr pTdMemoryInfo = (TdMemoryInfoPtr)((char*)ptr - sizeof(TdMemoryInfo));
@ -108,9 +118,13 @@ void *taosMemoryRealloc(void *ptr, int32_t size) {
((TdMemoryInfoPtr)tmp)->memorySize = size; ((TdMemoryInfoPtr)tmp)->memorySize = size;
return (char*)tmp + sizeof(TdMemoryInfo); return (char*)tmp + sizeof(TdMemoryInfo);
#else
return realloc(ptr, size);
#endif
} }
void taosMemoryFree(const void *ptr) { void taosMemoryFree(const void *ptr) {
#ifdef USE_TD_MEMORY
if (ptr == NULL) return; if (ptr == NULL) return;
TdMemoryInfoPtr pTdMemoryInfo = (TdMemoryInfoPtr)((char*)ptr - sizeof(TdMemoryInfo)); TdMemoryInfoPtr pTdMemoryInfo = (TdMemoryInfoPtr)((char*)ptr - sizeof(TdMemoryInfo));
@ -121,13 +135,20 @@ void taosMemoryFree(const void *ptr) {
} else { } else {
free((void*)ptr); free((void*)ptr);
} }
#else
return free((void*)ptr);
#endif
} }
int32_t taosMemorySize(void *ptr) { int32_t taosMemorySize(void *ptr) {
#ifdef USE_TD_MEMORY
if (ptr == NULL) return 0; if (ptr == NULL) return 0;
TdMemoryInfoPtr pTdMemoryInfo = (TdMemoryInfoPtr)((char*)ptr - sizeof(TdMemoryInfo)); TdMemoryInfoPtr pTdMemoryInfo = (TdMemoryInfoPtr)((char*)ptr - sizeof(TdMemoryInfo));
assert(pTdMemoryInfo->symbol == TD_MEMORY_SYMBOL); assert(pTdMemoryInfo->symbol == TD_MEMORY_SYMBOL);
return pTdMemoryInfo->memorySize; return pTdMemoryInfo->memorySize;
#else
return malloc_usable_size(ptr);
#endif
} }