ANITA S SUPER AWESOME RECITATION SLIDES

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

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

Dynamic Memory Allocation I

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 Nov 5, 2002

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

Dynamic Memory Allocation

Dynamic Memory Allocation

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

Dynamic Memory Allocation: Basic Concepts

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

Recitation #11 Malloc Lab. November 7th, 2017

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

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

Dynamic Memory Alloca/on: Basic Concepts

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

Dynamic Memory Alloca/on: Basic Concepts

Spring 2016, Malloc Lab: Writing Dynamic Memory Allocator

CS201: Lab #4 Writing a Dynamic Storage Allocator

Dynamic Memory Allocation

CS 153 Design of Operating Systems

CSE 361S Intro to Systems Software Final Project

Memory Allocation I. 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: Advanced Concepts

Recitation 10: Malloc Lab

Recitation 11: More Malloc Lab

Recitation: Cache Lab & C

Foundations of Computer Systems

Internal Fragmentation

Memory Allocation I. CSE 351 Autumn Instructor: Justin Hsia

Dynamic Memory Allocation: Advanced Concepts

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

Memory Allocation II. CSE 351 Autumn Instructor: Justin Hsia

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

Dynamic Memory Allocation II October 22, 2008

Binghamton University Dynamic Memory Alloca/on

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

CSCI 2021, Fall 2018 Malloc Lab: Writing a Dynamic Storage Allocator Assigned: Monday November 5th Due: Monday November 19th, 11:55PM

CS 105 Malloc Lab: Writing a Dynamic Storage Allocator See Web page for due date

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

Dynamic Memory Allocation. Zhaoguo Wang

CS 105 Malloc Lab: Writing a Dynamic Storage Allocator See Web page for due date

Dynamic Memory Allocation: Advanced Concepts

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

CSE 361 Fall 2017 Malloc Lab: Writing a Dynamic Storage Allocator Assigned: Monday Nov. 13, Due: Monday Dec. 05, 11:59PM

Dynamic Memory Management

1 Introduction. 2 Logistics. 3 Hand Out Instructions

Optimizing Dynamic Memory Management

CS 3214, Fall 2015 Malloc Lab: Writing a Dynamic Storage Allocator Due date: November 16, 2014, 11:59pm

Assignment 5. CS/ECE 354 Spring 2016 DUE: April 22nd (Friday) at 9 am

CS61, Fall 2012 Section 2 Notes

Lab 1: Dynamic Memory: Heap Manager

Review! Lecture 5 C Memory Management !

CS61C : Machine Structures

Dynamic Memory Allocation

COMP 321: Introduction to Computer Systems

Lecture 8 Dynamic Memory Allocation

CS 213, Fall 2002 Malloc Lab: Writing a Debugging Dynamic Storage Allocator Assigned: Fri Nov. 1, Due: Tuesday Nov. 19, 11:59PM

Memory management. Johan Montelius KTH

Writing a Dynamic Storage Allocator

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

CSci 4061 Introduction to Operating Systems. Programs in C/Unix

Foundations of Computer Systems

Memory Allocation II. CSE 351 Autumn Instructor: Justin Hsia

ECE454, Fall 2014 Homework3: Dynamic Memory Allocation Assigned: Oct 9th, Due: Nov 6th, 11:59PM

Dynamic Memory Allocation (and Multi-Dimensional Arrays)

CSCI0330 Intro Computer Systems Doeppner. Project Malloc. Due: 11/28/18. 1 Introduction 1. 2 Assignment Specification Support Routines 4

CS61C : Machine Structures

Dynamic Memory Management! Goals of this Lecture!

Dynamic Memory Alloca/on: Advanced Concepts

Memory Allocation III

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

CS61C : Machine Structures

10.1. CS356 Unit 10. Memory Allocation & Heap Management

CS61C Midterm Review on C & Memory Management

Memory (Stack and Heap)

Heap Arrays. Steven R. Bagley

COSC Software Engineering. Lecture 16: Managing Memory Managers

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

HW 3: Malloc CS 162. Due: Monday, March 28, 2016

Announcements. assign0 due tonight. Labs start this week. No late submissions. Very helpful for assign1

