root/library/doc/local/memory_management.dox @ 533

Revision 533, 4.7 kB (checked in by vbarta, 15 years ago)

added memory management page

Line 
1/*!
2\page Memory Management in BDM
3
4C++ memory management is notoriously flexible, allowing a wide range
5of efficient and dangerous techniques. BDM uses conventions which
6allow high implementation efficiency (not absolutely maximal, but
7within a measurement error of the most efficient way) while
8substantially reducing the danger of memory errors. These conventions
9are described below.
10
11\section Constructors
12
13\li Each configurable class must have public default constructor (that
14is, a constructor which takes no parameters). This is required by the
15configuration framework. Other constructors may also be defined when
16convenient.
17
18\li Each constructor must initialize all fields of the constructed
19object, to prevent unpredictable behavior.
20
21One consequence of the points above is the case when a default
22constructor doesn't have the data with which to initialize a new
23object - in that case it can simply call the default constructors of
24all its object fields (and base classes), but must explicitly
25initialize all numeric fields and raw pointers to 0. Such an object
26isn't valid as constructed and must have some additional
27initialization methods (typically from_settings, for reading its
28configured state), but it can at least be destroyed.
29
30\section Exceptions
31
32BDM uses exceptions to signal runtime and some logic errors. The
33library aims to provide the minimal exception safety (that is,
34throwing an exception doesn't crash and doesn't leak any resources)
35for all thrown exceptions \b except \b memory errors - when a program
36using BDM exhausts memory, it should be terminated as soon as possible
37(and in most cases it has probably already terminated by
38itself). Specific exceptions may provide stronger guarantees, as
39documented for specific cases. All exceptions thrown outside the
40library are descendants of std::exception.
41
42\section Pointers
43
44Pointers are used extensively (for efficiency), but usage of raw
45pointers should be minimized.
46
47Objects allocated by operator new should be assigned to a smart
48pointer instance immediately upon their construction, so that they can
49be automatically deleted after use. BDM implements its own
50reference-counted smart pointer template, bdm::shared_ptr, whose
51interface and semantics are close to the proposed standard
52std::tr1:shared_ptr (which is planned to replace bdm::shared_ptr once
53it becomes widely available). Note that objects allocated on the stack
54\b must not \b have their addresses passed to shared_ptr - that is a
55bug leading to intermittent runtime errors.
56
57Non-null heap pointers may also be kept in instances of object_ptr,
58which is convertible to (and from) shared_ptr. The SHAREDPTR macro (or
59SHAREDPTR2, for templated types) defines standartized names for these
60instances, simplifying library usage:
61
62\code
63// egamma_ptr is typedef for object_ptr<egamma>, whose default
64// constructor calls new egamma()
65egamma_ptr eG;
66eG->set_parameters ( a, b );
67
68epdf_array Coms ( 2 ); // epdf_array is typedef for Array<shared_ptr<epdf> >
69Coms ( 0 ) = eG; // object_ptr<T> is derived from shared_ptr<T>
70
71// The egamma instance doesn't leak: if the shared_ptr instance which
72// wraps it isn't assigned to anything else, the pointer is deleted by
73// the destructor of either object_ptr, or Array, whichever runs last.
74\endcode
75
76Pointers kept in object fields should be wrapped in a shared_ptr
77instance, which will automatically keep them valid (at least) for the
78lifetime of the containing object. When that isn't possible, it should
79be documented why their containing class doesn't delete them and who
80does.
81
82Pointers passed as arguments into functions (and methods) are
83generally not expected to stay valid after the function returns - when
84that is required, the parameter's documentation should specify the
85required scope:
86
87\code
88// the pointer must stay valid for the lifetime of the object
89CurrentContext ( const char *name, int idx );
90\endcode
91
92A simpler alternative is just to pass a shared pointer:
93
94\code
95class mepdf : public mpdf
96{
97        shared_ptr<epdf> iepdf;
98public:
99        mepdf (shared_ptr<epdf> em) {
100                iepdf = em;
101\endcode
102
103In the case above, passing a (constant) reference to shared_ptr<epdf>
104might be more efficient, but no measurements have been performed.
105
106Functions returning raw pointers should document the scope of their
107validity:
108
109\code
110//! Returns the stored pointer (which remains owned by this
111//! instance).
112T *get();
113\endcode
114
115Functions gnerally shouldn't return raw pointers allocated by operator
116new - such pointers should be wrapped in an instance of shared_ptr, so
117that the pointer's unlimited life expectancy is encoded in the
118function signature:
119
120\code
121virtual shared_ptr<epdf> marginal (const RV &rv) const;
122\endcode
123
124*/
Note: See TracBrowser for help on using the browser.