mirror of https://github.com/ARMmbed/mbed-os.git
platform: make C++ allocation wrappers log the correct caller address
The C++ "operator new" and "operator delete" (and their array variants) were logging the the caller address wrong. In practice if one used "operator new", the logged caller address pointed to mbed_retarget.cpp, not to the client. Fix this by exposing the alloc wrappers to the the retarget. Note: this fixes only the ARMCC variants, as the GCC ones have different different API and implementation.pull/5954/head
parent
544d6b5d88
commit
ae5a5862f1
|
@ -245,9 +245,16 @@ extern "C" {
|
||||||
void *SUPER_REALLOC(void *ptr, size_t size);
|
void *SUPER_REALLOC(void *ptr, size_t size);
|
||||||
void *SUPER_CALLOC(size_t nmemb, size_t size);
|
void *SUPER_CALLOC(size_t nmemb, size_t size);
|
||||||
void SUPER_FREE(void *ptr);
|
void SUPER_FREE(void *ptr);
|
||||||
|
void *malloc_wrapper(size_t size, void* caller);
|
||||||
|
void free_wrapper(void *ptr, void* caller);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
extern "C" void* SUB_MALLOC(size_t size) {
|
extern "C" void* SUB_MALLOC(size_t size) {
|
||||||
|
return malloc_wrapper(size, MBED_CALLER_ADDR());
|
||||||
|
}
|
||||||
|
|
||||||
|
extern "C" void* malloc_wrapper(size_t size, void* caller) {
|
||||||
void *ptr = NULL;
|
void *ptr = NULL;
|
||||||
#ifdef MBED_MEM_TRACING_ENABLED
|
#ifdef MBED_MEM_TRACING_ENABLED
|
||||||
mbed_mem_trace_lock();
|
mbed_mem_trace_lock();
|
||||||
|
@ -272,12 +279,13 @@ extern "C" void* SUB_MALLOC(size_t size) {
|
||||||
ptr = SUPER_MALLOC(size);
|
ptr = SUPER_MALLOC(size);
|
||||||
#endif // #ifdef MBED_HEAP_STATS_ENABLED
|
#endif // #ifdef MBED_HEAP_STATS_ENABLED
|
||||||
#ifdef MBED_MEM_TRACING_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();
|
mbed_mem_trace_unlock();
|
||||||
#endif // #ifdef MBED_MEM_TRACING_ENABLED
|
#endif // #ifdef MBED_MEM_TRACING_ENABLED
|
||||||
return ptr;
|
return ptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
extern "C" void* SUB_REALLOC(void *ptr, size_t size) {
|
extern "C" void* SUB_REALLOC(void *ptr, size_t size) {
|
||||||
void *new_ptr = NULL;
|
void *new_ptr = NULL;
|
||||||
#ifdef MBED_MEM_TRACING_ENABLED
|
#ifdef MBED_MEM_TRACING_ENABLED
|
||||||
|
@ -337,6 +345,10 @@ extern "C" void *SUB_CALLOC(size_t nmemb, size_t size) {
|
||||||
}
|
}
|
||||||
|
|
||||||
extern "C" void SUB_FREE(void *ptr) {
|
extern "C" void SUB_FREE(void *ptr) {
|
||||||
|
free_wrapper(ptr, MBED_CALLER_ADDR());
|
||||||
|
}
|
||||||
|
|
||||||
|
extern "C" void free_wrapper(void *ptr, void* caller) {
|
||||||
#ifdef MBED_MEM_TRACING_ENABLED
|
#ifdef MBED_MEM_TRACING_ENABLED
|
||||||
mbed_mem_trace_lock();
|
mbed_mem_trace_lock();
|
||||||
#endif
|
#endif
|
||||||
|
@ -354,7 +366,7 @@ extern "C" void SUB_FREE(void *ptr) {
|
||||||
SUPER_FREE(ptr);
|
SUPER_FREE(ptr);
|
||||||
#endif // #ifdef MBED_HEAP_STATS_ENABLED
|
#endif // #ifdef MBED_HEAP_STATS_ENABLED
|
||||||
#ifdef MBED_MEM_TRACING_ENABLED
|
#ifdef MBED_MEM_TRACING_ENABLED
|
||||||
mbed_mem_trace_free(ptr, MBED_CALLER_ADDR());
|
mbed_mem_trace_free(ptr, caller);
|
||||||
mbed_mem_trace_unlock();
|
mbed_mem_trace_unlock();
|
||||||
#endif // #ifdef MBED_MEM_TRACING_ENABLED
|
#endif // #ifdef MBED_MEM_TRACING_ENABLED
|
||||||
}
|
}
|
||||||
|
|
|
@ -1022,6 +1022,53 @@ extern "C" void __cxa_guard_abort(int *guard_object_p)
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
#if defined(MBED_MEM_TRACING_ENABLED) && (defined(__CC_ARM) || defined(__ICCARM__))
|
||||||
|
|
||||||
|
// If the memory tracing is enabled, the wrappers in mbed_alloc_wrappers.cpp
|
||||||
|
// provide the implementation for these. Note: this needs to use the wrappers
|
||||||
|
// instead of malloc()/free() as the caller address would point to wrappers,
|
||||||
|
// not the caller of "new" or "delete".
|
||||||
|
extern "C" void* malloc_wrapper(size_t size, const void* caller);
|
||||||
|
extern "C" void free_wrapper(void *ptr, const void* caller);
|
||||||
|
|
||||||
|
void *operator new(std::size_t count)
|
||||||
|
{
|
||||||
|
void *buffer = malloc_wrapper(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(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(count, MBED_CALLER_ADDR());
|
||||||
|
}
|
||||||
|
|
||||||
|
void *operator new[](std::size_t count, const std::nothrow_t& tag)
|
||||||
|
{
|
||||||
|
return malloc_wrapper(count, MBED_CALLER_ADDR());
|
||||||
|
}
|
||||||
|
|
||||||
|
void operator delete(void *ptr)
|
||||||
|
{
|
||||||
|
free_wrapper(ptr, MBED_CALLER_ADDR());
|
||||||
|
}
|
||||||
|
void operator delete[](void *ptr)
|
||||||
|
{
|
||||||
|
free_wrapper(ptr, MBED_CALLER_ADDR());
|
||||||
|
}
|
||||||
|
|
||||||
|
#else
|
||||||
void *operator new(std::size_t count)
|
void *operator new(std::size_t count)
|
||||||
{
|
{
|
||||||
void *buffer = malloc(count);
|
void *buffer = malloc(count);
|
||||||
|
@ -1059,6 +1106,8 @@ void operator delete[](void *ptr)
|
||||||
free(ptr);
|
free(ptr);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|
||||||
/* @brief standard c library clock() function.
|
/* @brief standard c library clock() function.
|
||||||
*
|
*
|
||||||
* This function returns the number of clock ticks elapsed since the start of the program.
|
* This function returns the number of clock ticks elapsed since the start of the program.
|
||||||
|
|
Loading…
Reference in New Issue