Dynamically allocating memory with RLPLite

I tried the C malloc function, but this kills the whole Hydra :slight_smile: Even does โ€˜new Object()โ€™ in C++.

Any hints on how I can dynamically allocate and free memory using RLPLite, or how I can make malloc (new) and free (delete) work?

Currently readin this article on how to implement my own functions: IBM Developer

Using the built-in way would be preferred however.

@ WouterH - As I mentioned on the other post (I only just saw this), my understanding with GCC (which is what I am using) is that you need to implement the methods like _sbrk specifically for the target device for the standard malloc, realloc etc. to work. This makes sense I guess, since the standard lib would not know where the โ€œheapโ€ is that can be used to allocate the memory from, so by implementing these function you provide the memory allocation strategy.

In our case we probably need to allocate from the RLP reserved memory, probably reserve some memory by tweeking the link script.

Of course, I am guessing here. So I might be way off, but this is what I understand so far.

@ WouterH - Did you make any headway with this?

Easy! Very easy! :slight_smile:

Go in NETMF porting kit, find simple heap (not heap) file, take it and use it :slight_smile:

@ Gus - you are a champion! I was planing to get into this when I get back from my trip, but now with this bit of insight I might have a go at it this evening.

@ Gus - Just to let you know, SimpleHeap worked perfectly!!! I will put up a video of the test I did, nothing fancy just some 2d Metaballs.

Thank you again.

Chris, I am interested to see you changes to enable dynamic allocation in RLPLite. :slight_smile:

@ Architect - I just finished integrating it, and so far so good. SimpleHeap has the following functions


void SimpleHeap_Initialize( void* buffer, unsigned length );
void SimpleHeap_Release( void* ptr );
void* SimpleHeap_Allocate( unsigned len );
void* SimpleHeap_ReAllocate( void* ptr, unsigned len );

The important function is SimpleHeap_Initialize, you have to call this to setup the heap from which the allocations will be done. What I did is I put the heap right at the top of the RLP reserved area. And after that I just used SimpleHeap_Allocate and SimpleHeap_Release to manage the memory allocations etc. I have an initialization RLP function which initializes the heap and after that I can use the heap from the rest of my RLP functions.

For my test I initalized the heap with a size of 0x4b000.


SimpleHeap_Initialize((void*)0xa00b5000, 0x4b000);

When I get back from my trip I will look at implementing the necessary stubs for newlib so that standard malloc/free etc. can run off this, never done anything like that before but there is a first time for everything :slight_smile:

Great! Makes sense. Thanks for sharing! :slight_smile:

Thanks for sharing! I did however realize that having a pre-allocated array is better for the 3d engine as this prevents heap fragmentation.