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

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

Optimizing Dynamic Memory Management

Dynamic Memory Management

Memory management. Johan Montelius KTH

Dynamic Memory Management! Goals of this Lecture!

Dynamic Memory Allocation

Dynamic Memory Allocation

Dynamic Memory Allocation. Zhaoguo Wang

Operating systems. Lecture 9

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

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

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

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

Operating System Labs. Yuanbin Wu

Dynamic Memory Allocation: Basic Concepts

Dynamic Memory Allocation: Basic Concepts

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

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

Dynamic Memory Allocation I

Dynamic Memory Allocation I Nov 5, 2002

Optimizing Dynamic Memory Management!

Dynamic Memory Alloca/on: Basic Concepts

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

CS61C : Machine Structures

Dynamic Memory Allocation

Memory Management. CS449 Fall 2017

10.1. CS356 Unit 10. Memory Allocation & Heap Management

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

Dynamic Memory Management

Dynamic Memory Alloca/on: Basic Concepts

ANITA S SUPER AWESOME RECITATION SLIDES

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

ECE 598 Advanced Operating Systems Lecture 10

Foundations of Computer Systems

ECE 598 Advanced Operating Systems Lecture 12

Secure Software Programming and Vulnerability Analysis

Memory Allocation I. CSE 351 Autumn Instructor: Justin Hsia

Memory Allocation I. CSE 351 Autumn Instructor: Justin Hsia

Lectures 13 & 14. memory management

C Structures & Dynamic Memory Management

CS61C : Machine Structures

Dynamic Memory Allocation (and Multi-Dimensional Arrays)

Applications of. Virtual Memory in. OS Design

COSC Software Engineering. Lectures 14 and 15: The Heap and Dynamic Memory Allocation

CS Computer Systems. Lecture 8: Free Memory Management

Review! Lecture 5 C Memory Management !

CS61C : Machine Structures

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

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

CS 153 Design of Operating Systems

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

14. Memory API. Operating System: Three Easy Pieces

Introduction to Linux, for Embedded Engineers Tutorial on Virtual Memory. Feb. 22, 2007 Tetsuyuki Kobayashi Aplix Corporation. [translated by ikoma]

Process Address Spaces and Binary Formats

2/9/18. Secure Coding. CYSE 411/AIT681 Secure Software Engineering. Agenda. Dynamic Memory Interface. Dynamic Memory Interface

Lecture 8 Dynamic Memory Allocation

Project 2 Overview: Part A: User space memory allocation

Dynamic Memory Allocation

CSCI 4061: Virtual Memory

Outline. 1 Details of paging. 2 The user-level perspective. 3 Case study: 4.4 BSD 1 / 19

COSC345 Software Engineering. The Heap And Dynamic Memory Allocation

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

Inside ptmalloc2. Peng Xu Sep 14, 2013

CSC 1600 Memory Layout for Unix Processes"

Homework 3 CS161 Computer Security, Fall 2008 Assigned 10/07/08 Due 10/13/08

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

Recitation #11 Malloc Lab. November 7th, 2017

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

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

CS 31: Intro to Systems Pointers and Memory. Kevin Webb Swarthmore College October 2, 2018

Dynamic Memory Allocation

CS 2461: Computer Architecture I: Heap: Dynamic Memory Allocation Data Structures. Recap. Variables and Structures

CS 31: Intro to Systems Pointers and Memory. Martin Gagne Swarthmore College February 16, 2016

CS61, Fall 2012 Section 2 Notes

Memory management. Single process. Multiple processes. How to: All memory assigned to the process Addresses defined at compile time

CSE506: Operating Systems CSE 506: Operating Systems

CS61C : Machine Structures

CS 322 Operating Systems Programming Assignment 4 Writing a memory manager Due: November 16, 11:30 PM

Run-time Environments -Part 3

COSC Software Engineering. Lecture 16: Managing Memory Managers

Memory Allocation in C C Programming and Software Tools. N.C. State Department of Computer Science

CSE506: Operating Systems CSE 506: Operating Systems

My malloc: mylloc and mhysa. Johan Montelius HT2016

Memory Management. To do. q Basic memory management q Swapping q Kernel memory allocation q Next Time: Virtual memory

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

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

Secure Coding in C and C++ Dynamic Memory Management Lecture 5 Jan 29, 2013

CS 241 Honors Memory

Dynamic Memory Allocation: Advanced Concepts

