Recitation #11 Malloc Lab. November 7th, 2017

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

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

ANITA S SUPER AWESOME RECITATION SLIDES

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

Dynamic Memory Allocation

Recitation 10: Malloc Lab

EL2310 Scientific Programming

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

Dynamic Memory Allocation

CS61, Fall 2012 Section 2 Notes

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

Spring 2016, Malloc Lab: Writing Dynamic Memory Allocator

CS201: Lab #4 Writing a Dynamic Storage Allocator

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

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

CSE 361S Intro to Systems Software Final Project

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

Class Information ANNOUCEMENTS

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

Dynamic Memory Allocation: Advanced Concepts

1 Introduction. 2 Logistics. 3 Hand Out Instructions

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

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

COMP 321: Introduction to Computer Systems

C: Pointers. C: Pointers. Department of Computer Science College of Engineering Boise State University. September 11, /21

My malloc: mylloc and mhysa. Johan Montelius HT2016

Review! Lecture 5 C Memory Management !

CS61C : Machine Structures

Implementing Malloc: Students and Systems Programming

CS61C Midterm Review on C & Memory Management

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

Recitation 11: More Malloc Lab

Motivation was to facilitate development of systems software, especially OS development.

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

Memory management. Johan Montelius KTH

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

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

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

Dynamic Allocation in C

Data Structure Series

Kurt Schmidt. October 30, 2018

Dynamic Memory Allocation

C Programming. Course Outline. C Programming. Code: MBD101. Duration: 10 Hours. Prerequisites:

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

Run-time Environments - 3

Lecture 2: C Programm

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

Quiz 0 Review Session. October 13th, 2014

C: Pointers, Arrays, and strings. Department of Computer Science College of Engineering Boise State University. August 25, /36

// Initially NULL, points to the dynamically allocated array of bytes. uint8_t *data;

CP2 Revision. theme: dynamic datatypes & data structures

Memory Allocation II. CSE 351 Autumn Instructor: Justin Hsia

Memory Allocation III

Structures, Unions Alignment, Padding, Bit Fields Access, Initialization Compound Literals Opaque Structures Summary. Structures

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

CS113: Lecture 9. Topics: Dynamic Allocation. Dynamic Data Structures

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

Project 4: Implementing Malloc Introduction & Problem statement

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

CSC 1600 Memory Layout for Unix Processes"

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

Arrays and Memory Management

CSCI 171 Chapter Outlines

Memory Management. CSC215 Lecture

Heap Arrays and Linked Lists. Steven R. Bagley

Reminder: compiling & linking

Recitation: Cache Lab & C

CS61C : Machine Structures

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

CS107 Handout 08 Spring 2007 April 9, 2007 The Ins and Outs of C Arrays

Dynamic Memory Allocation: Basic Concepts

Review: C Strings. A string in C is just an array of characters. Lecture #4 C Strings, Arrays, & Malloc

Lectures 13 & 14. memory management

Lecture Notes on Types in C

Limitations of the stack

CS 261 Fall C Introduction. Variables, Memory Model, Pointers, and Debugging. Mike Lam, Professor

Lecture 8 Dynamic Memory Allocation

Pointers, Dynamic Data, and Reference Types

Dynamic Memory Allocation

Administrivia. Introduction to Computer Systems. Pointers, cont. Pointer example, again POINTERS. Project 2 posted, due October 6

10.1. CS356 Unit 10. Memory Allocation & Heap Management

Data Structures and Algorithms for Engineers

PRINCIPLES OF OPERATING SYSTEMS

Memory Management: The process by which memory is shared, allocated, and released. Not applicable to cache memory.

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

Motivation was to facilitate development of systems software, especially OS development.

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

Dynamic Memory Allocation: Basic Concepts

CSE351 Spring 2010 Final Exam (9 June 2010)

Contents of Lecture 3

Dynamic Memory Allocation I Nov 5, 2002

Carnegie Mellon. Cache Lab. Recitation 7: Oct 11 th, 2016

Optimizing Dynamic Memory Management

Annotation Annotation or block comments Provide high-level description and documentation of section of code More detail than simple comments

Dynamic Memory: Alignment and Fragmentation

Dynamic Memory Allocation. Zhaoguo Wang

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

CS61C : Machine Structures

Transcription:

18-600 Recitation #11 Malloc Lab November 7th, 2017 1

