platform: Fix C++ allocation wrappers for GCC compilation

Fix the caller address logging on the GCC compilation too.
Previously the code logged the caller address as C++ wrapper,
not the actual caller of the C++ operator new or delete.
pull/5456/head
Tero Jääskö 2017-11-24 16:08:28 +02:00
parent 1cf5c45615
commit b81be1e34c
2 changed files with 58 additions and 2 deletions

View File

@ -81,12 +81,18 @@ extern "C" {
void * __real__realloc_r(struct _reent * r, void * ptr, size_t size);
void __real__free_r(struct _reent * r, void * ptr);
void* __real__calloc_r(struct _reent * r, size_t nmemb, size_t size);
void* malloc_wrapper(struct _reent * r, size_t size, void * caller);
void free_wrapper(struct _reent * r, void * ptr, void* caller);
}
// TODO: memory tracing doesn't work with uVisor enabled.
#if !defined(FEATURE_UVISOR)
extern "C" void * __wrap__malloc_r(struct _reent * r, size_t size) {
return malloc_wrapper(r, size, MBED_CALLER_ADDR());
}
extern "C" void * malloc_wrapper(struct _reent * r, size_t size, void * caller) {
void *ptr = NULL;
#ifdef MBED_MEM_TRACING_ENABLED
mbed_mem_trace_lock();
@ -111,7 +117,7 @@ extern "C" void * __wrap__malloc_r(struct _reent * r, size_t size) {
ptr = __real__malloc_r(r, size);
#endif // #ifdef MBED_HEAP_STATS_ENABLED
#ifdef MBED_MEM_TRACING_ENABLED
mbed_mem_trace_malloc(ptr, size, MBED_CALLER_ADDR());
mbed_mem_trace_malloc(ptr, size, caller);
mbed_mem_trace_unlock();
#endif // #ifdef MBED_MEM_TRACING_ENABLED
return ptr;
@ -160,6 +166,10 @@ extern "C" void * __wrap__realloc_r(struct _reent * r, void * ptr, size_t size)
}
extern "C" void __wrap__free_r(struct _reent * r, void * ptr) {
free_wrapper(r, ptr, MBED_CALLER_ADDR());
}
extern "C" void free_wrapper(struct _reent * r, void * ptr, void * caller) {
#ifdef MBED_MEM_TRACING_ENABLED
mbed_mem_trace_lock();
#endif
@ -177,7 +187,7 @@ extern "C" void __wrap__free_r(struct _reent * r, void * ptr) {
__real__free_r(r, ptr);
#endif // #ifdef MBED_HEAP_STATS_ENABLED
#ifdef MBED_MEM_TRACING_ENABLED
mbed_mem_trace_free(ptr, MBED_CALLER_ADDR());
mbed_mem_trace_free(ptr, caller);
mbed_mem_trace_unlock();
#endif // #ifdef MBED_MEM_TRACING_ENABLED
}

View File

@ -1068,7 +1068,53 @@ void operator delete[](void *ptr)
free_wrapper(ptr, MBED_CALLER_ADDR());
}
#elif defined(MBED_MEM_TRACING_ENABLED) && defined(__GNUC__)
#include <reent.h>
extern "C" void* malloc_wrapper(struct _reent * r, size_t size, void * caller);
extern "C" void free_wrapper(struct _reent * r, void * ptr, void * caller);
void *operator new(std::size_t count)
{
void *buffer = malloc_wrapper(_REENT, count, MBED_CALLER_ADDR());
if (NULL == buffer) {
error("Operator new out of memory\r\n");
}
return buffer;
}
void *operator new[](std::size_t count)
{
void *buffer = malloc_wrapper(_REENT, count, MBED_CALLER_ADDR());
if (NULL == buffer) {
error("Operator new[] out of memory\r\n");
}
return buffer;
}
void *operator new(std::size_t count, const std::nothrow_t& tag)
{
return malloc_wrapper(_REENT, count, MBED_CALLER_ADDR());
}
void *operator new[](std::size_t count, const std::nothrow_t& tag)
{
return malloc_wrapper(_REENT, count, MBED_CALLER_ADDR());
}
void operator delete(void *ptr)
{
free_wrapper(_REENT, ptr, MBED_CALLER_ADDR());
}
void operator delete[](void *ptr)
{
free_wrapper(_REENT, ptr, MBED_CALLER_ADDR());
}
#else
void *operator new(std::size_t count)
{
void *buffer = malloc(count);