Dynamic Storage Allocation

Advanced Programming & C++ Language

Intrusion Detection and Malware Analysis

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

Memory (Stack and Heap)

mmalloc The GNU memory-mapped malloc package Fred Fish Cygnus Support Mike Haertel Free Software Foundation

Lecture 13: Garbage Collection

Foundations of Computer Systems

Memory Allocation II. CSE 351 Autumn Instructor: Justin Hsia

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

Transcription:

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

Goals of this Lecture Help you learn about: The need for dynamic* memory mgmt (DMM) Implementing DMM using the heap section Implementing DMM using virtual memory * During program execution 2

System-Level Functions Covered As noted in the Exceptions and Processes lecture Linux system-level functions for dynamic memory management (DMM) Number Function Description 12 brk() Move the program break, thus changing the amount of memory allocated to the HEAP 12 sbrk() (Variant of previous) 9 mmap() Map a virtual memory page 11 munmap() Unmap a virtual memory page 3

Goals for DMM Goals for effective DMM: Time efficiency Allocating and freeing memory should be fast Space efficiency Pgm should use little memory Note Easy to reduce time or space Hard to reduce time and space 4

Agenda The need for DMM DMM using the heap section DMMgr 1: Minimal implementation DMMgr 2: Pad implementation Fragmentation DMMgr 3: List implementation DMMgr 4: doubly linked list implementation DMMgr 5: Bins implementation DMM using virtual memory DMMgr 6: VM implementation 5

Why Allocate Memory Dynamically? Why allocate memory dynamically? Problem Number of objects needed not known in advance (e.g., how many elements of linked list or tree?) Unknown object size (e.g., how large should the array be, in hash table?) How much memory to allocate? Solution 1 Guess! Solution 2 Allocate memory dynamically 6

Why Free Memory Dynamically? Why free memory dynamically? Problem Pgm should use little memory, i.e. Pgm should map few pages of virtual memory Mapping unnecessary VM pages bloats page tables, wastes memory/disk space Solution Free dynamically allocated memory that is no longer needed 7

Option 1: Automatic Freeing Run-time system frees unneeded memory Java, Python, Garbage collection Pros: Easy for programmer Fewer bugs Simpler interfaces between modules Fewer bugs Car c; Plane p;... c = new Car(); p = new Plane();... c = new Car();... Cons: Performed constantly overhead Performed periodically unexpected pauses Original Car object can t be accessed (these days, high-performance garbage collectors minimize overhead and pause latency) 8

Option 2: Manual Freeing Programmer frees unneeded memory C, C++, Objective-C, Pros Less overhead No unexpected pauses Cons More complex for programmer Opens possibility of memory-related bugs Dereferences of dangling pointers, double frees, memory leaks 9

Conclusion: Program in a safe, garbage-collected language! (not in C) Use unsafe languages with manual memory management (such as C) only for low-level programs where the overhead or latency of garbage collection is intolerable such as: OS kernels, device drivers, garbage collectors, memory managers All right then, let s see how manual memory management works in C 10

C memory allocation library Standard C dynamic-memory-management functions: void *malloc(size_t size); void free(void *ptr); void *calloc(size_t nmemb, size_t size); void *realloc(void *ptr, size_t size); Collectively define a dynamic memory manager (DMMgr) We ll focus on malloc() and free() 11

Implementing malloc() and free() Question: How to implement malloc() and free()? How to implement a DMMgr? Answer 1: Use the heap section of memory Answer 2: (Later in this lecture) 12

Agenda The need for DMM DMM using the heap section DMMgr 1: Minimal implementation DMMgr 2: Pad implementation Fragmentation DMMgr 3: List implementation DMMgr 4: doubly linked list implementation DMMgr 5: Bins implementation DMM using virtual memory DMMgr 6: VM implementation 13

The Heap Section of Memory Low memory High memory Heap start Program break Supported by Unix/Linux, MS Windows, Heap start is stable Program break points to end At process start-up, heap start == program break Can grow dynamically By moving program break to higher address Thereby (indirectly) mapping pages of virtual mem Can shrink dynamically By moving program break to lower address Thereby (indirectly) unmapping pages of virtual mem 14

Unix Heap Management Unix system-level functions for heap mgmt: int brk(void *p); Move the program break to address p Return 0 if successful and -1 otherwise void *sbrk(intptr_t n); Increment the program break by n bytes Return previous break if successful and (void*)-1 otherwise [therefore] If n is 0, return the current location of the program break Beware: On Linux has a known bug (overflow not handled); should call only with argument 0. Note: minimal interface (good!) 15

