summaryrefslogtreecommitdiff
path: root/mysys
diff options
context:
space:
mode:
authorSergei Golubchik <sergii@pisem.net>2013-01-23 16:20:39 +0100
committerSergei Golubchik <sergii@pisem.net>2013-01-23 16:20:39 +0100
commitc71fad9983b4a3c6e0d9e9eca02dadfd923ddbbb (patch)
tree76dbef3d7d96a3692039779f20f48938c260a5f1 /mysys
parent52051882002c1b88a427d50fb343a9ed3614ffe0 (diff)
downloadmariadb-git-c71fad9983b4a3c6e0d9e9eca02dadfd923ddbbb.tar.gz
cleanup:
* don't use 'myf flags', when 'my_bool is_thread_specific' is meant * call set_malloc_size_cb() for embedded too * warn in safemalloc if the memory is freed by a wrong thread sql/mysqld.cc: move set_malloc_size_cb() to a function that is also called for embedded sql/mysqld.h: gdb-friendly, one can put breakpoint on a function, but not on a macro sql/sql_class.cc: initialize thread_id earlier
Diffstat (limited to 'mysys')
-rw-r--r--mysys/my_malloc.c12
-rw-r--r--mysys/mysys_priv.h2
-rw-r--r--mysys/safemalloc.c76
3 files changed, 47 insertions, 43 deletions
diff --git a/mysys/my_malloc.c b/mysys/my_malloc.c
index 88f3f412faf..e9956263ce1 100644
--- a/mysys/my_malloc.c
+++ b/mysys/my_malloc.c
@@ -37,10 +37,10 @@
*(size_t*) p= (size) | (flag); \
(p)= (type_of_p) (((char*) (p)) + MALLOC_PREFIX_SIZE); \
}
-static inline size_t malloc_size_and_flag(void *p, myf *flags)
+static inline size_t malloc_size_and_flag(void *p, my_bool *is_thread_specific)
{
size_t size= MALLOC_SIZE(p);
- *flags= (size & 1);
+ *is_thread_specific= (size & 1);
return size & ~ (ulonglong) 1;
}
#define MALLOC_SIZE_AND_FLAG(p,b) malloc_size_and_flag(p, b);
@@ -60,10 +60,10 @@ static MALLOC_SIZE_CB malloc_size_cb_func= NULL;
decrement the memory usage
*/
-static void update_malloc_size(long long size, myf my_flags)
+static void update_malloc_size(long long size, my_bool is_thread_specific)
{
if (malloc_size_cb_func)
- malloc_size_cb_func(size, my_flags);
+ malloc_size_cb_func(size, is_thread_specific);
}
void set_malloc_size_cb(MALLOC_SIZE_CB func)
@@ -145,7 +145,7 @@ void *my_realloc(void *oldpoint, size_t size, myf my_flags)
{
void *point;
size_t old_size;
- myf old_flags;
+ my_bool old_flags;
DBUG_ENTER("my_realloc");
DBUG_PRINT("my",("ptr: %p size: %lu my_flags: %lu", oldpoint,
(ulong) size, my_flags));
@@ -210,7 +210,7 @@ void my_free(void *ptr)
if (ptr)
{
size_t old_size;
- myf old_flags;
+ my_bool old_flags;
old_size= MALLOC_SIZE_AND_FLAG(ptr, &old_flags);
update_malloc_size(- (longlong) old_size - MALLOC_PREFIX_SIZE, old_flags);
sf_free(MALLOC_FIX_POINTER_FOR_FREE(ptr));
diff --git a/mysys/mysys_priv.h b/mysys/mysys_priv.h
index aa4af92202a..61b7f2897d4 100644
--- a/mysys/mysys_priv.h
+++ b/mysys/mysys_priv.h
@@ -73,7 +73,7 @@ extern PSI_file_key key_file_charset, key_file_cnf;
void *sf_malloc(size_t size, myf my_flags);
void *sf_realloc(void *ptr, size_t size, myf my_flags);
void sf_free(void *ptr);
-size_t sf_malloc_usable_size(void *ptr, myf *my_flags);
+size_t sf_malloc_usable_size(void *ptr, my_bool *is_thread_specific);
#else
#define sf_malloc(X,Y) malloc(X)
#define sf_realloc(X,Y,Z) realloc(X,Y)
diff --git a/mysys/safemalloc.c b/mysys/safemalloc.c
index d9a04d9e8d1..6283ed20cca 100644
--- a/mysys/safemalloc.c
+++ b/mysys/safemalloc.c
@@ -207,47 +207,16 @@ void sf_free(void *ptr)
@return Size of block
*/
-size_t sf_malloc_usable_size(void *ptr, myf *flags)
+size_t sf_malloc_usable_size(void *ptr, my_bool *is_thread_specific)
{
struct st_irem *irem= (struct st_irem *)ptr - 1;
DBUG_ENTER("sf_malloc_usable_size");
- *flags= test(irem->flags & MY_THREAD_SPECIFIC);
+ *is_thread_specific= test(irem->flags & MY_THREAD_SPECIFIC);
DBUG_PRINT("exit", ("size: %lu flags: %lu", (ulong) irem->datasize,
- *flags));
+ (ulong)irem->flags));
DBUG_RETURN(irem->datasize);
}
-static void free_memory(void *ptr)
-{
- struct st_irem *irem= (struct st_irem *)ptr - 1;
-
- if ((irem->flags & MY_THREAD_SPECIFIC) &&
- irem->thread_id != sf_malloc_dbug_id())
- {
- DBUG_PRINT("warning",
- ("Memory: %p was allocated by thread %lu and freed by thread %lu", ptr, (ulong) irem->thread_id, (ulong) sf_malloc_dbug_id()));
- }
-
- pthread_mutex_lock(&sf_mutex);
- /* Remove this structure from the linked list */
- if (irem->prev)
- irem->prev->next= irem->next;
- else
- sf_malloc_root= irem->next;
-
- if (irem->next)
- irem->next->prev= irem->prev;
-
- /* Handle the statistics */
- sf_malloc_count--;
- pthread_mutex_unlock(&sf_mutex);
-
- /* only trash the data and magic values, but keep the stack trace */
- TRASH_FREE((uchar*)(irem + 1) - 4, irem->datasize + 8);
- free(irem);
- return;
-}
-
#ifdef HAVE_BACKTRACE
static void print_stack(void **frame)
{
@@ -277,6 +246,39 @@ static void print_stack(void **frame)
#define print_stack(X) fprintf(stderr, "???\n")
#endif
+static void free_memory(void *ptr)
+{
+ struct st_irem *irem= (struct st_irem *)ptr - 1;
+
+ if ((irem->flags & MY_THREAD_SPECIFIC) && irem->thread_id &&
+ irem->thread_id != sf_malloc_dbug_id())
+ {
+ fprintf(stderr, "Warning: %4lu bytes freed by T@%lu, allocated by T@%lu at ",
+ (ulong) irem->datasize,
+ (ulong) sf_malloc_dbug_id(), (ulong) irem->thread_id);
+ print_stack(irem->frame);
+ }
+
+ pthread_mutex_lock(&sf_mutex);
+ /* Remove this structure from the linked list */
+ if (irem->prev)
+ irem->prev->next= irem->next;
+ else
+ sf_malloc_root= irem->next;
+
+ if (irem->next)
+ irem->next->prev= irem->prev;
+
+ /* Handle the statistics */
+ sf_malloc_count--;
+ pthread_mutex_unlock(&sf_mutex);
+
+ /* only trash the data and magic values, but keep the stack trace */
+ TRASH_FREE((uchar*)(irem + 1) - 4, irem->datasize + 8);
+ free(irem);
+ return;
+}
+
static void warn(const char *format,...)
{
va_list args;
@@ -371,8 +373,10 @@ void sf_report_leaked_memory(my_thread_id id)
{
if (!id || (irem->thread_id == id && irem->flags & MY_THREAD_SPECIFIC))
{
- fprintf(stderr, "Warning: %4lu bytes lost, allocated at ",
- (ulong) irem->datasize);
+ my_thread_id tid = irem->thread_id && irem->flags & MY_THREAD_SPECIFIC ?
+ irem->thread_id : 0;
+ fprintf(stderr, "Warning: %4lu bytes lost, allocated by T@%lu at ",
+ (ulong) irem->datasize,tid);
print_stack(irem->frame);
total+= irem->datasize;
}