Merge pull request #48 from adamgreen/gccRetargetSbrk

gcc: Provide _sbrk implementation compatible with RTX
pull/62/head
Bogdan Marinescu 2013-09-04 01:12:31 -07:00
commit 45565cb055
1 changed files with 27 additions and 0 deletions

View File

@ -425,3 +425,30 @@ extern "C" void __iar_argc_argv() {
mbed_main();
}
#endif
// Provide implementation of _sbrk (low-level dynamic memory allocation
// routine) for GCC_ARM which compares new heap pointer with MSP instead of
// SP. This make it compatible with RTX RTOS thread stacks.
#if defined(TOOLCHAIN_GCC_ARM)
// Linker defined symbol used by _sbrk to indicate where heap should start.
extern "C" int __end__;
// Turn off the errno macro and use actual global variable instead.
#undef errno
extern "C" int errno;
// Dynamic memory allocation related syscall.
extern "C" caddr_t _sbrk(int incr) {
static unsigned char* heap = (unsigned char*)&__end__;
unsigned char* prev_heap = heap;
unsigned char* new_heap = heap + incr;
if (new_heap >= (unsigned char*)__get_MSP()) {
errno = ENOMEM;
return (caddr_t)-1;
}
heap = new_heap;
return (caddr_t) prev_heap;
}
#endif