Agenda The need for DMM DMM using the heap section DMMgr 1: Minimal implementation DMMgr 2: Pad implementation Fragmentation DMMgr 3: List implementation DMMgr 4: doubly linked list implementation DMMgr 5: Bins implementation DMM using virtual memory DMMgr 6: VM implementation 16

Minimal Impl Data structures One word: remember the current value of program break Algorithms (by examples) 17

Minimal Impl malloc(n) Example Remember the current program break (p) (initialize using sbrk(0)) p Call brk(p+n) to increase heap size n bytes p Return p, remember new p = p+n n bytes return p 18

Minimal Impl free(p) Example Do nothing! p 19

Minimal Impl Algorithms static void *current_break; void *malloc(size_t n) { char *p = current_break; if (!p) p=(char *)sbrk(0); if (brk(p+n) == -1) return NULL; current_break = p+n; return (void*)p; } void free(void *p) { } 20

Minimal Impl Performance Performance (general case) Time: bad One system call per malloc() Space: bad Each call of malloc() extends heap size No reuse of freed chunks 21

What s Wrong? Problem malloc() executes a system call every time Solution Redesign malloc() so it does fewer system calls Maintain a pad at the end of the heap 22

Agenda The need for DMM DMM using the heap section DMMgr 1: Minimal implementation DMMgr 2: Pad implementation Fragmentation DMMgr 3: List implementation DMMgr 4: doubly linked list implementation DMMgr 5: Bins implementation DMM using virtual memory DMMgr 6: VM implementation 23

Pad Impl Data structures inuse pad ppad pbrk pbrk: address of end of heap (i.e. the program break) ppad: address of beginning of pad char *ppad = NULL; char *pbrk = NULL; Algorithms (by examples) 24

Pad lmpl malloc(n) Example 1 n bytes ppad Are there at least n bytes between ppad and pbrk? Yes! Save ppad as p; add n to ppad pbrk n bytes p ppad pbrk Return p n bytes p ppad pbrk 25

Pad lmpl malloc(n) Example 2 < n bytes ppad pbrk Are there at least n bytes between ppad and pbrk? No! Call brk() to allocate (more than) enough additional memory n bytes ppad pbrk Set pbrk to new program break n bytes ppad pbrk Proceed as previously! 26

Pad Impl free(p) Example Do nothing! 27

Pad Impl inuse Algorithms ppad pad pbrk void *malloc(size_t n) { enum {MIN_ALLOC = 8192}; char *p; char *pnewbrk; if (pbrk == NULL) { pbrk = sbrk(0); ppad = pbrk; } void free(void *p) { } } if (ppad + n > pbrk) /* move pbrk */ { pnewbrk = max(ppad + n, pbrk + MIN_ALLOC); if (brk(pnewbrk) == -1) return NULL; pbrk = pnewbrk; } p = ppad; ppad += n; return p; 28

Pad Impl Performance Performance (general case) Time: good malloc() calls sbrk() initially malloc() calls brk() infrequently thereafter Space: bad No reuse of freed chunks 29

What s Wrong? Problem malloc() doesn t reuse freed chunks Solution free() marks freed chunks as free malloc() uses marked chunks whenever possible malloc() extends size of heap only when necessary 30

Agenda The need for DMM DMM using the heap section DMMgr 1: Minimal implementation DMMgr 2: Pad implementation Fragmentation DMMgr 3: List implementation DMMgr 4: doubly linked list implementation DMMgr 5: Bins implementation DMM using virtual memory DMMgr 6: VM implementation 31

Fragmentation At any given time, some heap memory chunks are in use, some are marked free inuse free DMMgr must be concerned about fragmentation 32

Internal Fragmentation Internal fragmentation: waste within chunks Example 100 bytes Client asks for 90 bytes DMMgr provides chunk of size 100 bytes 10 bytes wasted Generally Program asks for n bytes DMMgr provides chunk of size n+δ bytes Δ bytes wasted Space efficiency DMMgr should reduce internal fragmentation 33

External Fragmentation External fragmentation: waste between chunks Example 100 bytes 50 bytes Client asks for 150 bytes 150 bytes are available, but not contiguously DMMgr must extend size of heap Generally Program asks for n bytes n bytes are available, but not contiguously DMMgr must extend size of heap to satisfy request Space efficiency DMMgr should reduce external fragmentation 34

