Malloc Lab 15-213: Introduction to Computer Systems Recitation 11: Nov. 4, 2013 Marjorie Carlson Recitation A.

Slides:



Advertisements
Similar presentations
Malloc Recitation By sseshadr. Agenda Macros in C Pointer declarations Casting and Pointer Arithmetic Malloc.
Advertisements

Dynamic memory allocation
Dynamic Memory Allocation in C.  What is Memory What is Memory  Memory Allocation in C Memory Allocation in C  Difference b\w static memory allocation.
Dynamic Memory Management
Carnegie Mellon 1 Dynamic Memory Allocation: Basic Concepts : Introduction to Computer Systems 17 th Lecture, Oct. 21, 2010 Instructors: Randy Bryant.
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.
Carnegie Mellon 1 Dynamic Memory Allocation: Basic Concepts / : Introduction to Computer Systems 18 th Lecture, March 24, 2015 Instructors:
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.
Malloc Recitation Section K (Kevin Su) November 5 th, 2012.
Carnegie Mellon 1 Debugging : Introduction to Computer Systems Recitation 12: Monday, Nov. 9 th, 2013 Yixun Xu Section K.
18-213: Introduction to Computer Systems April 6, 2015
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.
Dynamic Memory Allocation I Nov 5, 2002 Topics Simple explicit allocators Data structures Mechanisms Policies class21.ppt “The course that gives.
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.
L6: Malloc Lab Writing a Dynamic Storage Allocator October 30, 2006
CS 11 C track: lecture 5 Last week: pointers This week: Pointer arithmetic Arrays and pointers Dynamic memory allocation The stack and the heap.
University of Washington Today Lab 5 out  Puts a *lot* together: pointers, debugging, C, etc.
Programming III SPRING 2015 School of Computer and Information Sciences Francisco R. Ortega, Ph.D. McKnight Fellow and GAANN Fellow LECTURE #6C Pointers,
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.
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.
CS 241 Discussion Section (11/17/2011). Outline Review of MP7 MP8 Overview Simple Code Examples (Bad before the Good) Theory behind MP8.
Carnegie Mellon Dynamic Memory Allocation : Introduction to Computer Systems Monday March 30th,
1 Lecture07: Memory Model 5/2/2012 Slides modified from Yin Lou, Cornell CS2022: Introduction to C.
Compiler Directives. The C Preprocessor u The C preprocessor (cpp) changes your source code based on instructions, or preprocessor directives, embedded.
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.
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.
Debugging Malloc Lab Detecting Memory-Related Errors.
Carnegie Mellon Dynamic Memory Allocation : Introduction to Computer Systems Recitation 11: Monday, Nov 3, 2014 SHAILIN DESAI SECTION L 1.
DYNAMIC MEMORY ALLOCATION. Disadvantages of ARRAYS MEMORY ALLOCATION OF ARRAY IS STATIC: Less resource utilization. For example: If the maximum elements.
Instructor: Phil Gibbons
Section 10: Memory Allocation Topics
CS 3214 Introduction to Computer Systems
Instructor: Haryadi Gunawi
Memory Management I: Dynamic Storage Allocation Oct 7, 1999
Recitation 11: More Malloc Lab
Dynamic Memory Allocation: Basic Concepts CS220: Computer Systems II
The Hardware/Software Interface CSE351 Winter 2013
Recitation 11: More Malloc Lab
Optimizing Malloc and Free
Memory Management I: Dynamic Storage Allocation March 2, 2000
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.
CSCI 380: Operating Systems Lecture #11 Instructor: William Killian
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
CSCI 380: Operating Systems Instructor: William Killian
Dynamic Memory Allocation: Basic Concepts CSCI 380: Operating Systems
Malloc Lab CSCI 380: Operating Systems
Presentation transcript:

Malloc Lab 15-213: Introduction to Computer Systems Recitation 11: Nov. 4, 2013 Marjorie Carlson Recitation A

