summaryrefslogtreecommitdiff
path: root/mysys/safemalloc.c
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/safemalloc.c
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/safemalloc.c')
-rw-r--r--mysys/safemalloc.c76
1 files changed, 40 insertions, 36 deletions
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;
}