DYNAMIC MEMORY ALLOCATION: BASIC CONCEPTSdjp3.westmont.edu/classes/2017_01_CS045/Lectures/... ·...

34
DYNAMIC MEMORY ALLOCATION: BASIC CONCEPTS CS 045 Computer Organization and Architecture Prof. Donald J. Patterson Adapted from Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition

Transcript of DYNAMIC MEMORY ALLOCATION: BASIC CONCEPTSdjp3.westmont.edu/classes/2017_01_CS045/Lectures/... ·...

Page 1: DYNAMIC MEMORY ALLOCATION: BASIC CONCEPTSdjp3.westmont.edu/classes/2017_01_CS045/Lectures/... · DYNAMIC MEMORY ALLOCATION ¢ Allocator maintains heap as collec0on of variable sized

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

Page 2: DYNAMIC MEMORY ALLOCATION: BASIC CONCEPTSdjp3.westmont.edu/classes/2017_01_CS045/Lectures/... · DYNAMIC MEMORY ALLOCATION ¢ Allocator maintains heap as collec0on of variable sized

• BASIC CONCEPTS

• IMPLICIT FREE LISTS

DYNAMIC MEMORY ALLOCATION: BASIC

Page 3: DYNAMIC MEMORY ALLOCATION: BASIC CONCEPTSdjp3.westmont.edu/classes/2017_01_CS045/Lectures/... · DYNAMIC MEMORY ALLOCATION ¢ Allocator maintains heap as collec0on of variable sized

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

Page 4: DYNAMIC MEMORY ALLOCATION: BASIC CONCEPTSdjp3.westmont.edu/classes/2017_01_CS045/Lectures/... · DYNAMIC MEMORY ALLOCATION ¢ Allocator maintains heap as collec0on of variable sized

DYNAMIC MEMORY ALLOCATION

¢  Allocatormaintainsheapascollec0onofvariablesizedblocks,whichareeitherallocatedorfree

¢  Typesofallocators!  Explicitallocator:applica&onallocatesandfreesspace

!  E.g.,mallocandfreeinC!  Implicitallocator:applica&onallocates,butdoesnotfreespace

!  E.g.garbagecollec&oninJava,ML,andLisp

¢  Willdiscusssimpleexplicitmemoryalloca0ontoday

Page 5: DYNAMIC MEMORY ALLOCATION: BASIC CONCEPTSdjp3.westmont.edu/classes/2017_01_CS045/Lectures/... · DYNAMIC MEMORY ALLOCATION ¢ Allocator maintains heap as collec0on of variable sized

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

Page 6: DYNAMIC MEMORY ALLOCATION: BASIC CONCEPTSdjp3.westmont.edu/classes/2017_01_CS045/Lectures/... · DYNAMIC MEMORY ALLOCATION ¢ Allocator maintains heap as collec0on of variable sized

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); !} !

Page 7: DYNAMIC MEMORY ALLOCATION: BASIC CONCEPTSdjp3.westmont.edu/classes/2017_01_CS045/Lectures/... · DYNAMIC MEMORY ALLOCATION ¢ Allocator maintains heap as collec0on of variable sized

LECTURE ASSUMPTIONS

¢  Memoryiswordaddressed.¢  Wordsareint-sized.

Allocatedblock(4words)

Freeblock(3words) Freeword

Allocatedword

Page 8: DYNAMIC MEMORY ALLOCATION: BASIC CONCEPTSdjp3.westmont.edu/classes/2017_01_CS045/Lectures/... · DYNAMIC MEMORY ALLOCATION ¢ Allocator maintains heap as collec0on of variable sized

ALLOCATION EXAMPLE

p1 = malloc(4)

p2 = malloc(5)

p3 = malloc(6)

free(p2)

p4 = malloc(2)

Page 9: DYNAMIC MEMORY ALLOCATION: BASIC CONCEPTSdjp3.westmont.edu/classes/2017_01_CS045/Lectures/... · DYNAMIC MEMORY ALLOCATION ¢ Allocator maintains heap as collec0on of variable sized

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

Page 10: DYNAMIC MEMORY ALLOCATION: BASIC CONCEPTSdjp3.westmont.edu/classes/2017_01_CS045/Lectures/... · DYNAMIC MEMORY ALLOCATION ¢ Allocator maintains heap as collec0on of variable sized

PERFORMANCE GOAL: THROUGHPUT¢  Givensomesequenceofmallocandfreerequests:

!  R0,R1,...,Rk,...,Rn-1

¢  Goals:maximizethroughputandpeakmemoryu;liza;on!  Thesegoalsareo+enconflic0ng

¢  Throughput:!  Numberofcompletedrequestsperunit0me

