compilation - cs.tau.ac.il · translation into c 4. compiling simple classes §fields are handled...
TRANSCRIPT
Compilation0368-3133Lecture12
CompilingObject-OrientedPrograms
NoamRinetzky
1
ObjectOrientedPrograms
§ C++,Java,C#,Python,…
§ Mainabstraction:Objects (usuallyoftypecalledclass)§ Code§ Data
§ NaturallysupportsAbstractDataTypeimplementations§ Informationhiding§ Evolution&reusability
§ Importantcharacteristic:Extension/Inheritance 2
ASimpleExample
3
Object
Vehicleint posmove(..)
Carint passengers
move(..)
Truckawait(..)
TranslationintoC
4
CompilingSimpleClasses
§ Fieldsarehandledasrecords§ Methodshaveuniquenames
5
classA{fielda1;fielda2;methodm1(){…}methodm2(int i){…}
}
a1a2
Runtimeobject
m1Am2A
Compile-TimeTable
voidm2A(classA *this,int i){//Bodyofm2withanyobject//object-fielda1asthisà a1…
}
CompilingSimpleClasses
§ Fieldsarehandledasrecords§ Methodshaveuniquenames
6
classA{fielda1;fielda2;methodm1(){…}methodm2(int i){…}
}
a1a2
Runtimeobject
m1Am2A
Compile-TimeTable
voidm2A(classA *this,int i){//Bodyofm2withany//object-fielda1asthisà a1…
}a.m2(5)
m2A(a,5)
FeaturesofOOlanguages
§ Inheritance§ Subclass gets(inherits)propertiesofsuperclass
§ Methodoverloading§ Multiplemethodswiththesamenameanddifferentsignatures(insameclass)
§ Abstract (akavirtual)methods§ Methodoverriding§ Multiplemethodswiththesamename&signaturebutwithdifferentimplementations(acrossclasses)
§ Dynamicdispatch§ Lookupmethodsby(runtime)typeoftargetobject
7
CompilingOOlanguages
§ “TranslationintoC”§ Powerfulruntimeenvironment
§ Adding“gluing”code
8
RuntimeEnvironment
§ MediatesbetweentheOSandtheprogramminglanguage
§ Hidesdetailsofthemachinefromtheprogrammer§ Rangesfromsimplesupportfunctionsallthewaytoafull-fledgedvirtualmachine
§ Handlescommontasks§ Runtimestack(activationrecords)§ Memorymanagement
§ Runtimetypeinformation§ Methodinvocation§ Typeconversions
9
MemoryLayout
10
stackgrowsdown(towardsloweraddresses)
heapgrowsup(towardshigher
addresses)
Heap
stack
code
staticdata
MemoryLayout
11
stackgrowsdown(towardsloweraddresses)
heapgrowsup(towardshigher
addresses)
Heap
stack
code
staticdata
Runtimetypeinformation
HandlingSingleInheritance
§ Simpletypeextension
12
class A {field a1;field a2;method m1() {…}method m2() {…}
}
class B extends A {field b1;method m3() {…}
}
AddingfieldsFieldsakaDatamembers,instancevariables
§ Addsmoreinformationtotheinheritedclass§ “Prefixing”fieldsensuresconsistency
13
class A {field a1;field a2;method m1() {…}method m2() {…}
}
class B extends A {field b1;method m2() {…}method m3() {…}
}
typedef struct {field a1;field a2;
} A;
void m1A_A(A* this){…}void m2A_A(A* this){…}
typedef struct {field a1;field a2;field b1;
} B;
void m2A_B(B* this) {…}void m3B_B(B* this) {…}
MethodOverriding
§ Redefinesfunctionality§ Morespecific§ Canaccessadditionalfields
14
class A {field a1;field a2;method m1() {…}method m2() {…}
}
class B extends A {field b1;method m2() {
… b1 …}method m3() {…}
}
MethodOverriding
§ Redefinesfunctionality§ Morespecific§ Canaccessadditionalfields
15
class A {field a1;field a2;method m1() {…}method m2() {…}
}
class B extends A {field b1;method m2() {
… b1 …}method m3() {…}
}m2isdeclaredanddefined
m2isredefined
MethodOverriding
§ Redefinesfunctionality§ Affectssemanticanalysis
16
a1a2
Runtimeobject
m1A_Am2A_A
Compile-TimeTable a1a2
Runtimeobject
b1
m1A_Am2A_B
Compile-TimeTable
m3B_B
class A {field a1;field a2;method m1() {…}method m2() {…}
}
class B extends A {field b1;method m2() {
… b1 …}method m3() {…}
}
MethodOverriding
§ Redefinesfunctionality§ Affectssemanticanalysis
17
a1a2
Runtimeobject
m1A_Am2A_A
Compile-TimeTable a1a2
Runtimeobject
b1
m1A_Am2A_B
Compile-TimeTable
m3B_B
class A {field a1;field a2;method m1() {…}method m2() {…}
}
class B extends A {field b1;method m2() {
… b1 …}method m3() {…}
}
declared defined
18
MethodOverriding
a1a2
Runtimeobject
m1A_Am2A_A
Compile-TimeTable a1a2
Runtimeobject
b1
m1A_Am2A_B
Compile-TimeTable
m3B_B
class A {field a1;field a2;method m1() {…}method m2() {…}
}
class B extends A {field b1;method m2() {
… b1 …}method m3() {…}
}
a.m2(5) // class(a) = A
m2A_A(a, 5)
b.m2(5) // class(b) = B
m2A_B(b, 5)
19
MethodOverriding
a1a2
Runtimeobject
m1A_Am2A_A
Compile-TimeTable a1a2
Runtimeobject
b1
m1A_Am2A_B
Compile-TimeTable
m3B_B
class A {field a1;field a2;method m1() {…}method m2() {…}
}
class B extends A {field b1;method m2() { … b1 …
}method m3() {…}
}typedef struct {
field a1;field a2;
} A;
void m1A_A(A* this){…}void m2A_A(A* this){…}
typedef struct {field a1;field a2;field b1;
} B;
void m2A_B(B* this) {…}void m3B_B(B* this) {…}
20
MethodOverriding
a1a2
Runtimeobject
m1A_Am2A_A
Compile-TimeTable a1a2
Runtimeobject
b1
m1A_Am2A_B
Compile-TimeTable
m3B_B
a.m2(5)//class(a)=A
m2A_A(a,5)
b.m2(5)//class(b)=B
m2A_B(b,5)
typedef struct {field a1;field a2;
} A;
void m1A_A(A* this){…}void m2A_A(A* this){…}
typedef struct {field a1;field a2;field b1;
} B;
void m2A_B(B* this) {…}void m3B_B(B* this) {…}
Abstractmethods&classes
§ Abstractmethods§ Declaredseparately,definedinchildclasses§ E.g.,C++purevirtualmethods,abstractmethodsinJava
§ Abstractclasses=classmayhaveabstractmethods§ E.G.,Java/C++abstractclasses§ Abstractclassescannotbeinstantiated
§ Abstractaka“virtual”
§ Inheritingabstractclasshandledlikeregularinheritance§ Compilerchecksabstractclassesarenotallocated 21
HandlingPolymorphism
§ WhenaclassBextendsaclassA§ variableoftypepointertoAmayactuallyrefertoobjectoftypeB
§ Upcasting fromasubclasstoasuperclass§ Prefixingfieldsguaranteesvalidity
22
classB*b=…;
classA*a=b;
a1a2b1
PointertoBPointertoAinsideB
(also)
classA *a=convert_ptr_to_B_to_ptr_A(b);
AB
DynamicBinding
§ Anobject(“pointer”)odeclaredtobeofclassAcanactuallybe(“refer”)toaclassB
§ Whatdoes‘o.m()’mean?§ Staticbinding§ Dynamicbinding
§ Dependsontheprogramminglanguagerules
§ Howtoimplementdynamicbinding?§ Theinvokedfunctionisnotknownatcompiletime§ NeedtooperateondataoftheBandAinconsistentway
23
ConceptualImpl.ofDynamicBinding
24
a1a2
Runtimeobject
m1A_Am2A_A
Compile-TimeTable a1a2
Runtimeobject
b1
m1A_Am2A_B
Compile-TimeTable
m3B_B
class A {field a1;field a2;method m1() {…}method m2() {…}
}
class B extends A {field b1;method m2() { … a3 …
}method m3() {…}
}typedef struct {
field a1;field a2;
} A;
void m1A_A(A* this){…}void m2A_A(A* this){…}
typedef struct {field a1;field a2;field b1;
} B;
void m2A_B(B* this) {…}void m3B_B(B* this) {…}
ConceptualImpl.ofDynamicBinding
25
a1a2
Runtimeobject
m1A_Am2A_A
Compile-TimeTable a1a2
Runtimeobject
b1
m1A_Am2A_B
Compile-TimeTable
m3B_B
switch(dynamic_type(p)){caseDynamic_class_A:m2_A_A(p,3);caseDynamic_class_B:m2_A_B(convert_ptr_to_A_to_ptr_B(p),3);
}
typedef struct {field a1;field a2;
} A;
void m1A_A(A* this){…}void m2A_A(A* this){…}
typedef struct {field a1;field a2;field b1;
} B;
void m2A_B(B* this) {…}void m3B_B(B* this) {…}
”p.m2(3)”
ConceptualImpl.ofDynamicBinding
26
a1a2
Runtimeobject
m1A_Am2A_A
Compile-TimeTable a1a2
Runtimeobject
b1
m1A_Am2A_B
Compile-TimeTable
m3B_B
switch(dynamic_type(p)){caseDynamic_class_A:m2_A_A(p,3);caseDynamic_class_B:m2_A_B(convert_ptr_to_A_to_ptr_B(p),3);
}
?
typedef struct {field a1;field a2;
} A;
void m1A_A(A* this){…}void m2A_A(A* this){…}
typedef struct {field a1;field a2;field b1;
} B;
void m2A_B(B* this) {…}void m3B_B(B* this) {…}
”p.m2(3)”
Moreefficientimplementation
§ Applypointerconversioninsublasses§ Usedispatchtabletoinvokefunctions§ Similartotableimplementationofcase
27
voidm2A_B(classA *this_A){Class_B *this=convert_ptr_to_A_ptr_to_A_B(this_A);…
}
Moreefficientimplementation
28
typedef struct {field a1;field a2;
} A;
void m1A_A(A* this){…}void m2A_A(A* this, int x){…}
typedef struct {field a1;field a2;field b1;
} B;
void m2A_B(A* thisA, int x){Class_B *this =
convert_ptr_to_A_to_ptr_to_B(thisA);…
}
void m3B_B(B* this){…}
Moreefficientimplementation
29
typedef struct {field a1;field a2;
} A;
void m1A_A(A* this){…}void m2A_A(A* this, int x){…}
typedef struct {field a1;field a2;field b1;
} B;
void m2A_B(A* thisA, int x){Class_B *this =
convert_ptr_to_A_to_ptr_to_B(thisA);…
}
void m3B_B(B* this){…}
a1
a2
Runtimeobjectm1A_A
m2A_A
(Runtime)DispatchTable
vtablep
classA *p;
Moreefficientimplementation
30
typedef struct {field a1;field a2;
} A;
void m1A_A(A* this){…}void m2A_A(A* this, int x){…}
typedef struct {field a1;field a2;field b1;
} B;
void m2A_B(A* thisA, int x){Class_B *this =
convert_ptr_to_A_to_ptr_to_B(thisA);…
}
void m3B_B(B* this){…}
a1
a2
Runtimeobjectm1A
m2A
(Runtime)DispatchTable
vtablep
classA *p;
m1A_A
m2A_A
Code
Moreefficientimplementation
31
typedef struct {field a1;field a2;
} A;
void m1A_A(A* this){…}void m2A_A(A* this, int x){…}
typedef struct {field a1;field a2;field b1;
} B;
void m2A_B(A* thisA, int x){Class_B *this =
convert_ptr_to_A_to_ptr_to_B(thisA);…
}
void m3B_B(B* this){…}
p.m2(3); p®dispatch_table®m2A(p,3);
a1
a2
Runtimeobjectm1A_A
m2A_A
(Runtime)DispatchTable
vtablep
classA *p;
Moreefficientimplementation
32
typedef struct {field a1;field a2;
} A;
void m1A_A(A* this){…}void m2A_A(A* this, int x){…}
typedef struct {field a1;field a2;field b1;
} B;
void m2A_B(A* thisA, int x){Class_B *this =
convert_ptr_to_A_to_ptr_to_B(thisA);…
}
void m3B_B(B* this){…}
p.m2(3); p®dispatch_table®m2A(p,3);
a1
a2
Runtimeobject
b1
vtablep m1A_A
m2A_B
(Runtime)DispatchTable
m3B_B
Moreefficientimplementation
33
typedef struct {field a1;field a2;
} A;
void m1A_A(A* this){…}void m2A_A(A* this, int x){…}
typedef struct {field a1;field a2;field b1;
} B;
void m2A_B(A* thisA, int x){Class_B *this =
convert_ptr_to_A_to_ptr_to_B(thisA);…
}
void m3B_B(B* this){…}
p.m2(3); p®dispatch_table®m2A(,3);
a1
a2
Runtimeobject
b1
vtablep
convert_ptr_to_B_to_ptr_to_A(p)
m1A_A
m2A_B
(Runtime)DispatchTable
m3B_B
Allocatingobjects(runtime)
§ x=newA()
§ AllocatememoryforA’sfields+pointertovtable§ Initializevtable topointtoA’svtable§ initializeA’sfields(callconstructor)§ returnobject
34
Executinga.m2(5)
§ Fetchtheclassdescriptoratoffset0froma§ Fetchthemethod-instancepointerpfromtheconstantm2offsetofa
§ callm2(jumptoaddressp)
35
MultipleInheritance
36
class C {field c1;field c2;method m1(){…}method m2(){…}
}
class D {field d1;
method m3() {…}method m4(){…}
}
class E extends C, D {field e1;
method m2() {…} method m4() {…}method m5(){…}
}
MultipleInheritance
§ Allowsunifyingbehaviors§ Butraisessemanticdifficulties§ Ambiguityofclasses§ Repeatedinheritance
§ Hardtoimplement§ Semanticanalysis§ Codegeneration
§ Prefixingnolongerwork§ Needtogeneratecodefordowncasts
§ Hardtouse
37
Asimpleimplementation
§ Mergedispatchtablesofsuperclases§ Generatecodeforupcastsanddowncasts
38
Asimpleimplementation
39
class C {field c1;field c2;method m1(){…}method m2(){…}
}
class D {field d1;
method m3() {…}method m4(){…}
}
class E extends C, D {field e1;
method m2() {…} method m4() {…}method m5(){…}
}
d1
e1
Runtimeobject
m3D_D
m4D_E
(Runtime)DispatchTable
m5E_E
vtable
c1
c2
vtable
Pointerto- E- CinsideE
Pointerto- DinsideE
m1C_C
m2C_E
Downcasting(EàC,D)
40
class C {field c1;field c2;method m1(){…}method m2(){…}
}
class D {field d1;
method m3() {…}method m4(){…}
}
class E extends C, D {field e1;
method m2() {…} method m4() {…}method m5(){…}
}
d1
e1
Runtimeobject
vtable
c1
c2
vtable
Pointerto- E- CinsideE
Pointerto- DinsideE
convert_ptr_to_E_to_ptr_to_C(e)=e;
convert_ptr_to_E_to_ptr_to_D(e)=e+sizeof(C);
m3D_D
m4D_E
(Runtime)DispatchTable
m5E_E
m1C_C
m2C_E
Upcasting(C,DàE)
41
class C {field c1;field c2;method m1(){…}method m2(){…}
}
class D {field d1;
method m3() {…}method m4(){…}
}
class E extends C, D {field e1;
method m2() {…} method m4() {…}method m5(){…}
}
d1
e1
Runtimeobject
vtable
c1
c2
vtable
Pointerto- E- CinsideE
Pointerto- DinsideE
convert_ptr_to_C_to_ptr_to_E(c)=c;
convert_ptr_to_D_to_ptr_to_E(d)=d- sizeof(C);
m3D_D
m4D_E
(Runtime)DispatchTable
m5E_E
m1C_C
m2C_E
IndependentmultipleInheritance
42
class C extends A {field c1;field c2;method m1(){…}method m2(){…}
}
class D extends A {field d1;
method m3(){…}method m4(){…}
}
class E extends C, D {field e1;
method m2() {…} method m4() {…}method m5(){…}
}
class A{field a1;field a2;method m1(){…}method m3(){…}
}
class A{field a1;field a2;method m1(){…}method m3(){…}
}
IndependentmultipleInheritance
43
class C extends A {field c1;field c2;method m1(){…}method m2(){…}
}
class D extends A {field d1;
method m3(){…}method m4(){…}
}
class E extends C, D {field e1;
method m2() {…} method m4() {…}method m5(){…}
}
class A{field a1;field a2;method m1(){…}method m3(){…}
}
class A{field a1;field a2;method m1(){…}method m3(){…}
}
IndependentmultipleInheritance
44
class C extends A {field c1;field c2;method m1(){…}method m2(){…}
}
class D extends A {field d1;
method m3(){…}method m4(){…}
}
class E extends C, D {field e1;
method m2() {…} method m4() {…}method m5(){…}
}
class A{field a1;field a2;method m1(){…}method m3(){…}
}
class A{field a1;field a2;method m1(){…}method m3(){…}
}
IndependentmultipleInheritance
45
class C extends A {field c1;field c2;method m1(){…}method m2(){…}
}
class D extends A {field d1;
method m3(){…}method m4(){…}
}
class E extends C, D {field e1;method m3(){…} //altexplicitqualificationmethod m2() {…} method m4() {…}method m5(){…}
}
class A{field a1;field a2;method m1(){…}method m3(){…}
}
class A{field a1;field a2;method m1(){…}method m3(){…}
}
class A{field a1;field a2;method m1(){…}method m3(){…}
}
IndependentInheritance
46
class C extends A{
field c1;field c2;method m1(){…}method m2(){…}
}
class D extends A{
field d1;
method m3(){…}method m4(){…}
}
class E extends C,D{
field e1;
method m2() {…} method m4() {…}method m5(){…}
}
e1
RuntimeEobject
m2C_E
m1A_A
(Runtime)DispatchTable
m3A_D
d1
Pointerto- E- CinsideE
Pointerto- DinsideE
m1A_C
m3A_A
a2
a2
c1
a1
vtable
c2
a1 m4D_E
m5E_E
vtable
Dependentmultipleinheritance
§ Superclasses sharetheirownsuperclass
§ Thesimplesolutiondoesnotwork§ Thepositionsofnestedfieldsdonotagree
47
DependentmultipleInheritance
48
class C extends A {field c1;field c2;method m1(){…}method m2(){…}
}
class D extends A {field d1;
method m3(){…}method m4(){…}
}
class E extends C, D {field e1;
method m2() {…} method m4() {…}method m5(){…}
}
class A{field a1;field a2;method m1(){…}method m3(){…}
}
DependentmultipleInheritance
49
class C extends A {field c1;field c2;method m1(){…}method m2(){…}
}
class D extends A {field d1;
method m3(){…}method m4(){…}
}
class E extends C, D {field e1;
method m2() {…} method m4() {…}method m5(){…}
}
class A{field a1;field a2;method m1(){…}method m3(){…}
}
DependentmultipleInheritance
50
class C extends A {field c1;field c2;method m1(){…}method m2(){…}
}
class D extends A {field d1;
method m3(){…}method m4(){…}
}
class E extends C, D {field e1;
method m2() {…} method m4() {…}method m5(){…}
}
class A{field a1;field a2;method m1(){…}method m3(){…}
}
DependentInheritance
§ Superclasses sharetheirownsuperclass
§ Thesimplesolutiondoesnotwork§ Thepositionsofnestedfieldsdonotagree
51
Implementation
§ Useanindextabletoaccessfields§ Accessoffsetsindirectly
52
Implementationclass C
extends A{field c1;field c2;method m1(){…}method m2(){…}
}
class D extends A{
field d1;
method m3(){…}method m4(){…}
}
class E extends C,D{
field e1;
method m2() {…} method m4() {…}method m5(){…}
}
class A{field a1;field a2;method m1(){…}method m3(){…}
}
53
d1
e1
RuntimeEobject
m2C_E
m1A_A
(Runtime)DispatchTable
m3A_D
Indextab
Pointerto- E- CinsideE
Pointerto- DinsideE
m1A_C
m3A_A
vtable
a1
a2
Indextab
vtable
c1
c2
2 3 4 5 8 9 -4 -3 2
m4D_E
m5E_EIndextables
ClassDescriptors
§ Runtimeinformationassociatedwithinstances§ Dispatchtables§ Invokedmethods
§ Indextables§ Sharedbetweeninstancesofthesameclass
§ Canhavemore(reflection)
54
InterfaceTypes
§ Javasupportslimitedformofmultipleinheritance§ Interfaceconsistsofseveralmethodsbutnofields
§ AclasscanimplementmultipleinterfacesSimplertoimplement/understand/use
55
publicinterfaceComparable{publicint compare(Comparableo);
}
InterfaceTypes
§ Implementation:recordwith2pointers:§ Aseparatedispatchtableperinterface§ Apointertotheobject
56
a1
a2
interfacetablevtable
i.table
object
vtable
field1
field2
a1
b1
a2
DynamicClassLoading
§ SupportedbysomeOOlanguages(Java)§ Atcompiletime§ theactualclassofagivenobjectatagivenprogrampointmaynotbeknown
§ Someaddresseshavetoberesolvedatruntime§ problemwhenmultipleinheritanceisallowed
§ Canusehashtabletomapfieldsnametopointerstofunctions
57
OtherOOFeatures
§ Informationhiding§ private/public/protectedfields§ Semanticanalysis(contexthandling)
§ Testingclassmembership§ Singleinheritance:lookupthechainof“supers”§ Ifinheritanceisbounded
§ Allocateparentarrayateveryclassdescriptors§ lookupat“depth-of-inheritance”index
§ Upcasting:(Possibly)runtimechecks§ Bx=(B)a//BextendsA 58
OptimizingOOlanguages
§ Hideadditionalcosts§ Eliminateruntimechecks
Ax=newB()//BextendsABy=(B)x
§ Eliminatedeadfields
§ Replacedynamicbystaticbindingwhenpossiblex=newB()//BextendsAandoverridesfx.f()//invokeB_f(x)§ Typepropagationanalysis~reachingdefinitions
59
OptimizingOOlanguages
§ Simultaneouslygeneratecodeformultipleclasses
§ GeneratecodeB_f()andC_f()§ Codespaceisanissue
60
classA{g(){}f(){g()}
}
classBextendsA{g(){}}
classCextendsA{g()}
Summary
§ OOisaprogramming/designparadigm§ OOfeaturescomplicatescompilation§ Semanticanalysis§ Codegeneration§ Runtime§ Memorymanagement
§ UnderstandingcompilationofOOcanbeusefulforprogrammers
61
62
Compilation0368-3133Lecture12b
MemoryManagement
NoamRinetzky
63
AST+Sym.Tab.
StagesofcompilationSourcecode
(program)
LexicalAnalysis
SyntaxAnalysis
Parsing
ContextAnalysis
Portable/Retargetable codegeneration
Targetcode
(executable)
Assembly
IRText
Tokenstream
AST
CodeGeneration
Compilationè Execution
AST+Sym.Tab.
Sourcecode
(program)
LexicalAnalysis
SyntaxAnalysis
Parsing
ContextAnalysis
Portable/Retargetable codegeneration
Targetcode
(executable)
IRText
Tokenstream
AST
CodeGeneration
Linker
Assembler
Loader
Symbo
licAdd
r
ObjectF
ile
ExecutableFile
image
Executingprogram
Runtim
eSystem
RuntimeEnvironment
§ MediatesbetweentheOSandtheprogramminglanguage§ Hidesdetailsofthemachinefromtheprogrammer
§ Rangesfromsimplesupportfunctionsallthewaytoafull-fledgedvirtualmachine
§ Handlescommontasks§ Runtimestack(activationrecords)§ Memorymanagement§ Dynamicoptimization§ Debugging§ …
66
Wheredoweallocatedata?
§ Activationrecords§ Lifetimeofallocateddatalimitedbyprocedurelifetime§ Stackframedeallocated (popped)whenprocedurereturn
§ Dynamicmemoryallocationontheheap
67
MemoryLayout
stackgrowsdown(towardsloweraddresses)
heapgrowsup(towardshigher
addresses)
heap
stack
code
staticdata
68
Alignment
§ Typically,canonlyaccessmemoryatalignedaddresses§ Either4-bytesor8-bytes
§ Whathappensifyouallocatedataofsize5bytes?§ Padding – thespaceuntilthenextalignedaddressesiskeptempty
§ (sidenote:x86,ismorecomplicated,asusual,andalsoallowsunalignedaccesses,butnotrecommended)
69
Allocatingmemory
§ InC- malloc§ void *malloc(size_t size)
§ Whydoesmalloc returnvoid*?§ Itjustallocatesachunkofmemory,withoutregardtoitstype
§ Howdoesmalloc guaranteealignment?§ Afterall,youdon’tknowwhattypeitisallocatingfor§ Ithastoalignforthelargestprimitivetype§ Inpracticeoptimizedfor8bytealignment(glibc-2.17)
70
MemoryManagement
§ Manualmemorymanagement§ Automaticmemorymanagement
71
Manualmemorymanagement
§ malloc§ free
a = malloc(…) ;// do something with afree(a);
72
malloc
§ whereismalloc implemented?§ howdoesitwork?
73
§ Adatastructurerecordsthelocationandsizeoffreecellsofmemory.
§ Theallocatorconsiderseachfreecellinturn,andaccordingtosomepolicy,choosesonetoallocate.
§ Threebasictypesoffree-listallocation:§ First-fit§ Next-fit§ Best-fit
Free-listAllocation
Memorychunks
75
Freelist
76
First-Fit
150KB 100KB 170KB 300KB 50KB
Allocated Free
120KB allocation request
30KB 100KB 170KB 300KB 50KB
30KB 100KB 170KB 300KB 50KB
50KB allocation request
30KB 50KB 170KB 300KB 50KB
First-Fit
30KB 50KB 170KB 300KB 50KB
200KB allocation request
30KB 50KB 170KB 100KB 50KB
First-Fit
§ Dispersaloffreememoryacrossapossiblylargenumberofsmallfreecells.
§ Negativeeffects:§ Canpreventallocationfromsucceeding§ Maycauseaprogramtousemoreaddressspace,moreresidentpagesandmorecachelines.
§ Fragmentationisimpracticaltoavoid:§ Usuallytheallocatorcannotknowwhatthefuturerequestsequencewillbe.
§ Evengivenaknownrequestsequence,doinganoptimalallocationisNP-hard.
§ UsuallyThereisatrade-offbetweenallocationspeedandfragmentation.
Fragmentation
§ Idea– usemultiplefree-listwhosemembersaresegregatedbysizeinordertospeedallocation.
§ Usuallyafixednumberk ofsizevaluess0 <s1<…<sk-1§ k+1freelistsf0,…,fk§ Forafreecell,b,onlistfi,
size b =𝑠2∀1 ≤ 𝑖 ≤ 𝑘 − 1size(b)>sk-1ifi=k
§ Whenrequestingacellofsizeb≤sk-1,theallocatorroundstherequestsizeuptothesmallestsi suchthatb ≤si.
§ Siiscalledasizeclass
Segregated-fitsAllocation
SegregatedFitAllocate(j):
result ← remove(freeLists[j])
if result = nulllarge ← allocateBlock()
if large = nullreturn null
initialize(large, sizes[j])
result ← remove(freeList[j])
return result
§ Listfk,forcellslargerthansk,isorganizedtouseoneofthebasicsingle-listalgorithm.
§ Per-celloverheadsforlargecellareabithigherbutintotalitisnegligible.
§ Themainadvantage:forsizeclassesotherthansk,allocationtypicallyrequiresconstanttime.
Segregated-fitsAllocation
fk-1
fk
f1
f0
s0
s1
sk-1
>sk-1 >sk-1
RuntimesupportforMM
§ C’sstandardlibraryprovidesbasicmemorymanagement§ GetsmemorypagesfromtheOS§ Maintainsinventoryoffreememorycells
§ mmap(),brk()
84
free
§ Freetoolate– wastememory(memoryleak)§ Freetooearly– danglingpointers/crashes§ Freetwice– error
85
Whencanwefreeanobject?
a = malloc(…) ;b = a;// free (a); ? c = malloc (…);if (b == c)
printf(“unexpected equality”);
Cannotfreeanobjectifithasareferencewithafutureuse!
86
Whencanfreexbeinsertedafterp?
pcannotfreex
xreferencesanobjectl
somereferencetolisused
Onallexecutionpathsafterptherearenousesofreferencestotheobjectreferencedbyxè insertingfreexafterpisvalid
87
AutomaticMemoryManagement
§ automaticallyfreememorywhenitisnolongerneeded
§ notlimitedtoOOlanguages§ prevalentinOOlanguagessuchasJava§ alsoinfunctionallanguages
88
Garbagecollection
§ approximatereasoningaboutobjectliveness§ usereachabilitytoapproximateliveness§ assumereachableobjectsarelive§ non-reachableobjectsaredead
89
GarbageCollection– ClassicalTechniques
§ referencecounting§ markandsweep§ copying
90
GCusingReferenceCounting
§ addareference-countfieldtoeveryobject§ howmanyreferencespointtoit
§ when(rc==0)theobjectisnonreachable§ nonreachable=>dead§ canbecollected(deallocated)
91
ManagingReferenceCounts§ Eachobjecthasareferencecounto.RC§ Anewlyallocatedobjectogetso.RC =1§ why?
§ write-barrierforreferenceupdatesupdate(x,old,new){old.RC--;new.RC++;if(old.RC ==0)collect(old);
}
§ collect(old)willdecrementRCforallchildrenandrecursivelycollectobjectswhoseRCreached0.
92
Cycles!
§ cannotidentifynon-reachablecycles§ referencecountsfornodesonthecyclewillneverdecrementto0
§ severalapproachesfordealingwithcycles§ ignore§ periodicallyinvokeatracingalgorithmtocollectcycles§ specializedalgorithmsforcollectingcycles
93
TheMark-and-SweepAlgorithm[McCarthy1960]
§ Markingphase§ markroots§ traceallobjectstransitivelyreachablefromroots§ markeverytraversedobject
§ Sweepphase§ scanallobjectsintheheap§ collectallunmarkedobjects
94
The Mark-Sweep algorithm
§ Traverseliveobjects&markblack.§ Whiteobjectscanbereclaimed.
stackHeap
registers
Roots
Note!Thisisnottheheap datastructure!
95
Triggering
New(A)=iffree_list isempty
mark_sweep()iffree_list isempty
return(“out-of-memory”)pointer=allocate(A)return(pointer)
Garbagecollectionistriggeredbyallocation
96
BasicAlgorithm
mark_sweep()=forPtr inRoots
mark(Ptr)sweep()
mark(Obj)=ifmark_bit(Obj)==unmarked
mark_bit(Obj)=markedforCinChildren(Obj)
mark(C)
Sweep()=p=Heap_bottomwhile(p<Heap_top)
if(mark_bit(p)==unmarked)thenfree(p)elsemark_bit(p)=unmarked;p=p+size(p)
97
Mark&Sweep Example
r1
r2
98
Mark&Sweep inDepth
mark(Obj)=ifmark_bit(Obj)==unmarked
mark_bit(Obj)=markedforCinChildren(Obj)
mark(C)
§ Howmuchmemorydoesitconsume?§ Recursiondepth?§ Canyoutraversetheheapwithoutworst-caseO(n)stack?
§ Deutch-Schorr-Waitealgorithmforgraphmarkingwithoutrecursionorstack(worksbyreversingpointers)
99
PropertiesofMark&Sweep
• Mostpopularmethodtoday• Simple• Doesnotmoveobjects,andsoheapmayfragment• Complexity
JMarkphase:liveobjects(dominantphase)L Sweepphase:heapsize
• Termination:eachpointertraversedonce• Engineeringtricksusedtoimproveperformance
100
Mark-Compact
§ Duringtherunobjectsareallocatedandreclaimed§ Gradually,theheapgetsfragmented§ Whenspaceistoofragmentedtoallocate,acompactionalgorithmisused
§ Moveallliveobjectstothebeginningoftheheapandupdateallpointerstoreferencethenewlocations
§ Compactionisverycostlyandweattempttorunitinfrequently,oronlypartially
101
The Heap
MarkCompact
§ Importantparametersofacompactionalgorithm
§ Keeporderofobjects?§ Useextraspaceforcompactordatastructures?§ Howmanyheappasses?§ Canitruninparallelonamulti-processor?
§ Wedonotelaborateinthisintro
102
CopyingGC
§ partitiontheheapintotwoparts§ oldspace§ newspace
§ CopyingGCalgorithm§ copyallreachable objectsfromoldspacetonewspace§ swaprolesofold/newspace
103
Example
old new
Roots
A
D
C
B
E
104
Example
old new
Roots
A
D
C
B
E
A
C
105
PropertiesofCopyingCollection
§ Compactionforfree§ Majordisadvantage:halfoftheheapisnotused§ “Touch”onlytheliveobjects
§ Goodwhenmostobjectsaredead§ Usuallymostnewobjectsaredead
§ Somemethodsuseasmallspaceforyoungobjectsandcollectthisspaceusingcopyinggarbagecollection
106
Averysimplisticcomparison
CopyingMark&sweepReferenceCounting
LiveobjectsMark=liveobjectsSweep=Sizeofheap
Pointerupdates+deadobjects
Complexity
Halfheapwasted
Bit/object+stackforDFS
Count/object+stackforDFS
Spaceoverhead
ForfreeAdditionalworkAdditionalworkCompaction
longlongMostlyshortPausetime
CyclecollectionMoreissues
107
ParallelMark&Sweep GC
r1
r2
Thread1Thread2
ParallelGC:mutator isstopped,GCthreadsruninparallel
108
ConcurrentMark&Sweep Example
r1
r2
X
X
ConcurrentGC:mutator andGCthreadsruninparallel,noneedtostopmutator
109
ConservativeGC
§ HowdoyoutrackpointersinlanguagessuchasC?§ Anyvaluecanbecastdowntoapointer
§ Howcanyoufollowpointersinastructure?
§ Easy– beconservative,consideranythingthatcanbeapointertobeapointer
§ Practical!(e.g.,Boehmcollector)
110
ConservativeGC
§ Canyouimplementaconservativecopying GC?
§ Whatistheproblem?
§ Cannotupdatepointerstothenewaddress…youdon’tknowwhetherthevalueisapointer,cannotupdateit
111
ModernMemoryManagement
§ Considersstandardprogramproperties§ Handleparallelism§ Stoptheprogramandcollectinparallelonallavailableprocessors
§ Runcollectionconcurrentlywiththeprogramrun
§ Cacheconsciousness§ Real-time
112
TerminologyRecap
§ Heap,objects
§ Allocate,free(deallocate,delete,reclaim)
§ Reachable,live,dead,unreachable
§ Roots§ Referencecounting,markandsweep,copying,
compaction,tracingalgorithms
§ Fragmentation
113