From 6baa845db313b52211d31f3f719ecc560b019a52 Mon Sep 17 00:00:00 2001 From: afwerar <1296468573@qq.com> Date: Mon, 28 Mar 2022 14:48:57 +0800 Subject: [PATCH] [TD-13758]: add memory option. --- source/os/CMakeLists.txt | 3 +++ source/os/src/osMemory.c | 21 +++++++++++++++++++++ 2 files changed, 24 insertions(+) diff --git a/source/os/CMakeLists.txt b/source/os/CMakeLists.txt index eea3903911..c467ab5fa3 100644 --- a/source/os/CMakeLists.txt +++ b/source/os/CMakeLists.txt @@ -13,6 +13,9 @@ find_path(IconvApiIncludes iconv.h PATHS) if(NOT IconvApiIncludes) add_definitions(-DDISALLOW_NCHAR_WITHOUT_ICONV) endif () +if(USE_TD_MEMORY) + add_definitions(-DUSE_TD_MEMORY) +endif () target_link_libraries( os pthread dl rt m ) diff --git a/source/os/src/osMemory.c b/source/os/src/osMemory.c index 12e89fdd73..3a76399812 100644 --- a/source/os/src/osMemory.c +++ b/source/os/src/osMemory.c @@ -14,6 +14,7 @@ */ #define ALLOW_FORBID_FUNC +#include #include "os.h" #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) { +#ifdef USE_TD_MEMORY void *tmp = malloc(size + sizeof(TdMemoryInfo)); if (tmp == NULL) return NULL; @@ -77,9 +79,13 @@ void *taosMemoryMalloc(int32_t size) { taosBackTrace(pTdMemoryInfo->stackTrace,TD_MEMORY_STACK_TRACE_DEPTH); return (char*)tmp + sizeof(TdMemoryInfo); +#else + return malloc(size); +#endif } void *taosMemoryCalloc(int32_t num, int32_t size) { +#ifdef USE_TD_MEMORY int32_t memorySize = num * size; char *tmp = calloc(memorySize + sizeof(TdMemoryInfo), 1); 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); return (char*)tmp + sizeof(TdMemoryInfo); +#else + return calloc(num, size); +#endif } void *taosMemoryRealloc(void *ptr, int32_t size) { +#ifdef USE_TD_MEMORY if (ptr == NULL) return taosMemoryMalloc(size); TdMemoryInfoPtr pTdMemoryInfo = (TdMemoryInfoPtr)((char*)ptr - sizeof(TdMemoryInfo)); @@ -108,9 +118,13 @@ void *taosMemoryRealloc(void *ptr, int32_t size) { ((TdMemoryInfoPtr)tmp)->memorySize = size; return (char*)tmp + sizeof(TdMemoryInfo); +#else + return realloc(ptr, size); +#endif } void taosMemoryFree(const void *ptr) { +#ifdef USE_TD_MEMORY if (ptr == NULL) return; TdMemoryInfoPtr pTdMemoryInfo = (TdMemoryInfoPtr)((char*)ptr - sizeof(TdMemoryInfo)); @@ -121,13 +135,20 @@ void taosMemoryFree(const void *ptr) { } else { free((void*)ptr); } +#else + return free((void*)ptr); +#endif } int32_t taosMemorySize(void *ptr) { +#ifdef USE_TD_MEMORY if (ptr == NULL) return 0; TdMemoryInfoPtr pTdMemoryInfo = (TdMemoryInfoPtr)((char*)ptr - sizeof(TdMemoryInfo)); assert(pTdMemoryInfo->symbol == TD_MEMORY_SYMBOL); return pTdMemoryInfo->memorySize; +#else + return malloc_usable_size(ptr); +#endif } -- GitLab