Dynamic Memory Allocation II November 7, 2007Keeping Track of Free BlocksExplicit Free ListsAllocating From Explicit Free ListsFreeing With Explicit Free ListsFreeing With a LIFO Policy (Case 1)Freeing With a LIFO Policy (Case 2)Freeing With a LIFO Policy (Case 3)Freeing With a LIFO Policy (Case 4)Explicit List SummaryKeeping Track of Free BlocksSegregated List (seglist) AllocatorsSeglist AllocatorSeglist Allocator (cont)For More Info on AllocatorsImplicit Memory Management: Garbage CollectionGarbage CollectionClassical GC AlgorithmsMemory as a GraphAssumptions For This LectureMark and Sweep CollectingMark and Sweep (cont.)Conservative Mark and Sweep in CMemory-Related Perils and PitfallsC operators (K&R p. 53)Review of C Pointer DeclarationsDereferencing Bad PointersReading Uninitialized MemoryOverwriting MemoryOverwriting MemoryOverwriting MemoryOverwriting MemoryOverwriting MemoryReferencing Nonexistent VariablesFreeing Blocks Multiple TimesReferencing Freed BlocksFailing to Free Blocks (Memory Leaks)Failing to Free Blocks (Memory Leaks)Dealing With Memory BugsDealing With Memory Bugs (cont.)Dynamic Memory Allocation IINovember 7, 2007Dynamic Memory Allocation IINovember 7, 200715-213Topics Explicit doubly-linked free lists Segregated free lists Garbage collection Review of pointers Memory-related perils and pitfallsclass19.ppt15-213, F’07–2–15-213, F’07Keeping Track of Free BlocksKeeping Track of Free Blocksz Method 1: Implicit list using lengths -- links all blocksz Method 2: Explicit list among the free blocks using pointers within the free blocksz Method 3: Segregated free lists Different free lists for different size classesz Method 4: Blocks sorted by size (not discussed) Can use a balanced tree (e.g. Red-Black tree) with pointers within each free block, and the length used as a key5 4 265 4 26–3–15-213, F’07Explicit Free ListsExplicit Free ListsUse 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 thesame order as the blocksA B C4 4 4 4 66 44 4 4Forward linksBack linksABC–4–15-213, F’07Allocating From Explicit Free ListsAllocating From Explicit Free ListsBefore:After:= malloc(…)(with splitting)–5–15-213, F’07Freeing With Explicit Free ListsFreeing With Explicit Free ListsInsertion policy: Where in the free list do you put a newly freed block? LIFO (last-in-first-out) policyz Insert freed block at the beginning of the free listz Pro: simple and constant timez Con: studies suggest fragmentation is worse than address ordered. Address-ordered policyz Insert freed blocks so that free list blocks are always in address order» i.e. addr(pred) < addr(curr) < addr(succ)z Con: requires searchz Pro: studies suggest fragmentation is lower than LIFO–6–15-213, F’07Freeing With a LIFO Policy (Case 1)Freeing With a LIFO Policy (Case 1)Insert the freed block at the root of the listfree( )RootBefore:RootAfter:–7–15-213, F’07Freeing With a LIFO Policy (Case 2)Freeing With a LIFO Policy (Case 2)Splice out predecessor block, coalesce both memory blocks and insert the new block at the root of the listfree( )RootBefore:RootAfter:–8–15-213, F’07Freeing With a LIFO Policy (Case 3)Freeing With a LIFO Policy (Case 3)Splice out successor block, coalesce both memory blocks and insert the new block at the root of the listfree( )RootBefore:RootAfter:–9–15-213, F’07Freeing With a LIFO Policy (Case 4)Freeing With a LIFO Policy (Case 4)Splice out predecessor and successor blocks, coalesce all 3 memory blocks and insert the new block at the root of the listfree( )RootBefore:RootAfter:–10–15-213, F’07Explicit List SummaryExplicit List SummaryComparison to implicit list: Allocate is linear time in number of free blocks instead of total blocks --z much faster allocates when most of the memory is full 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)Main use of linked lists is in conjunction with segregated free lists Keep multiple linked lists of different size classes, or possibly for different types of objectsDoes this increase internal frag?–11–15-213, F’07Keeping Track of Free BlocksKeeping Track of Free BlocksMethod 1: Implicit list using lengths -- links all blocksMethod 2: Explicit list among the free blocks using pointers within the free blocksMethod 3: Segregated free list Different free lists for different size classesMethod 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 key54 2654 26–12–15-213, F’07Segregated List (seglist) AllocatorsSegregated List (seglist) AllocatorsEach size class of blocks has its own free list1-2345-89-inf Often have separate size class for every small size (2,3,4,…) For larger sizes typically have a size class for each power of 2–13–15-213, F’07Seglist AllocatorSeglist AllocatorGiven an array of free lists, each one for some size classTo allocate a block of size n: Search appropriate free list for block of size m > n If an appropriate block is found:z Split block and place fragment on appropriate list (optional) If no block is found, try next larger class Repeat until block is foundIf no block is found: Request additional heap memory from OS (using sbrkfunction) Allocate block of n bytes from this new memory Place remainder as a single free block in largest size class.–14–15-213, F’07Seglist Allocator (cont)Seglist Allocator (cont)To free a block: Coalesce and place on appropriate list (optional)Advantages of seglist allocators Higher throughputz i.e., log time for power of two size classes Better memory utilizationz First-fit search of segregated free list approximates a best-fit search of entire heap.z Extreme case: Giving each block its own size class is equivalent to best-fit.–15–15-213, F’07For More Info on AllocatorsFor More Info on AllocatorsD. Knuth, “The Art of Computer Programming, Second Edition”, Addison Wesley, 1973 The classic reference on dynamic storage allocationWilson et al, “Dynamic Storage Allocation: A Survey and Critical Review”, Proc. 1995 Int’l Workshop on Memory Management, Kinross, Scotland, Sept, 1995. Comprehensive survey Available from CS:APP
View Full Document