DMMgr Desired Behavior Demo char *p1 = malloc(3); char *p2 = malloc(1); char *p3 = malloc(4); free(p2); char *p4 = malloc(6); free(p3); char *p5 = malloc(2); free(p1); free(p4); free(p5); 35

DMMgr Desired Behavior Demo char *p1 = malloc(3); char *p2 = malloc(1); char *p3 = malloc(4); free(p2); char *p4 = malloc(6); free(p3); char *p5 = malloc(2); free(p1); free(p4); free(p5); p1 Heap 0 } Heap 0xffffffff Stack 36

DMMgr Desired Behavior Demo char *p1 = malloc(3); char *p2 = malloc(1); char *p3 = malloc(4); free(p2); char *p4 = malloc(6); free(p3); char *p5 = malloc(2); free(p1); free(p4); free(p5); p1 p2 Heap 0 } Heap 0xffffffff Stack 37

DMMgr Desired Behavior Demo char *p1 = malloc(3); char *p2 = malloc(1); char *p3 = malloc(4); free(p2); char *p4 = malloc(6); free(p3); char *p5 = malloc(2); free(p1); free(p4); free(p5); p1 p2 p3 Heap 0 } Heap 0xffffffff Stack 38

DMMgr Desired Behavior Demo External fragmentation occurred char *p1 = malloc(3); char *p2 = malloc(1); char *p3 = malloc(4); free(p2); char *p4 = malloc(6); free(p3); char *p5 = malloc(2); free(p1); free(p4); free(p5); p1 p2 p3 Heap 0 } Heap 0xffffffff Stack 39

DMMgr Desired Behavior Demo char *p1 = malloc(3); char *p2 = malloc(1); char *p3 = malloc(4); free(p2); char *p4 = malloc(6); free(p3); char *p5 = malloc(2); free(p1); free(p4); free(p5); p1 p2 p3 p4 Heap 0 } Heap 0xffffffff Stack 40

DMMgr Desired Behavior Demo DMMgr coalesced two free chunks char *p1 = malloc(3); char *p2 = malloc(1); char *p3 = malloc(4); free(p2); char *p4 = malloc(6); free(p3); char *p5 = malloc(2); free(p1); free(p4); free(p5); p1 p2 p3 p4 Heap 0 } Heap 0xffffffff Stack 41

DMMgr Desired Behavior Demo DMMgr reused previously freed chunk char *p1 = malloc(3); char *p2 = malloc(1); char *p3 = malloc(4); free(p2); char *p4 = malloc(6); free(p3); char *p5 = malloc(2); free(p1); free(p4); free(p5); p1 p5, p2 p3 p4 Heap 0 } Heap 0xffffffff Stack 42

DMMgr Desired Behavior Demo char *p1 = malloc(3); char *p2 = malloc(1); char *p3 = malloc(4); free(p2); char *p4 = malloc(6); free(p3); char *p5 = malloc(2); free(p1); free(p4); free(p5); p1 p5, p2 p3 p4 Heap 0 } Heap 0xffffffff Stack 43

DMMgr Desired Behavior Demo char *p1 = malloc(3); char *p2 = malloc(1); char *p3 = malloc(4); free(p2); char *p4 = malloc(6); free(p3); char *p5 = malloc(2); free(p1); free(p4); free(p5); p1 p5, p2 p3 p4 Heap 0 } Heap 0xffffffff Stack 44

DMMgr Desired Behavior Demo char *p1 = malloc(3); char *p2 = malloc(1); char *p3 = malloc(4); free(p2); char *p4 = malloc(6); free(p3); char *p5 = malloc(2); free(p1); free(p4); free(p5); p1 p5, p2 p3 p4 Heap 0 } Heap 0xffffffff Stack 45

DMMgr Desired Behavior Demo DMMgr cannot: Reorder requests Client may allocate & free in arbitrary order Any allocation may request arbitrary number of bytes Move memory chunks to improve performance Client stores addresses Moving a memory chunk would invalidate client pointer! Some external fragmentation is unavoidable 46

Agenda The need for DMM DMM using the heap section DMMgr 1: Minimal implementation DMMgr 2: Pad implementation Fragmentation DMMgr 3: List implementation DMMgr 4: doubly linked list implementation DMMgr 5: Bins implementation DMM using virtual memory DMMgr 6: VM implementation 47