2 Important Notes about Malloc Lab Malloc lab has been updated from previous years Supports a full 64 bit address space rather than 32 bit Encourages a new programming style Use structures instead of macros Study the baseline implementation of implicit allocator to get a better idea Divided into two phases: Checkpoint 1: Due date: 11/17 Final: Due date: 11/27 Try to finish Cache Lab by Thursday; it will help with Malloc and during a much-needed Thanksgiving break! Get a correct, reasonably performing malloc by checkpoint Optimize malloc by final submission

3 Pointers: casting, arithmetic, and dereferencing

4 Pointer casting Cast from <type_a>* to <type_b>* Gives back the same value Changes the behavior that will happen when dereferenced <type_a>* to integer/ unsigned int / long Pointers are really just 8-byte numbers Taking advantage of this is an important part of malloc lab Be careful, though, as this can easily lead to errors integer/ unsigned int to <type_a>*

5 Pointer arithmetic The expression ptr + a doesn t mean the same thing as it would if ptr were an integer. Example: type_a* pointer = ; (void *) pointer2 = (void *) (pointer + a); This is really computing: pointer2 = pointer + (a * sizeof(type_a)) lea (pointer, a, sizeof(type_a)), pointer2; Pointer arithmetic on void* is undefined

6 Pointer arithmetic int * ptr = (int *)0x12341230; int * ptr2 = ptr + 1; char * ptr = (char *)0x12341230; char * ptr2 = ptr + 1; int * ptr = (int *)0x12341230; int * ptr2 = ((int *) (((char *) ptr) + 1));

7 Pointer arithmetic int * ptr = (int *)0x12341230; int * ptr2 = ptr + 1; //ptr2 is 0x12341234 char * ptr = (char *)0x12341230; char * ptr2 = ptr + 1; //ptr2 is 0x12341231 int * ptr = (int *)0x12341230; int * ptr2 = ((int *) (((char *) ptr) + 1)); //ptr2 is 0x12341231

8 Pointer dereferencing Basics It must be a POINTER type (or cast to one) at the time of dereference Cannot dereference expressions with type void* Dereferencing a t* evaluates to a value with type t

9 Pointer dereferencing What gets returned? int * ptr1 = (int *) malloc(sizeof(int)); *ptr1 = 0xdeadbeef; int val1 = *ptr1; int val2 = (int) *((char *) ptr1); What are val1 and val2?

Pointer dereferencing What gets returned? int * ptr1 = (int *) malloc(sizeof(int)); *ptr1 = 0xdeadbeef; int val1 = *ptr1; int val2 = (int) *((char *) ptr1); // val1 = 0xdeadbeef; // val2 = 0xffffffef; What happened?? 10

11 Malloc basics What is dynamic memory allocation? Terms you will need to know malloc/ calloc / realloc free sbrk payload fragmentation (internal vs. external) coalescing Bi-directional Immediate vs. Deferred

12 Concept Really, malloc only does three things: 1. Organize all blocks and store information about them in a structured way. 2. Using the structure made in 1), choose an appropriate location to allocate new memory. 3. Update the structure made in 1) when the user frees a block of memory

13

14 Fragmentation Internal fragmentation Result of payload being smaller than block size. void * m1 = malloc(3); void * m2 = malloc(3); m1, m2 both have to be aligned to 16 bytes External fragmentation

15

16 Goals 1. Run as fast as possible 2. Waste as little memory as possible What kind of implementation to use? Implicit list, explicit list, segregated lists, binary tree methods etc Can use specialized strategies depending on the size of allocations Adaptive algorithms are fine, though not necessary to get 100%. What fit algorithm to use? Best fit: choose the smallest block that is big enough to fit the requested allocation size First fit / next fit: search linearly starting from some location, and pick the first block that fits. Which one s faster, and which one uses less memory?

17 Implicit List From the root, can traverse across blocks using headers which store the size of the block Can find a free block this way Can take a while to find a free block How would you know when you have to call sbrk?

18 Explicit List Improvement over implicit list From a root, keep track of all free blocks in a (doubly) linked list Remember a doubly linked list has pointers to next and previous Optimization: using a singly linked list instead (how could we do this?) When malloc is called, can now find a free block quickly What happens if the list is a bunch of small free blocks but we want a really big one? How can we speed this up?

19 Segregated List An optimization for explicit lists Can be thought of as multiple explicit lists What should we group by? Grouped by size let us quickly find a block of the size we want What size/number of buckets should we use? This is up to you to decide

