Dynamic Memory Allocation: Advanced Concepts

Similar documents
Dynamic Memory Alloca/on: Advanced Concepts

Today. Dynamic Memory Allocation: Advanced Concepts. Explicit Free Lists. Keeping Track of Free Blocks. Allocating From Explicit Free Lists

Dynamic Memory Allocation II October 22, 2008

Internal Fragmentation

CMSC 313 COMPUTER ORGANIZATION & ASSEMBLY LANGUAGE PROGRAMMING

Dynamic Memory Allocation

Dynamic Memory Allocation: Advanced Concepts

Dynamic Memory Allocation: Advanced Concepts

Memory Allocation III

Dynamic Memory Allocation

Memory Allocation III

Foundations of Computer Systems

Memory Allocation III CSE 351 Spring (original source unknown)

Dynamic Memory Alloca/on: Advanced Concepts

Memory Allocation III

CS 33. Intro to Storage Allocation. CS33 Intro to Computer Systems XXVI 1 Copyright 2017 Thomas W. Doeppner. All rights reserved.

Dynamic Memory Allocation

Memory Allocation III

NEXT SET OF SLIDES FROM DENNIS FREY S FALL 2011 CMSC313.

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

Dynamic Memory Allocation. Basic Concepts. Computer Organization 4/3/2012. CSC252 - Spring The malloc Package. Kai Shen

Dynamic Memory Allocation

CS 153 Design of Operating Systems

Memory Allocation II. CSE 351 Autumn Instructor: Justin Hsia

Memory Allocation II. CSE 351 Autumn Instructor: Justin Hsia

CS61, Fall 2012 Section 2 Notes

CS 33. Storage Allocation. CS33 Intro to Computer Systems XXVII 1 Copyright 2017 Thomas W. Doeppner. All rights reserved.

Dynamic Memory Allocation. Gerson Robboy Portland State University. class20.ppt

Today. Dynamic Memory Allocation: Basic Concepts. Dynamic Memory Allocation. Dynamic Memory Allocation. malloc Example. The malloc Package

ANITA S SUPER AWESOME RECITATION SLIDES

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

Dynamic Memory Allocation I

Today. Dynamic Memory Allocation: Basic Concepts. Dynamic Memory Allocation. Dynamic Memory Allocation. malloc Example. The malloc Package

Dynamic Memory Allocation I Nov 5, 2002

CS61 Section Notes. Section 5 (Fall 2011) Topics to be covered Common Memory Errors Dynamic Memory Allocation Assignment 3: Malloc

Computer Systems A Programmer s Perspective 1 (Beta Draft)

Dynamic Memory Allocation

Dynamic Memory Allocation: Basic Concepts

Dynamic Memory Allocation: Basic Concepts

Recitation #11 Malloc Lab. November 7th, 2017

Introduction to Computer Systems /18 243, fall th Lecture, Oct. 22 th

Dynamic Memory Alloca/on: Basic Concepts

Binghamton University Dynamic Memory Alloca/on

Dynamic Memory Allocation. Zhaoguo Wang

Run-time Environments -Part 3

Run-time Environments - 3

Malloc Lab & Midterm Solutions. Recitation 11: Tuesday: 11/08/2016

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

Memory management. Johan Montelius KTH

CMSC 341 Lecture 2 Dynamic Memory and Pointers

Week 5, continued. This is CS50. Harvard University. Fall Cheng Gong

Lecture 8 Dynamic Memory Allocation

Programming Language Implementation

Q1: /8 Q2: /30 Q3: /30 Q4: /32. Total: /100

Recitation #12 Malloc Lab - Part 2. November 14th, 2017

CS 11 C track: lecture 5

Dynamic Memory Management

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

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

CSE 351, Spring 2010 Lab 7: Writing a Dynamic Storage Allocator Due: Thursday May 27, 11:59PM

Dynamic Memory Alloca/on: Basic Concepts

Outline. Briefly review the last class Pointers and Structs Memory allocation Linked lists

CPSC 213. Introduction to Computer Systems. Winter Session 2017, Term 2. Unit 1c Jan 24, 26, 29, 31, and Feb 2

Memory Allocation. Copyright : University of Illinois CS 241 Staff 1

CS61C : Machine Structures

Limitations of the stack

EL2310 Scientific Programming

