11 RunTimeAdministration1
11 RunTimeAdministration1
Run-Time Environment
Storage Organization
Storage Allocation Strategies
Dynamic Storage Allocation
Run-Time Environments
3
□ Data types
□ Operators
□ Procedures
□ Parameters and
such as:
1. Managing the processor stack.
2. Layout and allocation of memory for various variables used in the
source program.
3. Instructions to copy the actual parameters on top of the stack
when a function is called.
4. Allocating and de-allocating the memory dynamically with the
help of operating system.
5. The mechanism used by the target program to access variables.
6. The mechanism to pass parameters.
7. The interfaces to the operating system, input/output devices and
other programs.
Run-Time Environments
5
Run-Time Environment
Storage Organization
Storage Allocation Strategies
Dynamic Storage Allocation
Storage Organization
9
Data objects
. .. . ..
stack stack
Stack grows
free space free space
heap heap
Activation Record
12
temporaries
4) Access Link
refer to non-local data held in other returned value
activation records actual parameters
5) Control link
optional control link
points to the activation record of the caller
6) Actual parameters optional access link
used by the calling procedure to supply
saved machine status
parameters to the called procedure
(in practice these are passed in registers) local data
7) Returned value temporaries
used by the called procedure to return a
value to the calling procedure
(in practice it is returned in a register)
Local data
15
Run-Time Environment
Storage Organization
Storage Allocation Strategies
Dynamic Storage Allocation
Organization of storage
17
No recursive procedures
int i = 10;
code for function 1
int f(int j) code main() code for function 2
{ . ..
int k; code f() code for function n
int m;
… i (int)
} global / static area
main() stack
main() Activation
{ record k (int)
int k;
f(k); free space
} f()
Activation
record k (int)
m (int)
heap
Static allocation
23
Limitations:
□ The size required must be known at compile time.
temporaries
Calling Sequences
33
returned value
□ No need to know the middle part
actual parameters
of the callee’s activation record
optional control link
optional access link callee
saved machine status
local data
temporaries
Calling Sequences
34
temporaries
Call Sequence
35
Advantages:
It supports recursion as memory is always allocated on block
entry.
It allows to create data structures dynamically.
activation ends
A called activation outlives the caller
activation records
Heap Allocation
41
Fill a request of size s with block of size s ' where s ' is the
smallest size greater than or equal to s
For large blocks of storage use heap manager
For large amount of storage computation may take some time
to use up memory so that time taken by the manager may be
negligible compared to the computation time
Heap Allocation
48
In languages like C nested procedures are not allowed. That is, you
cannot define a procedure inside another procedure. So, if there is a
non- local reference to a name in some function then that variable must
be a global variable. The scope of a global variable holds within all the
functions except those in which the variables have been re- declared.
Storage for all names declared globally can be allocated statically. Thus
their positions will be known at compile time. In static allocation, we use
stack allocation. Any other name must be a local of the activation at the
top of the stack, accessible through the top pointer. Nested procedures
cause this scheme to fail because a non-local may then refer to a local of
parent variable which may be buried deep in the stack and not at the
top of stack. An important benefit of static allocation for non- locals is
that declared procedures can freely be passed as parameters and
returned as results (a function is passed in C by passing a pointer to it).
Procedure Parameters
65
program param (input,output);
procedure b( function h(n:integer): integer);
begin
writeln (h(2))
end;
procedure c;
var m: integer;
function f(n: integer): integer;
begin
f := m + n
end;
begin
m :=0; b(f)
end;
begin
c
end.
Dynamic Scope
70
Deep Access
Dispense with access links
term deep access comes from the fact that search may go
Call by value
actual parameters are evaluated and their rvalues are passed
Call by value
This is, in a sense, the simplest possible method of passing
parameters
if actual parameter is a name then lvalue is passed
swap(i,a[i])
temp = I
i = a[i]
a[i] = temp
Parameter Passing
82
Run-Time Environment
Storage Organization
Storage Allocation Strategies
Dynamic Storage Allocation
Dynamic Allocation
84
Generally languages like Lisp and MLwhich do not allow for explicit de-allocation
of memory do garbage collection. A reference to a pointer that is no longer
valid is called a 'dangling reference'. For example, consider this C code:
int main (void)
{
int* a=fun();
}
int* fun()
{
int a=3;
int* b=&a;
return b;
}
Here, the pointer returned by fun() no longer points to a valid address in
memory as the activation of fun() has ended. This kind of situation is called
a 'dangling reference'. In case of explicit allocation it is more likely to
happen as the user can de-allocate any part of memory, even something
that has to a pointer pointing to a valid piece of memory.
Dynamic Storage Allocation
91
Implicit De-allocation
Requires co-operation between user program and run
time system
Runtime system needs to know when a block is no longer in use
Implemented by fixing the format of storage blocks
Implicit deallocation requires cooperation between the user
program and run time package,