diff --git a/platform/mbed_alloc_wrappers.cpp b/platform/mbed_alloc_wrappers.cpp index eeb78411ba..58f153c70d 100644 --- a/platform/mbed_alloc_wrappers.cpp +++ b/platform/mbed_alloc_wrappers.cpp @@ -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 } diff --git a/platform/mbed_retarget.cpp b/platform/mbed_retarget.cpp index 11fcfa6989..8d3dbe3936 100644 --- a/platform/mbed_retarget.cpp +++ b/platform/mbed_retarget.cpp @@ -1068,7 +1068,53 @@ void operator delete[](void *ptr) free_wrapper(ptr, MBED_CALLER_ADDR()); } +#elif defined(MBED_MEM_TRACING_ENABLED) && defined(__GNUC__) + +#include + +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);