Weekly Update Malloc lab is out Due Thursday, Nov. 14 Start early Seriously... start early. “It is possible to write an efficient malloc package with a few pages of code. However, we can guarantee that it will be some of the most difficult and sophisticated code you have written so far in your career.”

Agenda Malloc Overview Casting & Pointer Review Macros & Inline Functions Malloc Design Debugging & an Action Plan

Dynamic Memory Allocators Are used to acquire memory for data structures whose size is known only at run time. Manage area in a part of memory known as the heap.

Allocation Example p1 = malloc(4) p2 = malloc(5) p3 = malloc(6) free(p2) p4 = malloc(2)

Malloc Lab Create a general-purpose allocator that dynamically modifies the size of the heap as required. The driver calls your functions on various trace files to simulate placing data in memory. Grade is based on: Space utilization (minimizing fragmentation) Throughput (processing requests quickly) Your heap checker Style & correctness, hand-graded as always

Functions You Will Implement mm_init initializes the heap before malloc is called. malloc returns a pointer to a free block (>= req. size). calloc same, but zeros the memory first. realloc changes the size of a previously allocated block. (May move it to another location.) free marks allocated memory available again. mm_checkheap debugging function (more on this later)

Functions You May Use mem_sbrk mem_heap_lo mem_heap_hi mem_heapsize Used for expanding the size of the heap. Allows you to dynamically increase your heap size as required. Helpful to initialize your heap. Returns a pointer to first byte in newly allocated heap area. mem_heap_lo Pointer to first byte of heap mem_heap_hi Pointer to last byte of heap mem_heapsize mem_pagesize

Agenda Malloc Overview Casting & Pointer Review Macros & Inline Functions Malloc Design Debugging & an Action Plan

Pointer Arithmetic *(arr + i) is equivalent to arr[i] Thus the result of arithmetic involving pointers depends on the type of the data the pointer points at. int *arr = 0x1000 short *arr = 0x1000 arr + 1 = 0x1004 arr + 1 = 0x1002 So ptr + i is really ptr + (i * sizeof(ptr-type)) example and pictures from http://www.cs.umd.edu/class/spring2003/cmsc311/Notes/BitOp/pointer.html

Pointer Casting Pointer casting can thus be used to make sure the pointer arithmetic comes out right. Since chars are 1 byte, casting a pointer as a char pointer then makes arithmetic on it work “normally.” int *ptr = 0x10203040 char *ptr2 = (char *)ptr + 2 char *ptr3 = (char *) (ptr + 2) = 0x10203042 = 0x10203048

Examples int *ptr = (int *) 0x12341234; int *ptr2 = ptr + 1; char *ptr = (char *) 0x12341234; char *ptr2 = ptr + 1; void *ptr = (int *) 0x12341234; void *ptr2 = ptr + 1; int *ptr = (int *) 0x12341234; int *ptr2 = ((int *) (((char *) ptr) + 1))); = 0x12341238 = 0x12341235 = 0x12341235 = 0x12341235 

Agenda Malloc Overview Casting & Pointer Review Macros & Inline Functions Malloc Design Debugging & an Action Plan

#define NAME replacement-text Macros #define NAME replacement-text Maps “name” to a definition or instruction. Macros are expanded by the preprocessor, i.e., before compile time. They’re faster than function calls. For malloc lab: use macros to give you quick (and reliable) access to header information — payload size, valid bit, pointers, etc.

Macros Useful for “magic number” constants – acts like a naïve search-and-replace #define ALIGNMENT 8 Useful for simple accesses and computations Use parentheses for computations. #define multByTwoA(x) 2*x #define multByTwoB(x) 2*(x) multByTwoA(5+1) multByTwoB(5+1) = 2*5+1 = 11 = 2*(5+1) = 12

Macros Useful for debugging __FILE__ is the file name (%s) __LINE__ is the line number (%d) __func__ is the function it’s in (%s) Output: hello from function hello This is line 9. Belongs to function: main In filename: macros.c

