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.

Slides:



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

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:
Chris Riesbeck, Fall 2007 Dynamic Memory Allocation Today Dynamic memory allocation – mechanisms & policies Memory bugs.
1. Dynamic Memory Allocation. Dynamic Memory Allocation Programmers use dynamic memory allocators (such as malloc ) to acquire VM at run time.  For data.
Dynamic Memory Allocation I October 16, 2008 Topics Simple explicit allocators Data structures Mechanisms Policies lecture-15.ppt “The course that.
Growing Arrays in C By: Victoria Tielebein CS 265- Spring 2011.
Dynamic Memory Allocation
1 Dynamic Memory Allocation. 2 Outline Implementation of a simple allocator Explicit Free List Segregated Free List Suggested reading: 10.9, 10.10, 10.11,
Memory allocation CSE 2451 Matt Boggus. sizeof The sizeof unary operator will return the number of bytes reserved for a variable or data type. Determine:
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.
Recitation 10 Anubhav Gupta Section D.
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.
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.
1 Inner Workings of Malloc and Free Professor Jennifer Rexford COS 217.
Recitation 9 (Nov. 8) Outline Virtual memory Lab 6 hints Reminders Lab 6: Get correctness points Exam 2: Nov. 16 (Next Tue) Minglong Shao
L6: Malloc Lab Writing a Dynamic Storage Allocator October 30, 2006
University of Washington Today Lab 5 out  Puts a *lot* together: pointers, debugging, C, etc.
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.
Carnegie Mellon Dynamic Memory Allocation : Introduction to Computer Systems Monday March 30th,
Consider Starting with 160 k of memory do: Starting with 160 k of memory do: Allocate p1 (50 k) Allocate p1 (50 k) Allocate p2 (30 k) Allocate p2 (30 k)
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!
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
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.
Dynamic Memory Management Jennifer Rexford 1. 2 Goals of this Lecture Dynamic memory management techniques Garbage collection by the run-time system (Java)
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.
Memory Management What if pgm mem > main mem ?. Memory Management What if pgm mem > main mem ? Overlays – program controlled.
Instructor: Phil Gibbons
Section 10: Memory Allocation Topics
Dynamic Memory Allocation I
Instructor: Haryadi Gunawi
Memory Management I: Dynamic Storage Allocation Oct 7, 1999
Dynamic Memory Allocation
Recitation 11: More Malloc Lab
Dynamic Memory Allocation I October 28, 2003
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
Recitation 11: More Malloc Lab
Dynamic Memory Allocation I
CS703 - Advanced Operating Systems
Dynamic Memory Allocation
Dynamic Memory Allocation I
Dynamic Memory Allocation: Basic Concepts /18-213/14-513/15-513: Introduction to Computer Systems 19th Lecture, October 30, 2018.
Dynamic Memory Allocation November 2, 2000
Dynamic Memory Allocation: Basic Concepts CSCI 380: Operating Systems
Malloc Lab CSCI 380: Operating Systems
Dynamic Memory Allocation I
Instructors: Majd Sakr and Khaled Harras
Understanding mm-helper.c Adding debugging info to mm-helper.c
Presentation transcript:

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

Memory Allocation Overview Maintains heap as a collection of various sized blocks Each block contiguous chunk of virtual mem. Allocating memory Freeing memory

Dynamic Memory Allocation Do not know memory requirements beforehand Explicit allocators C standard library ‘malloc’ and ‘free’ Implicit allocators (garbage collection) Lisp, ML, Java

C ‘Malloc’ and ‘Free’ Malloc allocates using: mmap, munmap void *sbrk(int incr) double word aligned (8-bytes) Free: void free(void *ptr) *ptr must be a valid pointer returned by malloc

Allocator Requirements and Goals Constraints Handle arbitrary request sequences Making immediate responses to requests Using only the heap Aligning blocks Not modifying allocated blocks Goals Maximize throughput Maximize utilization

Placement Policy Best-Fit First-Fit Next-Fit No placement policy is perfect! Unless you know the future

Best Fit not always ‘Best’! 6 bytes free Memory in use4 bytes free Consider the following accesses (not counting header sizes): 1.malloc(3) 2.malloc(3) 3.malloc(4) First Fit will be able to allocate memory for all three. Best fit will not!

Example implementation Section Understand every line of the code Implicit free list + immediate boundary tag coalescing + first fit Code is available at Use it as a starting point

Block format Header Pointer returned by malloc (Block Pointer (bp)) 32 bits >= what user asked for in malloc Footer Payload

Header/footer format Double word alignment Why is size only 29 bits? Lower 3 bits a = 1: allocated, or a = 0: free Pack size and allocated bits into a single integer Size = 24 (0x18). Block is allocated Header = 0 x18 | 0x1 = 0x a size

Heap format 8|1 0|1 Double Word Alignment (8 bytes) 4 bytes Pad PrologueEpilogue Allocated and Free Blocks HDRFTRHDRFTR

Example Assume initial 4-byte padding, no prologue, both header and foot, and first fit Allocator starts at 0xA Malloc(1) = ? Malloc(2) = ?

Very useful macros #define WSIZE 4 Size of word #define DSIZE 8 Size of double word #define CHUNKSIZE (1<<12) Initial size of heap (for expanding) #define OVERHEAD 8 Extra bytes used by header and footer

