Memory Models Gunnar Bergmann 30. November 2015 Manual memory - - PowerPoint PPT Presentation

memory models
SMART_READER_LITE
LIVE PREVIEW

Memory Models Gunnar Bergmann 30. November 2015 Manual memory - - PowerPoint PPT Presentation

Memory Models Gunnar Bergmann 30. November 2015 Manual memory management RAII Smart Pointers Rusts Ownership Conclusion Table of contents Manual memory management 1 RAII 2 Smart Pointers 3 Rusts Ownership 4 Conclusion 5


slide-1
SLIDE 1

Memory Models

Gunnar Bergmann

  • 30. November 2015
slide-2
SLIDE 2

Manual memory management RAII Smart Pointers Rust’s Ownership Conclusion

Table of contents

1

Manual memory management

2

RAII

3

Smart Pointers

4

Rust’s Ownership

5

Conclusion

Gunnar Bergmann Memory Models

  • 30. November 2015

2

slide-3
SLIDE 3

Manual memory management RAII Smart Pointers Rust’s Ownership Conclusion

Manual memory management

Problems caused by manual memory management: Memory leaks use after free double delete repeated resource release on all paths separation of allocation and release exceptions only with finally

Gunnar Bergmann Memory Models

  • 30. November 2015

3

slide-4
SLIDE 4

Manual memory management RAII Smart Pointers Rust’s Ownership Conclusion

Garbage Collection

memory overhead unpredictable collection cycle restricted to memory:

no deterministic destruction order no immediate destruction

= ⇒ Not always useful

Gunnar Bergmann Memory Models

  • 30. November 2015

4

slide-5
SLIDE 5

Manual memory management RAII Smart Pointers Rust’s Ownership Conclusion

Local variables

memory automatically reclaimed after end of scope stack allocated returned by copying the value pointers become invalid after end of scope

Gunnar Bergmann Memory Models

  • 30. November 2015

5

slide-6
SLIDE 6

Manual memory management RAII Smart Pointers Rust’s Ownership Conclusion

RAII

Classes contain constructor and destructor Constructor allocates resource Destructor frees resource Resource Acquisition Is Initialization

Gunnar Bergmann Memory Models

  • 30. November 2015

6

slide-7
SLIDE 7

Manual memory management RAII Smart Pointers Rust’s Ownership Conclusion

class S t r i n g { private : char∗ data ; // p o i n t e r to a c h a r a c t e r public : // Constructor S t r i n g ( const char∗ s ) { data = new char [ s t r l e n ( s )+1]; s t r c p y ( data , s ) ; } // d i s a b l e copying S t r i n g ( const S t r i n g &) = delete ; // Destructor ˜ S t r i n g () { delete [ ] data ; } };

Gunnar Bergmann Memory Models

  • 30. November 2015

7

slide-8
SLIDE 8

Manual memory management RAII Smart Pointers Rust’s Ownership Conclusion

Add a member function for appending strings: concat ( const char∗ s ) { char∗ old = data ; int l e n = s t r l e n ( old)+ s t r l e n ( s )+1; data = new char [ l e n ] ; // more memory s t r c p y ( data ,

  • ld ) ;

s t r c a t ( data , s ) ; delete [ ]

  • ld ;

// f r e e

  • ld memory

}

Gunnar Bergmann Memory Models

  • 30. November 2015

8

slide-9
SLIDE 9

Manual memory management RAII Smart Pointers Rust’s Ownership Conclusion

