Question

i am thinking of setting up a memory area in my STM32L151 (Cortex M3) for heap to be used in malloc().

I am using the GNU ARM toolchain and newlib.

I know how to set up the stack in the linker script, assign the stack address to SP... and that the ARM uC can access the stack via the stack pointer, SP.

My question is :how does GNU GCC compiler knows where is the heap address? I can set up the heap in the linker script like how i does for the stack.. But how do i pass the heap address information to the GCC compiler?

Thank you very much.

Was it helpful?

Solution

I did something similar on a cortex-m3 platform at a previous job, also using new lib. I went about it by implementing a custom _sbrk()/_sbrk_r() function, which malloc() uses. You would create a static array as large as you need for the heap, and your _sbrk()/_sbrk_r() function would adjust within that.

For example (only minimal error checking, for clarity):

static char mem_array[MAX_HEAP_SIZE];
static char *_cur_brk = mem_array;
void *_sbrk_r(struct _reent *reent, ptrdiff_t diff)
{
    char *_old_brk = _cur_brk;
    if (_cur_brk + diff > MAX_HEAP_SIZE) {
        errno = ENOMEM;
        return (void *)-1;
    }
    _cur_brk += diff;
    return _old_brk;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top