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;
}