|
1
|
- An alternative to garbage collection
- Bartosz Milewski
- www.relisoft.com
|
|
2
|
- Resource Acquisition Is Initialization
- Ellis, Stroustrup
“The Annotated C++ Reference Manual” Addison-Wesley 1990
|
|
3
|
|
|
4
|
- 1. Readability. Easy to read and understand. Not obscured by lots of
inessential bookkeeping. Easy to factorize and assimilate one concept at
a time.
- 2. Verifiability. Reasonably easy to verify correctness. Locality—you
shouldn't have to refer to many separate pieces of code to prove
correctness. Easy to debug.
- 3. Maintainability. Easy to modify—it shouldn't be fragile. A small
modification shouldn't make it fail in unexpected ways. The creator or
the maintainer should have as few opportunities as possible for making a
mistake.
|
|
5
|
|
|
6
|
|
|
7
|
|
|
8
|
|
|
9
|
|
|
10
|
- Resource: “something” that must be released after it’s acquired
- Memory
- Heap objects created using new
- Handles: critical section, file, device context
- States: lock, reference count, progress UI (hourglass cursor)
- Objects: recursively
|
|
11
|
- Owner: “something” that is responsible for the release of the resource
- Scopes (global, local)—automatic destruction of stack objects and
global/static objects
- Objects: recursively—automatic destruction of sub-objects
- Built-in ownership mechanisms
- Destruction at the end of the scope
- Stack unwinding during exceptions
- Recursive destruction of sub-objects
|
|
12
|
- Idea: make use of built-in mechs
- Acquire resources in constructors (one resource per object)
- Release resources in destructors
|
|
13
|
- Note that heap objects are resources, so they must always have an owner
object
- class Foo
{
public:
Foo () : _bar (new Bar)
{}
~Foo () { delete _bar;
}
private:
Bar * _bar;
};
|
|
14
|
- Gurantee that all resources have owners -> no resource leaks
- Raw resources owned by objects
- Objects owned by other objects and scopes
- Each scope is eventually exited, q.e.d.
- Note: Heap objects (allocated using new) are resources—must have owners
- Locality of proof—constructors/destructors
|
|
15
|
- Built-in object transfer
- Returning objects by value
- Built-in object cloning
- Copy construction
- Assignment
- Passing arguments by value (a combination of the two)
- Copy constructors and operator= provide hooks to control transfers
|
|
16
|
- Idea: make use of built-in mechs
- Pass resources inside transfer objects with value-like semantics
- Example std::auto_ptr (move semantics)
- Ownership policies
- Disallow transfer: private copy-co/assignment
- Exclusive: auto_ptr-like
- Reference counted
|
|
17
|
- Equivalent to Garbage Collection
- 99% can be solved by auto_ptr
- Allocation of heap resources
- auto_ptr<Foo> foo (new Foo);
- foo.reset (new Foo);
- : _foo (new Foo) // constructor
- Ownership transfer
- auto_ptr<Foo> MakeFoo ();
- void EatFoo (auto_ptr<Foo> foo);
|
|
18
|
- Standard containers don’t work with auto_ptr
- Standard containers work with ref-counted smart pointers (Alexandrescu)
- Must use RC pointers all over (-)
- No support in the standard yet (-)
- Overhead (-)
- Works with all containers (+)
|
|
19
|
- Works with std::auto_ptr (big +)
- Little overhead (+)
- Must be generalized to other containers (-) [less needed as it might
seem]
- Not supported by the standard (-)
|
|
20
|
- A vector of pointers with modified interface and semantics
|
|
21
|
|
|
22
|
|
|
23
|
- Each heap object has an auto_ptr or auto_vector owner at all times
- Each polymorphic class has a virtual destructor
- Makes a very dependable cheap garbage collection mechanism
- But there is more!
|
|
24
|
- Traditional garbage collection is useless with other resources
- Critical section and Lock example
- Windows resources
- Handles
- Files, memory mapped files
- Threads
- Etc.
|
|
25
|
|
|
26
|
|
|
27
|
|
|
28
|
- Dipose is to be specialized
|
|
29
|
|
|
30
|
|
|
31
|
|
|
32
|
|
|
33
|
|
|
34
|
|
|
35
|
|
|
36
|
- RM revolutionized our own work at Reliable Software
- Easy to enforce and maintain
- Global searches for new and delete
- Global searches for virtual
- Our own Windows library using RM
- More at www.ReliSoft.com
|