1 Dynamic Memory Allocation (II) Implementation. 2 Outline Implementation of a simple allocator Explicit Free List Segregated Free List Suggested reading:

Slides:



Advertisements
Similar presentations
Dynamic Memory Allocation II
Advertisements

Malloc Recitation By sseshadr. Agenda Macros in C Pointer declarations Casting and Pointer Arithmetic Malloc.
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 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.
1. Dynamic Memory Allocation. Dynamic Memory Allocation Programmers use dynamic memory allocators (such as malloc ) to acquire VM at run time.  For data.
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.
1 Binghamton University Dynamic Memory Allocation: Advanced Concepts CS220: Computer Systems II.
Dynamic Memory Allocation I October 16, 2008 Topics Simple explicit allocators Data structures Mechanisms Policies lecture-15.ppt “The course that.
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,
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.
Recitation 10 Anubhav Gupta Section D.
5/23/20151 GC16/3011 Functional Programming Lecture 19 Memory Allocation Techniques.
Carnegie Mellon Introduction to Computer Systems /18-243, spring th Lecture, Mar. 31 st Instructors: Gregory Kesden and Markus Püschel.
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.
1 1 Lecture 4 Structure – Array, Records and Alignment Memory- How to allocate memory to speed up operation Structure – Array, Records and Alignment Memory-
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.
Dynamic Memory Allocation I November 1, 2006 Topics Simple explicit allocators Data structures Mechanisms Policies class18.ppt “The course that.
Recitation 9 (Nov. 8) Outline Virtual memory Lab 6 hints Reminders Lab 6: Get correctness points Exam 2: Nov. 16 (Next Tue) Minglong Shao
Memory Allocation CS Introduction to Operating Systems.
The memory allocation problem Define the memory allocation problem Memory organization and memory allocation schemes.
L6: Malloc Lab Writing a Dynamic Storage Allocator October 30, 2006
Explicit Free Lists Use data space for link pointers –Typically doubly linked –Still need boundary tags for coalescing –It is important to realize that.
Memory Management II: Dynamic Storage Allocation Mar 7, 2000 Topics Segregated free lists –Buddy system Garbage collection –Mark and Sweep –Copying –Reference.
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.
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.
Carnegie Mellon /18-243: Introduction to Computer Systems Instructors: Bill Nace and Gregory Kesden (c) All Rights Reserved. All work.
Dynamic Memory Allocation II Topics Explicit doubly-linked free lists Segregated free lists Garbage collection.
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!
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.
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.
Memory Management What if pgm mem > main mem ?. Memory Management What if pgm mem > main mem ? Overlays – program controlled.
Chapter 17 Free-Space Management
Instructor: Phil Gibbons
Dynamic Memory Allocation II
Section 10: Memory Allocation Topics
CS 3214 Introduction to Computer Systems
Dynamic Memory Allocation
Recitation 11: More Malloc Lab
Dynamic Memory Allocation: Basic Concepts CS220: Computer Systems II
User-Level Dynamic Memory Allocation: Malloc and Free
Dynamic Memory Allocation I
CS703 - Advanced Operating Systems
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
Memory Allocation II CSE 351 Winter 2018
Dynamic Memory Allocation: Basic Concepts CSCI 380: Operating Systems
Malloc Lab CSCI 380: Operating Systems
CS703 - Advanced Operating Systems
Understanding mm-helper.c Adding debugging info to mm-helper.c
Presentation transcript:

1 Dynamic Memory Allocation (II) Implementation

2 Outline Implementation of a simple allocator Explicit Free List Segregated Free List Suggested reading: 9.9

3 Review word Format of allocated and free blocks a = 1: allocated block a = 0: free block size: block size payload: application data (allocated blocks only) boundary tag (footer) header size payload and padding a sizea 00 00

4 1 int mm_init(void); 2 void *mm_malloc(size_t size); 3 void mm_free(void *bp); Implementing a Simple Allocator

5 1 /* private global variables */ 2 static void *mem_heap; /* points to first byte of the heap */ 3 static void *mem_brk; /* points to last byte of the heap */ 4 static void *mem_max_addr; /* max virtual address for the heap */ 5 Initialize

6 6 /* 7 * mem_init - initializes the memory system model 8 */ 9 void mem_init(void) 10 { 11 mem_heap = (char *)Malloc(MAX_HEAP); 12 mem_brk = (char *)mem_heap; 13 mem_max_addr = (char *) (mem_heap + MAX_HEAP); 14 } 15 Initialize

7 16 /* 17 * mem_sbrk - simple model of the sbrk function. Extends the 18 * heap by incr bytes and returns the start address of the new 19 * area. In this model, the heap cannot be shrunk. 20 */ Initialize

