diff options
author | Sergei Golubchik <serg@mariadb.org> | 2020-01-29 13:50:26 +0100 |
---|---|---|
committer | Sergei Golubchik <serg@mariadb.org> | 2020-03-10 19:24:22 +0100 |
commit | 7c58e97bf6f80a251046c5b3e7bce826fe058bd6 (patch) | |
tree | 7d32d26b320cf83296ee0ede2ea164ad116c4de8 /mysys/my_malloc.c | |
parent | 2ac3121af2767186c489054db5d4871d04b8eef4 (diff) | |
download | mariadb-git-7c58e97bf6f80a251046c5b3e7bce826fe058bd6.tar.gz |
perfschema memory related instrumentation changes
Diffstat (limited to 'mysys/my_malloc.c')
-rw-r--r-- | mysys/my_malloc.c | 176 |
1 files changed, 81 insertions, 95 deletions
diff --git a/mysys/my_malloc.c b/mysys/my_malloc.c index d54270a7f20..33880f023a9 100644 --- a/mysys/my_malloc.c +++ b/mysys/my_malloc.c @@ -19,35 +19,17 @@ #include "mysys_err.h" #include <m_string.h> -/* If we have our own safemalloc (for debugging) */ -#if defined(SAFEMALLOC) -#define MALLOC_SIZE_AND_FLAG(p,b) sf_malloc_usable_size(p,b) -#define MALLOC_PREFIX_SIZE 0 -#define MALLOC_STORE_SIZE(a,b,c,d) -#define MALLOC_FIX_POINTER_FOR_FREE(a) a -#else -/* - * We use double as prefix size as this guarantees the correct - * alignment on all platforms and will optimize things for - * memcpy(), memcmp() etc. - */ -#define MALLOC_PREFIX_SIZE (sizeof(double)) -#define MALLOC_SIZE(p) (*(size_t*) ((char*)(p) - MALLOC_PREFIX_SIZE)) -#define MALLOC_STORE_SIZE(p, type_of_p, size, flag) \ -{\ - *(size_t*) p= (size) | (flag); \ - (p)= (type_of_p) (((char*) (p)) + MALLOC_PREFIX_SIZE); \ -} -static inline size_t malloc_size_and_flag(void *p, my_bool *is_thread_specific) +struct my_memory_header { - size_t size= MALLOC_SIZE(p); - *is_thread_specific= (size & 1); - return size & ~ (ulonglong) 1; -} -#define MALLOC_SIZE_AND_FLAG(p,b) malloc_size_and_flag(p, b); -#define MALLOC_FIX_POINTER_FOR_FREE(p) (((char*) (p)) - MALLOC_PREFIX_SIZE) -#endif /* SAFEMALLOC */ + PSI_thread *m_owner; + size_t m_size; + PSI_memory_key m_key; +}; +typedef struct my_memory_header my_memory_header; +#define HEADER_SIZE 24 +#define USER_TO_HEADER(P) ((my_memory_header*)((char *)(P) - HEADER_SIZE)) +#define HEADER_TO_USER(P) ((char*)(P) + HEADER_SIZE) /** Inform application that memory usage has changed @@ -82,12 +64,13 @@ void set_malloc_size_cb(MALLOC_SIZE_CB func) @return A pointer to the allocated memory block, or NULL on failure. */ -void *my_malloc(size_t size, myf my_flags) +void *my_malloc(PSI_memory_key key, size_t size, myf my_flags) { - void* point; + my_memory_header *mh; + void *point; DBUG_ENTER("my_malloc"); - DBUG_PRINT("my",("size: %lu my_flags: %lu", (ulong) size, my_flags)); - compile_time_assert(sizeof(size_t) <= sizeof(double)); + DBUG_PRINT("my",("size: %zu flags: %lu", size, my_flags)); + compile_time_assert(sizeof(my_memory_header) <= HEADER_SIZE); if (!(my_flags & (MY_WME | MY_FAE))) my_flags|= my_global_flags; @@ -96,11 +79,15 @@ void *my_malloc(size_t size, myf my_flags) if (!size) size=1; - /* We have to align size to be able to store markers in it */ + /* We have to align size as we store MY_THREAD_SPECIFIC flag in the LSB */ size= ALIGN_SIZE(size); - point= sf_malloc(size + MALLOC_PREFIX_SIZE, my_flags); - if (point == NULL) + if (DBUG_EVALUATE_IF("simulate_out_of_memory", 1, 0)) + mh= NULL; + else + mh= (my_memory_header*) sf_malloc(size + HEADER_SIZE, my_flags); + + if (mh == NULL) { my_errno=errno; if (my_flags & MY_FAE) @@ -109,22 +96,19 @@ void *my_malloc(size_t size, myf my_flags) my_error(EE_OUTOFMEMORY, MYF(ME_BELL+ME_ERROR_LOG+ME_FATAL),size); if (my_flags & MY_FAE) abort(); + point= NULL; } else { - MALLOC_STORE_SIZE(point, void*, size, - MY_TEST(my_flags & MY_THREAD_SPECIFIC)); - update_malloc_size(size + MALLOC_PREFIX_SIZE, - MY_TEST(my_flags & MY_THREAD_SPECIFIC)); - TRASH_ALLOC(point, size); - DBUG_EXECUTE_IF("simulate_out_of_memory", - { - /* my_free() handles memory accounting */ - my_free(point); - point= NULL; - }); + int flag= MY_TEST(my_flags & MY_THREAD_SPECIFIC); + mh->m_size= size | flag; + mh->m_key= PSI_CALL_memory_alloc(key, size, & mh->m_owner); + update_malloc_size(size + HEADER_SIZE, flag); + point= HEADER_TO_USER(mh); if (my_flags & MY_ZEROFILL) bzero(point, size); + else + TRASH_ALLOC(point, size); } DBUG_PRINT("exit",("ptr: %p", point)); DBUG_RETURN(point); @@ -134,62 +118,57 @@ void *my_malloc(size_t size, myf my_flags) /** @brief wrapper around realloc() - @param oldpoint pointer to currently allocated area + @param old_point pointer to currently allocated area @param size new size requested, must be >0 @param my_flags flags @note if size==0 realloc() may return NULL; my_realloc() treats this as an error which is not the intention of realloc() */ -void *my_realloc(void *oldpoint, size_t size, myf my_flags) +void *my_realloc(PSI_memory_key key, void *old_point, size_t size, myf my_flags) { + my_memory_header *old_mh, *mh; void *point; size_t old_size; my_bool old_flags; DBUG_ENTER("my_realloc"); - DBUG_PRINT("my",("ptr: %p size: %lu my_flags: %lu", oldpoint, - (ulong) size, my_flags)); + DBUG_PRINT("my",("ptr: %p size: %zu flags: %lu", old_point, size, my_flags)); DBUG_ASSERT(size > 0); - if (!oldpoint && (my_flags & MY_ALLOW_ZERO_PTR)) - DBUG_RETURN(my_malloc(size, my_flags)); + if (!old_point && (my_flags & MY_ALLOW_ZERO_PTR)) + DBUG_RETURN(my_malloc(key, size, my_flags)); + + old_mh= USER_TO_HEADER(old_point); + old_size= old_mh->m_size & ~1; + old_flags= old_mh->m_size & 1; + + DBUG_ASSERT(old_mh->m_key == key || old_mh->m_key == PSI_NOT_INSTRUMENTED); + DBUG_ASSERT(old_flags == MY_TEST(my_flags & MY_THREAD_SPECIFIC)); size= ALIGN_SIZE(size); - old_size= MALLOC_SIZE_AND_FLAG(oldpoint, &old_flags); - /* - Test that the new and old area are the same, if not MY_THREAD_MOVE is - given - */ - DBUG_ASSERT((MY_TEST(my_flags & MY_THREAD_SPECIFIC) == old_flags) || - (my_flags & MY_THREAD_MOVE)); - if ((point= sf_realloc(MALLOC_FIX_POINTER_FOR_FREE(oldpoint), - size + MALLOC_PREFIX_SIZE, my_flags)) == NULL) + mh= sf_realloc(old_mh, size + HEADER_SIZE, my_flags); + + if (mh == NULL) { + if (size < old_size) + DBUG_RETURN(old_point); + my_errno=errno; if (my_flags & MY_FREE_ON_ERROR) { /* my_free will take care of size accounting */ - my_free(oldpoint); - oldpoint= 0; + my_free(old_point); + old_point= 0; } - if (my_flags & MY_HOLD_ON_ERROR) - DBUG_RETURN(oldpoint); - my_errno=errno; if (my_flags & (MY_FAE+MY_WME)) my_error(EE_OUTOFMEMORY, MYF(ME_BELL + ME_FATAL), size); + point= NULL; } else { - MALLOC_STORE_SIZE(point, void*, size, - MY_TEST(my_flags & MY_THREAD_SPECIFIC)); - if (MY_TEST(my_flags & MY_THREAD_SPECIFIC) != old_flags) - { - /* memory moved between system and thread specific */ - update_malloc_size(-(longlong) old_size - MALLOC_PREFIX_SIZE, old_flags); - update_malloc_size((longlong) size + MALLOC_PREFIX_SIZE, - MY_TEST(my_flags & MY_THREAD_SPECIFIC)); - } - else - update_malloc_size((longlong)size - (longlong)old_size, old_flags); + mh->m_size= size | old_flags; + mh->m_key= PSI_CALL_memory_realloc(key, old_size, size, & mh->m_owner); + update_malloc_size((longlong)size - (longlong)old_size, old_flags); + point= HEADER_TO_USER(mh); } DBUG_PRINT("exit",("ptr: %p", point)); @@ -204,56 +183,63 @@ void *my_realloc(void *oldpoint, size_t size, myf my_flags) */ void my_free(void *ptr) { + my_memory_header *mh; + size_t old_size; + my_bool old_flags; DBUG_ENTER("my_free"); DBUG_PRINT("my",("ptr: %p", ptr)); - if (ptr) - { - size_t old_size; - my_bool old_flags; - old_size= MALLOC_SIZE_AND_FLAG(ptr, &old_flags); - update_malloc_size(- (longlong) old_size - MALLOC_PREFIX_SIZE, old_flags); + + if (ptr == NULL) + DBUG_VOID_RETURN; + + mh= USER_TO_HEADER(ptr); + old_size= mh->m_size & ~1; + old_flags= mh->m_size & 1; + PSI_CALL_memory_free(mh->m_key, old_size, mh->m_owner); + + update_malloc_size(- (longlong) old_size - HEADER_SIZE, old_flags); + #ifndef SAFEMALLOC - /* - Trash memory if not safemalloc. We don't have to do this if safemalloc - is used as safemalloc will also do trashing - */ - TRASH_FREE(ptr, old_size); + /* + Trash memory if not safemalloc. We don't have to do this if safemalloc + is used as safemalloc will also do trashing + */ + TRASH_FREE(ptr, old_size); #endif - sf_free(MALLOC_FIX_POINTER_FOR_FREE(ptr)); - } + sf_free(mh); DBUG_VOID_RETURN; } -void *my_memdup(const void *from, size_t length, myf my_flags) +void *my_memdup(PSI_memory_key key, const void *from, size_t length, myf my_flags) { void *ptr; DBUG_ENTER("my_memdup"); - if ((ptr= my_malloc(length,my_flags)) != 0) + if ((ptr= my_malloc(key, length,my_flags)) != 0) memcpy(ptr, from, length); DBUG_RETURN(ptr); } -char *my_strdup(const char *from, myf my_flags) +char *my_strdup(PSI_memory_key key, const char *from, myf my_flags) { char *ptr; size_t length= strlen(from)+1; DBUG_ENTER("my_strdup"); - if ((ptr= (char*) my_malloc(length, my_flags))) + if ((ptr= (char*) my_malloc(key, length, my_flags))) memcpy(ptr, from, length); DBUG_RETURN(ptr); } -char *my_strndup(const char *from, size_t length, myf my_flags) +char *my_strndup(PSI_memory_key key, const char *from, size_t length, myf my_flags) { char *ptr; DBUG_ENTER("my_strndup"); - if ((ptr= (char*) my_malloc(length+1, my_flags))) + if ((ptr= (char*) my_malloc(key, length+1, my_flags))) { memcpy(ptr, from, length); ptr[length]= 0; |