Dynamic Memory Allocation II November 3, 2006Keeping 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 SummarySlide 11Segregated 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 MemorySlide 30Slide 31Slide 32Slide 33Referencing Nonexistent VariablesFreeing Blocks Multiple TimesReferencing Freed BlocksFailing to Free Blocks (Memory Leaks)Slide 38Dealing With Memory BugsDealing With Memory Bugs (cont.)Dynamic Memory Allocation IINovember 3, 2006Dynamic Memory Allocation IINovember 3, 2006TopicsExplicit doubly-linked free listsSegregated free listsGarbage collectionReview of pointersMemory-related perils and pitfallsclass19.ppt15-213“The course that gives CMU its Zip!”– 2 –15-213, F’06Keeping 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 listsDifferent free lists for different size classesMethod 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’06Explicit Free ListsExplicit Free ListsUse data space for link pointersTypically doubly linkedStill need boundary tags for coalescingIt is important to realize that links are not necessarily in the same order as the blocksA B C4 4 4 4 66 44 4 4Forward linksBack linksABC– 4 –15-213, F’06Allocating From Explicit Free ListsAllocating From Explicit Free ListsBefore:After:= malloc(…)(with splitting)– 5 –15-213, F’06Freeing 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) policyInsert freed block at the beginning of the free listPro: simple and constant timeCon: studies suggest fragmentation is worse than address ordered.Address-ordered policyInsert freed blocks so that free list blocks are always in address order»i.e. addr(pred) < addr(curr) < addr(succ) Con: requires search Pro: studies suggest fragmentation is lower than LIFO– 6 –15-213, F’06Freeing With a LIFO Policy (Case 1)Freeing With a LIFO Policy (Case 1)Insert the freed block at the root of the listfree( )RootRootBefore:After:– 7 –15-213, F’06Freeing 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( )RootRootBefore:After:– 8 –15-213, F’06Freeing 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( )RootRootBefore:After:– 9 –15-213, F’06Freeing 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( )RootRootBefore:After:– 10 –15-213, F’06Explicit List SummaryExplicit List SummaryComparison to implicit list:Allocate is linear time in number of free blocks instead of total blocks -- 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 listSome extra space for the links (2 extra words needed for each block)Main use of linked lists is in conjunction with segregated free listsKeep multiple linked lists of different size classes, or possibly for different types of objectsDoes this increase internal frag?– 11 –15-213, F’06Keeping 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 listDifferent free lists for different size classesMethod 4: Blocks sorted by sizeCan 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– 12 –15-213, F’06Segregated List (seglist) AllocatorsSegregated List (seglist) AllocatorsEach size class of blocks has its own free list1-2345-89-infOften 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’06Seglist 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 > nIf an appropriate block is found:Split block and place fragment on appropriate list (optional)If no block is found, try next larger classRepeat until block is foundIf no block is found:Request additional heap memory from OS (using sbrk function)Allocate block of n bytes from this new memoryPlace remainder as a single free block in largest size class.– 14 –15-213, F’06Seglist Allocator (cont)Seglist Allocator (cont)To free a block:Coalesce and place on appropriate list (optional)Advantages of seglist allocatorsHigher throughput i.e., log time for power of two size classesBetter memory utilizationFirst-fit search of segregated free list approximates a best-fit search of entire heap.Extreme case: Giving each block its own size class is equivalent to best-fit.– 15 –15-213, F’06For More Info on AllocatorsFor More Info on AllocatorsD. Knuth, “The Art of Computer Programming, Second Edition”, Addison Wesley, 1973The 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 surveyAvailable from CS:APP student site (csapp.cs.cmu.edu)– 16
View Full Document