8 21 void *mem_sbrk(int incr) 22 { 23 void *old_brk = mem_brk; if ( (incr mem_max_addr)) { 26 errno = ENOMEM; 27 fprintf(stderr, “ ERROR: mem_sbrk failed. Ran out of memory …\n ”) 28 return (void *)-1; 29 } 30 mem_brk += incr; 31 return old_brk; 32 } Initialize

9 Data Structure

10 1 /* Basic constants and macros */ 2 #define WSIZE 4 /* word size (bytes) */ 3 #define DSIZE 8 /* double word size (bytes) */ 4#define CHUNKSIZE (1<<12) /* Extend heap by this amount (bytes) */ 5 6 #define MAX(x, y) ((x) > (y)? (x) : (y)) 7 8 /* Pack a size and allocated bit into a word */ 9 #define PACK(size, alloc) ((size) | (alloc)) /* Read and write a word at address p */ 12 #define GET(p) (*(unsigned int *)(p)) 13 #define PUT(p, val) (*(unsigned int *)(p) = (val)) 14 Macros

11 15 /* Read the size and allocated fields from address p */ 16 #define GET_SIZE(p) (GET(p) & ˜0x7) 17 #define GET_ALLOC(p) (GET(p) & 0x1) /* Given block ptr bp, compute address of its header and footer */ 20 #define HDRP(bp) ((char *)(bp) - WSIZE) 21 #define FTRP(bp) ((char *)(bp) + GET_SIZE(HDRP(bp)) - DSIZE) /* Given block ptr bp, compute address of next and previous blocks */ 24 #define NEXT_BLKP(bp) ((char *)(bp) +GET_SIZE(((char *)(bp)-WSIZE))) 25 #define PREV_BLKP(bp) ((void *)(bp) - GET_SIZE(((void *)(bp) - DSIZE))) Macros

12 1 int mm_init(void) 2 { 3 /* create the initial empty heap */ 4 if ((heap_listp = mem_sbrk(4*WSIZE)) == (void *) -1) 5 return -1; 6 PUT(heap_listp, 0); /* alignment padding */ 7 PUT(heap_listp+(1*WSIZE), PACK(DSIZE, 1)); /* prologue header */ 8 PUT(heap_listp+(2*WSIZE), PACK(DSIZE, 1)); /* prologue footer */ 9 PUT(heap_listp+(3*WSIZE), PACK(0, 1)); /* epilogue header */ 10 heap_listp += (2*WIZE); /* Extend the empty heap with a free block of CHUNKSIZE bytes */ 13 if (extend_heap(CHUNKSIZE/WSIZE) == NULL) 14 return -1; 15 return 0; 16 } mm_init()

13 1 static void *extend_heap(size_t words) 2 { 3 char *bp; 4 size_t size; 5 6 /* Allocate an even number of words to maintain alignment */ 7 size = (words % 2) ? (words+1) * WSIZE : words * WSIZE; 8 if ((long)(bp = mem_sbrk(size)) == -1) 9 return NULL; /* Initialize free block header/footer and the epilogue header */ 12 PUT(HDRP(bp), PACK(size, 0)); /* free block header */ 13 PUT(FTRP(bp), PACK(size, 0)); /* free block footer */ 14 PUT(HDRP(NEXT_BLKP(bp)), PACK(0, 1)); /* new epilogue header */ /* Coalesce if the previous block was free */ 17 return coalesce(bp); 18 } mm_init()

14 1 void mm_free(void *bp) 2 { 3 size_t size = GET_SIZE(HDRP(bp)); 4 5 PUT(HDRP(bp), PACK(size, 0)); 6 PUT(FTRP(bp), PACK(size, 0)); 7 coalesce(bp); 8 } 9 mm_free()

15 10 static void *coalesce(void *bp) 11 { 12 size_t prev_alloc = GET_ALLOC(FTRP(PREV_BLKP(bp))); 13 size_t next_alloc = GET_ALLOC(HDRP(NEXT_BLKP(bp))); 14 size_t size = GET_SIZE(HDRP(bp)); if (prev_alloc && next_alloc) { /* Case 1 */ 17 return bp; 18 } else if (prev_alloc && !next_alloc) { /* Case 2 */ 21 size += GET_SIZE(HDRP(NEXT_BLKP(bp))); 22 PUT(HDRP(bp), PACK(size, 0)); 23 PUT(FTRP(bp), PACK(size,0)); 24 return(bp); 25 } 26 mm_free()

16 27 else if (!prev_alloc && next_alloc) { /* Case 3 */ 28 size += GET_SIZE(HDRP(PREV_BLKP(bp))); 29 PUT(FTRP(bp), PACK(size, 0)); 30 PUT(HDRP(PREV_BLKP(bp)), PACK(size, 0)); 31 return(PREV_BLKP(bp)); 32 } else { /* Case 4 */ 35 size += GET_SIZE(HDRP(PREV_BLKP(bp))) + 36 GET_SIZE(FTRP(NEXT_BLKP(bp))); 37 PUT(HDRP(PREV_BLKP(bp)), PACK(size, 0)); 38 PUT(FTRP(NEXT_BLKP(bp)), PACK(size, 0)); 39 return(PREV_BLKP(bp)); 40 } 41 } mm_free()