!  Example:

!  5,000malloccallsand5,000freecallsin10seconds!  Throughputis1,000opera0ons/second

Page 11: DYNAMIC MEMORY ALLOCATION: BASIC CONCEPTSdjp3.westmont.edu/classes/2017_01_CS045/Lectures/... · DYNAMIC MEMORY ALLOCATION ¢ Allocator maintains heap as collec0on of variable sized

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

Page 12: DYNAMIC MEMORY ALLOCATION: BASIC CONCEPTSdjp3.westmont.edu/classes/2017_01_CS045/Lectures/... · DYNAMIC MEMORY ALLOCATION ¢ Allocator maintains heap as collec0on of variable sized

INTERNAL FRAGMENTATION

¢  Foragivenblock,internalfragmenta,onoccursifpayloadissmallerthanblocksize

¢  Causedby

!  Overheadofmaintainingheapdatastructures!  Paddingforalignmentpurposes!  Explicitpolicydecisions

(e.g.,toreturnabigblocktosa>sfyasmallrequest)

¢  Dependsonlyonthepa<ernofpreviousrequests!  Thus,easytomeasure

PayloadInternalfragmenta@on

Block

Internalfragmenta@on

Page 13: DYNAMIC MEMORY ALLOCATION: BASIC CONCEPTSdjp3.westmont.edu/classes/2017_01_CS045/Lectures/... · DYNAMIC MEMORY ALLOCATION ¢ Allocator maintains heap as collec0on of variable sized

EXTERNAL FRAGMENTATION

¢  Occurswhenthereisenoughaggregateheapmemory,butnosinglefreeblockislargeenough

¢  Dependsonthepa:ernoffuturerequests!  Thus,difficulttomeasure

p1 = malloc(4)

p2 = malloc(5)

p3 = malloc(6)

free(p2)

p4 = malloc(6) Oops!(whatwouldhappennow?)

Page 14: DYNAMIC MEMORY ALLOCATION: BASIC CONCEPTSdjp3.westmont.edu/classes/2017_01_CS045/Lectures/... · DYNAMIC MEMORY ALLOCATION ¢ Allocator maintains heap as collec0on of variable sized

IMPLEMENTATION ISSUES

¢  Howdoweknowhowmuchmemorytofreegivenjustapointer?

¢  Howdowekeeptrackofthefreeblocks?

¢  Whatdowedowiththeextraspacewhenalloca=ngastructurethatissmallerthanthefreeblockitisplacedin?

¢  Howdowepickablocktouseforalloca=on--manymightfit?

¢  Howdowereinsertfreedblock?

Page 15: DYNAMIC MEMORY ALLOCATION: BASIC CONCEPTSdjp3.westmont.edu/classes/2017_01_CS045/Lectures/... · DYNAMIC MEMORY ALLOCATION ¢ Allocator maintains heap as collec0on of variable sized

KNOWING HOW MUCH TO FREE¢  Standardmethod

!  Keepthelengthofablockinthewordprecedingtheblock.!  Thiswordiso7encalledtheheaderfieldorheader

!  Requiresanextrawordforeveryallocatedblock

p0 = malloc(4)

p0

free(p0)

blocksize payload

5

Page 16: DYNAMIC MEMORY ALLOCATION: BASIC CONCEPTSdjp3.westmont.edu/classes/2017_01_CS045/Lectures/... · DYNAMIC MEMORY ALLOCATION ¢ Allocator maintains heap as collec0on of variable sized

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

Page 17: DYNAMIC MEMORY ALLOCATION: BASIC CONCEPTSdjp3.westmont.edu/classes/2017_01_CS045/Lectures/... · DYNAMIC MEMORY ALLOCATION ¢ Allocator maintains heap as collec0on of variable sized

• BASIC CONCEPTS

• IMPLICIT FREE LISTS

DYNAMIC MEMORY ALLOCATION: BASIC

Page 18: DYNAMIC MEMORY ALLOCATION: BASIC CONCEPTSdjp3.westmont.edu/classes/2017_01_CS045/Lectures/... · DYNAMIC MEMORY ALLOCATION ¢ Allocator maintains heap as collec0on of variable sized

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

Page 19: DYNAMIC MEMORY ALLOCATION: BASIC CONCEPTSdjp3.westmont.edu/classes/2017_01_CS045/Lectures/... · DYNAMIC MEMORY ALLOCATION ¢ Allocator maintains heap as collec0on of variable sized

DETAILED IMPLICIT FREE LIST EXAMPLE

Startof

heap

Double-wordaligned

8/0 16/1 16/132/0

Unused

0/1

Allocatedblocks:shadedFreeblocks:unshadedHeaders:labeledwithsizeinbytes/allocatedbit

