Today  Explicit free lists Dynamic Memory Allocation:  Segregated free lists  Garbage collection Advanced Concepts  Memory-related perils and pitfalls CSci 2021: Machine Architecture and Organization April 27th-29th, 2020 Your instructor: Stephen McCamant Based on slides originally by: Randy Bryant, Dave O’Hallaron 1 2 Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition Explicit Free Lists Keeping Track of Free Blocks  Method 1: Implicit free list using length — links all blocks Free Allocated (as before) Size a Size a 5 Next 4 6 2 Prev Payload and padding  Method 2: Explicit free list among the free blocks using pointers Size a Size a 5 4 6 2  Method 3: Segregated free list  Maintain list(s) of free blocks, not all blocks  Different free lists for different size classes  The “next” free block could be anywhere  So we need to store forward/back pointers, not just sizes  Method 4: Blocks sorted by size  Still need boundary tags for coalescing  Can use a balanced tree (e.g. Red-Black tree) with pointers within each  Luckily we track only free blocks, so we can use payload area free block, and the length used as a key 3 4 Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition Explicit Free Lists Allocating From Explicit Free Lists conceptual graphic  Logically: Before A B C  Physically: blocks can be in any order After (with splitting) Forward (next) links A B 4 4 4 4 6 6 4 4 4 4 C Back (prev) links = malloc(…) 5 6 Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition 1
Freeing With Explicit Free Lists Freeing With a LIFO Policy (Case 1) conceptual graphic  Insertion policy : Where in the free list do you put a newly Before freed block? free( )  LIFO (last-in-first-out) policy  Insert freed block at the beginning of the free list Root  Pro: simple and constant time  Con: studies suggest fragmentation is worse than address ordered  Insert the freed block at the root of the list  Address-ordered policy  Insert freed blocks so that free list blocks are always in address order: After addr(prev) < addr(curr) < addr(next)  Con: requires search Root  Pro: studies suggest fragmentation is lower than LIFO 7 8 Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition Freeing With a LIFO Policy (Case 2) Freeing With a LIFO Policy (Case 3) conceptual graphic conceptual graphic Before Before free( ) free( ) Root Root  Splice out successor block, coalesce both memory blocks and  Splice out predecessor block, coalesce both memory blocks, and insert the new block at the root of the list insert the new block at the root of the list After After Root Root 9 10 Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition Freeing With a LIFO Policy (Case 4) Explicit List Summary conceptual graphic  Comparison to implicit list: Before free( )  Allocate is linear time in number of free blocks instead of all blocks  Much faster when most of the memory is full Root  Slightly more complicated allocate and free since needs to splice blocks in and out of the list  Some extra space for the links (2 extra words needed for each block)  Does this increase internal fragmentation?  Splice out predecessor and successor blocks, coalesce all 3 memory blocks and insert the new block at the root of the list  Most common use of linked lists is in conjunction with After segregated free lists  Keep multiple linked lists of different size classes, or possibly for different types of objects Root 11 12 Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition 2
Keeping Track of Free Blocks Today  Method 1: Implicit list using length — links all blocks  Explicit free lists  Segregated free lists 5 4 6 2  Garbage collection  Memory-related perils and pitfalls  Method 2: Explicit list among the free blocks using pointers 5 4 6 2  Method 3: Segregated free list  Different free lists for different size classes  Method 4: Blocks sorted by size  Can use a balanced tree (e.g. Red-Black tree) with pointers within each free block, and the length used as a key 13 15 Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition Segregated List (Seglist) Allocators Seglist Allocator  Given an array of free lists, each one for some size class  Each size class of blocks has its own free list 1-2  To allocate a block of size n :  Search appropriate free list for block of size m > n  If an appropriate block is found: 3  Split block and place fragment on appropriate list (optional) 4  If no block is found, try next larger class  Repeat until block is found 5-8  If no block is found:  Request additional heap memory from OS (using sbrk() ) 9-inf  Allocate block of n bytes from this new memory  Place remainder as a single free block in largest size class.  Often have separate classes for each small size  For larger sizes: One class for each two-power size 16 17 Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition Seglist Allocator (cont.) More Info on Allocators  To free a block:  D. Knuth, “ The Art of Computer Programming ”, 2 nd edition,  Coalesce and place on appropriate list Addison Wesley, 1973  The classic reference on dynamic storage allocation  Advantages of seglist allocators  Higher throughput  Wilson et al, “ Dynamic Storage Allocation: A Survey and  log time for power-of-two size classes Critical Review ”, Proc. 1995 Int’l Workshop on Memory  Better memory utilization Management, Kinross, Scotland, Sept, 1995.  First-fit search of segregated free list approximates a best-fit  Comprehensive survey search of entire heap.  Available from CS:APP student site (csapp.cs.cmu.edu)  Extreme case: Giving each block its own size class is equivalent to best-fit. 18 19 Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition 3
Recommend
More recommend