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

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

CSE 361S Intro to Systems Software Final Project

Spring 2016, Malloc Lab: Writing Dynamic Memory Allocator

CS201: Lab #4 Writing a Dynamic Storage Allocator

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

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

COMP 321: Introduction to Computer Systems

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

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

Lecture 8 Dynamic Memory Allocation

1 Introduction. 2 Logistics. 3 Hand Out Instructions

Lab 1: Dynamic Memory: Heap Manager

Optimizing Dynamic Memory Management

Dynamic Memory Allocation

Project 4: Implementing Malloc Introduction & Problem statement

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

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

Dynamic Memory Management! Goals of this Lecture!

Dynamic Memory Allocation. Zhaoguo Wang

Memory management. Johan Montelius KTH

Dynamic Memory Management

My malloc: mylloc and mhysa. Johan Montelius HT2016

Writing a Dynamic Storage Allocator

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

CS61C : Machine Structures

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

HW 1: Shell. Contents CS 162. Due: September 18, Getting started 2. 2 Add support for cd and pwd 2. 3 Program execution 2. 4 Path resolution 3

Shell Project, part 3 (with Buddy System Memory Manager) ( points)

COSC345 Software Engineering. The Heap And Dynamic Memory Allocation

Dynamic Memory Allocation I

Dynamic Memory Allocation I Nov 5, 2002

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

Dynamic Memory: Alignment and Fragmentation

CSE351 Spring 2010 Final Exam (9 June 2010)

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

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

Dynamic Memory Allocation: Basic Concepts

Project 2. Assigned: 02/20/2015 Due Date: 03/06/2015

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

ANITA S SUPER AWESOME RECITATION SLIDES

10.1. CS356 Unit 10. Memory Allocation & Heap Management

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

CSE351 Spring 2010 Final Exam (9 June 2010)

CS 241 Honors Memory

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

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

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

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

Dynamic Memory Allocation

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

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

Project 3a: Malloc and Free

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

Recitation 11: More Malloc Lab

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

Segmentation. Multiple Segments. Lecture Notes Week 6

CS Operating Systems

CS Operating Systems

BBM 201 DATA STRUCTURES

ECE 2400 Computer Systems Programming, Fall 2018 PA2: List and Vector Data Structures

Memory (Stack and Heap)

CS 62 Practice Final SOLUTIONS

Due: 9 February 2017 at 1159pm (2359, Pacific Standard Time)

Heap Arrays and Linked Lists. Steven R. Bagley

Dynamic Memory Allocation: Basic Concepts

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

Operating System Labs. Yuanbin Wu

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

CS24: INTRODUCTION TO COMPUTING SYSTEMS. Spring 2017 Lecture 7

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

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

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

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

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

CS Week 8 Lab Assignment 3. Teaching Assistant Henrique Potter

CSE351 Spring 2018, Final Exam June 6, 2018

Dynamic Memory Allocation. CS 351: Systems Programming Michael Saelee

UW CSE 351, Winter 2013 Final Exam

Dynamic Memory Management

COMPUTER SCIENCE 4500 OPERATING SYSTEMS

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

Heap Arrays. Steven R. Bagley

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

Intermediate Programming, Spring 2017*

Project 2 Overview: Part A: User space memory allocation

ECE 598 Advanced Operating Systems Lecture 10

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

Review! Lecture 5 C Memory Management !

Dynamic Memory Alloca/on: Basic Concepts

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

CS61C : Machine Structures

Robust Memory Management Schemes

VMem. By Stewart Lynch.

Dynamic Memory Alloca/on: Basic Concepts

Recitation #11 Malloc Lab. November 7th, 2017

Memory Allocation II. CSE 351 Autumn Instructor: Justin Hsia

Memory: Overview. CS439: Principles of Computer Systems February 26, 2018

CS61C : Machine Structures

CS61C : Machine Structures

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

Transcription:

CS 162 Due: Monday, March 28, 2016 1 Introduction Your task in this assignment is to implement your own memory allocator from scratch. This will expose you to POSIX interfaces, force you to reason about memory, and pose interesting algorithmic challenges. The man pages for malloc and sbrk are excellent resources for this assignment. 2 Setup cd ~/code/personal git pull staff master cd hw3 You will find a simple skeleton in mm_alloc.c. mm_alloc defines an interface with three functions mm_malloc, mm_free, mm_realloc. You will need to implement these functions. Do not change the names! You will also find a file named mm_test.c, which contains code to load your implementation and a basic sanity check. Feel free to change this file as it won t be graded. 1

3 Background: Getting Memory from the OS 3.1 Process Memory Each process has its own virtual address space. Parts of this address space are mapped to physical memory through address translation. In order to build a memory allocator, we need to understand how the heap in particular is structured. The heap is a continuous (in terms of virtual addresses) space of memory with three bounds: The bottom of the heap. The top of the heap, known as the break. The break can be changed using brk and sbrk. The break marks the end of the mapped memory space. Above the break lies virtual addresses which have not been mapped to physical addresses by the OS. The hard limit of the heap, which the break cannot surpass (managed through sys/resource.h s functions getrlimit(2) and setrlimit(2)) In this assignment, you ll be allocating blocks of memory in the mapped region and moving the break appropriately whenever you need to expand the mapped region. 2

