Merge pull request #11050 from taosdata/fix/ZhiqiangWang/TD-13758-add-memory-option

[TD-13758]<fix>: add memory option.
This commit is contained in:
Zhiqiang Wang 2022-03-28 15:15:01 +08:00 committed by GitHub
commit 577743635b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 27 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')
@ -32,6 +33,7 @@ typedef struct TdMemoryInfo
#else #else
#include<execinfo.h> #include<execinfo.h>
#define STACKCALL __attribute__((regparm(1), noinline)) #define STACKCALL __attribute__((regparm(1), noinline))
void **STACKCALL taosGetEbp(void) { void **STACKCALL taosGetEbp(void) {
void **ebp = NULL; void **ebp = NULL;
@ -41,6 +43,7 @@ void **STACKCALL taosGetEbp(void) {
: "memory"); /* not affect register */ : "memory"); /* not affect register */
return (void **)(*ebp); return (void **)(*ebp);
} }
int32_t taosBackTrace(void **buffer, int32_t size) { int32_t taosBackTrace(void **buffer, int32_t size) {
int32_t frame = 0; int32_t frame = 0;
void **ebp; void **ebp;
@ -59,6 +62,7 @@ int32_t taosBackTrace(void **buffer, int32_t size) {
} }
return frame; return frame;
} }
#endif #endif
// char **taosBackTraceSymbols(int32_t *size) { // char **taosBackTraceSymbols(int32_t *size) {
@ -68,6 +72,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 +82,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 +99,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 +121,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 +138,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
} }