Optimizing Dynamic Memory Management

Memory Allocation I. CSE 351 Autumn Instructor: Justin Hsia

Dynamic Memory Management! Goals of this Lecture!

Class Information ANNOUCEMENTS

DAY 3. CS3600, Northeastern University. Alan Mislove

10.1. CS356 Unit 10. Memory Allocation & Heap Management

Automatic Memory Management

CS 153 Design of Operating Systems

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

Princeton University Computer Science 217: Introduction to Programming Systems. Dynamic Memory Management

Princeton University. Computer Science 217: Introduction to Programming Systems. Dynamic Memory Management

Dynamic Storage Allocation

o Code, executable, and process o Main memory vs. virtual memory

Foundations of Computer Systems

One-Slide Summary. Lecture Outine. Automatic Memory Management #1. Why Automatic Memory Management? Garbage Collection.

Dynamic Memory Management

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

CS61C Midterm Review on C & Memory Management

Quiz 0 Review Session. October 13th, 2014

Heap Arrays. Steven R. Bagley

CS 241 Data Organization Binary Trees

Review! Lecture 5 C Memory Management !

CS61C : Machine Structures

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

Carnegie Mellon. Malloc Boot Camp. Stan, Nikhil, Kim

Memory Allocation I. CSE 351 Autumn Instructor: Justin Hsia

CS61C : Machine Structures

CSE 509: Computer Security

Acknowledgements These slides are based on Kathryn McKinley s slides on garbage collection as well as E Christopher Lewis s slides

Secure Coding Techniques

CMSC 330: Organization of Programming Languages

Low-Level C Programming. Memory map Pointers Arrays Structures

Engine Support System. asyrani.com

Transcription:

Dynamic Memory Allocation: Advanced Concepts Keeping Track of Free Blocks Method 1: Implicit list using length links all blocks 5 4 6 Method : Explicit list among the free blocks using pointers 5 4 6 Kai Shen Method 3: Segregated free list Different free lists for different size classes Method 4: Blocks sorted by size Can use a balanced tree (e.g. Red Black tree) with pointers within each free block, and the length used as a key 1 Explicit Free Lists Allocated (as before) Free Size a Size a Payload and padding Nextt Prev Logically: Explicit Free Lists A B C Physically: blocks can be in any order Size a Size a Maintain list(s) () of free blocks, not allblocks The next free block could be anywhere So we need to store forward/back pointers, not just sizes Luckily we track only free blocks, the space usage is pretty much free Still need boundary tags for coalescing Forward (next) links A B 4 4 4 4 6 6 4 4 4 4 C Back (prev) links 3 4 CSC5 - Spring 01 1

Allocating from Explicit Free Lists First fit Next fit Best fit Before Allocating From Explicit Free Lists conceptual graphic After (with splitting) 5 = malloc( ) 6 Freeing With Explicit Free Lists Freeing to Front Where in the free list do you put a newly freed block? An intuitive approach is to insert at the front of the free list Before free( ) conceptual graphic Pro: simple and constant time Root Insert the freed block at the root of the list After Root 7 8 CSC5 - Spring 01

Before Root Freeing to Front (Coalescing) free( ) conceptual graphic Splice out predecessor block, coalesce both memory blocks, and insert the new block at the root of the list After Explicit List Summary Comparison to implicit list: Allocate is linear time in number of free blocks instead of all blocks Much faster when most of the memory is full Slightly more complicated allocation and free since needs to splice blocks in and out of the list Some extra space for the links ( extra words needed for each block) Is this a big deal? But linear allocation time is still bad! Root 9 10 Keeping Track of Free Blocks Method 1: Implicit list using length links all blocks 5 4 6 Method : Explicit list among the free blocks using pointers Segregated List Allocators Each size class of blocks has its own free list 1 3 5 4 6 Method 3: Segregated free list Different free lists for different size classes Method 4: Blocks sorted by size Can use a balanced tree (e.g. Red Black tree) with pointers within each free block, and the length used as a key 4 5 8 9 inf 11 1 CSC5 - Spring 01 3

