RIOT-OS/RIOT — pull request #1782 by benpicco (2014-10-08 21:42:37+00:00)
Title: check if the requested memory is really available in _sbrk_r

Most RIOT ports do not check whether the memory returned by `_sbrk_r` actually exists, lpc1768 is the notable exception.

For it and all other newlib using ports, heap comes after the stack and occupies the remaining available RAM, so it's only necessary to check whether the heap pointer would exceed the physical range.

Unfortunately I'm not very knowledgeable about linkerscripts, I'd like the `.heap` section to be as big as LENGTH(ram) - end_of_stack, however I didn't find out how to do that. So I ended up with the not as nice, but also correct

. = LENGTH(ram) - SIZEOF(.stack) - SIZEOF(.bss) - SIZEOF(.relocate);

Does someone know a nicer solution?
The alignment is also just cargo cult from the stack section, but seems reasonable.

I've also re-added an old test that causes a hard fault without the patch (because it eventually tries to allocate memory that doesn't exist), but works as intended with the patch, even free works (due to newlib magic).