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: Explicit free list allocator The free-block list is a doubly-linked list � � �� � � Physical view (blocks can be in any order) $%&'(&)"*+,-./"01+23" 8" 4" !" !" !" !" #" #" !" !" !" !" 9" 4(52"*6&,7/"01+23" 4
Lab 5: Block format Free Block Allocated Block header header size tags size tags next prev payload padding no footer! footer size (more later) 5
Lab 5: Block format Free Block Allocated Block header header size tags size tags next prev payload size size padding size The size includes the whole block 6
Lab 5: Block format Allocated Block header size tags mm_malloc() returns a pointer to here (the first byte of the payload) payload padding no footer! (more later) 7
Lab 5: Block format Free Block . . . header next prev footer (empty space) 8 bytes 8 bytes 8 bytes 8 bytes Allocated Block ... header mm_malloc() returns a pointer to here 8 bytes (the first byte of the payload) 8
Lab 5: Block tags Free Block Size Tag header size tags 63 62 61 ... 3 2 1 0 next prev Size: aligned to 2 3 = 8 Tag bit 0: this block allocated? Tag bit 1: preceding block allocated? Tag bit 2: unused footer size Adjacent blocks in memory precedes the red block 9
Lab 5: Adjacent blocks Block 1 Block 2 Block 3 tags tags tags payload padding next prev next prev size size size size size preceding preceding prev 10
Lab 5: Adjacent blocks Block 1 Block 2 Block 3 tags tags tags payload padding next prev next prev size size size size size Tag bits Tag bits Tag bits this allocated: 0 this allocated: 1 this allocated: 0 preeceding allocated: ? preceding allocated: 0 preeceding allocated: 1 ... 1 0 ... 0 1 ... ? 0 11
Lab 5: Block data structure Free Block mm.c size tags struct BlockInfo { size_t sizeAndTags; next struct BlockInfo *next; prev struct BlockInfo *prev; }; size 12
Lab 5: Block data structure mm.c struct BlockInfo { size_t sizeAndTags; struct BlockInfo *next; struct BlockInfo *prev; }; Free list // global variable size tags size tags FREE_LIST_HEAD next next prev prev ∅ size size 13
Lab 5: Source code overview We give you 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) You implement void* mm_malloc(size_t size) void mm_free(void *ptr) 14
Lab 5: Hints − You will have to do some pointer arithmetic ... use: POINTER_ADD(), POINTER_SUB() − You don’t need any more global variables − You don’t need to call any functions in memlib.h − We did a lot of the tedious linked-list manipulation for you ... use it! − Do the simple thing first ... our solution is ~75 lines total, including comments − Debug with gdb ... use command: print *(BlockInfo*)p ... this allows you to view pointer p through the “lens” of a BlockInfo, i.e., it pretends that p points to a BlockInfo 15
Today • Lab 5 • Reference counting 16
Reference Counting Basic idea: count the # of pointers to each object Example Heap Ref count void foo() { size tags 1 (in the header) int *x = malloc(4); payload } padding 17
Reference Counting Basic idea: count the # of pointers to each object Example Heap Ref count void foo() { size tags 2 int *x = malloc(4); int *p = x; ... payload } padding 18
Reference Counting When the count=0, free the object Example Heap Ref count void foo() { size tags 0 int *x = malloc(4); is zero int *p = x; (automatic free!) ... payload // x and p go out-of-scope } padding 19
Reference Counting Example: a tree (ref counts in purple) root 1 1 1 1 1 1 1 20
Reference Counting Example: a tree (ref counts in purple) root tmp (e.g., for a traversal) 2 1 1 1 1 1 1 21
Reference Counting Example: a tree (ref counts in purple) root 1 tmp (e.g., for a traversal) 2 1 1 1 1 1 22
Reference Counting Example: a tree (ref counts in purple) root 1 1 1 tmp (e.g., for a traversal) 2 1 1 1 23
Reference Counting Example: a tree (ref counts in purple) root 1 1 1 1 1 1 1 24
Reference Counting Example: a tree (ref counts in purple) root = NULL X 0 1 1 1 1 1 1 25
Reference Counting Example: a tree (ref counts in purple) root = NULL 0 0 1 1 1 1 26
Reference Counting Example: a tree (ref counts in purple) root = NULL 0 0 0 1 27
Reference Counting Example: a tree (ref counts in purple) root = NULL 0 28
Reference Counting Example: a directed graph (ref counts in purple) root 1 1 1 1 1 1 29
Reference Counting Example: a directed graph (ref counts in purple) root 1 1 1 1 2 1 30
Reference Counting Example: a directed graph (ref counts in purple) root 1 1 2 1 2 1 31
Reference Counting Example: a directed graph (ref counts in purple) root = NULL 0 1 2 1 2 1 32
Reference Counting Example: a directed graph (ref counts in purple) root = NULL 0 1 1 2 1 33
Reference Counting Example: a directed graph (ref counts in purple) root = NULL 1 Reference counting cannot garbage collect cycles! 1 1 1 34
Recommend
More recommend