List Impl Data structures Free list Next chunk in free list size header payload chunk Free list contains all free chunks In order by mem addr Each chunk contains header & payload Payload is used by client Header contains chunk size & (if free) addr of next chunk in free list Algorithms (by examples) 48

List Impl: malloc(n) Example 1 Free list < n >= n too small reasonable Free list < n >= n return this Search list for big-enough chunk Note: first-fit (not best-fit) strategy Found & reasonable size Remove from list and return payload 49

List Impl: malloc(n) Example 2 Free list < n >> n too small too big Free list < n n return this Search list for big-enough chunk Found & too big Split chunk, return payload of tail end Note: Need not change links 50

List Impl: free(p) Example Free list Free list free this Search list for proper insertion spot Insert chunk into list (Not finished yet!) 51

List Impl: free(p) Example (cont.) Free list Free list current chunk next chunk In list coalesced chunk Look at current chunk Next chunk in memory == next chunk in list Remove both chunks from list Coalesce Insert chunk into list (Not finished yet!) 52

List Impl: free(p) Example (cont.) Free list Free list prev chunk in list current chunk coalesced chunk Look at prev chunk in list Next in memory == next in list Remove both chunks from list Coalesce Insert chunk into list (Finished!) 53

List Impl: malloc(n) Example 3 Free list Free list too small too small too small n Search list for big-enough chunk None found Call brk() to increase heap size Insert new chunk at end of list (Not finished yet!) new large chunk 54

List Impl: malloc(n) Example 3 (cont.) Free list n Free list prev chunk In list new large chunk n Look at prev chunk in list Next chunk memory == next chunk in list Remove both chunks from list Coalesce Insert chunk into list Then proceed to use the new chunk, as before (Finished!) new large chunk 55

List Impl Algorithms (see precepts for more precision) malloc(n) Search free list for big-enough chunk Chunk found & reasonable size remove, use Chunk found & too big split, use tail end Chunk not found increase heap size, create new chunk New chunk reasonable size remove, use New chunk too big split, use tail end free(p) Search free list for proper insertion spot Insert chunk into free list Next chunk in memory also free remove both, coalesce, insert Prev chunk in memory free remove both, coalesce, insert 56

List Impl Performance Space Some internal & external fragmentation is unavoidable Headers are overhead Overall: good Time: malloc() Must search free list for big-enough chunk Bad: O(n) But often acceptable Time: free() Must search free list for insertion spot Bad: O(n) Often very bad 57

Princeton University Computer Science 217: Introduction to Programming Systems Dynamic Memory Management, continued 58

Minimal Impl Performance Performance (general case) Time: bad One system call per malloc() Space: bad Each call of malloc() extends heap size No reuse of freed chunks 59

Pad Impl Performance Performance (general case) Time: good malloc() calls sbrk() initially malloc() calls brk() infrequently thereafter Space: bad No reuse of freed chunks 60

Unsorted-list-no-coalesce performance Space Time: malloc() Time: free() 61

List Impl Performance Space Some internal & external fragmentation is unavoidable Headers are overhead Overall: good Time: malloc() Must search free list for big-enough chunk Bad: O(n) But often acceptable Time: free() Must search free list for insertion spot Bad: O(n) Often very bad 62

What s Wrong? Problem free() must traverse (long) free list, so can be (very) slow Solution Use a doubly linked list 63

Agenda The need for DMM DMM using the heap section DMMgr 1: Minimal implementation DMMgr 2: Pad implementation Fragmentation DMMgr 3: List implementation DMMgr 4: doubly linked list implementation DMMgr 5: Bins implementation DMM using virtual memory DMMgr 6: VM implementation 64

doubly linked List Impl Data structures Next chunk in free list Status bit: 0 free 1 in use Prev chunk in free list 1 size size header payload footer chunk Free list is doubly linked Each chunk contains header, payload, footer Payload is used by client Header contains status bit, chunk size, & (if free) addr of next chunk in list Footer contains redundant chunk size & (if free) addr of prev chunk in list Free list is unordered 65

doubly linked List Impl Typical heap during program execution: Free list 66