Page 20: DYNAMIC MEMORY ALLOCATION: BASIC CONCEPTSdjp3.westmont.edu/classes/2017_01_CS045/Lectures/... · DYNAMIC MEMORY ALLOCATION ¢ Allocator maintains heap as collec0on of variable sized

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)

Page 21: DYNAMIC MEMORY ALLOCATION: BASIC CONCEPTSdjp3.westmont.edu/classes/2017_01_CS045/Lectures/... · DYNAMIC MEMORY ALLOCATION ¢ Allocator maintains heap as collec0on of variable sized

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)

Page 22: DYNAMIC MEMORY ALLOCATION: BASIC CONCEPTSdjp3.westmont.edu/classes/2017_01_CS045/Lectures/... · DYNAMIC MEMORY ALLOCATION ¢ Allocator maintains heap as collec0on of variable sized

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

Page 23: DYNAMIC MEMORY ALLOCATION: BASIC CONCEPTSdjp3.westmont.edu/classes/2017_01_CS045/Lectures/... · DYNAMIC MEMORY ALLOCATION ¢ Allocator maintains heap as collec0on of variable sized

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

Page 24: DYNAMIC MEMORY ALLOCATION: BASIC CONCEPTSdjp3.westmont.edu/classes/2017_01_CS045/Lectures/... · DYNAMIC MEMORY ALLOCATION ¢ Allocator maintains heap as collec0on of variable sized

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

Page 25: DYNAMIC MEMORY ALLOCATION: BASIC CONCEPTSdjp3.westmont.edu/classes/2017_01_CS045/Lectures/... · DYNAMIC MEMORY ALLOCATION ¢ Allocator maintains heap as collec0on of variable sized

CONSTANT TIME COALESCING

Allocated

Allocated

Allocated

Free

Free

Allocated

Free

Free

Blockbeingfreed

Case1 Case2 Case3 Case4

Page 26: DYNAMIC MEMORY ALLOCATION: BASIC CONCEPTSdjp3.westmont.edu/classes/2017_01_CS045/Lectures/... · DYNAMIC MEMORY ALLOCATION ¢ Allocator maintains heap as collec0on of variable sized

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

Page 27: DYNAMIC MEMORY ALLOCATION: BASIC CONCEPTSdjp3.westmont.edu/classes/2017_01_CS045/Lectures/... · DYNAMIC MEMORY ALLOCATION ¢ Allocator maintains heap as collec0on of variable sized

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

Page 28: DYNAMIC MEMORY ALLOCATION: BASIC CONCEPTSdjp3.westmont.edu/classes/2017_01_CS045/Lectures/... · DYNAMIC MEMORY ALLOCATION ¢ Allocator maintains heap as collec0on of variable sized

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

Page 29: DYNAMIC MEMORY ALLOCATION: BASIC CONCEPTSdjp3.westmont.edu/classes/2017_01_CS045/Lectures/... · DYNAMIC MEMORY ALLOCATION ¢ Allocator maintains heap as collec0on of variable sized

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

Page 30: DYNAMIC MEMORY ALLOCATION: BASIC CONCEPTSdjp3.westmont.edu/classes/2017_01_CS045/Lectures/... · DYNAMIC MEMORY ALLOCATION ¢ Allocator maintains heap as collec0on of variable sized

DISADVANTAGES OF BOUNDARY TAGS

¢  Internalfragmenta,on

¢  Canitbeop,mized?!  Whichblocksneedthefootertag?

!  Whatdoesthatmean?

Page 31: DYNAMIC MEMORY ALLOCATION: BASIC CONCEPTSdjp3.westmont.edu/classes/2017_01_CS045/Lectures/... · DYNAMIC MEMORY ALLOCATION ¢ Allocator maintains heap as collec0on of variable sized

SUMMARY

Page 32: DYNAMIC MEMORY ALLOCATION: BASIC CONCEPTSdjp3.westmont.edu/classes/2017_01_CS045/Lectures/... · DYNAMIC MEMORY ALLOCATION ¢ Allocator maintains heap as collec0on of variable sized

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

Page 33: DYNAMIC MEMORY ALLOCATION: BASIC CONCEPTSdjp3.westmont.edu/classes/2017_01_CS045/Lectures/... · DYNAMIC MEMORY ALLOCATION ¢ Allocator maintains heap as collec0on of variable sized

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

Page 34: DYNAMIC MEMORY ALLOCATION: BASIC CONCEPTSdjp3.westmont.edu/classes/2017_01_CS045/Lectures/... · DYNAMIC MEMORY ALLOCATION ¢ Allocator maintains heap as collec0on of variable sized