28
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

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

Embed Size (px)

Citation preview

Page 1: 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

Week 12 (March 28th)

Outline Memory Allocation Lab 6

Reminders Lab 6:

April 7th (next Thurs) Start Early!!!

Exam 2: April 5th (Next Tue)

TA: Kun Gao

Page 2: 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

Page 3: 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

Do not know memory requirements beforehand Explicit allocators

C standard library ‘malloc’ and ‘free’ Implicit allocators (garbage collection)

Lisp, ML, Java

Page 4: 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

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

Page 5: 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

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

Page 6: 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

Placement Policy

Best-Fit First-Fit Next-Fit No placement policy is perfect!

Unless you know the future

Page 7: 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

Best Fit not always ‘Best’!

6 bytes free Memory in use 4 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!

Page 8: 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

Example implementation

Section 10.9.12 Understand every line of the code

Implicit free list + immediate boundary tag coalescing + first fit

Code is available athttp://csapp.cs.cmu.edu/public/ics/code/vm/malloc.c

Use it as a starting point

Page 9: 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

Block format

Header

Pointer returnedby malloc (Block Pointer (bp))

32 bits

32 bits

>= what user askedfor in malloc

Footer

Payload

Page 10: 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

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 = 0x19

0 0 asize

3 2 1 0 31

Page 11: 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

Heap format

8|1 8|1 0|1

Double Word Alignment(8 bytes)

4 bytes Pad Prologue Epilogue

Allocated and Free Blocks

HDR FTR HDR FTR

Page 12: 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

Example

Assume initial 4-byte padding, no prologue, both header and foot, and first fit

Allocator starts at 0xA0000000 Malloc(1) = ? Malloc(2) = ?

Page 13: 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

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

Page 14: 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

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)

Page 15: 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

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)))

Page 16: 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

Using the Macros

What is the size of the next block in memory? size_t size =

GET_SIZE(HDRP(NEXT_BLKP(bp)));

Page 17: 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

Initializing the heapint 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; }

Page 18: 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

Extending the heapstatic 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); }

Page 19: 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

Mallocvoid *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;

}

Page 20: 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

Finding first fitstatic 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;}

Page 21: 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

Mallocvoid *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;

}

Page 22: 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

Placing a block in a free blockstatic 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));

} }

Page 23: 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

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);

}

Page 24: 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

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;

}

Page 25: 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

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

Page 26: 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

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)

Page 27: 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

Lab 6

Strategies?

Page 28: 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

Garbage Collection

Reference Counting Mark and Sweep Copy Collect