Memory Allocation II. CSE 351 Autumn Instructor: Justin Hsia

Similar documents
Memory Allocation II. CSE 351 Autumn Instructor: Justin Hsia

Memory Allocation I. CSE 351 Autumn Instructor: Justin Hsia

Memory Allocation I. CSE 351 Autumn Instructor: Justin Hsia

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: 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

Dynamic Memory Allocation

Dynamic Memory Allocation: Basic Concepts

Dynamic Memory Allocation

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

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

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

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

Dynamic Memory Allocation I Nov 5, 2002

Dynamic Memory Allocation I

Dynamic Memory Alloca/on: Basic Concepts

Caches III. CSE 351 Autumn Instructor: Justin Hsia

Dynamic Memory Alloca/on: Basic Concepts

The Stack & Procedures

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

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

Integers II. CSE 351 Autumn Instructor: Justin Hsia

Administrivia. Caches III. Making memory accesses fast! Associativity. Cache Organization (3) Example Placement

CS 153 Design of Operating Systems

Floating Point. CSE 351 Autumn Instructor: Justin Hsia

Dynamic Memory Allocation

Floating Point. CSE 351 Autumn Instructor: Justin Hsia

Binghamton University Dynamic Memory Alloca/on

Caches II. CSE 351 Autumn Instructor: Justin Hsia

Dynamic Memory Allocation: Advanced Concepts

Memory Allocation III

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

Internal Fragmentation

Dynamic Memory Allocation II October 22, 2008

Memory, Data, & Addressing I

Foundations of Computer Systems

Virtual Memory II. CSE 351 Autumn Instructor: Justin Hsia

ANITA S SUPER AWESOME RECITATION SLIDES

Foundations of Computer Systems

x86 64 Programming I CSE 351 Autumn 2017 Instructor: Justin Hsia

CS 153 Design of Operating Systems

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

Memory Allocation III

Arrays. CSE 351 Autumn Instructor: Justin Hsia

Dynamic Memory Allocation: Advanced Concepts

Dynamic Memory Allocation: Advanced Concepts

Memory Allocation III

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

Optimizing Dynamic Memory Management

Dynamic Memory Allocation. Zhaoguo Wang

Recitation #11 Malloc Lab. November 7th, 2017

Virtual Memory Wrap Up

Dynamic Memory Alloca/on: Advanced Concepts

CMSC 313 Spring 2010 Exam 3 May 17, 2010

Dynamic Memory Allocation

Java and C. CSE 351 Autumn 2018

Structs & Alignment. CSE 351 Autumn Instructor: Justin Hsia

Processes. CSE 351 Autumn Instructor: Justin Hsia

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

Review! Lecture 5 C Memory Management !

CS61C : Machine Structures

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

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

10.1. CS356 Unit 10. Memory Allocation & Heap Management

Operating System Labs. Yuanbin Wu

Memory management. Johan Montelius KTH

CS61C : Machine Structures

Engine Support System. asyrani.com

CSE351 Spring 2010 Final Exam (9 June 2010)

CSE351 Spring 2010 Final Exam (9 June 2010)

CSCI 237 Sample Final Exam

Structs and Alignment

Dynamic Memory Management

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

My malloc: mylloc and mhysa. Johan Montelius HT2016

CS61C : Machine Structures

UW CSE 351, Winter 2013 Final Exam

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

CSE351 Autumn 2013 Final Exam (11 Dec 2013)

Integers II. CSE 351 Autumn Instructor: Justin Hsia

Dynamic Memory Allocation

Cache Wrap Up, System Control Flow

Recitation 10: Malloc Lab

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

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

Midterm Sample Answer ECE 454F 2008: Computer Systems Programming Date: Tuesday, Oct 28, p.m. - 5 p.m.

1 Introduction. 2 Logistics. 3 Hand Out Instructions

Allocator Basics. Dynamic Memory Allocation in the Heap (malloc and free) Allocator Goals: malloc/free. Internal Fragmentation

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

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

There are 16 total numbered pages, 7 Questions. You have 2 hours. Budget your time carefully!

Dynamic Memory Management

Lecture 7 More Memory Management Slab Allocator. Slab Allocator

CS61C : Machine Structures

