Lab 3: Malloc Lab. “What do we need to do?”  Due 11/26  One more assignment after this one  Partnering  Non-Honors students may work with one other.

Slides:



Advertisements
Similar presentations
Pointers.
Advertisements

Malloc Recitation By sseshadr. Agenda Macros in C Pointer declarations Casting and Pointer Arithmetic Malloc.
Dynamic Memory Management
Dynamic Memory Allocation I Topics Simple explicit allocators Data structures Mechanisms Policies CS 105 Tour of the Black Holes of Computing.
Dynamic Memory Allocation I Topics Basic representation and alignment (mainly for static memory allocation, main concepts carry over to dynamic memory.
Chris Riesbeck, Fall 2007 Dynamic Memory Allocation Today Dynamic memory allocation – mechanisms & policies Memory bugs.
Week 12 (March 28th) Outline Memory Allocation Lab 6 Reminders Lab 6: April 7 th (next Thurs) Start Early!!! Exam 2: April 5 th (Next Tue) TA: Kun Gao.
Dynamic Memory Allocation I October 16, 2008 Topics Simple explicit allocators Data structures Mechanisms Policies lecture-15.ppt “The course that.
Kernighan/Ritchie: Kelley/Pohl:
1 Dynamic Memory Allocation: Basic Concepts Andrew Case Slides adapted from Jinyang Li, Randy Bryant and Dave O’Hallaro.
1 Optimizing Malloc and Free Professor Jennifer Rexford COS 217 Reading: Section 8.7 in K&R book
Malloc Recitation Section K (Kevin Su) November 5 th, 2012.
Binghamton University CS-220 Spring 2015 Binghamton University CS-220 Spring 2015 Heap Management.
CPSC 388 – Compiler Design and Construction
Memory Management Memory Areas and their use Memory Manager Tasks:
Dynamic Memory Allocation I Nov 5, 2002 Topics Simple explicit allocators Data structures Mechanisms Policies class21.ppt “The course that gives.
1 Optimizing Malloc and Free Professor Jennifer Rexford
Dynamic Memory Allocation I November 1, 2006 Topics Simple explicit allocators Data structures Mechanisms Policies class18.ppt “The course that.
Memory Allocation CS Introduction to Operating Systems.
Real-Time Concepts for Embedded Systems Author: Qing Li with Caroline Yao ISBN: CMPBooks.
L6: Malloc Lab Writing a Dynamic Storage Allocator October 30, 2006
University of Washington Today Finished up virtual memory On to memory allocation Lab 3 grades up HW 4 up later today. Lab 5 out (this afternoon): time.
ECE 103 Engineering Programming Chapter 47 Dynamic Memory Alocation Herbert G. Mayer, PSU CS Status 6/4/2014 Initial content copied verbatim from ECE 103.
1 Dynamic Memory Allocation: Basic Concepts. 2 Today Basic concepts Implicit free lists.
Writing You Own malloc() March 29, 2003 Topics Explicit Allocation Data structures Mechanisms Policies class19.ppt “The course that gives CMU its.
Chapter 17 Free-Space Management Chien-Chung Shen CIS, UD
CS 241 Discussion Section (11/17/2011). Outline Review of MP7 MP8 Overview Simple Code Examples (Bad before the Good) Theory behind MP8.
CS 241 Section Week #9 (11/05/09). Topics MP6 Overview Memory Management Virtual Memory Page Tables.
Dynamic Memory Allocation II
Carnegie Mellon 1 Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition Dynamic Memory Allocation: Basic Concepts :
Dynamic Memory Allocation April 9, 2002 Topics Simple explicit allocators Data structures Mechanisms Policies Reading: 10.9 Problems: and class21.ppt.
University of Washington Today More memory allocation!
CS 241 Discussion Section (2/9/2012). MP2 continued Implement malloc, free, calloc and realloc Reuse free memory – Sequential fit – Segregated fit.
Malloc Lab : Introduction to Computer Systems Recitation 11: Nov. 4, 2013 Marjorie Carlson Recitation A.
Carnegie Mellon 1 Dynamic Memory Allocation: Basic Concepts Instructors: Adapted from CMU course
CS 241 Discussion Section (12/1/2011). Tradeoffs When do you: – Expand Increase total memory usage – Split Make smaller chunks (avoid internal fragmentation)
CSE 351 Dynamic Memory Allocation 1. Dynamic Memory Dynamic memory is memory that is “requested” at run- time Solves two fundamental dilemmas: How can.
1 Dynamic Memory Allocation (II) Implementation. 2 Outline Implementation of a simple allocator Explicit Free List Segregated Free List Suggested reading:
Carnegie Mellon 1 Malloc Lab : Introduction to Computer Systems Friday, July 10, 2015 Shen Chen Xu.
University of Washington Implementation Issues How do we know how much memory to free given just a pointer? How do we keep track of the free blocks? How.
Carnegie Mellon 1 Malloc Recitation Ben Spinelli Recitation 11: November 9, 2015.
Carnegie Mellon Dynamic Memory Allocation : Introduction to Computer Systems Recitation 11: Monday, Nov 3, 2014 SHAILIN DESAI SECTION L 1.
LINKED LISTS.
Memory Management I: Dynamic Storage Allocation Oct 8, 1998 Topics User-level view Policies Mechanisms class14.ppt Introduction to Computer Systems.
Chapter 17 Free-Space Management
Instructor: Phil Gibbons
Section 10: Memory Allocation Topics
Memory Management Memory Areas and their use Memory Manager Tasks:
Dynamic Memory Allocation I
Memory Management I: Dynamic Storage Allocation Oct 7, 1999
Memory Management Memory Areas and their use Memory Manager Tasks:
Dynamic Memory Allocation: Basic Concepts CS220: Computer Systems II
The Hardware/Software Interface CSE351 Winter 2013
Optimizing Malloc and Free
Memory Management I: Dynamic Storage Allocation March 2, 2000
CS Introduction to Operating Systems
User-Level Dynamic Memory Allocation: Malloc and Free
Dynamic Memory Allocation I
CS703 - Advanced Operating Systems
Dynamic Memory Allocation I
Dynamic Memory Allocation: Basic Concepts /18-213/14-513/15-513: Introduction to Computer Systems 19th Lecture, October 30, 2018.
Memory Allocation II CSE 351 Autumn 2017
Memory Allocation II CSE 351 Winter 2018
Dynamic Memory Allocation November 2, 2000
Memory Allocation II CSE 351 Winter 2018
Dynamic Memory Allocation: Basic Concepts CSCI 380: Operating Systems
Malloc Lab CSCI 380: Operating Systems
Memory Management Memory Areas and their use Memory Manager Tasks:
CS703 - Advanced Operating Systems
Presentation transcript:

Lab 3: Malloc Lab

“What do we need to do?”

 Due 11/26  One more assignment after this one  Partnering  Non-Honors students may work with one other person (if they so choose)  Must give credit where credit is due  Honors students may not work with a partner  Only need to hand in one file (mm.c) Logistics

 Implement a memory management library  Three main functions to be implemented:  malloc(): reserve a block of memory  free(): release a chunk of memory  realloc(): resize a reserved chunk  Two main goals (50/50 split):  Space efficiency (utilization)  Speed efficiency (throughput) Overview of the Malloc Lab

 int mm_init(void)  Will be called by the trace-driven program in order to perform necessary initializations  ie. allocating initial heap area  Return -1 if an error occurs and 0 if otherwise  void *mm_malloc(size_t size)  Returns pointer to an allocated block payload of at least size bytes  Block must lie within heap area  Block cannot overlap with another allocated chunk  Must return 8-byte aligned pointer Functions to Edit

 void mm_free(void *ptr)  Frees the block pointed to by ptr  Returns nothing  void *mm_realloc(void *ptr, size_t size)  If ptr is NULL, call mm_malloc(size)  If size==0, call mm_free(ptr)  If otherwise, change the size of the block of memory pointed to by ptr to size bytes and returns the address of this new block  Possible for this address to remain the same as for the old block  ie: if a consecutive block of memory is free and satisfies the given size requirement  Contents of memory block remain the same (as much as possible if allocating to a smaller size block) Functions to Edit (Continued)

 You will also need to implement a function called mm_check()  Mostly for your own benefit  Implementation will depend on your design  Calls to mm_check() should be removed from the copy of your code that you turn in  We will check mm_check() manually for correctness and style  Make sure to use mm_check() as you go along, preferably each time you add a new feature  Style Consistency Checking

 Is every chunk of memory in the free list actually free?  Are there any consecutive free chunks of memory that escaped coalescing?  Coalescing means combining consecutive free chunks of memory into one big chunk of memory  Is every chunk of memory that is free actually in the free list?  Do any allocated chunks of memory overlap? What should you check the heap for?

 How will you keep track of used and freed memory chunks?  You are not allowed to use the actual malloc() to make room for data structures  How will you track memory chunks while maintaining space efficiency?  You must minimize storage overhead for metadata  How will you track memory chunks while maintaining speed efficiency?  Topic of next recitation Main Challenges of this Lab

 Since you cannot use the library malloc(), you will need to use:  mem_sbrk(n): grows heap by positive n bytes  mem_heap_lo(): pointer to the lowest byte in the heap  mem_heap_hi(): pointer to the first byte above the heap  mem_heapsize(): size of heap  size of heap = mem_heap_hi() – mem_heap_lo() What functions can you use?

 Functionally, arrays and pointers are the same  array[n] = *(array + n)  Arrays tend to be seen as more aesthetically pleasing while pointers tend to demonstrate a closer version to what the assembly instructions are doing to represent the higher level language code  Implicit multiplication  ptr + offset = (char*)ptr + (sizeof(*ptr))*(offset)  threeintegers[1] = *(threeintegers + 4 bytes)  You cannot offset void* ptr because the implicit coefficient is impossible to determine  Solution: type-casting (ie: *((char*)ptr + 1) = *(ptr + 1 byte)) Pointers and Arrays

Advanced Features and Performance Tuning

 No explicit structure is used to track the location of free/allocated memory chunks  Instead, the overhead in each chunk containing the size of the chunk and the allocated bit of the chunk form a “block list”  Note that all chunks of memory will be in this list, not just the free chunks  Location of the next chunk determined based on the size of the current chunk Implicit Free List

Implicit Free List (Continued)  Finding a free block  First fit:  Search list from beginning and choose first free block that fits; if no such block exists, must call mem_sbrk to extend the heap area  Takes time linear in total number of blocks (free and allocated)  Next fit:  Starts first fit search where the last search left off instead of at the beginning of the list  Best fit:  Search entirety of list and choose the free block closest in size to the size desired  Slower than first fit and next fit

Implicit Free List (Continued) *Pictures adapted from Matt Welsh, Harvard University

Implicit Free List (Continued)

 Linked list among free blocks  Use space typically reserved for actual data for metadata, specifically the link pointers  Typically doubly linked list is used  Still need boundary tags for coalescing  Links don’t necessarily need to be in the same order as the order of the blocks in memory Explicit Free List

Explicit Free List (Continued)  Insertion Policy  Address-ordered policy  Insert freed blocks so that the free list is always in address order  Requires a search using boundary tags  LIFO (last-in-first-out)  Insert freed block at the beginning of the list  Constant time operation  Possibly more fragmentation than with aforementioned policy

Explicit Free List (Continued)

 Utilize different free lists for different size classes  Each size class has its own collection of blocks  May enable quicker determination of appropriately- sized chunks of memory  May need to be used in combination with other types of lists such as an explicit free list in order to enable coalescing Segregated Free List

Segregated Free List (Continued)

 Store free chunks of memory is binary tree format, determining each chunk’s position within the tree based on the size of the chunk  Optimal choice of structure since finding a free chunk of size n takes O(log(n)) time as opposed to O(n) time with a simple linked list  Certain test cases you are given are optimized to work more efficiently when a binary tree free list is being utilized  Things to think about:  How to store chunks of memory of equal size  How to coalesce consecutive free chunks of memory Binary Tree Free List

For( mem_block : list){ if(alloc(mem_blocks){ //1, check if header and footer are consistent // check alloc bit and size } else{ //1, check if the mem_block exists in the free list //2, does size match? Is it free? } More Heap Consistency Check

When a request of size (n) is served with a free block of size (m) such that m > n Solution/ Design Choice: to split the remaining (m-n) memory block and make it a new free block Or just assume it is allocated as well. Fragmentation

 Start with the implicit list implementation provided in your textbook  Code it and understand it!  Think about how to implement a heap checker for the implicit list implementation  Start implementing an explicit list  Add to your heap checker as you add placement policies to your explicit list implementation  Move on to a segregated free list implementation  If you have time to spare move on to a binary tree implementation Suggestions on where to begin:

Important.