Automatic destruction at end of lifetime Destructors of members and base classes automatically called = ⇒ simple composition of classes Immediate destructor call Allows other resources than memory: { lock guard <mutex> guard ( some mutex ) ; // . . . code i n s i d e the mutex } // some mutex a u t o m a t i c a l l y unlocked

Gunnar Bergmann Memory Models

  • 30. November 2015

9

slide-10
SLIDE 10

Manual memory management RAII Smart Pointers Rust’s Ownership Conclusion

Destruction Order

Destroy local variables at end of scope First run code of the destructor then destroy members and base classes Reverse of construction order Stack unwind on raised exception: Go back the call stack and destroy all objects.

Gunnar Bergmann Memory Models

  • 30. November 2015

10

slide-11
SLIDE 11

Manual memory management RAII Smart Pointers Rust’s Ownership Conclusion

Already solved problems: Memory leaks double delete repeated resource release on all paths separation of allocation and release exceptions only with finally Remaining and new problems: Use after free Strict hierarchies required; no cyclic references

Gunnar Bergmann Memory Models

  • 30. November 2015

11

slide-12
SLIDE 12

Manual memory management RAII Smart Pointers Rust’s Ownership Conclusion

Containers

can store multiple objects of the same type use RAII to call all destructors can move objects internally = ⇒ can cause dangling references / iterators

Gunnar Bergmann Memory Models

  • 30. November 2015

12

slide-13
SLIDE 13

Manual memory management RAII Smart Pointers Rust’s Ownership Conclusion

Smart Pointers

Definition (Owning pointer)

Pointer that may need to free its memory.

Definition (Raw pointer)

Pointer like in C, for example int*.

Definition (Smart pointer)

Pointer-like object that manages its own memory.

Gunnar Bergmann Memory Models

  • 30. November 2015

13

slide-14
SLIDE 14

Manual memory management RAII Smart Pointers Rust’s Ownership Conclusion

Unique Pointer

C++: unique_ptr Rust: Box unique ownership hold a reference automatically deallocate it

Gunnar Bergmann Memory Models

  • 30. November 2015

14

slide-15
SLIDE 15

Manual memory management RAII Smart Pointers Rust’s Ownership Conclusion

Shared Pointer

C++: shared_ptr Rust: Rc shared ownership uses reference counting increases counter in copy operation decreases counter in destructor and maybe destroys object reference cycles can cause leaks

Gunnar Bergmann Memory Models

  • 30. November 2015

15

slide-16
SLIDE 16

Manual memory management RAII Smart Pointers Rust’s Ownership Conclusion

Weak Pointer

C++: weak_ptr Rust: Weak no ownership, pointer can dangle can be upgraded to a shared pointer used to break reference cycles

Gunnar Bergmann Memory Models

  • 30. November 2015

16

slide-17
SLIDE 17

Manual memory management RAII Smart Pointers Rust’s Ownership Conclusion

without weak pointer

Root Object 1 Object 2

Gunnar Bergmann Memory Models

  • 30. November 2015

17

slide-18
SLIDE 18

Manual memory management RAII Smart Pointers Rust’s Ownership Conclusion

with weak pointer

Root Object 1 Object 2

Gunnar Bergmann Memory Models

  • 30. November 2015

18

slide-19
SLIDE 19

Manual memory management RAII Smart Pointers Rust’s Ownership Conclusion

RAII in garbage collected languages

traditionally gc’ed languages use finally finally is more verbose than RAII and can be forgotten easily D uses garbage collector for memory and RAII for other resources Some languages provide RAII-like behavior

Gunnar Bergmann Memory Models

  • 30. November 2015

19

slide-20
SLIDE 20

Manual memory management RAII Smart Pointers Rust’s Ownership Conclusion

RAII in garbage collected languages

For example Python: with open( ” t e s t . f i l e ” ) as f : content = f . read ()

Gunnar Bergmann Memory Models

  • 30. November 2015

20

slide-21
SLIDE 21

Manual memory management RAII Smart Pointers Rust’s Ownership Conclusion

Rust

Rust aims at memory safety, abstractions without overhead and multithreading. detect problems at compile time no dangling references thread safety no overhead It uses ownership, borrowing and lifetimes.

Gunnar Bergmann Memory Models

  • 30. November 2015

21

slide-22
SLIDE 22

Manual memory management RAII Smart Pointers Rust’s Ownership Conclusion

Ownership

  • nly a single variable can access a value
  • wnership can be transferred to another variable, e.g. inside a

function can not use a variable after move some exceptions when transferring and copying is equal

Gunnar Bergmann Memory Models

  • 30. November 2015

22

slide-23
SLIDE 23

Manual memory management RAII Smart Pointers Rust’s Ownership Conclusion

Ownership

l e t a = vec ! [ 1 , 2 , 3 ] ; l e t b = a ; // does not work a . push ( 4 ) ; // append 4

Gunnar Bergmann Memory Models

  • 30. November 2015

23

slide-24
SLIDE 24

Manual memory management RAII Smart Pointers Rust’s Ownership Conclusion

Borrowing

temporarily borrow instead of transferring ownership like references move borrowed value is forbidden (no dangling references) similar to read-write-lock just one reference can mutate the variable = ⇒ no dangling iterators

Gunnar Bergmann Memory Models

  • 30. November 2015

24

slide-25
SLIDE 25

Manual memory management RAII Smart Pointers Rust’s Ownership Conclusion

Borrowing

fn foo () −> &i32 { l e t a = 12; r e t u r n &a ; } l e t x = vec ! [ 1 , 2 , 3 ] ; l e t r e f e r e n c e = &mut x ; p r i n t l n !(”{}” , x ) ; Borrowing is not threadsafe but can be used for it: { l e t guard = mutex . lock ( ) . unwrap ( ) ; modify value (&mut guard ) ; }

Gunnar Bergmann Memory Models

  • 30. November 2015

25

slide-26
SLIDE 26

Manual memory management RAII Smart Pointers Rust’s Ownership Conclusion

Lifetimes

mechanism for implementing ownership and borrowing every type has a lifetime can not take a reference with a larger lifetimes

  • bject can not be moved while a reference exists
  • ften they can be automatically generated

Gunnar Bergmann Memory Models

  • 30. November 2015

26

slide-27
SLIDE 27

Manual memory management RAII Smart Pointers Rust’s Ownership Conclusion

Lifetimes

s t r u c t Foo { x : i32 , } // the l i f e t i m e ’ a i s a g e n e r i c parameter // i t r e t u r n s a r e f e r e n c e with the same // l i v e t i m e as the input fn f i r s t x <’a>( f i r s t : &’a Foo , second : &Foo ) −> &’a i32 { &f i r s t . x }

Gunnar Bergmann Memory Models

  • 30. November 2015

27

slide-28
SLIDE 28

Manual memory management RAII Smart Pointers Rust’s Ownership Conclusion

Anonymous functions

skip

lambda functions can access local variable use ownership model for checks simplest type borrows environment = ⇒ can not be returned second type takes ownership = ⇒ variables can not be used from outside

Gunnar Bergmann Memory Models

  • 30. November 2015

28

slide-29
SLIDE 29

Manual memory management RAII Smart Pointers Rust’s Ownership Conclusion

Anonymous functions

Some can be called only once.

  • wnership is transferred into the called functions

destroyed at end of call function can consume data same concept available for methods

Gunnar Bergmann Memory Models

  • 30. November 2015

29

slide-30
SLIDE 30

Manual memory management RAII Smart Pointers Rust’s Ownership Conclusion

Anonymous functions in C++

no checks by the compiler individual capture per value allows more complex expressions to be captured (C++14) more flexibility, less safety no self consuming functions in C++

Gunnar Bergmann Memory Models

  • 30. November 2015

30

slide-31
SLIDE 31

Manual memory management RAII Smart Pointers Rust’s Ownership Conclusion

Anonymous functions

l e t f = move | | { // . . . }; fn do something ( s e l f ) { // . . . } // s e l f i s destroyed [ x , // by value &r , // by r e f e r e n c e p = make unique<int >(0) // g e n e r a l i z e d capture ] ( auto parameter1 ) { // the code in the f u n c t i o n }

Gunnar Bergmann Memory Models

  • 30. November 2015

31

slide-32
SLIDE 32

Manual memory management RAII Smart Pointers Rust’s Ownership Conclusion

Ownership in C++

  • wnership was already used for reasoning about code

just the checks are new At CppCon 2015 a tool for checks similar to Rusts’ was announced.

Gunnar Bergmann Memory Models

  • 30. November 2015

32

slide-33
SLIDE 33

Manual memory management RAII Smart Pointers Rust’s Ownership Conclusion

Conclusion

RAII: safe building blocks for resource handling good program structure dangling references manually break cyclic references (not always possible) need to choose adequate type RAII is general solution for resources without cycles ⇐ ⇒ GC is general solution for memory handling

Gunnar Bergmann Memory Models

  • 30. November 2015

33

slide-34
SLIDE 34

Manual memory management RAII Smart Pointers Rust’s Ownership Conclusion

Conclusion

Ownership: prevents dangling references improves safety does not solve all issues requires lots of annotations

Gunnar Bergmann Memory Models

  • 30. November 2015

34

slide-35
SLIDE 35

Manual memory management RAII Smart Pointers Rust’s Ownership Conclusion

Thank you for listening. Questions?

Gunnar Bergmann Memory Models

  • 30. November 2015

35