Override malloc and free

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Override malloc and free

3,623 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by robertpalmerjr on Fri Oct 02 12:01:56 MST 2015
I am wanting to override malloc and free in my project.  I am using freeRTOS and RedLib.  RedLib has functions that call malloc (various printf variants, are there others?) I would like my code to override malloc so that when RedLib calls malloc it calls my code which calls the freeRTOS implementation.

In standard gcc, I see that there is a -wrap,object linker flag.  That does not appear to work in LPCxpresso.  I know if a symbol is WEAK linked, it can be overridden by just defining the symbol in your own code, but it appears that malloc and free are not weak linked.

Is there a way to do this?
0 Kudos
Reply
4 Replies

2,746 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by rocketdawg on Wed Oct 07 06:28:53 MST 2015

Quote: TheFallGuy

Btw: what is special about your implementation that means you don't want to use the standard version?



The library version of malloc and free might not be thread safe.  Printf might not be either.

0 Kudos
Reply

2,746 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by lpcxpresso-support on Mon Oct 05 13:14:48 MST 2015
You might want to consider coming at this from a different direction. If you define CR_PRINTF_CHAR in the properties for your project, then this should avoid the printf variant that uses malloc. Thus avoiding the malloc version from Redlib altogether.

https://www.lpcware.com/content/faq/lpcxpresso/using-printf

Regards,
LPCXpresso Support

0 Kudos
Reply

2,746 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by robertpalmerjr on Mon Oct 05 10:39:01 MST 2015
I have already tried that but providing my own implementation in the sources leads to a linker error:

I added this to my main.c:

void *malloc(size_t size)
{
    return NULL;
}


and the resulting compile generated this error.  By the way, I have also tested this with version 7.7.2 with the same result.

/Applications/lpcxpresso_5.2.4_2122/lpcxpresso/tools/bin/../lib/gcc/arm-none-eabi/4.6.2/../../../../arm-none-eabi/lib/armv7-m/libcr_c.a(alloc.o): In function `malloc':
alloc.c:(.text.malloc+0x0): multiple definition of `malloc'
./src_codered/retarget.o:/Volumes/Ingenutec/Active Project Code/IPS/workspace/Meter/Release/../src_codered/retarget.c:77: first defined here
collect2: ld returned 1 exit status


As for what's so special - not a thing.  I just want to use only ONE memory allocation scheme, the one in freeRTOS.  This allows me to better manage the RAM rather than having freeRTOS allocations for some things (in the freeRTOS "heap") and malloc allocations in a different heap.
0 Kudos
Reply

2,746 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by TheFallGuy on Fri Oct 02 16:36:57 MST 2015
Just provide your own implementations in your sources. The linker wil, resolve yours and not use the ones from the redlib library.

Btw: what is special about your implementation that means you don't want to use the standard version?
0 Kudos
Reply