20 Implementation Hurdles How do we know where the blocks are? How do we know how big the blocks are? How do we know which blocks are free? Remember: can t buffer calls to malloc and free must deal with them real-time. Remember: calls to free only takes a pointer, not a pointer and a size. Solution: Need a data structure to store information on the blocks Where do I keep this data structure? We can t allocate a space for it, that s what we are writing!

21 The data structure Requirements: The data structure needs to tell us where the blocks are, how big they are, and whether they re free We need to be able to CHANGE the data structure during calls to malloc and free We need to be able to find the next free block that is a good fit for a given payload We need to be able to quickly mark a block as free/allocated We need to be able to detect when we re out of blocks. What do we do when we re out of blocks?

22 The data structure Common types Implicit List Root -> block1 -> block2 -> block3 -> Explicit List Root -> free block 1 -> free block 2 -> free block 3 -> Segregated List Small-malloc root -> free small block 1 -> free small block 2 -> Medium-malloc root -> free medium block 1 -> Large-malloc root -> free block chunk1 ->

Playing with structures Consider the following structure, where a block refers to an allocation unit Each block consists of some metadata (header) and the actual data (payload) /* Basic declarations */ typedef uint64_t word_t; static const size_t wsize = sizeof(word_t); /* Basic declarations */ typedef uint64_t word_t; static const size_t wsize = sizeof(word_t); typedef struct block { word_t header; word_t alloc; char payload[0]; } block_t; Why is this reasonable? typedef struct block { // Header contains size + allocation flag word_t header; char payload[0]; } block_t; 23

24 Playing with structures The contents of the header is populated as follows /* Pack size and allocation bit into single word */ static word_t pack(size_t size, bool alloc) { } return size alloc; /* Basic declarations */ typedef uint64_t word_t; static const size_t wsize = sizeof(word_t); typedef struct block { // Header contains size + allocation flag word_t header; char payload[0]; } block_t;

25 Playing with structures How do we set the value in the header, given the block and values? /* Set fields in block header */ static void write_header(block_t *block, size_t size, bool alloc) { /* Basic declarations */ typedef uint64_t word_t; static const size_t wsize = sizeof(word_t); } block->header = pack(size, alloc); typedef struct block { // Header contains size + allocation flag word_t header; char payload[0]; } block_t;

26 Playing with structures How do we extract the value of the size, given the header? How do we extract the value of the size, given pointer to block? /* Extract size from header */ static size_t extract_size(word_t word) { return (word & ~(word_t) 0x7); } /* Get block size */ static size_t get_size(block_t *block) { return extract_size(block->header); } /* Basic declarations */ typedef uint64_t word_t; static const size_t wsize = sizeof(word_t); typedef struct block { // Header contains size + allocation flag word_t header; char payload[0]; } block_t;

27 Playing with structures How do we write to the end of the block? /* Set fields in block footer */ static void write_footer(block_t *block, size_t size, bool alloc) { } word_t *footerp = (word_t *)((block->payload) + get_size(block) - 2*wsize); *footerp = pack(size, alloc); /* Basic declarations */ typedef uint64_t word_t; static const size_t wsize = sizeof(word_t); typedef struct block { // Header contains size + allocation flag word_t header; char payload[0]; } block_t;

28 Playing with structures How do we get to the start of the block, given the pointer to the payload? /* Locate start of block, given pointer to payload */ static block_t *payload_to_header(void *bp) { } return (block_t *)(((char *)bp) - offsetof(block_t, payload)); /* Basic declarations */ typedef uint64_t word_t; static const size_t wsize = sizeof(word_t); typedef struct block { // Header contains size + allocation flag word_t header; char payload[0]; } block_t;

29 GDB Practice Using GDB well in Malloc lab can save you HOURS of debugging time! Turn off gcc optimization before running GDB (-O0) Don t forget to turn it back on (-O3) for the benchmark! 5 commands to remember: 1. backtrace 2. frame 3. disassemble 4. print <reg> 5. watch

30 Words of Wisdom Write a heap checker first. Please just do it and thank us later! Check for conditions that you know your heap should have. Printf <<<<< GDB Use version control, otherwise you ll regret it Don t feel bad about throwing away broken solutions! Start early, read the handout carefully. Warnings: Most existing Malloc literature from the book has slightly different guidelines, they may be out of date