Run-time Environments - 3

Similar documents
Run-time Environments -Part 3

Run-time Environments - 2

Heap Management portion of the store lives indefinitely until the program explicitly deletes it C++ and Java new Such objects are stored on a heap

Motivation for Dynamic Memory. Dynamic Memory Allocation. Stack Organization. Stack Discussion. Questions answered in this lecture:

Run-Time Environments/Garbage Collection

6.172 Performance Engineering of Software Systems Spring Lecture 9. P after. Figure 1: A diagram of the stack (Image by MIT OpenCourseWare.

CS61C : Machine Structures

Example. program sort; var a : array[0..10] of integer; procedure readarray; : function partition (y, z :integer) :integer; var i, j,x, v :integer; :

Dynamic Storage Allocation

Process s Address Space. Dynamic Memory. Backing the Heap. Dynamic memory allocation 3/29/2013. When a process starts the heap is empty

Memory Allocation. Static Allocation. Dynamic Allocation. Dynamic Storage Allocation. CS 414: Operating Systems Spring 2008

Programming Language Implementation

Programming Languages

Scope, Functions, and Storage Management

Compiler Construction

A.Arpaci-Dusseau. Mapping from logical address space to physical address space. CS 537:Operating Systems lecture12.fm.2

Concepts Introduced in Chapter 7

Compiler Construction

G Programming Languages - Fall 2012

Name, Scope, and Binding. Outline [1]

Class Information ANNOUCEMENTS

ECE 598 Advanced Operating Systems Lecture 10

CS61C Midterm Review on C & Memory Management

Compilers. 8. Run-time Support. Laszlo Böszörmenyi Compilers Run-time - 1

Run-time Environments -Part 1

Programming Languages Third Edition. Chapter 10 Control II Procedures and Environments

Dynamic Memory Allocation

Run Time Environments

CS 536 Introduction to Programming Languages and Compilers Charles N. Fischer Lecture 11

Design Issues. Subroutines and Control Abstraction. Subroutines and Control Abstraction. CSC 4101: Programming Languages 1. Textbook, Chapter 8

Binding and Storage. COMP 524: Programming Language Concepts Björn B. Brandenburg. The University of North Carolina at Chapel Hill

In multiprogramming systems, processes share a common store. Processes need space for:

CSE 504: Compilers. Expressions. R. Sekar

Lecture 14. No in-class files today. Homework 7 (due on Wednesday) and Project 3 (due in 10 days) posted. Questions?

In Java we have the keyword null, which is the value of an uninitialized reference type

Dynamic Memory Allocation

Events, Memory Management

Lecture 7: Binding Time and Storage

Optimizing Dynamic Memory Management

System Software Assignment 1 Runtime Support for Procedures

Garbage Collection Algorithms. Ganesh Bikshandi

Memory Management 3/29/14 21:38

Run-time Environments

Run-time Environments

INITIALISING POINTER VARIABLES; DYNAMIC VARIABLES; OPERATIONS ON POINTERS

Weeks 6&7: Procedures and Parameter Passing

Lectures 13 & 14. memory management

Memory management. Johan Montelius KTH

Procedure and Object- Oriented Abstraction

Review! Lecture 5 C Memory Management !


CS61C : Machine Structures

Dynamic Memory Management! Goals of this Lecture!

Short Notes of CS201

Memory Management Basics

Advances in Programming Languages: Regions

ECE 598 Advanced Operating Systems Lecture 12

CS201 - Introduction to Programming Glossary By

CS61C : Machine Structures

CS61C : Machine Structures

Engine Support System. asyrani.com

Dynamic Memory Management

CS61C : Machine Structures

CS 345. Garbage Collection. Vitaly Shmatikov. slide 1

CS61C : Machine Structures

CS61C : Machine Structures

Declaring Pointers. Declaration of pointers <type> *variable <type> *variable = initial-value Examples:

COP4020 Programming Languages. Names, Scopes, and Bindings Prof. Robert van Engelen

COMP26120: Linked List in C (2018/19) Lucas Cordeiro

Lecture 13: Garbage Collection

Pointers II. Class 31

CS 330 Lecture 18. Symbol table. C scope rules. Declarations. Chapter 5 Louden Outline

Recitation #11 Malloc Lab. November 7th, 2017

Dynamic Memory Allocation: Advanced Concepts

Secure Software Programming and Vulnerability Analysis

Runtime. The optimized program is ready to run What sorts of facilities are available at runtime

A program execution is memory safe so long as memory access errors never occur:

Dynamic Memory Management

[0569] p 0318 garbage

Memory Management. COMP755 Advanced Operating Systems

! Those values must be stored somewhere! Therefore, variables must somehow be bound. ! How?

Lecture 3: C Programm

Lectures 5-6: Introduction to C

CS 241 Honors Memory

Lecture 7 More Memory Management Slab Allocator. Slab Allocator

Every language has its own scoping rules. For example, what is the scope of variable j in this Java program?

Types and Type Inference

Memory Management. Didactic Module 14 Programming Languages - EEL670 1

Memory Management. Chapter Fourteen Modern Programming Languages, 2nd ed. 1

CSCI-UA /2. Computer Systems Organization Lecture 19: Dynamic Memory Allocation: Basics

Run Time Environment

G Programming Languages - Fall 2012

Last week. Data on the stack is allocated automatically when we do a function call, and removed when we return

CSE 307: Principles of Programming Languages

! What is main memory? ! What is static and dynamic allocation? ! What is segmentation? Maria Hybinette, UGA. High Address (0x7fffffff) !

Intrusion Detection and Malware Analysis

Lectures 5-6: Introduction to C

Project. there are a couple of 3 person teams. a new drop with new type checking is coming. regroup or see me or forever hold your peace

Garbage Collection (1)

Advanced Programming & C++ Language

Transcription:

Run-time Environments - 3 Y.N. Srikant Computer Science and Automation Indian Institute of Science Bangalore 560 012 NPTEL Course on Principles of Compiler Design

Outline of the Lecture n What is run-time support? (in part 1) n Parameter passing methods (in part 1) n Storage allocation (in part 2) n Activation records (in part 2) n Static scope and dynamic scope n Passing functions as parameters n Heap memory management n Garbage Collection Y.N. Srikant 2

Static Scope and Dynamic Scope n Static Scope A global identifier refers to the identifier with that name that is declared in the closest enclosing scope of the program text Uses the static (unchanging) relationship between blocks in the program text n Dynamic Scope A global identifier refers to the identifier with that name associated with the most recent activation record Uses the actual sequence of calls that is executed in the dynamic (changing) execution of the program n Both are identical as far as local variables are concerned Y.N. Srikant 3

Static Scope and Dynamic Scope : An Example int x = 1, y = 0; int g(int z) { return x+z;} int f(int y) { int x; x = y+1; return g(y*x); } y = f(3); -------------------------------- After the call to g, Static scope: x = 1 Dynamic scope: x = 4 x 1 y 0 y 3 x 4 z 12 Stack of activation records after the call to g outer block f(3) g(12) Y.N. Srikant 4

Static Scope and Dynamic Scope: Another Example float r = 0.25; void show() { printf( %f,r); } void small() { float r = 0.125; show(); } int main (){ show(); small(); printf( \n ); show(); small(); printf( \n ); } n Under static scoping, the output is 0.25 0.25 0.25 0.25 n Under dynamic scoping, the output is 0.25 0.125 0.25 0.125 Y.N. Srikant 5

Implementing Dynamic Scope Deep Access Method n Use dynamic link as static link n Search activation records on the stack to find the first AR containing the non-local name n The depth of search depends on the input to the program and cannot be determined at compile time n Needs some information on the identifiers to be maintained at runtime within the ARs n Takes longer time to access globals, but no overhead when activations begin and end Y.N. Srikant 6

Deep Access Method - Example Stack of activation records x? Main Global variable search direction x? R x? Q Calling sequence: Main à R à Q à R Base Next R Currently active procedure Y.N. Srikant 7

Implementing Dynamic Scope Shallow Access Method n n n n Allocate maximum static storage needed for each name (based on the types) When a new AR is created for a procedure p, a local name n in p takes over the static storage allocated to name n Global variables are also accessed from the static storage Temporaries are located in the AR Therefore, all variable (not temp) accesses use static addresses The previous value of n held in static storage is saved in the AR of p and is restored when the activation of p ends Direct and quick access to globals, but some overhead is incurred when activations begin and end Y.N. Srikant 8

Shallow Access Method - Example Main Stack of activation records Static storage for UNIQUE names (max storage based on types of the names) R Q Space for temps and for saving variables from static storage Space for temps and for saving variables from static storage Calling sequence: Main à R à Q à R Base R Space for temps and for saving variables from static storage Currently active procedure Next Y.N. Srikant 9

Passing Functions as Parameters An example: main() { int x = 4; int f (int y) { return x*y; } int g (int int h){ int x = 7; return h(3) + x; } g(f); } n n n A language has first-class functions if functions can be declared within any scope passed as arguments to other functions returned as results of functions In a language with first-class functions and static scope, a function value is generally represented by a closure a pair consisting of a pointer to function code and a pointer to an activation record Passing functions as arguments is very useful in structuring of systems using callbacks Y.N. Srikant 10

Passing Functions as Parameters Implementation SL chain g(f) h(3) x=4 main SL x=7 SL y=3 pointer to code for f closure for parameter h AR for the call f(3) An example: main() { int x = 4; int f (int y) { return x*y; } int g (int int h){ int x = 7; return h(3) + x; } g(f); } Y.N. Srikant 11

Passing Functions as Parameters: Implementation An example: main() { int x = 4; int f (int y) { return x*y; } int g (int int h){ int x = 7; return h(3) + x; } g(f); } n n n n n In this example, when executing the call h(3), h is really f and 3 is the parameter y of f Without passing a closure, the AR of the main program cannot be accessed, and hence, the value of x within f will not be 4 In the call g(f), f is passed as a closure Closure may also contain information needed to set up AR (e.g., size of space for local variables, etc.) When processing the call h(3), after setting up an AR for h (i.e., f), the SL for the AR is set up using the AR pointer in the closure for f that has been passed to the call g(f) Y.N. Srikant 12

Heap Memory Management n Heap is used for allocating space for objects created at run time For example: nodes of dynamic data structures such as linked lists and trees n Dynamic memory allocation and deallocation based on the requirements of the program malloc() and free() in C programs new() and delete() in C++ programs new() and garbage collection in Java programs n Allocation and deallocation may be completely manual (C/C++), semi-automatic (Java), or fully automatic (Lisp) Y.N. Srikant 13

Memory Manager n Manages heap memory by implementing mechanisms for allocation and deallocation, both manual and automatic n Goals Space efficiency: minimize fragmentation Program efficiency: take advantage of locality of objects in memory and make the program run faster Low overhead: allocation and deallocation must be efficient n Heap is maintained either as a doubly linked list or as bins of free memory chunks (more on this later) Y.N. Srikant 14

Allocation and Deallocation n In the beginning, the heap is one large and contiguous block of memory n As allocation requests are satisfied, chunks are cut off from this block and given to the program n As deallocations are made, chunks are returned to the heap and are free to be allocated again (holes) n After a number of allocations and deallocations, memory becomes fragmented and is not contiguous n Allocation from a fragmented heap may be made either in a first-fit or best-fit manner n After a deallocation, we try to coalesce contiguous holes and make a bigger hole (free chunk) Y.N. Srikant 15

First-Fit and Best-Fit Allocation Strategies n The first-fit strategy picks the first available chunk that satisfies the allocation request n The best-fit strategy searches and picks the smallest (best) possible chunk that satisfies the allocation request n Both of them chop off a block of the required size from the chosen chunk, and return it to the program n The rest of the chosen chunk remains in the heap Y.N. Srikant 16

First-Fit and Best-Fit Allocation Strategies n Best-fit strategy has been shown to reduce fragmentation in practice, better than first-fit strategy n Next-fit strategy tries to allocate the object in the chunk that has been split recently Tends to improve speed of allocation Tends to improve spatial locality since objects allocated at about the same time tend to have similar reference patterns and life times (cache behaviour may be better) Y.N. Srikant 17

Bin-based Heap n Free space is organized into bins according to their sizes (Lea Memory Manager in GCC) Many more bins for smaller sizes, because there are many more small objects A bin for every multiple of 8-byte chunks from 16 bytes to 512 bytes Then approximately logarithmically (double previous size) Within each small size bin, chunks are all of the same size In others, they are ordered by size The last chunk in the last bin is the wilderness chunk, which gets us a chunk by going to the operating system Y.N. Srikant 18

Bin-based Heap An Example index size 2 3 exact bins... 64 65 sorted bins 127 16 24 32... 512 576 640... 2 31 chunks Ref: From Lea s article on memory manager in GCC Y.N. Srikant 19

Managing and Coalescing Free Space n Should coalesce adjacent chunks and reduce fragmentation Many small chunks together cannot hold one large object In the Lea memory manager, no coalescing in the exact size bins, only in the sorted bins Boundary tags (free/used bit and chunk size) at each end of a chunk (for both used and free chunks) A doubly linked list of free chunks Y.N. Srikant 20

Boundary Tags and Doubly Linked List 3 adjacent chunks. Chunk B has been freed just now and returned to the free list. Chunks A and B can be merged, and this is done just before inserting it into the linked list. The merged chunk AB may have to be placed in a different bin. Chunk A Chunk B Chunk C... 0 200 200 0 0 100 100 0 1 120 120 1.. free freed just now occupied Y.N. Srikant 21

Problems with Manual Deallocation n Memory leaks Failing to delete data that cannot be referenced Important in long running or nonstop programs n Dangling pointer dereferencing Referencing deleted data n Both are serious and hard to debug n Solution: automatic garbage collection Y.N. Srikant 22

Garbage Collection n Reclamation of chunks of storage holding objects that can no longer be accessed by a program n GC should be able to determine types of objects Then, size and pointer fields of objects can be determined by the GC Languages in which types of objects can be determined at compile time or run-time are type safe n Java is type safe n C and C++ are not type safe because they permit type casting, which creates new pointers n Thus, any memory location can be (theoretically) accessed at any time and hence cannot be considered inaccessible Y.N. Srikant 23

Reachability of Objects n The root set is all the data that can be accessed (reached) directly by a program without having to dereference any pointer n Recursively, any object whose reference is stored in a field of a member of the root set is also reachable n New objects are introduced through object allocations and add to the set of reachable objects n Parameter passing and assignments can propagate reachability n Assignments and ends of procedures can terminate reachability Y.N. Srikant 24