doubly linked List Impl Algorithms (see precepts for more precision) malloc(n) Search free list for big-enough chunk Chunk found & reasonable size remove, set status, use Chunk found & too big remove, split, insert tail, set status, use front Chunk not found increase heap size, create new chunk, insert New chunk reasonable size remove, set status, use New chunk too big remove, split, insert tail, set status, use front 67

doubly linked List Impl Algorithms (see precepts for more precision) free(p) Set status Search free list for proper insertion spot Insert chunk into free list Next chunk in memory also free remove both, coalesce, insert Prev chunk in memory free remove both, coalesce, insert 68

doubly linked List Impl Performance Consider sub-algorithms of free() Insert chunk into free list Linked list version: slow Traverse list to find proper spot doubly linked list version: fast Insert at front! Remove chunk from free list Linked list version: slow Traverse list to find prev chunk in list doubly linked list version: fast Use backward pointer of current chunk to find prev chunk in list 69

doubly linked List Impl Performance Consider sub-algorithms of free() Determine if next chunk in memory is free Linked list version: slow Traverse free list to see if next chunk in memory is in list doubly linked list version: fast Free list current next Use current chunk s size to find next chunk Examine status bit in next chunk s header 70

doubly linked List Impl Performance Consider sub-algorithms of free() Determine if prev chunk in memory is free Linked list version: slow Traverse free list to see if prev chunk in memory is in list doubly linked list version: fast Free list prev current Fetch prev chunk s size from its footer Do ptr arith to find prev chunk s header Examine status bit in prev chunk s header 71

Using payload space for management or, only free chunks need to be in the free-list Status Next chunk in free list Prev chunk in free list 1 size size header payload footer Status 1 size size header payload footer Status Next chunk in free list Prev chunk in free list 1 size size header payload This trick is NOT part of assignment 6! footer 72

Another use for the extra size field: error checking Status 1 size size The rain in Spain is mainly in the plain header payload footer char *s = (char *)malloc(32);... strcpy(s, "The rain in Spain is mainly in the plain.");... printf("%s\n", s); free(s); 73

doubly linked List Impl Performance Observation: All sub-algorithms of free() are fast free() is fast! 74

doubly linked List Impl Performance Space Some internal & external fragmentation is unavoidable Headers & footers are overhead Overall: Good Time: free() All steps are fast Good: O(1) Time: malloc() Must search free list for big-enough chunk Bad: O(n) Often acceptable Subject to bad worst-case behavior E.g. long free list with big chunks at end 75

doubly linked List Impl Performance with use-payload-space-for-management Space Some internal & external fragmentation is unavoidable Headers & footers are overhead Overall: Good Time: free() All steps are fast Good: O(1) Time: malloc() Must search free list for big-enough chunk Bad: O(n) Often acceptable Subject to bad worst-case behavior E.g. long free list with big chunks at end 76

What s Wrong? Problem malloc() must traverse doubly linked list, so can be slow Solution Use multiple doubly linked lists (bins) 77

Agenda The need for DMM DMM using the heap section DMMgr 1: Minimal implementation DMMgr 2: Pad implementation Fragmentation DMMgr 3: List implementation DMMgr 4: doubly linked list implementation DMMgr 5: Bins implementation DMM using virtual memory DMMgr 6: VM implementation 78

Bins Impl Data structures 10 11 12 MAX_BIN doubly linked list containing free chunks of size 10 doubly linked list containing free chunks of size 11 doubly linked list containing free chunks of size 12 doubly linked list containing free chunks of size >= MAX_BIN Use an array; each element is a bin Each bin is a doubly linked list of free chunks As in previous implementation bin[i] contains free chunks of size i Exception: Final bin contains chunks of size MAX_BIN or larger (More elaborate binning schemes are common) 79

Bins Impl Algorithms (see precepts for more precision) malloc(n) Search free list proper bin(s) for big-enough chunk Chunk found & reasonable size remove, set status, use Chunk found & too big remove, split, insert tail, set status, use front Chunk not found increase heap size, create new chunk New chunk reasonable size remove, set status, use New chunk too big remove, split, insert tail, set status, use front free(p) Set status Insert chunk into free list proper bin Next chunk in memory also free remove both, coalesce, insert Prev chunk in memory free remove both, coalesce, insert 80

Bins Impl Performance Space Pro: For small chunks, uses best-fit (not first-fit) strategy Could decrease internal fragmentation and splitting Con: Some internal & external fragmentation is unavoidable Con: Headers, footers, bin array are overhead Overall: good Time: malloc() Pro: Binning limits list searching Search for chunk of size i begins at bin i and proceeds downward Con: Could be bad for large chunks (i.e. those in final bin) Performance degrades to that of list version Overall: very good: O(1) Time: free() Very good: O(1) 81