CS 153 Design of Operating Systems

Memory Management. CS449 Fall 2017

CSC 1600 Memory Layout for Unix Processes"

Project 4: Implementing Malloc Introduction & Problem statement

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

Recitation: C Review. TA s 20 Feb 2017

G52CPP C++ Programming Lecture 9

Week 9 Part 1. Kyle Dewey. Tuesday, August 28, 12

CS61C : Machine Structures

Memory Allocation III CSE 351 Spring (original source unknown)

Deep C. Multifile projects Getting it running Data types Typecasting Memory management Pointers. CS-343 Operating Systems

DAY 3. CS3600, Northeastern University. Alan Mislove

CS356: Discussion #12-13 Dynamic Memory and Allocation Lab. Marco Paolieri Illustrations from CS:APP3e textbook

CS24: INTRODUCTION TO COMPUTING SYSTEMS. Spring 2017 Lecture 7

Transcription:

ANITA S SUPER AWESOME RECITATION SLIDES 15/18-213: Introduction to Computer Systems Dynamic Memory Allocation Anita Zhang, Section M

UPDATES Cache Lab style points released Don t fret too much Shell Lab to be graded Last turn in was yesterday Turn around time of 1-2 weeks Malloc Lab due Thursday, April 11, 2013 Probably your last chance to use late days

WELCOME TO MALLOC Dynamic Memory Allocation Managing Free Blocks Finding a Free Block Splitting Blocks Allocating/ Freeing Blocks Malloc Lab Tips Mal = bad Loc = place Bad Place

REMEMBER THIS? Rubber Duck Debugging

DYNAMIC MEMORY Programmers use dynamic memory allocators (i.e. malloc) to acquire memory For sizes only known at runtime Dynamic memory allocators manage an area of process virtual memory known as the heap 0 User stack Heap (via malloc) Uninitialized data (.bss) Initialized data (.data) Program text (.text) Top of heap (brk ptr)

MANAGING FREE BLOCKS Method 1: Implicit list using length links all blocks 5 4 6 2 Method 2: Explicit list among the free blocks using pointers 5 4 6 2 Additionally: Segregated free list Different free lists for different size classes Additionally: 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

COMPARING MANAGEMENT FORMATS Implicit Free List Explicit Free List 1 word 1 word 1 word Size a Size a Size a Next Payload Payload and padding Prev Optional padding Size a Size a Format of allocated and free blocks Allocated blocks Free blocks

VISUALIZING EXPLICIT FREE LISTS Logically: A B C Physically (any order): A 4 4 4 4 6 6 4 4 4 4 C Forward (next) links B Back (prev) links

SEGREGATED FREE LISTS Each size class of blocks has its own free list May also be called a bucket 1-2 3 4 5-8 9-inf Often have separate classes for each small size For larger sizes: One class for each power of two

FINDING FREE BLOCKS First fit: Search from the beginning Choose the first free block that fits Can take linear time depending on the total number of blocks in the list Can cause splinters at the beginning of list Many small free blocks left at the beginning

FINDING FREE BLOCKS Next fit Searches starting where previous search finished Often faster than first fit Avoids re-scanning blocks of the wrong size Some research suggests that fragmentation is worse K&R has an example of this

FINDING FREE BLOCKS Best fit Chooses the best fitting free block Fits with the fewest bytes left over Keeps fragments small Usually improves memory utilization Will typically run slower than first fit If the best block is larger than we need, may split it

FINDING FREE BLOCKS OVERVIEW 3 Methods First Fit Next Fit Best Fit What if no blocks are large enough? Extend the heap Use brk() or sbrk() system calls Malloc Lab: use mem_sbrk() Allocates more bytes to the end of the heap; high overhead sbrk(0) returns a pointer to top of the current heap Key: Use what you need, save the rest as a free block

SPLITTING BLOCKS What happens if the block we have is too big? Split it up Key: Use what you need, save the rest as a free block Implicit lists Correct size maintains list Explicit lists (If segregated) determine correct bucket size Follow insertion policy

PROPERLY FREEING BLOCKS Simplest implementation: Need only clear the allocated flag void free_block(ptr p) { *p = *p & -2 }...But can lead to false fragmentation 4 4 4 2 2 free(p) p 4 4 4 2 2 malloc(5) Oops! There is enough free space, but the allocator won t be able to find it