3.2 sbrk Initially the mapped region of the heap will have a size of 0. To expand the mapped rangeion, we have to manipulate the position of the break. The recommended syscall for doing this is sbrk. void *sbrk(int increment); sbrk increments the position of the break by increment bytes and returns the address of the previous break (i.e. the beginning of newly mapped memory). To get the current position of the break, pass in an increment of 0. For more information, read the man page. 3.3 Heap Data Structure A simple memory allocator can be implemented using a linked list data structure. The elements of the linked list will be the allocated blocks of memory on the heap. To structure our data, each allocated block of memory will be preceded by a header containing metadata. For each block, we include the following metadata: prev, next: pointers to metadata describing the adjacent blocks free: a boolean describing whether or not this block is free size: the allocated size of the block of memory You might also consider using a zero-length array to serve as a pointer to the memory block. 3

4 Your Assignment There are many ways to structure a memory allocator. You will be implementing a memory allocator using a linked list of memory blocks, as described in the previous section. In this section, we ll describe how allocation, deallocation, and reallocation should work in this scheme. 4.1 Allocation void *mm_malloc(size_t size); The user will pass in the requested allocation size. Make sure the returned pointer is pointing to the beginning of the allocated space, not your metadata header. One simple algorithm for finding available memory is called first fit. When your memory allocator is called to allocate some memory, it iterates through its blocks until it finds a sufficiently large free block of memory. If no sufficiently large free block is found, use sbrk to create more space on the heap. If the first block of memory you find is so big that it can accommodate both the newly allocated block and another block in addition, then the block is split in two; one block to hold the newly allocated block, and a residual free block. If it s a bit larger than what you need, but not big enough for a new block (i.e. it s not big enough to hold the metadata of a new block), be aware that you will have some unused space at the end of the block. Return NULL if you cannot allocate the new requested size. Return NULL if the requested size is 0. For ease of grading, we ask that you zero-fill your allocated memory before returning a pointer to it. 4.2 Deallocation void mm_free(void *ptr); When a user is done using their memory, they ll call upon your memory allocator to free their memory, passing in the pointer ptr that they received from mm alloc. Deallocating doesn t mean you have to release the memory back to the OS; you ll just be able to allocate that block for something else now. As a side-effect of splitting blocks in your allocation procedure, you might run into issues of fragmentation: when your blocks become too small for large allocation requests, even though you have a sufficiently large section of free memory. To solve this, you must coalesce consecutive free blocks upon freeing a block that is adjacent to other free blocks. Your deallocation function should do nothing if passed a NULL pointer. 4.3 Reallocation void* mm_realloc(void* ptr, size_t size); Reallocation should resize the allocated block at ptr to size. A suggested implementation is to first free the block referenced by ptr, then mm alloc a block of the specified size, and finally memcpy the old data over. The new extended area should be set to 0. Make sure you handle the following edge cases. Return NULL if you cannot allocate the new requested size. In this case, do not modify the original block. 4

realloc(ptr, 0) is equivalent to calling mm free(ptr) and returning NULL. realloc(null, n) is equivalent to calling mm alloc(n). realloc(null, 0) is equivalent to calling mm alloc(0), which should just return NULL. Make sure you handle the case where the argument size is less than the original size. 5 Submission Details To submit and push to autograder, first commit your changes, then do: git push personal master Within 30 minutes you should receive an email from the autograder. (If you havent received an email within half an hour, please notify the instructors via a private post on Piazza.) 5

A Extra Information A.1 Unmapped Region and No Man s Land We saw earlier that the break marks the end of the mapped virtual address space. By this assumption, accessing addresses above the break should trigger an error ( bus error or segmentation fault ). The virtual address space is mapped in quanta of pages (usually some multiple of 4096 bytes). When sbrk is called, the OS will have to map more memory to the heap. To do that, it maps an entire page from physical memory to the mapped region of the heap. Now, it is possible that the break doesn t end up exactly on a page boundary. In this situation, what is the status of the memory between the break and the page boundary? It turns out that this memory is accessible, even though it is above the break and thus should be unmapped in theory. Bugs related to this issue are particularly insidious, because no error will occur if you read from or write to this no man s land. 6

B ε Bonus The ε bonus isn t worth extra points. There are many things you can do to improve your allocator. (Warning: The autograder expects you to use the first-fit algorithm. If you want to do extra features, do them only AFTER you ve gotten full credit on the autograder.) Make it threadsafe! This doesn t mean putting a lock around all calls to malloc, but actually protecting the data structures such that multiple threads can make allocations at the same time. A good way to do this (not the only way) is to lock certain sizes of allocations, so two threads asking for 4kb would block, but two thread askings for 4kb and 32kb would not block. You can improve your allocation algorithm. First fit is one of the simplest to implement. Another more advanced strategy is the buddy allocator. Implement realloc properly to extend the current allocation block if possible. C Other Data Structures A list of free blocks for each allocation size. The advantage here is that malloc can be a constant time operation if a sufficiently large free block exists, as opposed to the linear time operation of iterating over a linked list in search of a large enough free block, without even knowing if there exists such a block. A free interval tree. These let you represent every memory allocation as an extent (start, length). The leaves of the tree correspond to regions of unused memory. If N bytes are requested, it should be possible to scan the interval tree for (> N)-sized pieces of memory in O(log n) time, so long as it s properly balanced. 7