Dynamic Memory Allocation in the Heapcs240/f16/slides/allocator.pdf · Dynamic Memory Allocation in...

31
Dynamic Memory Allocation in the Heap (malloc and free) Now: Explicit allocators (a.k.a. manual memory management) Later: implicit allocators (a.k.a. automatic memory management)

Transcript of Dynamic Memory Allocation in the Heapcs240/f16/slides/allocator.pdf · Dynamic Memory Allocation in...

DynamicMemoryAllocationintheHeap(malloc andfree)

Now:Explicitallocators(a.k.a.manualmemorymanagement)

Later:implicitallocators(a.k.a.automaticmemorymanagement)

Addr Perm Contents Managedby Initialized

2N-1 Stack RW Procedurecontext Compiler Run-time

Heap RW Dynamicdata structures

Programmer,malloc/free,new/GC

Run-time

Statics RW Globalvariables/staticdatastructures

Compiler/Assembler/Linker Startup

Literals R Stringliterals Compiler/Assembler/Linker Startup

Text X Instructions Compiler/Assembler/Linker Startup

0

HeapAllocation

AllocatorBasicsPagestoocoarse-grainedforallocatingindividualobjects.Instead:flexible-sized,word-alignedblocks.

void* malloc(size_t size);

void free(void* ptr);

3

numberofcontiguousbytesrequiredpointertonewlyallocatedblockofatleastthatsize

pointertoallocatedblocktofree

Allocatedblock(4words)

Freeblock(3words)

Freeword

Allocatedword

Example (64-bitwords)

4

p1 = malloc(32);

p2 = malloc(40);

p3 = malloc(48);

free(p2);

p4 = malloc(16);

AllocatorGoals:malloc/free1.Programmerdoesnotdecidelocationsofdistinctobjects.

Programmerdecides:whatsize,whenneeded,whennolongerneeded

2.Fastallocation.mallocs/secondorbytesmalloc'd/second

3.Highmemoryutilization.Mostofheapcontainsnecessaryprogramdata.Littlewastedspace.

Enemy:fragmentation – unusedmemorythatcannotbeallocated.

InternalFragmentationpayloadsmallerthanblock

Causesmetadataalignmentpolicydecisions

6

payload

block

Internalfragmentation

ExternalFragmentation (64-bitwords)

Totalfreespacelargeenough,butnocontiguousfreeblocklargeenough

Dependsonthepatternoffuturerequests.

7

p1 = malloc(32);

p2 = malloc(40);

p3 = malloc(48);

free(p2);

p4 = malloc(48);

ImplementationIssues1. Determinehowmuchtofree givenjustapointer.

2. Keeptrackoffreeblocks.

3. Pickablocktoallocate.

4. Choosewhatdowithextraspacewhenallocating astructurethatissmallerthanthefreeblockused.

5. Makeafreedblockavailableforfuturereuse.

8

KnowingHowMuchtoFreeKeeplengthofblockinheader wordprecedingblock

9

free(p0);

p0 = malloc(32);

p0

blocksizemetadata datapayload

48

Takesextraspace!

KeepingTrackofFreeBlocks

Method1:Implicitlist ofallblocksusinglength

Method2:Explicitlist offreeblocksusingpointers

Method3:SeglistDifferentfreelistsfordifferentsizeblocks

Moremethodsthatwewillskip…

10

40 32 1648

40 32 1648

ImplicitFreeList:BlockFormat

11

blocksize

1word

payload(applicationdata,whenallocated)

a

optionalpadding

16-bytealignedsizeshave4zeroesinlow-orderbits00000000000100000010000000110000…

StealLSBforstatusflag.LSB=1:allocatedLSB=0:free

Blockmetadata:1. Blocksize2. Allocationstatus

Storeinoneheaderword.

ImplicitFreeList:HeapLayout

12

16|0 32|1 64|0 32|1 0|1

Freeword

Allocatedword

Allocatedwordwasted

Startofheap

Payloadsstartat16-byte(2-word)alignment.Blockssizesaremultiplesof16bytes.

BlockHeader(metadata)blocksize|blockallocated?

Specialend-heapwordLookslikeheaderofzero-sizeallocateblock.

Initialwordcan'tbepartofblock.

Mayforceinternalfragmentation.

ImplicitFreeList: FindingaFreeBlockFirstfit:

Searchlistfrombeginning,choosefirst freeblockthatfits

Nextfit:Dofirst-fitstartingwhereprevioussearchfinished

Bestfit:Searchthelist,choosethebest freeblock:fits,withfewestbytesleftover

13

16

ImplicitFreeList: AllocatingaFreeBlock

14

16 1648

16 1632

p = malloc(24);

p

Nowshowingallocationstatusflagimplicitlywithshading.

Block Splitting

Allocatedspace≤ freespace.Useitall?Splititup?

ImplicitFreeList: FreeingaBlock

