This is the mail archive of the gcc-help@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Are the gcc memory hooks sometimes bypassed?


Hi All,


For a c++ arm application I need to trace the memory allocations. To do this I am using the gcc memory hooks. For now I am only printing the allocations and deallocations, see code below.

However, the malloc's and free's don't add up. Sometimes I see a free on a memory block that didn't the malloc hook before. Or the memory is freed twice. Of course this could be a bug in my code, although I don't get a segfault. But I also see that malloc sometimes returns a pointer that it has returned before and there has been no free in the meantime (at least my free hook wasn't called).

So my guess is that certain malloc's and free's are not passed through my hooks. Note that when I only trace the c++ allocations then things do add up nicely.

Does anyone have any ideas?


Regards,

Fabian van der Werf



#include <stdlib.h>
#include <stdio.h>
#include <pthread.h>
#include <new>
#include <unistd.h>
#include <string.h>
#include <malloc.h>

pthread_mutex_t lock = PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP;

static void push_memhooks();
static void pop_memhooks();

static void *malloc_hook(size_t size, const void *ret)
{

    pthread_mutex_lock(&lock);

    pop_memhooks();

    void *mem = malloc(size);

    if (mem) {
        printf("malloc %p\n", mem);
    }

    push_memhooks();

    pthread_mutex_unlock(&lock);

    return mem;
}

static void *realloc_hook(void* ptr, size_t size, const void *ret)
{
    pthread_mutex_lock(&lock);

    pop_memhooks();

    void* mem = realloc(ptr, size);

    if (mem) {
        printf("realloc %p -> %p\n", ptr, mem);
    }

    push_memhooks();

    pthread_mutex_unlock(&lock);

    return mem;
}

static void* memalign_hook(size_t boundary, size_t size, const void *ret)
{
    pthread_mutex_lock(&lock);
    pop_memhooks();

    void* mem = memalign(boundary, size);

    if (mem) {
        printf("memalign %p\n", mem);
    }

    push_memhooks();

    pthread_mutex_unlock(&lock);

    return mem;
}

static void free_hook(void *mem, const void *ret)
{
    pthread_mutex_lock(&lock);

    pop_memhooks();

    free(mem);

    printf("free %p\n", mem);

    push_memhooks();

    pthread_mutex_unlock(&lock);
}

void *operator new(size_t size)
{
    void* mem = malloc(size);

    if (!mem) {
        throw std::bad_alloc();
    }

    return mem;
}

void operator delete(void* mem)
{
    free(mem);
}

void *operator new[](size_t size)
{      
    void* mem = malloc(size);

    if (!mem) {
        throw std::bad_alloc();
    }

    return mem;
}

void operator delete[](void* mem)
{
    free(mem);
}

static int memhooks = 0;

static void push_memhooks()
{
    if (++memhooks == 1) {
        __malloc_hook = malloc_hook;
        __realloc_hook = realloc_hook;
        __free_hook = free_hook;
        __memalign_hook = memalign_hook;
    }
}

static void pop_memhooks()
{
    if (--memhooks == 0) {
        __malloc_hook = NULL;
        __realloc_hook = NULL;
        __free_hook = NULL;
        __memalign_hook = NULL;
    }
}

static void install_memhooks ()
{
    push_memhooks();
}

void (*__malloc_initialize_hook)(void) = install_memhooks;


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]