lactose99 wrote on Sunday, August 31, 2014:
Hello.
I have been seeing some strange stuff in my software (debugger stepping wrong, variables passed to a function that land with the wrong value…). I wrote a quick function to see how much free memory is left, and it indicates I am out of memory.
Some info:
-using RX600 CPU
-using heap3.c
-optimization = 0
My outOfMemory function assumes stack is at the top moving down, and heap is on the bottom moving up. Experimentally, this matches my observations. So is measuring from the bottom of the stack to the top of the heap a valid way of determining free memory ?
void outOfMemoryCheck()
{
// address of this var used to find bottom of stack
int32_t testStackVar = 0;
uint8_t *pHeap = NULL;
#define TEST_ALLOCATION_SIZE 8
// determine the top of the heap
pHeap = malloc(TEST_ALLOCATION_SIZE);
// available_memory is bottom_of_stack - top_of_heap
// stack bottom should never be below heap top
if((NULL == pHeap) || ((int32_t) pHeap > (int32_t) &testStackVar))
{
// we are out of memory, loop here
while(1);
}
free(pHeap);
}
Thanks.