CS61, Fall 2012 Section 2 Notes

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

Heap Management. Heap Allocation

Buffer Overflows. CSE 351 Autumn Instructor: Justin Hsia

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

Transcription:

Memory Allocation II CSE 351 Autumn 2017 Instructor: Justin Hsia Teaching Assistants: Lucas Wotton Michael Zhang Parker DeWilde Ryan Wong Sam Gehman Sam Wolfson Savanna Yee Vinny Palaniappan http://xkcd.com/1909/

Administrivia Homework 5 due Friday (12/1) Lab 5 due next Friday (12/8) Final Exam: Wed, Dec. 13 @ 12:30pm in KNE 120 Same seating chart as Midterm Review Session: Mon, Dec. 11 @ 5:00pm in EEB 105 Cumulative (midterm clobber policy applies) You get TWO double sided handwritten 8.5 sheets 11 cheat Recommended that you reuse or remake your midterm cheat sheet 2

Peer Instruction Question Which allocation strategy and requests remove external fragmentation in this Heap? B3 was the last fulfilled request. http://pollev.com/justinh (A) (B) Best fit: malloc(50), malloc(50) First fit: malloc(50), malloc(30) payload size 50 10 30 10 50 B2 B3 (C) (D) Next fit: malloc(30), malloc(50) Next fit: malloc(50), malloc(30) 50 Start of heap B1 3

Implicit List: Allocating in a Free Block Allocating in a free block: splitting Since allocated space might be smaller than free space, we might want to split the block Assume ptr points to a free block and has unscaled pointer arithmetic void split(ptr b, int bytes) { // bytes = desired block size int newsize = ((bytes+7) >> 3) << 3; // round up to multiple of 8 int oldsize = *b; // why not mask out low bit? *b = newsize; // initially unallocated if (newsize < oldsize) *(b+newsize) = oldsize - newsize; // set length in remaining } // part of block (UNSCALED +) malloc(12): ptr b = find(12+4) split(b, 12+4) allocate(b) 8 1 24 0 8 1 b 8 1 16 1 8 0 8 1 Free box Allocated box Newly allocated box 4

Implicit List: Freeing a Block Simplest implementation just clears allocated flag void free(ptr p) {*(p-box) &= -2;} But can lead to false fragmentation free(p) 8 1 16 1 8 0 8 1 p 8 1 16 0 8 0 8 1 Free box Allocated box Block of interest malloc(20) Oops! There is enough free space, but the allocator won t be able to find it 5

Implicit List: Coalescing with Next Join (coalesce) with next block if also free free(p) 8 1 16 1 8 0 8 1 p 8 1 24 0 8 0 8 1 logically gone Free box Allocated box Block of interest void free(ptr p) { // p points to payload ptr b = p BOX; // b points to block header *b &= -2; // clear allocated bit ptr next = b + *b; // find next block (UNSCALED +) if ((*next & 1) == 0) // if next block is not allocated, *b += *next; // add its size to this block } How do we coalesce with the previous block? 6

Implicit List: Bidirectional Coalescing Boundary tags [Knuth73] Replicate header at bottom (end) of free blocks Allows us to traverse backwards, but requires extra space Important and general technique! 16/0 16/0 16/1 16/124/0 24/0 16/1 16/1 Format of allocated and free blocks: Header size payload and padding a a = 1: allocated block a = 0: free block size: block size (in bytes) Boundary tag (footer) size a payload: application data (allocated blocks only) 7

Constant Time Coalescing Case 1 Case 2 Case 3 Case 4 Block being freed Allocated Allocated Free Free Allocated Free Allocated Free 8

Constant Time Coalescing Case 1 m1 1 m1 1 Case 2 m1 1 m1 1 n 1 m1 1 n 0 m1 1 n 1 m1 1 m1 1 n+m2 0 n 1 m2 1 n 0 m2 1 n 1 m2 0 m2 1 m2 1 m2 0 n+m2 0 Case 3 m1 0 n+m1 0 Case 4 m1 0 m1 0 n 1 m1 0 n 1 n+m1+m2 0 n 1 m2 1 n+m1 0 m2 1 n 1 m2 0 m2 1 m2 1 m2 0 n+m1+m2 0

Implicit Free List Review Questions 16/0 16/0 16/1 16/124/0 24/0 16/1 16/1 What is the block header? What do we store and how? What are boundary tags and why do we need them? When we coalesce free blocks, how many neighboring blocks do we need to check on either side? Why is this? If I want to check the size of the th block forward from the current block, how many memory accesses do I make? 10

Keeping Track of Free Blocks = 4 byte box (free) = 4 byte box (allocated) 1) Implicit free list using length links all blocks using math No actual pointers, and must check each block if allocated or free 20 16 24 8 2) Explicit free list among only the free blocks, using pointers 20 16 24 8 3) Segregated free list Different free lists for different size classes 4) Blocks sorted by size Can use a balanced binary tree (e.g. red black tree) with pointers within each free block, and the length used as a key 11

