From e42130e9cdf6e7282403dbcf74968cf43facb8e0 Mon Sep 17 00:00:00 2001 From: Monty Date: Mon, 17 Aug 2020 18:56:47 +0300 Subject: Fixes that enables my_new.cc (new wrapper using my_malloc) This is not enabled by default, as there are leaks in the server that needs to be fixed first. One can compile with -DUSE_MYSYS_NEW to find the memory leaks from 'new'. More comments can be found in mysys/my_new.cc --- mysys/my_new.cc | 35 ++++++++++++++++++++++++++++------- mysys/my_static.c | 1 + mysys/mysys_priv.h | 1 + mysys/safemalloc.c | 6 +++++- 4 files changed, 35 insertions(+), 8 deletions(-) (limited to 'mysys') diff --git a/mysys/my_new.cc b/mysys/my_new.cc index 88080f3e7eb..ae92d6a293e 100644 --- a/mysys/my_new.cc +++ b/mysys/my_new.cc @@ -25,34 +25,55 @@ #include "mysys_priv.h" #include -#ifdef USE_MYSYS_NEW +/* + We don't yet enable the my new operators by default. + The reasons (for MariaDB) are: + + - There are several global objects in plugins (wsrep_info, InnoDB, + tpool) that allocates data with 'new'. These objects are not + freed properly before exit() is called and safemalloc will report + these as lost memory. The proper fix is to ensure that all plugins + either ensure that all objects frees there data or the global object are + changed to a pointer that as allocated and freed on demand. + Doing this will make it easier to find leaks and also speed up plugin + loads when we don't have to initialize a lot of objects until they + are really needed. + - Rocksdb calls malloc_usable_size, that will crash if used with new based + on my_malloc. One suggested fix would be to not define + ROCKSDB_MALLOC_USABLE_SIZE if MYSYS_USE_NEW is defined. + + When the above is fixed, we can add enable ADD_DEFINITIONS(-DUSE_MYSYS_NEW) + in CMakeLists.txt +*/ + +#if defined(USE_MYSYS_NEW) void *operator new (size_t sz) { - return (void *) my_malloc (sz ? sz : 1, MYF(0)); + return (void *) my_malloc(key_memory_new, sz ? sz : 1, MYF(0)); } void *operator new[] (size_t sz) { - return (void *) my_malloc (sz ? sz : 1, MYF(0)); + return (void *) my_malloc(key_memory_new, sz ? sz : 1, MYF(0)); } void* operator new(std::size_t sz, const std::nothrow_t&) throw() { - return (void *) my_malloc (sz ? sz : 1, MYF(0)); + return (void *) my_malloc(key_memory_new, sz ? sz : 1, MYF(0)); } void* operator new[](std::size_t sz, const std::nothrow_t&) throw() { - return (void *) my_malloc (sz ? sz : 1, MYF(0)); + return (void *) my_malloc(key_memory_new, sz ? sz : 1, MYF(0)); } -void operator delete (void *ptr, std::size_t) +void operator delete (void *ptr, std::size_t) throw () { my_free(ptr); } -void operator delete (void *ptr) +void operator delete (void *ptr) throw () { my_free(ptr); } diff --git a/mysys/my_static.c b/mysys/my_static.c index 3c4b9efc1f8..d0f20a5cc9c 100644 --- a/mysys/my_static.c +++ b/mysys/my_static.c @@ -48,6 +48,7 @@ PSI_memory_key key_memory_my_err_head; PSI_memory_key key_memory_my_file_info; PSI_memory_key key_memory_pack_frm; PSI_memory_key key_memory_charsets; +PSI_memory_key key_memory_new= PSI_INSTRUMENT_MEM; #ifdef _WIN32 PSI_memory_key key_memory_win_SECURITY_ATTRIBUTES; diff --git a/mysys/mysys_priv.h b/mysys/mysys_priv.h index adf2d39046a..c5e05ec7955 100644 --- a/mysys/mysys_priv.h +++ b/mysys/mysys_priv.h @@ -88,6 +88,7 @@ extern PSI_memory_key key_memory_my_err_head; extern PSI_memory_key key_memory_my_file_info; extern PSI_memory_key key_memory_pack_frm; extern PSI_memory_key key_memory_charsets; +extern PSI_memory_key key_memory_new; #ifdef _WIN32 extern PSI_memory_key key_memory_win_SECURITY_ATTRIBUTES; diff --git a/mysys/safemalloc.c b/mysys/safemalloc.c index e2d59f8e824..34e57f36bb5 100644 --- a/mysys/safemalloc.c +++ b/mysys/safemalloc.c @@ -39,8 +39,12 @@ static pthread_mutex_t sf_mutex; static int init_done= 0; #ifndef SF_REMEMBER_FRAMES +#ifdef USE_MYSYS_NEW +#define SF_REMEMBER_FRAMES 14 +#else #define SF_REMEMBER_FRAMES 8 -#endif +#endif /* USE_MYSYS_NEW */ +#endif /* SF_REMEMBER_FRAMES */ /* ignore the first two frames (sf_malloc itself, and my_malloc) */ #define SF_FRAMES_SKIP 2 -- cgit v1.2.1