17 1 void *mm_malloc (size_t size) 2 { 3 size_t asize; /* adjusted block size */ 4 size_t extendsize; /* amount to extend heap if no fit */ 5 char *bp; 6 7 /* Ignore spurious requests */ 8 if (size <= 0) 9 return NULL; /* Adjust block size to include overhead and alignment reqs. */ 12 if (size <= DSIZE) 13 asize = 2*DSIZE; 14 else 15 asize = DSIZE * ((size + (DSIZE) + (DSIZE-1)) / DSIZE); 16 mm_malloc()

18 17 /* Search the free list for a fit */ 18 if ((bp = find_fit(asize)) != NULL) { 19 place (bp, asize); 20 return bp; 21 } /* No fit found. Get more memory and place the block */ 24 extendsize = MAX (asize, CHUNKSIZE) ; 25 if ((bp = extend_heap (extendsize/WSIZE)) == NULL) 26 return NULL; 27 place (bp, asize); 28 return bp; 29 } mm_malloc()

19 1.static void *find_fit(size_t asize) 2.{ 3. void *bp ; /* first fit search */ 6. for (bp = heap_listp; GET_SIZE(HDRP(bp)) > 0 ; bp = NEXT_BLKP(bp) ) { 7. if (!GET_ALLOC(HDRP(bp)) && (asize<=GET_SIZE(HDRP(bp)))) { 8. return bp; 9. } 10. } 11. return NULL; /*no fit */ 12. } mm_alloc()

20 1.static void place(void *bp, size_t asize) 2.{ 3. size_t csize = GET_SIZE(HDRP(bp)) ; if ( (csize –asize) >= (DSIZE + OVERHEAD) ) { 6. PUT(HDRP(bp), PACK(asize, 1)) ; 7. PUT(FTRP(bp), PACK(asize, 1)) ; 8. bp = NEXT_BLKP(bp) ; 9. PUT(HDRP(bp), PACK(csize-asize, 0) ; 10. PUT(FTRP(bp), PACK(csize-asize, 0) ; 11. } else { 12. PUT(HDRP(bp), PACK(csize, 1) ; 13. PUT(FTRP(bp), PACK(csize, 1) ; 14. } 15.} mm_alloc()

21 Explicit free lists ABC Forward links Back links A B C

22 Explicit free lists Explicit list among the free blocks using pointers within the free blocks Use data space for link pointers –Typically doubly linked –Still need boundary tags for coalescing –It is important to realize that links are not necessarily in the same order as the blocks

Explicit free lists 23

24 Freeing with explicit free lists Where to put the newly freed block in the free list –LIFO (last-in-first-out) policy insert freed block at the beginning of the free list pro: simple and constant time con: studies suggest fragmentation is worse than address ordered.

25 Freeing with explicit free lists Where to put the newly freed block in the free list –Address-ordered policy insert freed blocks so that free list blocks are always in address order –i.e. addr(pred) < addr(curr) < addr(succ) con: requires search pro: studies suggest fragmentation is better than LIFO

26 Segregated Storage Each size “class” has its own collection of blocks –Often have separate collection for every small size (2,3,4,…) –For larger sizes typically have a collection for each power of 2

27 Segregated Storage

28 Separate heap and free list for each size class No splitting To allocate a block of size n: –if free list for size n is not empty, allocate first block on list (note, list can be implicit or explicit) –if free list is empty, get a new page create new free list from all blocks in page allocate first block on list –constant time Simple segregated storage

29 To free a block: –Add to free list Tradeoffs: –fast, but can fragment badly Simple segregated storage

30 Array of free lists, each one for some size class Segregated fits

31 To allocate a block of size n: –search appropriate free list for block of size m > n –if an appropriate block is found: split block and place fragment on appropriate list (optional) –if no block is found, try next larger class –repeat until block is found –if no blocks is found in all classes, try more heap memory Segregated fits

32 To free a block: –coalesce and place on appropriate list (optional) Tradeoffs –faster search than sequential fits (i.e., log time for power of two size classes) –controls fragmentation A simple first-fit approximates a best-fit over entire heap –coalescing can increase search times deferred coalescing can help Segregated fits

33 A special case of segregated fits –Each size is power of 2 Initialize –A heap of size 2 m Buddy Systems

34 Allocate –Roundup to power of 2 such as 2 k –Find a free block of size 2 j (k  j  m) –Split the block in half until j=k Each remaining half block (buddy) is placed on the appreciate free list Free –Continue coalescing with the free buddies Buddy Systems

Next Physical and Virtual Addressing Address Spaces VM as a Tool for Caching VM as a Tool for Memory Management VM as a Tool for Memory Protection Suggested reading: 9.1~9.5 35