Explicit Free Lists Allocated block: Free block: size a size a next payload and padding prev size a size a (same as implicit free list) Use list(s) of free blocks, rather than implicit list of all blocks The next free block could be anywhere in the heap So we need to store next/previous pointers, not just sizes Since we only track free blocks, so we can use payload for pointers Still need boundary tags (header/footer) for coalescing 12

Doubly Linked Lists Linear Root Needs head/root pointer First node prev pointer is NULL Last node next pointer is NULL Good for first fit, best fit Start Circular Still have pointer to tell you which node to start with No NULL pointers (term condition is back at starting point) Good for next fit, best fit 13

Explicit Free Lists Logically: doubly linked list A B C Physically: blocks can be in any order A Forward (next) links 16 16 16 16 24 24 16 16 16 16 C B Back (prev) links 14

Allocating From Explicit Free Lists Note: These diagrams are not very specific about where inside a block a pointer points. In reality we would always point to one place (e.g. start/header of a block). Before After (with splitting) = malloc( ) 15

Allocating From Explicit Free Lists Note: These diagrams are not very specific about where inside a block a pointer points. In reality we would always point to one place (e.g. start/header of a block). Before After (fully allocated) = malloc( ) 16

Freeing With Explicit Free Lists Insertion policy: Where in the free list do you put the newly freed block? LIFO (last in first out) policy Insert freed block at the beginning (head) of the free list Pro: simple and constant time Con: studies suggest fragmentation is worse than the alternative Address ordered policy Insert freed blocks so that free list blocks are always in address order: address(previous) < address(current) < address(next) Con: requires linear time search Pro: studies suggest fragmentation is better than the alternative 17

Coalescing in Explicit Free Lists Case 1 Case 2 Case 3 Case 4 Block being freed Allocated Allocated Free Free Allocated Free Allocated Free Neighboring free blocks are already part of the free list 1) Remove old block from free list 2) Create new, larger coalesced block 3) Add new block to free list (insertion policy) How do we tell if a neighboring block if free? 18

Freeing with LIFO Policy (Case 1) Boundary tags not shown, but don t forget about them! Before free( ) Root Insert the freed block at the root of the list After Root 19

Freeing with LIFO Policy (Case 2) Boundary tags not shown, but don t forget about them! Before free( ) Root Splice successor block out of list, coalesce both memory blocks, and insert the new block at the root of the list After Root 20

Freeing with LIFO Policy (Case 3) Boundary tags not shown, but don t forget about them! Before free( ) Root Splice predecessor block out of list, coalesce both memory blocks, and insert the new block at the root of the list After Root 21

Freeing with LIFO Policy (Case 4) Boundary tags not shown, but don t forget about them! Before free( ) Root After Splice predecessor and successor blocks out of list, coalesce all 3 memory blocks, and insert the new block at the root of the list Root 22

Do we always need the boundary tags? Allocated block: Free block: size a size a next payload and padding prev size a (same as implicit free list) Lab 5 suggests no size a 23

Explicit List Summary Comparison with implicit list: Block allocation is linear time in number of free blocks instead of all blocks Much faster when most of the memory is full Slightly more complicated allocate and free since we need to splice blocks in and out of the list Some extra space for the links (2 extra pointers needed for each free block) Increases minimum block size, leading to more internal fragmentation Most common use of explicit lists is in conjunction with segregated free lists Keep multiple linked lists of different size classes, or possibly for different types of objects 24