Wrapping malloc and free

Discuss how to use the features of Atollic TrueSTUDIO, including the editor, assembler, C/C++ compiler, linker, debugger, static code analysis and team collaboration tools.

Moderators: Markus Girdland, Mattias Norlander

antm
Posts: 5
Joined: Mon Mar 11, 2019 10:35 am

Wrapping malloc and free

Postby antm » Mon Mar 11, 2019 10:45 am

Atollic Truestudio v 9.3.0
Project generated using STM32 CubeMX v5.1.0

Hi,

I'm working on a project using STM32 and FreeRTOS, I would like to make sure that any calls to malloc and free in the application actually call the FreeRTOS memory allocation functions.

So, I have the following code in a file:
//--------------------------------------------------------------------
#include "FreeRTOS.h"

extern void * pvPortMalloc(size_t aNoOfBytes);
extern void vPortFree(void* aPtr);

void* __wrap_malloc(size_t aNoOfBytes)
//
// Wrapper around newlib's "malloc".
//
{
void* ptr=pvPortMalloc(aNoOfBytes);
return(ptr);
}

void __wrap_free(void* aPtr)
//
// Wrapper around newlib's "free".
//
{
vPortFree(aPtr);
}
//--------------------------------------------------------------------

I also have to tell the linker to wrap malloc and free, which I try to do with the following:

-Xlinker --wrap=malloc -Xlinker --wrap=free

This, however, doesn't work. There appear to be multiple places in the linker options to add the above line, but none work... if I put a breakpoint in __wrap_malloc I get a complaint from Atollic that the file does not exist in the debug output, so it is not compiling it.

Has anyone else managed to implemented this successfully? Or maybe spot a mistake in my approach?

Thanks,

Anthony

antm
Posts: 5
Joined: Mon Mar 11, 2019 10:35 am

Re: Wrapping malloc and free

Postby antm » Mon Mar 11, 2019 3:08 pm

To answer my own question, the code is correct.

The linker options need to be added under settings->Tool Settings-> C Linker->Miscellaneous

Other options

-Wl,--wrap=malloc, -Wl,--wrap=free

This works.


Return to “Atollic TrueSTUDIO tool discussions”

Who is online

Users browsing this forum: No registered users and 15 guests