storage management
TRANSCRIPT
![Page 1: Storage Management](https://reader034.vdocuments.site/reader034/viewer/2022042606/544f9723b1af9fe42f8b46bb/html5/thumbnails/1.jpg)
Storage Management
• Different features in a language causes different storage
management techniques to be used.
– FORTRAN: no recursive calls, no dynamic storage
management.
– Pascal: stack-based storage management.
– LISP: garbage collection.
Language implementers decide about the details.
Programmers don’t know about it.
10/18/2012 Lovely Professional University,
![Page 2: Storage Management](https://reader034.vdocuments.site/reader034/viewer/2022042606/544f9723b1af9fe42f8b46bb/html5/thumbnails/2.jpg)
Major Run-Time Elements Requiring
Storage
• Data
• Operations
10/18/2012 Lovely Professional University,
![Page 3: Storage Management](https://reader034.vdocuments.site/reader034/viewer/2022042606/544f9723b1af9fe42f8b46bb/html5/thumbnails/3.jpg)
Data and program Requiring Storage
• Code segments for translated user programs.
• System run-time programs.
– Supporting user programs.
– Like library routines, software interpreters or translator, storage
management routines.
• User-defined data structures and constants.
• Subprogram return points.
10/18/2012 Lovely Professional University,
![Page 4: Storage Management](https://reader034.vdocuments.site/reader034/viewer/2022042606/544f9723b1af9fe42f8b46bb/html5/thumbnails/4.jpg)
• Referencing environments.
– Identifier associations (LISP A-list)
• Temporaries in expression evaluation.
– Recursive function calls make it a lot.
• Temporaries in parameter transmission.
– Resulting values for evaluation of actual parameters are stored
in temporaries until the total evaluation is completed.
Data and program Requiring Storage
10/18/2012 Lovely Professional University,
![Page 5: Storage Management](https://reader034.vdocuments.site/reader034/viewer/2022042606/544f9723b1af9fe42f8b46bb/html5/thumbnails/5.jpg)
• Input-output buffers.
– Temporary storage areas used between the time of the actual
physical transfer of the data to or from external storage and
the program-initiated input / output operation.
• Miscellaneous system data.
– System data like tables, status information for input-output,
...
Data and program Requiring Storage
10/18/2012 Lovely Professional University,
![Page 6: Storage Management](https://reader034.vdocuments.site/reader034/viewer/2022042606/544f9723b1af9fe42f8b46bb/html5/thumbnails/6.jpg)
Storage organization
Subdivision of Runtime Memory
Typical subdivision of runtime memory into code and data
areas
code
Static data
stack
heap
The generated target code;
Data objects;
A counterpart of the control stack to
keep track of procedure activations
10/18/2012 Lovely Professional University,
![Page 7: Storage Management](https://reader034.vdocuments.site/reader034/viewer/2022042606/544f9723b1af9fe42f8b46bb/html5/thumbnails/7.jpg)
Storage allocation strategies
Storage allocation strategies are as follows:
– Static allocation
• Lays out storage for all data objects at compile time.
– Stack allocation
• Manages the run-time storage as a stack.
– Heap allocation
• Allocates and de-allocates storage as needed at run time from a heap.
10/18/2012 Lovely Professional University,
![Page 8: Storage Management](https://reader034.vdocuments.site/reader034/viewer/2022042606/544f9723b1af9fe42f8b46bb/html5/thumbnails/8.jpg)
Names are bound to storage as the program is compiled,
so there is no need for a run-time support package.
• Since the bindings do not change at run time,
• Every time a procedure is activated,
• Its names are bound to the same storage locations.
Static allocation
10/18/2012 Lovely Professional University,
![Page 9: Storage Management](https://reader034.vdocuments.site/reader034/viewer/2022042606/544f9723b1af9fe42f8b46bb/html5/thumbnails/9.jpg)
Some Limitations
– The size of a data object and constraints on its
position in memory must be known at compile
time.
– Recursive procedures are restricted, because all
activations of a procedure use the same
bindings for local name.
– Data structures cannot be created dynamically.
10/18/2012 Lovely Professional University,
![Page 10: Storage Management](https://reader034.vdocuments.site/reader034/viewer/2022042606/544f9723b1af9fe42f8b46bb/html5/thumbnails/10.jpg)
– A separate area of run-time memory, called
a heap.
– Heap allocation parcels out pieces of
contiguous storage, as needed for activation
records or other objects. Pieces may be de-
allocated in any order.
I -11 J -11 X 0 A -21
Heap allocation
10/18/2012 Lovely Professional University,
![Page 11: Storage Management](https://reader034.vdocuments.site/reader034/viewer/2022042606/544f9723b1af9fe42f8b46bb/html5/thumbnails/11.jpg)
Heap allocation is useful if either of the following is
possible.
1. The value of local names must be retained when
an activation ends.
2. A called activation outlives the caller.
10/18/2012 Lovely Professional University,
![Page 12: Storage Management](https://reader034.vdocuments.site/reader034/viewer/2022042606/544f9723b1af9fe42f8b46bb/html5/thumbnails/12.jpg)
Main idea
– Based on the idea of a control stack; storage is
organized as a stack, and activation records are pushed
and popped as activations begin and end, respectively.
– Storage for the locals in each call of a procedure is
contained in the activation record for that call.
– Locals are bound to fresh storage in each activation
10/18/2012 Lovely Professional University,
![Page 13: Storage Management](https://reader034.vdocuments.site/reader034/viewer/2022042606/544f9723b1af9fe42f8b46bb/html5/thumbnails/13.jpg)
10/18/2012 Lovely Professional University,
![Page 14: Storage Management](https://reader034.vdocuments.site/reader034/viewer/2022042606/544f9723b1af9fe42f8b46bb/html5/thumbnails/14.jpg)
10/18/2012 Lovely Professional University,
![Page 15: Storage Management](https://reader034.vdocuments.site/reader034/viewer/2022042606/544f9723b1af9fe42f8b46bb/html5/thumbnails/15.jpg)
10/18/2012 Lovely Professional University,
![Page 16: Storage Management](https://reader034.vdocuments.site/reader034/viewer/2022042606/544f9723b1af9fe42f8b46bb/html5/thumbnails/16.jpg)
10/18/2012 Lovely Professional University,
![Page 17: Storage Management](https://reader034.vdocuments.site/reader034/viewer/2022042606/544f9723b1af9fe42f8b46bb/html5/thumbnails/17.jpg)
10/18/2012 Lovely Professional University,
![Page 18: Storage Management](https://reader034.vdocuments.site/reader034/viewer/2022042606/544f9723b1af9fe42f8b46bb/html5/thumbnails/18.jpg)
10/18/2012 Lovely Professional University,
![Page 19: Storage Management](https://reader034.vdocuments.site/reader034/viewer/2022042606/544f9723b1af9fe42f8b46bb/html5/thumbnails/19.jpg)
“i “ is bounded to the most recently created i
10/18/2012 Lovely Professional University,
![Page 20: Storage Management](https://reader034.vdocuments.site/reader034/viewer/2022042606/544f9723b1af9fe42f8b46bb/html5/thumbnails/20.jpg)
10/18/2012 Lovely Professional University,
![Page 21: Storage Management](https://reader034.vdocuments.site/reader034/viewer/2022042606/544f9723b1af9fe42f8b46bb/html5/thumbnails/21.jpg)
10/18/2012 Lovely Professional University,
![Page 22: Storage Management](https://reader034.vdocuments.site/reader034/viewer/2022042606/544f9723b1af9fe42f8b46bb/html5/thumbnails/22.jpg)
Binding of names • Same name may denote different data objects
(i.e., locations) at runtime
• Two mapping are used
– Environment
• maps a name to a storage (or binding of a name to a storage)
– State
• maps storage to value
name locatio
n value
environment state
10/18/2012 Lovely Professional University,
![Page 23: Storage Management](https://reader034.vdocuments.site/reader034/viewer/2022042606/544f9723b1af9fe42f8b46bb/html5/thumbnails/23.jpg)
10/18/2012 Lovely Professional University,
![Page 24: Storage Management](https://reader034.vdocuments.site/reader034/viewer/2022042606/544f9723b1af9fe42f8b46bb/html5/thumbnails/24.jpg)
Procedure, Activation and Lifetime
• A procedure is activated when called
• The lifetime of an activation of a procedure is the sequence of steps between the first and last steps in the execution of the procedure body
• A procedure is recursive if a new activation can begin before an earlier activation of the same procedure has ended
10/18/2012 Lovely Professional University,
![Page 25: Storage Management](https://reader034.vdocuments.site/reader034/viewer/2022042606/544f9723b1af9fe42f8b46bb/html5/thumbnails/25.jpg)
10/18/2012 Lovely Professional University,
![Page 26: Storage Management](https://reader034.vdocuments.site/reader034/viewer/2022042606/544f9723b1af9fe42f8b46bb/html5/thumbnails/26.jpg)
Activation Records/Frames
A contiguous block of storage that stores information needed
by a single execution of a procedure
A general activation record
Returned value
Actual parameters
Optional control link
Optional access link
Saved machine status
Local data
temporaries
Such as those values in the evaluation of expressions
Data that is local to the execution of a procedure
Information about the state of the machine just before the procedure
is called
Refer to non-local data held in other activation records/fixed place
Point to the activation record of the caller
Used by the caller to supply parameters to the callee
The value that the callee return to caller
10/18/2012 Lovely Professional University,
![Page 27: Storage Management](https://reader034.vdocuments.site/reader034/viewer/2022042606/544f9723b1af9fe42f8b46bb/html5/thumbnails/27.jpg)
10/18/2012 Lovely Professional University,
![Page 28: Storage Management](https://reader034.vdocuments.site/reader034/viewer/2022042606/544f9723b1af9fe42f8b46bb/html5/thumbnails/28.jpg)
Activation Trees Activation trees
Represents the activations of procedures during program execution using a tree
Activation of procedure
› Execution of a procedure body
› Lifetime of activation Sequence of steps between first and last statements
Non-overlapping,
nested activation,
Recursive if a new activation can begin before an earlier activation of the same procedure
has ended.
10/18/2012 Lovely Professional University,
![Page 29: Storage Management](https://reader034.vdocuments.site/reader034/viewer/2022042606/544f9723b1af9fe42f8b46bb/html5/thumbnails/29.jpg)
10/18/2012 Lovely Professional University,
![Page 30: Storage Management](https://reader034.vdocuments.site/reader034/viewer/2022042606/544f9723b1af9fe42f8b46bb/html5/thumbnails/30.jpg)
Activation Trees
A tree to depict the control flow enters and leaves activation of a procedure
f(4)
f(3) f(2)
f(2) f(1) f(1) f(0)
f(1) f(0)
Enter f(4) Enter f(3) Enter f(2) Enter f(1) Leave f(1) Enter f(0) Leave f(0) Leave f(2) Enter f(1) Leave f(1) Leave f(3)
Enter f(2) Enter f(1) Leave f(1) Enter f(0) Leave f(0) Leave f(2) Leave f(4)
10/18/2012 Lovely Professional University,
![Page 31: Storage Management](https://reader034.vdocuments.site/reader034/viewer/2022042606/544f9723b1af9fe42f8b46bb/html5/thumbnails/31.jpg)
10/18/2012 Lovely Professional University,
![Page 32: Storage Management](https://reader034.vdocuments.site/reader034/viewer/2022042606/544f9723b1af9fe42f8b46bb/html5/thumbnails/32.jpg)
10/18/2012 Lovely Professional University,
![Page 33: Storage Management](https://reader034.vdocuments.site/reader034/viewer/2022042606/544f9723b1af9fe42f8b46bb/html5/thumbnails/33.jpg)
parent
Occurs
first
10/18/2012 Lovely Professional University,
![Page 34: Storage Management](https://reader034.vdocuments.site/reader034/viewer/2022042606/544f9723b1af9fe42f8b46bb/html5/thumbnails/34.jpg)
• Each node represents an activation of a procedure
• The root represents the activation of the main program
• The node for a is the parent of the node for b if and only if the control flows from activation of a to b, and
• The node for a is to the left of the node for b if and only if the lifetime of a occurs before the lifetime of b.
10/18/2012 Lovely Professional University,
![Page 35: Storage Management](https://reader034.vdocuments.site/reader034/viewer/2022042606/544f9723b1af9fe42f8b46bb/html5/thumbnails/35.jpg)
Control Flow • The control flow in a program P corresponds
to depth-first traversal of activation tree – Starts at the root
– Visits a node before its children
– Recursively visits children at each node from left-to-right
– The sequence of procedure calls corresponds to a preorder traversal of the activation tree
– The sequence of returns corresponds to postorder traversal of the activation tree
10/18/2012 Lovely Professional University,
![Page 36: Storage Management](https://reader034.vdocuments.site/reader034/viewer/2022042606/544f9723b1af9fe42f8b46bb/html5/thumbnails/36.jpg)
Sequences of Calls and Returns • Procedures calls implemented by calling Sequences
• Calling sequences consists of code that allocates an activation record on the stack by entering information into the fields
• Return sequences same as code to calling sequences – They restore the state of the machine in a way that the caller can
continue its execution
• Code in calling sequences divided between Caller and Callee
• In general, if procedure p calls Procedure q – The activation of q terminates normally
– The activation of q aborts and p ends at the same time
– The activation of q terminates due to some exception. The procedure p may or may not terminate
10/18/2012 Lovely Professional University,
![Page 37: Storage Management](https://reader034.vdocuments.site/reader034/viewer/2022042606/544f9723b1af9fe42f8b46bb/html5/thumbnails/37.jpg)
Activation Records: Control Stack (or run-time Stack)
• Control stack (run-time stack) – Keeps track of live procedure activation (or
FRAME)
• Where the root of the activation tree is at the bottom of the stack – Having node nj on the top of the stack means
• There exists a path in Activation tree from node nj to the root
• E.g,
– <root,…,ni, nj>
10/18/2012 Lovely Professional University,
![Page 38: Storage Management](https://reader034.vdocuments.site/reader034/viewer/2022042606/544f9723b1af9fe42f8b46bb/html5/thumbnails/38.jpg)
e.g., If control is currently in the activation write() of the tree, then activation
record for write() is at the top of the control stack.
Below it is the activation record for output(), and the bottom is the activation
record for main()
10/18/2012 Lovely Professional University,
![Page 39: Storage Management](https://reader034.vdocuments.site/reader034/viewer/2022042606/544f9723b1af9fe42f8b46bb/html5/thumbnails/39.jpg)
10/18/2012 Lovely Professional University,
![Page 40: Storage Management](https://reader034.vdocuments.site/reader034/viewer/2022042606/544f9723b1af9fe42f8b46bb/html5/thumbnails/40.jpg)
10/18/2012 Lovely Professional University,
![Page 41: Storage Management](https://reader034.vdocuments.site/reader034/viewer/2022042606/544f9723b1af9fe42f8b46bb/html5/thumbnails/41.jpg)
10/18/2012 Lovely Professional University,
![Page 42: Storage Management](https://reader034.vdocuments.site/reader034/viewer/2022042606/544f9723b1af9fe42f8b46bb/html5/thumbnails/42.jpg)
10/18/2012 Lovely Professional University,
![Page 43: Storage Management](https://reader034.vdocuments.site/reader034/viewer/2022042606/544f9723b1af9fe42f8b46bb/html5/thumbnails/43.jpg)
10/18/2012 Lovely Professional University,
![Page 44: Storage Management](https://reader034.vdocuments.site/reader034/viewer/2022042606/544f9723b1af9fe42f8b46bb/html5/thumbnails/44.jpg)
10/18/2012 Lovely Professional University,
![Page 45: Storage Management](https://reader034.vdocuments.site/reader034/viewer/2022042606/544f9723b1af9fe42f8b46bb/html5/thumbnails/45.jpg)
• In this example, as is true in general, procedure activations are nested in time. If an activation of procedure p calls procedure q, then that activation of q must end before the activation of p can end.
• There are three common cases:
1. The activation of q terminates normally. Then in essentially any language, control resumes just after the point of p at which the call to q was made.
2. The activation of q, or some procedure q called, either directly or indirectly, aborts; i.e., it becomes impossible for execution to continue. In that case, p ends simultaneously with q.
3. The activation of q terminates because of an exception that q cannot handle. Procedure p may handle the exception, in which case the activation of q has terminated while the activation of p continues, although not necessarily from the point at which the call to q was made. If p cannot handle the exception, then this activation of p terminates at the same time as the activation of q, and presumably the exception will be handled by some other open activation of a procedure
10/18/2012 Lovely Professional University,
![Page 46: Storage Management](https://reader034.vdocuments.site/reader034/viewer/2022042606/544f9723b1af9fe42f8b46bb/html5/thumbnails/46.jpg)
10/18/2012 Lovely Professional University,
![Page 47: Storage Management](https://reader034.vdocuments.site/reader034/viewer/2022042606/544f9723b1af9fe42f8b46bb/html5/thumbnails/47.jpg)
10/18/2012 Lovely Professional University,
![Page 48: Storage Management](https://reader034.vdocuments.site/reader034/viewer/2022042606/544f9723b1af9fe42f8b46bb/html5/thumbnails/48.jpg)
Designing Calling Sequences
• Values communicated between caller and callee are generally placed at the beginning of callee’s activation record
• Fixed-length items: are generally placed at the middle
• Items whose size may not be known early enough: are placed at the end of activation record
• We must locate the top-of-stack pointer judiciously: a common approach is to have it point to the end of fixed length fields.
10/18/2012 Lovely Professional University,
![Page 49: Storage Management](https://reader034.vdocuments.site/reader034/viewer/2022042606/544f9723b1af9fe42f8b46bb/html5/thumbnails/49.jpg)
10/18/2012 Lovely Professional University,
![Page 50: Storage Management](https://reader034.vdocuments.site/reader034/viewer/2022042606/544f9723b1af9fe42f8b46bb/html5/thumbnails/50.jpg)
Calling Sequence
• The caller evaluates the actual parameters
• The caller stores a return address and the old value of top-sp into the callee's activation record.
• The callee saves the register values and other status information.
• The callee initializes its local data and begins execution.
10/18/2012 Lovely Professional University,
![Page 51: Storage Management](https://reader034.vdocuments.site/reader034/viewer/2022042606/544f9723b1af9fe42f8b46bb/html5/thumbnails/51.jpg)
Implementing Variable Length Data layout on Stack: Another Example
• Procedure p calls procedure q
• P has three local arrays
• Sizes of arrays are not determined at compile time
10/18/2012 Lovely Professional University,
![Page 52: Storage Management](https://reader034.vdocuments.site/reader034/viewer/2022042606/544f9723b1af9fe42f8b46bb/html5/thumbnails/52.jpg)
Access to dynamically allocated arrays (NOTE: Here the top of the stack is downwards)
Ref. Text book 10/18/2012 Lovely Professional University,
![Page 53: Storage Management](https://reader034.vdocuments.site/reader034/viewer/2022042606/544f9723b1af9fe42f8b46bb/html5/thumbnails/53.jpg)
Top and top-sp pointers
• Top - marks the actual top of the stack ( next activation record will begin at this location)
• Top-sp - locates the fixed length fields of the top activation record
• These are restored from the saved control link in the activation record of the called function
10/18/2012 Lovely Professional University,
![Page 54: Storage Management](https://reader034.vdocuments.site/reader034/viewer/2022042606/544f9723b1af9fe42f8b46bb/html5/thumbnails/54.jpg)
Reposition of top and top-sp
• Positions of these pointers is computed in terms of sizes that will be known at run time
• Code to reposition these is generated at compile time
10/18/2012 Lovely Professional University,
![Page 55: Storage Management](https://reader034.vdocuments.site/reader034/viewer/2022042606/544f9723b1af9fe42f8b46bb/html5/thumbnails/55.jpg)
Better Alternative
• Allocate dynamic data on heap
• Maintain only the address of the allocated memory (fixed )
[heap memory management will be discussed later while discussion on garbage collection]
10/18/2012 Lovely Professional University,
![Page 56: Storage Management](https://reader034.vdocuments.site/reader034/viewer/2022042606/544f9723b1af9fe42f8b46bb/html5/thumbnails/56.jpg)
Static versus dynamic memory allocation
Static – Storage allocation decision is static if it can be made by the compiler only by looking at the source code text
example: all variables of fixed size
struct sample { int x, y; float u,v,w;}
Dynamic –
Storage allocation decision is dynamic if it is based on execution of the program e.g.
(1) allocation of memory (heap) through malloc() etc
(2) allocation of memory (stack) during function calls
10/18/2012 Lovely Professional University,
![Page 57: Storage Management](https://reader034.vdocuments.site/reader034/viewer/2022042606/544f9723b1af9fe42f8b46bb/html5/thumbnails/57.jpg)
Access Links: Nested procedure definitions
(Example language-only looks like C) fun1()
{
integer a,b,c;
a=2;b=3;c=4;
integer fun2()
{
integer c,d,e;
c=5;d=7;
e=a+b+c+d;
return e;
}
}
fun1:
Locals : a,b,c
fun2:
Locals : c,d,e
fun2 needs to access values of a
and b
c is redefined in fun 2
Access link is required
C does not require this feature as the
scope of variables is within the function
definition
10/18/2012 Lovely Professional University,
![Page 58: Storage Management](https://reader034.vdocuments.site/reader034/viewer/2022042606/544f9723b1af9fe42f8b46bb/html5/thumbnails/58.jpg)
d=7
Access link-none
Returned values-none
Control link – to self/driver
Actual parameter - none xFFF
C
xFFF
8
xFFF
4
xFFF
0
xFFE
C
xFFE
8
xFFE
4
xFFE
0
Control links-to fun1
Returned value ‘e’
Actual parameter -none
a=2
Link(=xFFFC for example) to access values a,b
c=5
xFFD
C
xFFD
8
xFFD
4
xFFD
0
b=3
c=4 Activation
record of fun1()
Fixed variables
Activation
record of fun2()
NOTE:
1. Sizes of the activation records for different functions are different
2. Some fields are common in all activation records
The nesting
depth of 1 is
handled here
10/18/2012 Lovely Professional University,
![Page 59: Storage Management](https://reader034.vdocuments.site/reader034/viewer/2022042606/544f9723b1af9fe42f8b46bb/html5/thumbnails/59.jpg)
An example : Let the nesting depth of p be np
q()
{ int b
…
w(){ int d;
…………….f(){ int e;
………….. s(){
p(){
int a;
a=b+d+e;
}
}
}
}
Assuming b,c,d
are not redefined 10/18/2012 Lovely Professional University,
![Page 60: Storage Management](https://reader034.vdocuments.site/reader034/viewer/2022042606/544f9723b1af9fe42f8b46bb/html5/thumbnails/60.jpg)
Whose activation record is at the top?
Activation Record of q()
Activation Record of w()
……
……
Activation Record of s()
Activation Record of f()
……
Activation Record of p() p() (at nesting depth np)
needs to access different
activation records
np >=nq
10/18/2012 Lovely Professional University,
![Page 61: Storage Management](https://reader034.vdocuments.site/reader034/viewer/2022042606/544f9723b1af9fe42f8b46bb/html5/thumbnails/61.jpg)
10/18/2012 Lovely Professional University,
![Page 62: Storage Management](https://reader034.vdocuments.site/reader034/viewer/2022042606/544f9723b1af9fe42f8b46bb/html5/thumbnails/62.jpg)
10/18/2012 Lovely Professional University,
![Page 63: Storage Management](https://reader034.vdocuments.site/reader034/viewer/2022042606/544f9723b1af9fe42f8b46bb/html5/thumbnails/63.jpg)
10/18/2012 Lovely Professional University,