Segregated List Allocator To allocate a block of size n: Search appropriate free list for block of size m > n If an appropriate block is found: Split block and place fragment on appropriate list (optional) If no block is found, try next larger class Repeat until block is found To free a block: Coalesce and place on appropriate list (optional) Advantagesof segregated listallocators High speed Linear on the number of lists log time for power of two size classes Good memory utilization Approximates a best fit search of entire heap. 13 Implicit Memory Management: Garbage Collection Garbage collection: automatic reclamation of heap allocated storage application never has to free How does the memory manager know when memory can be freed? We cannot predict what is going to be used in the future with knowing future program execution and data input But we can tell that certain heap objects cannot be accessed if there are no references to them void foo() { String[] p = new String[]; return; /* p block is now garbage */ All heap accesses must be made through known references 14 Memory as a Graph Mark and Sweep Collecting We view memory as a directed graph Each data object is a node in the graph Each reference is an edge in the graph Registers, global variables, locations on the stack are always reachable Root nodes Heap nodes reachable Not reachable (garbage) Garbage Collection Use extra mark bit in the head of each block Mark: Start at roots and set mark bit on each reachable block Sweep: Scan all blocks and free blocks that are not marked Before mark After mark root Note: arrows here denote memory refs, not free list ptrs. Mark bit set A node (object) is reachable if there is a path from any root to that node. Non reachable nodes are garbage (cannot be accessed by the application) 15 After sweep free free 16 CSC5 - Spring 01 4

Garbage Collection for C Problem for C garbage collection References (pointers) are not strongly regulated Forinstance instance, can point tomiddle ofan allocated block ptr Header So how to identify the block when pointer points to the middle? Search all allocated blocks Can use a balanced binary tree to keep track of all allocated blocks in address order Memory Related Perils and Pitfalls Dereferencing bad pointers Reading uninitialized memory Overwriting memory Misunderstanding of pointer arithmetic Referencing nonexistent variables Referencing freed blocks Failing to free blocks Are we done? C types can be cast back and forth I save ptr 104 and add 104 when using it 17 18 Dereferencing Bad Pointers Reading Uninitialized Memory int val;... scanf( %d, val); Assuming that heap data is initialized to zero /* return y = Ax */ int *matvec(int **A, int *x) { int *y = malloc(n*sizeof(int)); int i, j; for (i=0; i<n; i++) for (j=0; j<n; j++) y[i] += A[i][j]*x[j]; return y; 19 0 CSC5 - Spring 01 5

Overwriting Memory int **p; p = malloc(n*sizeof(int *)); for (i=0; i<=n; i++) { p[i] = malloc(m*sizeof(int)); Overwriting Memory Not checking the max string size char s[8]; int i; gets(s); /* reads 13456789 from stdin */ Basis for classic buffer overflow attacks Off by one error 1 Misunderstanding of Pointer Arithmetic Referencing Nonexistent Variables int *search(int *p, int val) { while (*p && *p!= val) p += sizeof(int); Forgetting that local variables disappear when a function returns int *foo () { int val; return p; return &val; 3 4 CSC5 - Spring 01 6

Referencing Freed Blocks Failing to Free Blocks (Memory Leaks) x = malloc(n*sizeof(int)); <manipulate x> free(x);... y = malloc(m*sizeof(int)); for (i=0; i<m; i++) y[i] = x[i]++; Slow, long term killer! foo() { int *x = malloc(n*sizeof(int));... return; 5 6 Failing to Free Blocks (Memory Leaks) Freeing only part of a data structure struct list { int val; struct list *next; ; foo() { struct list *head = malloc(sizeof(struct list)); head->val = 0; head->next = NULL; <create and manipulate the rest of the list>... free(head); return; Dealing With Memory Bugs Conventional debugger (gdb) Not able to do much Debugging malloc Wrapper around conventional malloc Add canary values on allocation boundaries and do sanity checks sometimes Some malloc implementations contain checking code Linux glibc malloc: setenv MALLOC_CHECK_ Binary translator: valgrind (Linux), Purify Maintain memory map/status structure for each byte Instrument data accesses to check for errors Garbage collection (Boehm Weiser Conservative GC) Let the system free blocks instead of the programmer. 7 8 CSC5 - Spring 01 7

Disclaimer These slides were adapted from the CMU course slides provided alongwith the textbook of Computer Systems: A programmer s Perspective by Bryant and O Hallaron. The slides are intended for the sole purpose of teaching the computer organization course at the University of Rochester. 9 CSC5 - Spring 01 8