COALESCING Combining blocks in nearby memory Implicit lists Look backwards/ forwards using block sizes. Explicit lists Look backwards/ forwards using block sizes Seg. List: Use the new block size to find the bucket Case 1 Case 2 Case 3 Case 4 Block being freed Allocated Allocated Allocated Free Free Allocated Free Free

INSERTION POLICY Where should freed blocks go? LIFO (last-in-first-out) Insert freed block at the beginning of the free list Pro: Simple and constant time Con: Studies suggest fragmentation is worse than address ordered Address-ordered Keep freed blocks list sorted in address order Pro: Studies suggest fragmentation is lower than LIFO Con: Requires searching

ABOUT MALLOC LAB You need to implement the following functions: int mm_init(void); void *malloc(size_t size); void free(void *ptr); void *realloc(void *ptr, size_t size); void *calloc (size_t nmemb, size_t size); void mm_checkheap(int); Scored on efficiency and throughput Cannot call system memory functions Use helper functions Consider version control

DESIGN QUESTIONS (IN NO ORDER) How do we efficiently manage freed blocks? When should we coalesce? What are the ideal bucket sizes? How can we increase throughput? Latency? Which search algorithm is better? What insertion policy should I use?

HEAP CHECKER void mm_checkheap(int) Write it early; update it with your implementation Ensures the heap is sane Everything should either be allocated or listed Your pointers are pointing to the correct blocks Look over lecture notes on garbage collection Particularly mark & sweep This function is meant to be correct, not efficient

KEYWORDS inline Copies function code into location of each function call Avoids overhead of a function call (once assembled) Can often be used in place of macros Strong type checking and input handling, unlike macros static Pretty much like static variables Resides in a single place in memory Limits scope of function to the current file Should use this for helper functions only called locally Avoids polluting namespace static inline Combined effect

DEBUGGING Using printf, assert, etc. only in debug mode Comment out #define for the else case #define DEBUG #ifdef DEBUG # define dbg_printf(...) printf( VA_ARGS ) # define dbg_assert(...) assert( VA_ARGS ) # define dbg(...) VA_ARGS #else # define dbg_printf(...) # define dbg_assert(...) # define dbg(...) #endif

DEBUGGING Valgrind Powerful debugging and analysis technique Rewrites text section of executable object file Can detect all errors as a debugging malloc Can also check each individual reference at runtime GDB Bad pointers Overwriting Referencing outside of allocated block Pro Tip: The O2 flag is used in the Makefile May give unexpected results when using GDB

VERSION CONTROL Warning: You may have to rewrite your malloc once or twice in the next week Use version control so you don t lose track Here s a good reference on Git The Messy Circuit Analogy

COMMON MISCONCEPTIONS Global data structures is not the same as declaring types Use mem_sbrk to get space for your data structures typedef struct { int x; int y; } point; Vs. point a = {5, 6}; Casting is your friend in this lab Data from mem_sbrk is like any other data The driver resets the heap by calling mm_init May require you to update some of your pointers 64 bit addresses, but the heap 2 32 bytes Use this information as you see fit

GETTING STARTED Read the 32 bit implicit list in CS:APP Understand the macros, then steal them Don t copy and paste from the CS:APP website Typing it yourself will give you epiphanies The coalescing code provided is great Implement a 64 bit malloc Super naïve and inefficient may be a good start Implement mm_checkheap for this heap pattern

GETTING MORE POINTS Implicit list malloc is worth no credit Last checked it was worth ~40 points Explicit free list is expected Gets you to the ~80 point range Update from explicit to segregated free lists Puts you in the ~90 point range

FINAL WORDS FROM PREVIOUS YEARS Write mm_checkheap Write mm_checkheap well Write coalescing to make bugs more apparent, then fix bugs using mm_checkheap Start now You ll be spending a lot of time pointer chasing Accelerate neutrinos past the speed of light, enabling you to start three days ago Good luck!

QUESTIONS AND CREDITS SLIDE Rubber Duck Git Reference Some picture of a messy circuit