View previous topic :: View next topic |
Author |
Message |
Pret
Joined: 18 Jul 2006 Posts: 92 Location: Iasi, Romania
|
RAM used - worst case |
Posted: Wed Sep 05, 2007 1:42 am |
|
|
Sice all memory is reserved for a tipial code, with (or without) stack, interrupt handling and so on, how can be two different sizes of occupied RAM? What does "worst case" means since there is no stack and memory required for all routines are well known? Which one is relevant? |
|
|
icesynth
Joined: 03 Sep 2007 Posts: 32 Location: Edmonton, Alberta
|
|
Posted: Wed Sep 05, 2007 8:20 am |
|
|
Use the "Worst Case" value. _________________ Programming for the the real world.
--Chris Burchett
Sylver Technologies Inc. |
|
|
Neutone
Joined: 08 Sep 2003 Posts: 839 Location: Houston
|
Re: RAM used - worst case |
Posted: Wed Sep 05, 2007 11:15 am |
|
|
Pret wrote: | Sice all memory is reserved for a tipial code, with (or without) stack, interrupt handling and so on, how can be two different sizes of occupied RAM? What does "worst case" means since there is no stack and memory required for all routines are well known? Which one is relevant? |
Local variable are only active while the function they are declared within is active. Worst case then would be while running a function having many or large local variables. |
|
|
Pret
Joined: 18 Jul 2006 Posts: 92 Location: Iasi, Romania
|
Re: RAM used - worst case |
Posted: Wed Sep 05, 2007 2:09 pm |
|
|
Neutone wrote: | Local variable are only active while the function they are declared within is active. Worst case then would be while running a function having many or large local variables. |
I dont know what exactly do you mean with "active function", but since worst case is relevant what's the use of previous case? Also i could say 1 used byte of RAM... but worst case 500 bytes of RAM. |
|
|
ckielstra
Joined: 18 Mar 2004 Posts: 3680 Location: The Netherlands
|
|
Posted: Wed Sep 05, 2007 4:16 pm |
|
|
The key is in the difference from global declared variables and local variables.
On a 'normal' processor with a stack pointer the size of the data pushed onto the stack would vary depending on where you are in the program flow. Every nested function call and local variable would add to the size of the stack but returning from a function would decrease the stack size again.
The PIC processor has no hardware stack pointer, that's why many PIC compilers are assigning all variables to a fixed memory location. Compared to the competitors the CCS compiler is smart in this respect as it analyzes your source code for variables that are never used at the same time so it can 're-use' the memory location.
This re-use feature is where the 'minimum' and 'worst case' come from. The minimum memory is the amount of memory allocated to memory that is assigned to a single variable and at no point in time can be used by another variable, an example is a global variable.
The 'worst case' memory is more like a 'maximum' amount of memory used, i.e. the value of 'minimum' plus the total amount of 'local' variables. Subtract these two values and you get the maximum stack size value.
Example 1; 2 large global buffers Code: | int8 buf1[50];
int8 buf2[50];
void func1()
{
Buf1[0] = 1;
}
void func2()
{
Buf2[0] = 1;
}
void main()
{
int8 Local;
func1();
func2();
} | Ram used at main() level: 106
Worst case: 106
Both values are equal, it makes no sense at all to report two values as you are saying.
Example 2; the global buffers are now local to the function Code: | void func1()
{
int8 buf1[50];
Buf1[0] = 1;
}
void func2()
{
int8 buf2[50];
Buf2[0] = 1;
}
void main()
{
int8 Local;
func1();
func2();
} | Ram used at main() level: 6
Worst case: 56
Now both values are different. The mimimum RAM size is reduced to 6 bytes for CCS internal variables and the Local variable. The memory space for the Buf arrays can be re-used and worst-case RAM usage has dropped from 106 to 56 bytes. |
|
|
Pret
Joined: 18 Jul 2006 Posts: 92 Location: Iasi, Romania
|
|
Posted: Thu Sep 06, 2007 12:27 am |
|
|
I see... Practically, difference between those values are bytes available for other functions if you put them in main. Interesting...
Thanks. |
|
|
|