Hello,
I have a situation where I would like to reuse a large block of memory used by one thread, later on in the thread. I was under the impression that function memory is added to the stack and should be available when it goes out of scope, is this the case?
Using -report in my makefile to see the stack usage it seems that the memory is not available after it has gone out of scope, and so more stack space is used than I would like.
To give an example:
void function1() { int array1[5000]; int array2[5000]; // populate memory and do stuff return; } void function2() { int array1[5000]; int array2[5000]; // do stuff return; } int main() { function1(); function2(); }
Should this program require 40k (4*5000*2) or 80k? I was hoping memory allocated within a fucntion would be available again outside but that doesn't seem to be happening in my case. In reality the memory will be different types (unsigned char[] and int[]) so defininig it beforehand and passing through might be a problem? Ideally I want a new definition/allocation for the second function, but not have it require any more memory since memory from function1 is no longer needed. Hopefully there is something simple I am overlooking.
Thanks,
Greg
How to reuse XC stack memory?
-
- Newbie
- Posts: 1
- Joined: Fri Apr 04, 2014 4:48 pm
-
- XCore Expert
- Posts: 754
- Joined: Thu Dec 10, 2009 6:56 pm
Hi,
On entering the function there will be stack allocated for the variables declared in the function and this stack space will be released
when returning from the function.
If we look at the disassembly of function2 we see the following:
There are 10001 stack words allocated (40004 bytes) and released.
If we look at the report function of the tools we see the following:
This matches our expection that roughly 40KB of stack space is used by the program.
On entering the function there will be stack allocated for the variables declared in the function and this stack space will be released
when returning from the function.
If we look at the disassembly of function2 we see the following:
Code: Select all
function2: entsp 10001 retsp 10001
There are 10001 stack words allocated (40004 bytes) and released.
If we look at the report function of the tools we see the following:
Code: Select all
Constraint check for "tile[0]" (node "0", tile 0): Cores available: 8, used: 1 . OKAY Timers available: 10, used: 1 . OKAY Chanends available: 32, used: 0 . OKAY Memory available: 65536, used: 40828 . OKAY (Stack: 40144, Code: 588, Data: 96)Constraints checks PASSED.
This matches our expection that roughly 40KB of stack space is used by the program.
Code: Select all