Very useful macros #define PACK(size, alloc) ((size) | (alloc)) #define GET(p) (*(size_t *)(p)) #define PUT(p, val) (*(size_t *)(p) = (val)) #define GET_SIZE(p) (GET(p) & ~0x7) #define GET_ALLOC(p) (GET(p) & 0x1)

Very useful macros #define HDRP(bp) ((char *)(bp) - WSIZE) #define FTRP(bp) ((char *)(bp) + GET_SIZE(HDRP(bp)) - DSIZE) #define NEXT_BLKP(bp) ((char *)(bp) + GET_SIZE(((char *)(bp) - WSIZE))) #define PREV_BLKP(bp) ((char *)(bp) - GET_SIZE(((char *)(bp) - DSIZE)))

Using the Macros What is the size of the next block in memory? size_t size = GET_SIZE(HDRP(NEXT_BLKP(bp)));

Initializing the heap int mm_init(void) { if ((heap_listp = mem_sbrk(4*WSIZE)) == NULL) return -1; PUT(heap_listp, 0); PUT(heap_listp+WSIZE, PACK(OVERHEAD, 1)); PUT(heap_listp+DSIZE, PACK(OVERHEAD, 1)); PUT(heap_listp+WSIZE+DSIZE, PACK(0, 1)); heap_listp += DSIZE; if (extend_heap(CHUNKSIZE/WSIZE) == NULL) return -1; return 0; }

Extending the heap static void *extend_heap(size_t words) { char *bp; size_t size; size = (words % 2) ? (words+1)*WSIZE : words*WSIZE; if ((int)(bp = mem_sbrk(size)) < 0) return NULL; PUT(HDRP(bp), PACK(size, 0)); PUT(FTRP(bp), PACK(size, 0)); PUT(HDRP(NEXT_BLKP(bp)), PACK(0, 1)); return coalesce(bp); }

Malloc void *mm_malloc(size_t size) { size_t asize, extendsize; char *bp; if (size <= 0) return NULL; if (size <= DSIZE) asize = DSIZE+OVERHEAD; else asize = DSIZE*((size+(OVERHEAD)+(DSIZE-1))/DSIZE); if ((bp = find_fit(asize)) != NULL) { place(bp, asize); return bp; } extendsize = MAX(asize,CHUNKSIZE); if ((bp = extend_heap(extendsize/WSIZE)) == NULL) return NULL; place(bp, asize); return bp; }

Finding first fit static void *find_fit(size_t asize) { void *bp; for (bp = heap_listp; GET_SIZE(HDRP(bp)) > 0; bp = NEXT_BLKP(bp)) if (!GET_ALLOC(HDRP(bp)) && (asize <= GET_SIZE(HDRP(bp)))) return bp; return NULL; }

Malloc void *mm_malloc(size_t size) { size_t asize, extendsize; char *bp; if (size <= 0) return NULL; if (size <= DSIZE) asize = DSIZE+OVERHEAD; else asize = DSIZE*((size+(OVERHEAD)+(DSIZE-1))/DSIZE); if ((bp = find_fit(asize)) != NULL) { place(bp, asize); return bp; } extendsize = MAX(asize,CHUNKSIZE); if ((bp = extend_heap(extendsize/WSIZE)) == NULL) return NULL; place(bp, asize); return bp; }

Placing a block in a free block static void place(void *bp, size_t asize) { size_t csize = GET_SIZE(HDRP(bp)); if ((csize - asize) >= (DSIZE + OVERHEAD)) { PUT(HDRP(bp), PACK(asize, 1)); PUT(FTRP(bp), PACK(asize, 1)); bp = NEXT_BLKP(bp); PUT(HDRP(bp), PACK(csize-asize, 0)); PUT(FTRP(bp), PACK(csize-asize, 0)); } else { PUT(HDRP(bp), PACK(csize, 1)); PUT(FTRP(bp), PACK(csize, 1)); }

Free void mm_free(void *bp) { size_t size = GET_SIZE(HDRP(bp)); PUT(HDRP(bp), PACK(size, 0)); PUT(FTRP(bp), PACK(size, 0)); coalesce(bp); }

Coalesce: called by mm_free() & extend_heap() static void *coalesce(void *bp) { size_t prev_alloc = GET_ALLOC(FTRP(PREV_BLKP(bp))); size_t next_alloc = GET_ALLOC(HDRP(NEXT_BLKP(bp))); size_t size = GET_SIZE(HDRP(bp)); if (prev_alloc && next_alloc) { return bp; } else if (prev_alloc && !next_alloc) { …… } else if (!prev_alloc && next_alloc) { size += GET_SIZE(HDRP(PREV_BLKP(bp))); PUT(FTRP(bp), PACK(size, 0)); PUT(HDRP(PREV_BLKP(bp)), PACK(size, 0)); bp = PREV_BLKP(bp); } else { …… } return bp; }

Design options Organize free blocks Implicit free list Explicit free list Segregate lists/search trees Find free blocks First fit/Next fit/Best Fit Blocks sorted by address with first fit

Lab 6 Implementation mm_init, malloc, free, realloc, calloc mm_heapcheck sanity checks You will be graded on Correctness (25) Performance + Space (60 max) Interposition (5) Consistency Checker (5) Style (5)

Lab 6 Strategies?

Garbage Collection Reference Counting Mark and Sweep Copy Collect