15

16 1632 16

p

malloc(40);

16 1632 16

Externalfragmentation!Enoughspace,notoneblock.

Clearallocated flag.free(p);

CoalescingFreeBlocks

16

32 1632 16

free(p)

32 1648 16

logicallygone

p

Coalesce withfollowingfree block.

Coalesce withpreceding free block?

BidirectionalCoalescing:BoundaryTags

17

Boundarytag(footer)

32 32 32 32 48 3248 32

Header blocksize

payload(applicationdata,whenallocated)

a

optionalpadding

blocksize a

[Knuth73]

Constant-TimeCoalescing:4cases

19

m1 1

m1 1n 1

FreedBlockn 1m2 1

m2 1

m1 1

m1 1n 0

n 0m2 1

m2 1

m1 1

m1 1n+m2 0

n+m2 0

m1 1

m1 1n 1

FreedBlockn 1m2 0

m2 0

m1 0

m1 0n 1

FreedBlockn 1m2 1

m2 1

n+m1 0

n+m1 0m2 1

m2 1

m1 0

m1 0n 1

FreedBlockn 1m2 0

m2 0

n+m1+m2 0

n+m1+m2 0

Summary:ImplicitFreeListsImplementation: simple

Allocate: O(blocksinheap)Free: O(1)

Memoryutilization: dependsonplacementpolicy

Notwidelyusedinpracticesomespecialpurposeapplications

Splitting,boundarytags,coalescingaregeneraltoall allocators.

20

ExplicitFreeLists

Explicitlistoffree blocks ratherthan implicitlistofall blocks.

21

Freeblock:Allocatedblock:

(sameasimplicitfreelist)

blocksize

payload(applicationdata,whenallocated)

a

optionalpadding

blocksize a

blocksize a

next pointer

prev pointer

blocksize a

ExplicitFreeLists:Listvs.MemoryOrderAbstractly: doubly-linkedlists

Concretely: freelistblocksinanymemory order

22

A B C

32 32 32 32 4848 3232 32 32

Next

Previous

A B

C

Previous

Next

ListOrder≠ MemoryOrder

ExplicitFreeLists:AllocatingaFreeBlock

23

Before

After

= malloc(…)

(withsplitting)

ExplicitFreeLists:FreeingaBlockInsertionpolicy:Whereinthefreelistdoyouaddafreedblock?

LIFO(last-in-first-out) policyPro: simpleandconstanttimeCon: studiessuggestfragmentationisworsethanaddressordered

Address-ordered policyCon: linear-timesearchtoinsertfreedblocksPro: studiessuggestfragmentationislowerthanLIFO

LIFOExample:4casesoffreedblockneighbor status.

25

FreeingwithLIFOPolicy:betweenallocatedblocks

Insertthefreedblockatheadoffreelist.

26

free( )

Head

Head

Before

After

FreeingwithLIFOPolicy:betweenfreeandallocated

Spliceoutpredecessorblock,coalescebothmemoryblocks,andinsertthenewblockattheheadofthefreelist.

27

free( )

Head

Head

Before

After

Couldbeoneitherorbothsides...

FreeingwithLIFOPolicy:betweenallocatedandfree

Spliceoutsuccessorblock,coalescebothmemoryblocksandinsertthenewblockattheheadofthefreelist.

28

free( )

Head

Head

Before

After

FreeingwithLIFOPolicy:betweenfreeblocks

Spliceoutpredecessorandsuccessorblocks,coalesceall3memoryblocksandinsertthenewblockattheheadofthelist.

29

free( )

Head

Head

Before

After

Summary:ExplicitFreeListsImplementation: fairlysimple

Allocate: O(free blocks) vs.O(all blocks)Free: O(1) vs.O(1)

Memoryutilization:dependsonplacementpolicylargerminimumblocksize(next/prev)vs.implicitlist

Usedwidelyinpractice,oftenwithmoreoptimizations.

Splitting,boundarytags,coalescing aregeneraltoall allocators.

36

Seglist AllocatorsEachsizebrackethasitsownfreelist

Fasterbest-fitallocation...

38

32

48-64

80-inf

16

Summary: AllocatorPoliciesAllpoliciesoffertrade-offsinfragmentationandthroughput.

Placementpolicy:First-fit,next-fit,best-fit,etc.Seglists approximatebest-fitinlowtime

Splittingpolicy:Always?Sometimes?Sizebound?

Coalescingpolicy:Immediatevs.deferred

41

Remembrallocator BlockFormat

42

Freeblock:Allocatedblock:blocksize

payload

1p blocksize a

next pointer

prev pointer

blocksize

0p

payload

blocksize a

next pointer

prev pointer

blocksize

01

blocksize

payload

10

blocksize

payload

11Minimumblocksize?- Implicitfreelist- Explicitfreelist

Update2headersoneachmalloc/free.