Macros Useful for debugging: conditional printfs // #define DEBUG # ifdef DEBUG #define dbg_printf(...) printf(__VA_ARGS__) #else #define dbg_printf(...) #endif

Inline Functions Alternative to macros: still more efficient than a function call, and easier to get right! #define max(A,B) ((A) > (B) ? (A) : (B)) vs. inline int max(int a, int b) { return a > b ? a : b? } The compiler replaces each call to the function with the code for the function itself. (So, no stack setup, no call/ret.) Useful for small, frequently called functions.

Agenda Malloc Overview Casting & Pointer Review Macros & Inline Functions Malloc Design Debugging & an Action Plan

Malloc Design You have a ton of design decisions to make!  Thinking about fragmentation Method of managing free blocks Implicit List Explicit List Segregated Free List Policy for finding free blocks First fit Next fit Best fit Free-block insertion policy Coalescing (or not)

Fragmentation Internal fragmentation Result of payload being smaller than block size. Header & footer Padding for alignment Mostly unavoidable.

Fragmentation External fragmentation Oops! (what would happen now?) Occurs when there is enough aggregate heap memory, but no single free block is large enough Some policies are better than others at minimizing external fragmentation. p1 = malloc(4) p2 = malloc(5) p3 = malloc(6) free(p2) p4 = malloc(6) Oops! (what would happen now?)

Managing free blocks Implicit list Uses block length to find the next block. Connects all blocks (free and allocated). All blocks have a 1-word header before the payload that tells you: its size (so you know where to look for the next header) and whether or not it’s allocated You may also want a 1-word footer so that you can crawl the list in both directions to coalesce.

Managing free blocks Explicit list A list of free blocks, each of which stores a pointer to the next free block. Since only free blocks store this info, the pointers can be stored where the payload would be. This allows you to search the free blocks much more quickly. Requires an insertion policy.

Managing free blocks Segregated free list Each size class has its own free list. Finding an appropriate block is much faster (so next fit may become good enough); coalescing and reinsertion are harder.

Finding free blocks First fit Next fit Best fit Start from the beginning. Find the first free block. Linear time. Next fit Search starting from where previous search finished. Often faster than first fit. Best fit Choose the free block closet in size to what you need. Better memory utilization (less fragmentation), but it’s very slow to traverse the full list. What if no blocks are large enough? Extend the heap

Insertion policy Where should free blocks go? LIFO (Last In First Out) Blocks that have just been free()d. “Leftovers” when allocating part of a block. LIFO (Last In First Out) Insert the free block at the beginning of the list. Simple and constant time. Studies suggest potentially worse fragmentation. Address-Ordered Keep free blocks list sorted in address order. Studies suggest better fragmentation. Slower since you have to find where it belongs.

Coalescing policy Use the block size in the header to look left & right. Implicit list: Write new size in the header of first block & footer of last block. Explicit list: Must also relink the new block according to your insertion policy. Segregated list: Must also use the new block size to figure out which bucket to put the new block in.

Agenda Malloc Overview Casting & Pointer Review Macros & Inline Functions Malloc Design Debugging & an Action Plan

Debugging Debugging malloc lab is hard! rubber duck debugging GDB valgrind mm_checkheap

mm_checkheap mm_checkheap A consistency checker to check the correctness of your heap. Write it early and update as needed. What to check for? Anything that could go wrong! Focus on correctness, not efficiency. Once you get it working, it should be silent and only output when your heap has messed up. You can insert a call to it before & after functions to pin down exactly where things are going wrong. Do not request debugging help from a TA without a working checkheap. address alignment consistency of header & footer whether free blocks are coalescing consistency of linked list pointers whether blocks are being placed in the right segregated list …

Suggested action plan Start early — make the most use of empty office hours. Keep consulting the handout (e.g. the “rules”) throughout your coding process. Understand and implement a basic implicit list design. Write your heap checker. Come up with something faster/more memory efficient. Implement it. Debug it. Git commit and/or submit. Goto 5.

Questions? GOOD LUCK!!