Question
Using malloc
Posted on March 18, 2015 at 09:04
Hi
I have a bit of a problem when using malloc so I was wondering what others think about this. I use malloc in my application, mainly because the application is entirely asynchronous so either I have to use malloc or I need buffers of my datatypes declared in masses. I chose malloc due to its flexibility.I was using Atollic TrueStudio and everything built just fine, but now I wanted to try my hand with basic Eclipse and the following toolchain: https://launchpad.net/gcc-arm-embeddedOnce I made this switch I was struck with undefined reference to `_sbrk'. Fair enough since I use malloc so I look around and find a newlib.c with some basic functions for read and write (using UART) and also an _sbrk implementation.caddr_t _sbrk(int incr) {
extern char _ebss; // Defined by the linker static char *heap_end; char *prev_heap_end; if (heap_end == 0) { heap_end = &_ebss; } prev_heap_end = heap_end; char * stack = (char*) __get_MSP(); if (heap_end + incr > stack) { _write (STDERR_FILENO, ''Heap and stack collision\n'', 25); errno = ENOMEM; return (caddr_t) -1; } heap_end += incr; return (caddr_t) prev_heap_end;}The problem is that __get_MSP is deprecated as far as google could tell me. So if you aren't allowed to read the stack pointer value, how can you detect a stack collision?So my questions are many:Can I link with another library to get malloc to work or maybe even just a linker flag?Is it possible to get the stack pointer value or should I instead create a large buffer in memory and let it be the borders form _sbrk?Should I use another toolchain?Is there some other solution or angle I am not seeing?So feel free to point out anything I am open to any idea or solution.Many thanks in advance,Per Smitt #stack-pointers