CSE 351: Week 9
Tom Bergan, TA
1
CSE 351: Week 9 Tom Bergan, TA 1 Today Lab 5 Reference counting - - PowerPoint PPT Presentation
CSE 351: Week 9 Tom Bergan, TA 1 Today Lab 5 Reference counting 2 Lab 5: Explicit free list allocator Your goals Implement: void* mm_malloc(size_t bytes); Implement: void mm_free(void *ptr); We give you a starting point 3 Lab 5:
1
2
3
4
!" !" !" #" #" !" !" !" !" $%&'(&)"*+,-./"01+23" 4(52"*6&,7/"01+23" 8" 4" 9"
5
size next prev tags size
size payload padding tags header footer header no footer! (more later)
6
size next prev tags size
size payload padding tags
header header
7
size payload padding tags
mm_malloc() returns a pointer to here
(the first byte of the payload) header no footer! (more later)
8
. . .
header 8 bytes next 8 bytes prev 8 bytes footer 8 bytes (empty space)
...
header 8 bytes
mm_malloc() returns a pointer to here
(the first byte of the payload)
9
header footer size next prev tags size
63 62 61 ... 3 2 1
Size: aligned to 23 = 8 Tag bit 0: this block allocated? Tag bit 1: preceding block allocated? Tag bit 2: unused Adjacent blocks in memory precedes the red block
10
preceding preceding prev size next prev tags size size payload padding tags size next prev tags size
11
size next prev tags size size payload padding tags size next prev tags size
Tag bits
this allocated: 0 preeceding allocated: ? ... ? ... 1 ... 1
Tag bits
this allocated: 1 preceding allocated: 0
Tag bits
this allocated: 0 preeceding allocated: 1
12
struct BlockInfo { size_t sizeAndTags; struct BlockInfo *next; struct BlockInfo *prev; };
size next prev tags size
13
struct BlockInfo { size_t sizeAndTags; struct BlockInfo *next; struct BlockInfo *prev; }; // global variable FREE_LIST_HEAD
size next prev tags size
size next prev tags size
14
void* searchFreeList(int reqSize) // Manipulate the free list (assumes freeBlock is on the free list) void insertFreeBlock(BlockInfo *freeBlock) void removeFreeBlock(BlockInfo *freeBlock) void coalesceFreeBlock(BlockInfo *freeBlock) // Get more memory from the OS and add it to the free list void requestMoreSpace(size_t reqSize)
void* mm_malloc(size_t size) void mm_free(void *ptr)
15
16
17
void foo() { int *x = malloc(4); }
1 size payload padding tags (in the header)
18
void foo() { int *x = malloc(4); int *p = x; ... }
2 size payload padding tags
19
void foo() { int *x = malloc(4); int *p = x; ... // x and p go out-of-scope }
size payload padding tags
(automatic free!)
20
(ref counts in purple)
21
(ref counts in purple)
(e.g., for a traversal)
22
(ref counts in purple)
(e.g., for a traversal)
23
(ref counts in purple)
(e.g., for a traversal)
24
(ref counts in purple)
25
(ref counts in purple)
26
(ref counts in purple)
27
(ref counts in purple)
28
(ref counts in purple)
29
(ref counts in purple)
30
(ref counts in purple)
31
(ref counts in purple)
32
(ref counts in purple)
33
(ref counts in purple)
34
(ref counts in purple)