Upload
others
View
11
Download
0
Embed Size (px)
Citation preview
DYNAMIC MEMORY ALLOCATION:BASIC CONCEPTSCS 045
Computer Organization and Architecture
Prof. Donald J. PattersonAdapted from Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition
• BASIC CONCEPTS
• IMPLICIT FREE LISTS
DYNAMIC MEMORY ALLOCATION: BASIC
DYNAMIC MEMORY ALLOCATION
¢ Programmersusedynamicmemoryallocators(suchasmalloc)toacquireVMatrun5me.! Fordatastructureswhose
sizeisonlyknownatrun4me.
¢ Dynamicmemoryallocatorsmanageanareaofprocessvirtualmemoryknownastheheap.
Heap(viamalloc)
Programtext(.text)
Ini5alizeddata(.data)
Unini5alizeddata(.bss)
Userstack
0
Topofheap(brk ptr)
Applica5on
DynamicMemoryAllocator
Heap
DYNAMIC MEMORY ALLOCATION
¢ Allocatormaintainsheapascollec0onofvariablesizedblocks,whichareeitherallocatedorfree
¢ Typesofallocators! Explicitallocator:applica&onallocatesandfreesspace
! E.g.,mallocandfreeinC! Implicitallocator:applica&onallocates,butdoesnotfreespace
! E.g.garbagecollec&oninJava,ML,andLisp
¢ Willdiscusssimpleexplicitmemoryalloca0ontoday
THE “MALLOC” PACKAGE#include <stdlib.h>
void *malloc(size_t size) ! Successful:
! Returnsapointertoamemoryblockofatleastsizebytesalignedtoan8-byte(x86)or16-byte(x86-64)boundary
! Ifsize == 0,returnsNULL! Unsuccessful:returnsNULL(0)andsetserrno
void free(void *p) ! Returnstheblockpointedatbyptopoolofavailablememory
! pmustcomefromapreviouscalltomalloc orrealloc
Otherfunc+ons
! calloc:VersionofmallocthatiniIalizesallocatedblocktozero.! realloc:Changesthesizeofapreviouslyallocatedblock.! sbrk:Usedinternallybyallocatorstogroworshrinktheheap
EXAMPLE OF MALLOC USAGE#include <stdio.h> !#include <stdlib.h> !!void foo(int n) { ! int i, *p; !! /* Allocate a block of n ints */! p = (int *) malloc(n * sizeof(int)); ! if (p == NULL) { ! perror("malloc"); ! exit(0); ! } !! /* Initialize allocated block */! for (i=0; i<n; i++) !
p[i] = i; !!! /* Return allocated block to the heap */! free(p); !} !
LECTURE ASSUMPTIONS
¢ Memoryiswordaddressed.¢ Wordsareint-sized.
Allocatedblock(4words)
Freeblock(3words) Freeword
Allocatedword
ALLOCATION EXAMPLE
p1 = malloc(4)
p2 = malloc(5)
p3 = malloc(6)
free(p2)
p4 = malloc(2)
CONSTRAINTS
¢ Applica'ons! Canissuearbitrarysequenceofmallocandfreerequests! freerequestmustbetoamalloc’dblock
¢ Allocators! Can’tcontrolnumberorsizeofallocatedblocks
! Mustrespondimmediatelytomallocrequests! i.e.,can’treorderorbufferrequests
! Mustallocateblocksfromfreememory
! i.e.,canonlyplaceallocatedblocksinfreememory! Mustalignblockssotheysa>sfyallalignmentrequirements
! 8-byte(x86)or16-byte(x86-64)alignmentonLinuxboxes
! Canmanipulateandmodifyonlyfreememory
! Can’tmovetheallocatedblocksoncetheyaremalloc’d! i.e.,compac>onisnotallowed
PERFORMANCE GOAL: THROUGHPUT¢ Givensomesequenceofmallocandfreerequests:
! R0,R1,...,Rk,...,Rn-1
¢ Goals:maximizethroughputandpeakmemoryu;liza;on! Thesegoalsareo+enconflic0ng
¢ Throughput:! Numberofcompletedrequestsperunit0me
! Example:
! 5,000malloccallsand5,000freecallsin10seconds! Throughputis1,000opera0ons/second
PERFORMANCE GOAL: PEAK MEMORY UTIL.
¢ Givensomesequenceofmallocandfreerequests:! R0,R1,...,Rk,...,Rn-1
¢ Def:AggregatepayloadPk! malloc(p)resultsinablockwithapayloadofpbytes! A4errequestRkhascompleted,theaggregatepayloadPkisthesumof
currentlyallocatedpayloads
¢ Def:CurrentheapsizeHk! AssumeHkismonotonicallynondecreasing
! i.e.,heaponlygrowswhenallocatorusessbrk
¢ Def:Peakmemoryu;liza;ona<erk+1requests! Uk=(maxi<=kPi)/Hk
INTERNAL FRAGMENTATION
¢ Foragivenblock,internalfragmenta,onoccursifpayloadissmallerthanblocksize
¢ Causedby
! Overheadofmaintainingheapdatastructures! Paddingforalignmentpurposes! Explicitpolicydecisions
(e.g.,toreturnabigblocktosa>sfyasmallrequest)
¢ Dependsonlyonthepa<ernofpreviousrequests! Thus,easytomeasure
PayloadInternalfragmenta@on
Block
Internalfragmenta@on
EXTERNAL FRAGMENTATION
¢ Occurswhenthereisenoughaggregateheapmemory,butnosinglefreeblockislargeenough
¢ Dependsonthepa:ernoffuturerequests! Thus,difficulttomeasure
p1 = malloc(4)
p2 = malloc(5)
p3 = malloc(6)
free(p2)
p4 = malloc(6) Oops!(whatwouldhappennow?)
IMPLEMENTATION ISSUES
¢ Howdoweknowhowmuchmemorytofreegivenjustapointer?
¢ Howdowekeeptrackofthefreeblocks?
¢ Whatdowedowiththeextraspacewhenalloca=ngastructurethatissmallerthanthefreeblockitisplacedin?
¢ Howdowepickablocktouseforalloca=on--manymightfit?
¢ Howdowereinsertfreedblock?
KNOWING HOW MUCH TO FREE¢ Standardmethod
! Keepthelengthofablockinthewordprecedingtheblock.! Thiswordiso7encalledtheheaderfieldorheader
! Requiresanextrawordforeveryallocatedblock
p0 = malloc(4)
p0
free(p0)
blocksize payload
5
KEEPING TRACK OF FREE BLOCKS
¢ Method1:Implicitlistusinglength—linksallblocks
¢ Method2:Explicitlistamongthefreeblocksusingpointers
¢ Method3:Segregatedfreelist
! Differentfreelistsfordifferentsizeclasses
¢ Method4:Blockssortedbysize! Canuseabalancedtree(e.g.Red-Blacktree)withpointerswithineach
freeblock,andthelengthusedasakey
5 4 26
5 4 26
• BASIC CONCEPTS
• IMPLICIT FREE LISTS
DYNAMIC MEMORY ALLOCATION: BASIC
METHOD 1: IMPLICIT LIST¢ Foreachblockweneedbothsizeandalloca3onstatus
! Couldstorethisinforma1onintwowords:wasteful!
¢ Standardtrick! Ifblocksarealigned,somelow-orderaddressbitsarealways0! Insteadofstoringanalways-0bit,useitasaallocated/freeflag! Whenreadingsizeword,mustmaskoutthisbit
Size
1word
Formatofallocatedandfreeblocks
Payload
a=1:Allocatedblocka=0:FreeblockSize:blocksizePayload:applica3ondata(allocatedblocksonly)
a
Op3onalpadding
DETAILED IMPLICIT FREE LIST EXAMPLE
Startof
heap
Double-wordaligned
8/0 16/1 16/132/0
Unused
0/1
Allocatedblocks:shadedFreeblocks:unshadedHeaders:labeledwithsizeinbytes/allocatedbit
IMPLICIT LIST: FINDING A FREE BLOCK¢ Firstfit:
! Searchlistfrombeginning,choosefirstfreeblockthatfits:! Cantakelinear7meintotalnumberofblocks(allocatedandfree)! Inprac7ceitcancause“splinters”atbeginningoflist
¢ Nextfit:! Likefirstfit,butsearchliststar7ngwhereprevioussearchfinished! ShouldoCenbefasterthanfirstfit:avoidsre-scanningunhelpfulblocks! Someresearchsuggeststhatfragmenta7onisworse
¢ Bestfit:! Searchthelist,choosethebestfreeblock:fits,withfewestbytesleCover! Keepsfragmentssmall—usuallyimprovesmemoryu7liza7on! Willtypicallyrunslowerthanfirstfit
p = start; while ((p < end) && \\ not passed end ((*p & 1) || \\ already allocated (*p <= len))) \\ too small p = p + (*p & -2); \\ goto next block (word addressed)
IMPLICIT LIST: ALLOCATING IN FREE BLOCK¢ Alloca&nginafreeblock:spli%ng
! Sinceallocatedspacemightbesmallerthanfreespace,wemightwanttosplittheblock
void addblock(ptr p, int len) { int newsize = ((len + 1) >> 1) << 1; // round up to even int oldsize = *p & -2; // mask out low bit *p = newsize | 1; // set new length if (newsize < oldsize) *(p+newsize) = oldsize - newsize; // set length in remaining } // part of block
4 4 26
4 24
p
24
addblock(p, 4)
IMPLICIT LIST: FREEING A BLOCK¢ Simplestimplementa,on:
! Needonlyclearthe“allocated”flag void free_block(ptr p) { *p = *p & -2 }
! Butcanleadto“falsefragmenta7on”
4 24 24
free(p) p
4 4 24 2
malloc(5) Oops!
There is enough free space, but the allocator won’t be able to find it
IMPLICIT LIST: COALESCING
¢ Join(coalesce)withnext/previousblocks,iftheyarefree! Coalescingwithnextblock
! Buthowdowecoalescewithpreviousblock?
void free_block(ptr p) { *p = *p & -2; // clear allocated flag next = p + *p; // find next block if ((*next & 1) == 0) *p = *p + *next; // add to this block if } // not allocated
4 24 2
free(p) p
4 4 2
4
6 2
logicallygone
IMPLICIT LIST: BIDIRECTIONAL COALESCING ¢ Boundarytags[Knuth73]
! Replicatesize/allocatedwordat“bo;om”(end)offreeblocks! Allowsustotraversethe“list”backwards,butrequiresextraspace! Importantandgeneraltechnique!
Size
Formatofallocatedandfreeblocks
Payloadandpadding
a=1:Allocatedblocka=0:FreeblockSize:TotalblocksizePayload:Applica<ondata(allocatedblocksonly)
a
Size aBoundarytag(footer)
4 4 4 4 6 46 4
Header
CONSTANT TIME COALESCING
Allocated
Allocated
Allocated
Free
Free
Allocated
Free
Free
Blockbeingfreed
Case1 Case2 Case3 Case4
CONSTANT TIME COALESCING (CASE 1)
m1 1
m1 1
n 1
n 1
m2 1
m2 1
m1 1
m1 1
n 0
n 0
m2 1
m2 1
CONSTANT TIME COALESCING (CASE 2)
m1 1
m1 1
n 1
n 1
m2 0
m2 0
m1 1
m1 1
n+m2 0
n+m2 0
CONSTANT TIME COALESCING (CASE 3)
m1 0
m1 0
n 1
n 1
m2 1
m2 1
n+m1 0
n+m1 0
m2 1
m2 1
CONSTANT TIME COALESCING (CASE 4)
m1 0
m1 0
n 1
n 1
m2 0
m2 0
n+m1+m2 0
n+m1+m2 0
DISADVANTAGES OF BOUNDARY TAGS
¢ Internalfragmenta,on
¢ Canitbeop,mized?! Whichblocksneedthefootertag?
! Whatdoesthatmean?
SUMMARY
SUMMARY OF KEY ALLOCATOR POLICIES
¢ Placementpolicy:! First-fit,next-fit,best-fit,etc.! Tradesofflowerthroughputforlessfragmenta=on ! Interes'ngobserva'on:segregatedfreelists(nextlecture)
approximateabestfitplacementpolicywithouthavingtosearchen=refreelist
¢ Spli0ngpolicy:! Whendowegoaheadandsplitfreeblocks?! Howmuchinternalfragmenta=onarewewillingtotolerate?
¢ Coalescingpolicy:! Immediatecoalescing:coalesceeach=mefreeiscalled! Deferredcoalescing:trytoimproveperformanceoffreebydeferring
coalescingun=lneeded.Examples:! Coalesceasyouscanthefreelistformalloc! Coalescewhentheamountofexternalfragmenta=onreachessomethreshold
IMPLICIT LISTS: SUMMARY
¢ Implementa)on:verysimple¢ Allocatecost:
! linear(meworstcase
¢ Freecost:! constant(meworstcase! evenwithcoalescing
¢ Memoryusage:! willdependonplacementpolicy! First-fit,next-fitorbest-fit
¢ Notusedinprac)ceformalloc/free becauseoflinear-)mealloca)on! usedinmanyspecialpurposeapplica(ons
¢ However,theconceptsofspliAngandboundarytagcoalescingaregeneraltoallallocators