Compiler construction in4020 – lecture 12 Koen Langendoen Delft University of Technology The Netherlands.

Slides:



Advertisements
Similar presentations
Memory Management Chapter FourteenModern Programming Languages, 2nd ed.1.
Advertisements

Garbage collection David Walker CS 320. Where are we? Last time: A survey of common garbage collection techniques –Manual memory management –Reference.
Procedures. 2 Procedure Definition A procedure is a mechanism for abstracting a group of related operations into a single operation that can be used repeatedly.
The University of Adelaide, School of Computer Science
Introduction to Memory Management. 2 General Structure of Run-Time Memory.
Compiler construction in4020 – lecture 10 Koen Langendoen Delft University of Technology The Netherlands.
Lecture 10: Heap Management CS 540 GMU Spring 2009.
CSE 5317/4305 L11: Storage Allocation1 Storage Allocation Leonidas Fegaras.
Garbage Collection  records not reachable  reclaim to allow reuse  performed by runtime system (support programs linked with the compiled code) (support.
5. Memory Management From: Chapter 5, Modern Compiler Design, by Dick Grunt et al.
Run-time organization  Data representation  Storage organization: –stack –heap –garbage collection Programming Languages 3 © 2012 David A Watt,
User-Level Memory Management in Linux Programming
Garbage Collection CSCI 2720 Spring Static vs. Dynamic Allocation Early versions of Fortran –All memory was static C –Mix of static and dynamic.
CS 326 Programming Languages, Concepts and Implementation Instructor: Mircea Nicolescu Lecture 18.
CPSC 388 – Compiler Design and Construction
Chapter 8 Runtime Support. How program structures are implemented in a computer memory? The evolution of programming language design has led to the creation.
Memory Management. History Run-time management of dynamic memory is a necessary activity for modern programming languages Lisp of the 1960’s was one of.
Garbage Collection Mooly Sagiv html://
Memory Management Professor Yihjia Tsai Tamkang University.
CS 61C L07 More Memory Management (1) Garcia, Fall 2004 © UCB Lecturer PSOE Dan Garcia inst.eecs.berkeley.edu/~cs61c CS61C.
Memory Management Chapter 5 Mooly Sagiv
Memory Allocation. Three kinds of memory Fixed memory Stack memory Heap memory.
1 1 Lecture 4 Structure – Array, Records and Alignment Memory- How to allocate memory to speed up operation Structure – Array, Records and Alignment Memory-
Runtime The optimized program is ready to run … What sorts of facilities are available at runtime.
Chapter 10 Storage Management Implementation details beyond programmer’s control Storage/CPU time trade-off Binding times to storage.
Run-Time Storage Organization
Run time vs. Compile time
Memory Allocation and Garbage Collection. Why Dynamic Memory? We cannot know memory requirements in advance when the program is written. We cannot know.
The environment of the computation Declarations introduce names that denote entities. At execution-time, entities are bound to values or to locations:
Garbage collection (& Midterm Topics) David Walker COS 320.
Garbage Collection Mooly Sagiv
Routines and Control Flows Code Generation Professor Yihjia Tsai Tamkang University.
1 Run time vs. Compile time The compiler must generate code to handle issues that arise at run time Representation of various data types Procedure linkage.
Compiler construction in4020 – lecture 11 Koen Langendoen Delft University of Technology The Netherlands.
CS61C L07 More Memory Management (1) Garcia, Spring 2008 © UCB Lecturer SOE Dan Garcia inst.eecs.berkeley.edu/~cs61c CS61C.
SEG Advanced Software Design and Reengineering TOPIC L Garbage Collection Algorithms.
Chapter 7: Runtime Environment –Run time memory organization. We need to use memory to store: –code –static data (global variables) –dynamic data objects.
CS3012: Formal Languages and Compilers The Runtime Environment After the analysis phases are complete, the compiler must generate executable code. The.
Dynamic Memory Allocation Questions answered in this lecture: When is a stack appropriate? When is a heap? What are best-fit, first-fit, worst-fit, and.
Compiler Construction
Chapter 5: Programming Languages and Constructs by Ravi Sethi Activation Records Dolores Zage.
Runtime Environments. Support of Execution  Activation Tree  Control Stack  Scope  Binding of Names –Data object (values in storage) –Environment.
Memory Management II: Dynamic Storage Allocation Mar 7, 2000 Topics Segregated free lists –Buddy system Garbage collection –Mark and Sweep –Copying –Reference.
OOPLs /FEN March 2004 Object-Oriented Languages1 Object-Oriented Languages - Design and Implementation Java: Behind the Scenes Finn E. Nordbjerg,
1 Records Record aggregate of data elements –Possibly heterogeneous –Elements/slots are identified by names –Elements in same fixed order in all records.
CS 326 Programming Languages, Concepts and Implementation Instructor: Mircea Nicolescu Lecture 9.
COMP3190: Principle of Programming Languages
11/26/2015IT 3271 Memory Management (Ch 14) n Dynamic memory allocation Language systems provide an important hidden player: Runtime memory manager – Activation.
CSE 425: Control Abstraction I Functions vs. Procedures It is useful to differentiate functions vs. procedures –Procedures have side effects but usually.
University of Washington Wouldn’t it be nice… If we never had to free memory? Do you free objects in Java? 1.
Runtime The optimized program is ready to run … What sorts of facilities are available at runtime.
RUNTIME ENVIRONMENT AND VARIABLE BINDINGS How to manage local variables.
2/4/20161 GC16/3011 Functional Programming Lecture 20 Garbage Collection Techniques.
CS61C L07 More Memory Management (1) Garcia, Spring 2010 © UCB Lecturer SOE Dan Garcia inst.eecs.berkeley.edu/~cs61c CS61C.
CS412/413 Introduction to Compilers and Translators April 21, 1999 Lecture 30: Garbage collection.
Runtime Environments Chapter 7. Support of Execution  Activation Tree  Control Stack  Scope  Binding of Names –Data object (values in storage) –Environment.
Memory Management CSCI 2720 Spring What is memory management? “the prudent utilization of this scarce resource (memory), whether by conservation,
Storage Allocation Mechanisms
Stack and Heap Memory Stack resident variables include:
Inst.eecs.berkeley.edu/~cs61c CS61C : Machine Structures Lecture 7 – More Memory Management Lecturer PSOE Dan Garcia
Dynamic Memory Allocation
Concepts of programming languages
Strategies for automatic memory management
Memory Allocation CS 217.
Topic 3-b Run-Time Environment
Chapter 12 Memory Management
UNIT V Run Time Environments.
CS703 - Advanced Operating Systems
RUN-TIME STORAGE Chuen-Liang Chen Department of Computer Science
Inst.eecs.berkeley.edu/~cs61c CS61C : Machine Structures Lecture 7 – More Memory Management Lecturer PSOE Dan Garcia
Presentation transcript:

Compiler construction in4020 – lecture 12 Koen Langendoen Delft University of Technology The Netherlands

Summary of lecture 11 (nested) routines activation records routine descriptors code generation for control flow statements conditional expressions: true & false labels case-statement: jump table for-statement: overflow static link dynamic link ret. address parameter i mies parameter i noot static link dynamic link ret. address

Quiz 6.30 Why are parameters stacked in the reverse order? That is, why is the last parameter pushed first when calling a routine? working stack parameter n parameter 1... registers dynamic link return address local variables FP SP parameter k parameter 1...

Overview: memory management explicit deallocation malloc() + free() implicit deallocation: garbage collection reference counting mark & scan two-space copying

Memory management What has a compiler to do with memory management? compiler uses heap-allocated data structures modern languages have automatic data (de)allocation garbage collection part of runtime support system compiler usually assists in identifying pointers

Data allocation with explicit deallocation malloc() find free block of requested size mark it in use return a pointer to it. free() mark the block as not in use. #include void *calloc(size_t nmemb, size_t size); void *malloc(size_t size); void free(void *ptr); void *realloc(void *ptr, size_t size);

Heap layout SIZESIZE S 0 SIZESIZE S 1 SIZESIZE S 0 SIZESIZE S 1... lowhigh marked free marked in use block chunk in use free pointer to user data

Free() PROCEDURE Free (Block pointer): SET Chunk pointer TO Block pointer – Admin size; SET Chunk pointer.free TO True; SIZESIZE S 1 SIZESIZE S 0 SIZESIZE S 1... free SIZESIZE S 0 in use SIZESIZE S 1 free

Malloc() SIZESIZE S 1 SIZESIZE S 0 SIZESIZE S 1... free FUNCTION Malloc (Block size) RETURNS a generic pointer: SET Pointer TO Free block of size (Block size); IF pointer /= NULL: RETURN pointer; Coalesce free chunks (); RETURN Free block of size (Block size); SIZESIZE S 0 in use SIZESIZE S 1 free

walk chunks from low to high check if chunk is free AND large enough if so, mark chunk in use AND return block pointer SIZESIZE S 0 in use SIZESIZE S 1 free Free block of size (request) SIZESIZE S 1 SIZESIZE S 0 SIZESIZE S 1... free SIZESIZE S 0 block pointer

walk chunks from low to high check if chunk is free AND large enough if so, mark chunk in use AND return block pointer SIZESIZE S 0 in use SIZESIZE S 1 free Free block of size (request) walk chunks from low to high check if chunk is free AND large enough if so, mark chunk in use AND return block pointer optimization: split chunk to free unused part SIZESIZE S 1 SIZESIZE S 0 SIZESIZE S 1... free SIZESIZE S 0 SIZESIZE S 1 block pointer

Free block of size FUNCTION Free block of size (Block size) RETURNS a generic pointer: SET Chunk ptr TO Heap low; SET Request TO Block size + Admin size; WHILE Chunk ptr < Heap high: IF Chunk ptr.free AND Chunk ptr.size >= Request: Split chunk (Chunk ptr, Request) SET Chunk ptr.free TO False; RETURN Chunk ptr + Admin size; SET Chunk ptr TO Chunk ptr + Chunk ptr.size; RETURN NULL;

next SIZESIZE S 0 in use SIZESIZE S 1 free Coalesce free chunks () walk chunks from low to high check if chunk is free if so, coalesce all subsequent free chunks SIZESIZE S 1 SIZESIZE S 0 SIZESIZE S 1... SIZESIZE S 0 SIZESIZE S 1 next

Coalesce free chunks PROCEDURE Coalesce free chunks (): SET Chunk ptr TO Heap low; WHILE Chunk ptr < Heap high: IF Chunk ptr.free: SET Next TO Chunk ptr + Chunk ptr.size; WHILE Next < Heap high AND Next.free: SET Next TO Next + Next.size; SET Chunk ptr.size TO Next - Chunk ptr; SET Chunk ptr TO Chunk ptr + Chunk ptr.size;

Optimizations free: poor performance (linear search) malloc: irregular performance (coalesce phase) solutions: free lists indexed by size coalesce at free() SIZESIZE S 0 in use SIZESIZE S 1 free SIZESIZE S 1 SIZESIZE S 0 SIZESIZE S 1... SIZESIZE S 0 SIZESIZE S 1 2 log(size)3456 free list use first field as next ptr

Malloc() with free lists FUNCTION Malloc (Block size) RETURNS a generic pointer: SET Chunk size TO Block size + Admin size; SET Index TO 2 log(Chunk size); IF Index < 3: SET Index TO 3; IF Index <= 10 AND Free list[Index] /= NULL: SET Pointer TO Free list[Index]; SET Free list[Index].next TO Pointer.next; RETURN Pointer + Admin size; RETURN Free block of size (Block size);

Exercise (5 min.) give the pseudo code for free() when using free lists indexed by size.

Answers

PROCEDURE Free (Block pointer): SET Chunk pointer TO Block pointer – Admin size; SET Index TO 2 log(Chunk pointer.size); IF Index <= 10: SET Chunk pointer.next TO Free list[Index]; SET Free list[Index] TO Chunk pointer; ELSE SET Chunk pointer.free TO True; // Coalesce subsequent free chunks

Break

Garbage collection memory allocation is explicit (new) memory deallocation is implicit garbage set: all chunks that will no longer be used by the program chunks without incoming pointers chunks that are unreachable from non-heap data

Example A C D E F B heap root set

Garbage A C D E B heap root set B B E E F

Cyclic garbage C E F B heap root set D A garbage? “no-pointers”: NO “not-reachable”: YES

Compiler assistance: identifying pointers pointers inside chunks user-defined data structures compiler: generate self-descriptive chunks pointers located outside the heap (root set) global data + stack compiler: generate activation record descriptions

Self-descriptive chunks bitmap per data type problem: overhead per chunk / interpretation compiler-generated routine per data type calls GC for each pointer problem: recursion organize data type to start off with n pointers solution: n can be squeezed into chunk admin

Reference counting record #pointers to each chunk reclaim when reference count drops to zero heap root set AD BE FC

Maintaining reference counts VAR p, q : pointer;... p := q; PROCEDURE Free recursively (Pointer): FOR each field f i of record Pointer: IF Points into the heap (f i ): Decrement f i.ref count; IF f i.ref count = 0: Free recursively (f i ); Free chunk (Pointer); source IF Points into the heap (q): Increment q.ref count; IF Points into the heap (p): Decrement p.ref count; IF p.ref count = 0: Free recursively (p); SET p TO q; target pointer assignment:

Mark & scan A C D E F B heap root set mark all reachable chunks scan heap for unmarked chunks that can be freed B E

Mark & scan PROCEDURE Mark (Pointer): IF NOT Points into the heap (Pointer): RETURN; SET Pointer.marked TO True; FOR each field f i of record Pointer: Mark (f i ); PROCEDURE Scan (): SET Chunk ptr TO Heap low; WHILE Chunk ptr < Heap high: IF Chunk ptr.marked: SET Chunk ptr.marked TO False; ELSE SET Chunk ptr.free TO True; SET Chunk ptr TO Chunk ptr + Chunk ptr.size;

Advanced marking problem: mark() is recursive solution: embed stack in the chunks each chunk records: a count denoting which child pointer is next a pointer to the parent node

Advanced marking S ptrptr ptrptr ptrptr S ptrptr ptrptr ptrptr free bit mark bit pointer cnt size to parent 0 0 S ptrptr ptrptr ptrptr

Advanced marking: pointer reversal avoid additional parent pointer use the n-th child pointer when visiting child n S ptrptr ptrptr ptrptr S ptrptr ptrptr ptrptr to parent

Two-space copying most chunks have a short live time memory fragmentation must be addressed partition heap in two spaces copy all reachable chunks to consecutive locations fromto

Two-space copying most chunks have a short live time memory fragmentation must be addressed partition heap in two spaces copy all reachable chunks to consecutive locations fromtofrom to

Copying to to-space copy root set leave forwarding pointers scan to-space for reachable cells in from-space C D E F B from to A C A scan

Copying to to-space copy root set leave forwarding pointers scan to-space for reachable cells in from-space C E F B from to A C scan A D

Copying to to-space copy root set leave forwarding pointers scan to-space for reachable cells in from-space C E F B from to A C scan A D

Copying to to-space copy root set leave forwarding pointers scan to-space for reachable cells in from-space C E B from to A C scan A D F

Copying to to-space copy root set leave forwarding pointers scan to-space for reachable cells in from-space C E B from to A C scan A D F

Copying to to-space copy root set leave forwarding pointers scan to-space for reachable cells in from-space C E B from to A C scan A D F

Copying to to-space copy root set leave forwarding pointers scan to-space for reachable cells in from-space C E B from to A C A D F scan

Copying to to-space copy root set leave forwarding pointers scan to-space for reachable cells in from-space from to C A D F scan

Summary Memory management explicit deallocation malloc() + free() implicit deallocation: garbage collection reference counting mark & scan two-space copying

Homework study sections: Compaction Generational garbage collection assignment 2: make Asterix OO deadline June 4 08:59 print handout for last week [blackboard]