www.ti.com
Data Memory
Notice that both a() and b() freely use some of the same scratch registers and no saving and restoring of these registers is necessary. This is possible because both functions, a() and b(), agree on the set of scratch registers and that values in these registers are indeterminate at the beginning of each function.
By analogy, we partition all memory into two groups: scratch and persistent.
∙Scratch memory is memory that is freely used by an algorithm without regard to its prior contents, i.e., no assumptions about the content can be made by the algorithm and the algorithm is free to leave it in any state.
∙Persistent memory is used to store state information while an algorithm instance is not executing.
Persistent memory is any area of memory that an algorithm can write to assume that the contents are unchanged between successive invocations of the algorithm within an application. All physical memory has this behavior, but applications that share memory among multiple algorithms may opt to overwrite some regions of memory (e.g.,
A special variant of persistent memory is the
Note that a simpler alternative to declaring
The importance of making a distinction between scratch memory and persistent memory is illustrated in Figure
Figure 2-1. Scratch vs Persistent Memory Allocation
Algorithm A |
| Scratch |
| |
Algorithm B |
|
|
|
|
|
|
|
| |
| Scratch |
|
| |
Algorithm C |
|
|
|
|
|
|
|
| |
| Scratch |
| ||
0 |
|
| ||
|
|
|
| |
|
|
|
| |
Algorithm C 1 |
| Scratch |
| |
Physical |
|
|
|
|
|
|
|
| |
|
|
|
| |
Memory |
| Scratch |
| |
|
|
|
|
|
|
|
|
| |
|
|
|
|
|
| 0000 |
|
|
Persistent A
Persistent B
Persistent A | Persistent B | |
|
|
|
FFFF
All algorithm scratch memory can be "overlaid" on the same physical memory. Without the distinction between scratch and persistent memory, it would be necessary to strictly partition memory among algorithms, making the total memory requirement the sum of all algorithms'memory requirements. On the other hand, by making the distinction, the total memory requirement for a collection of algorithms is the sum of each algorithm'sdistinct persistent memory, plus any shared
SPRU352G | General Programming Guidelines | 21 |