Reputation: 9389
I have an STM32F0 application using the yagarto GCC compiler. I've implemented _sbrk newlib_stubs.c. The problem I'm running into is I'm calling malloc(256) an in-turn malloc in invoking _sbrk and asking for 4K of memory. My system only has a total of 8K so obviously this is a problem. I've monitored calls to _sbrk and it seems to make a few calls prior to this allocating smaller amounts of memory.
Right now I'm getting around this by holding my nose to avoid the stench and doing something like:
if(requested > 512)
requested = 512;
in _sbrk before I reposition the heap.
Although the system seems rock solid, I'm fairly certain this is going to come back and haunt me when I least expect it.
Upvotes: 0
Views: 264
Reputation: 9389
With the subtle hints provided in the comments about not using malloc, I just ended up creating something to allocate my own memory. I don't ever need to release it so that made it simple. Only thing that stung me for about an hour was maintain the 32 bit alignment.
Here's my implementation pm_ stands for poor mans.
#define HEAPSIZE 0x800 //2K
uint16_t __heapPtr = 0;
uint8_t __customHeap[HEAPSIZE];
void *pm_malloc(uint16_t size){
while(__heapPtr % 4 != 0)
__heapPtr++;
void *block = &__customHeap[__heapPtr];
if(size + __heapPtr > HEAPSIZE)
return 0;
__heapPtr += size;
return block;
}
My life has been C#, Java and Objective-C for the past 15 years, so any feedback would be appreciated.
Upvotes: 1