DMMgr Impl Summary (so far) Implementation Space Time (1) Minimal Bad Malloc: Bad Free: Good (2) Pad Bad Malloc: Good Free: Good (3) List Good Malloc: Bad (but could be OK) Free: Bad (4) doubly linked List Good (5) Bins Good Malloc: Good Free: Good Malloc: Bad (but could be OK) Free: Good Assignment 6: Given (3), compose (4) and (5) 82

Agenda The need for DMM DMM using the heap section DMMgr 1: Minimal implementation DMMgr 2: Pad implementation Fragmentation DMMgr 3: List implementation DMMgr 4: doubly linked list implementation DMMgr 5: Bins implementation DMM using virtual memory DMMgr 6: VM implementation 83

Unix VM Mapping Functions Unix allows application programs to map/unmap VM explicitly void *mmap(void *p, size_t n, int prot, int flags, int fd, off_t offset); Creates a new mapping in the virtual address space of the calling process p: the starting address for the new mapping n: the length of the mapping If p is NULL, then the kernel chooses the address at which to create the mapping; this is the most portable method of creating a new mapping On success, returns address of the mapped area int munmap(void *p, size_t n); Deletes the mappings for the specified address range 84

Unix VM Mapping Functions Typical call of mmap() for allocating memory p = mmap(null, n, PROT_READ PROT_WRITE, MAP_PRIVATE MAP_ANON, 0, 0); Asks OS to map a new read/write area of virtual memory containing n bytes Returns the virtual address of the new area on success, (void*)-1 on failure Typical call of munmap() status = munmap(p, n); Unmaps the area of virtual memory at virtual address p consisting of n bytes Returns 0 on success, -1 on failure See Bryant & O Hallaron book and man pages for details 85

Agenda The need for DMM DMM using the heap section DMMgr 1: Minimal implementation DMMgr 2: Pad implementation Fragmentation DMMgr 3: List implementation DMMgr 4: doubly linked list implementation DMMgr 5: Bins implementation DMM using virtual memory DMMgr 6: VM implementation 86

VM Mapping Impl Data structures size header payload chunk Each chunk consists of a header and payload Each header contains size 87

VM Mapping Impl Algorithms void *malloc(size_t n) { size_t *p; if (n == 0) return NULL; p = mmap(null, n + sizeof(size_t), PROT_READ PROT_WRITE, MAP_PRIVATE MAP_ANONYMOUS, 0, 0); if (p == (void*)-1) return NULL; *p = n + sizeof(size_t); /* Store size in header */ p++; /* Move forward from header to payload */ return p; } void free(void *p) { if (p == NULL) return; p--; /* Move backward from payload to header */ munmap(p, *p); } 88

VM Mapping Impl Performance Space Fragmentation problem is delegated to OS Overall: Depends on OS Time For small chunks One system call (mmap()) per call of malloc() One system call (munmap()) per call of free() Overall: bad For large chunks free() unmaps (large) chunks of memory, and so shrinks page table Overall: good 89

The GNU Implementation Observation malloc() and free() on CourseLab are from the GNU (the GNU Software Foundation) Question How are GNU malloc() and free() implemented? Answer For small chunks Use heap (sbrk() and brk()) Use bins implementation For large chunks Use VM directly (mmap() and munmap()) 90

Segregated metadata 2 4 6 contiguous Data layout: no size field, no header at all! Malloc: look up in bins array, use first element of linked list Free: find size (somehow), put back at head of that bin s list 91

How free() finds the size 006FA8B0000 006FA8BFFFF 00381940000 0038194FFFF Hash table: 006FA8B 2 0038194 4 0058217 6 etc. 006FA8B0080 page number offset in page 92

Segregated metadata performance Space No overhead for header: very very good, O(1) No coalescing, fragmentation may occur, possibly bad Time malloc: very very good, O(1) free: hash-table lookup, good, O(1) 93

Summary The need for dynamic memory management Unknown object size DMM using the heap section On Unix: sbrk() and brk() Complicated data structures and algorithms Good for managing small memory chunks DMM using virtual memory On Unix: mmap() and munmap() Good for managing large memory chunks See Appendix for additional approaches/refinements 94