source: ThirdParty/mpqc_open/src/lib/util/ref/ref.dox@ 6b1a6a

Action_Thermostats Add_AtomRandomPerturbation Add_RotateAroundBondAction Add_SelectAtomByNameAction Adding_Graph_to_ChangeBondActions Adding_MD_integration_tests Adding_StructOpt_integration_tests AutomationFragmentation_failures Candidate_v1.6.0 Candidate_v1.6.1 ChangeBugEmailaddress ChangingTestPorts ChemicalSpaceEvaluator Combining_Subpackages Debian_Package_split Debian_package_split_molecuildergui_only Disabling_MemDebug Docu_Python_wait EmpiricalPotential_contain_HomologyGraph_documentation Enable_parallel_make_install Enhance_userguide Enhanced_StructuralOptimization Enhanced_StructuralOptimization_continued Example_ManyWaysToTranslateAtom Exclude_Hydrogens_annealWithBondGraph FitPartialCharges_GlobalError Fix_ChronosMutex Fix_StatusMsg Fix_StepWorldTime_single_argument Fix_Verbose_Codepatterns ForceAnnealing_goodresults ForceAnnealing_oldresults ForceAnnealing_tocheck ForceAnnealing_with_BondGraph ForceAnnealing_with_BondGraph_continued ForceAnnealing_with_BondGraph_continued_betteresults ForceAnnealing_with_BondGraph_contraction-expansion GeometryObjects Gui_displays_atomic_force_velocity IndependentFragmentGrids_IntegrationTest JobMarket_RobustOnKillsSegFaults JobMarket_StableWorkerPool JobMarket_unresolvable_hostname_fix ODR_violation_mpqc_open PartialCharges_OrthogonalSummation PythonUI_with_named_parameters QtGui_reactivate_TimeChanged_changes Recreated_GuiChecks RotateToPrincipalAxisSystem_UndoRedo StoppableMakroAction Subpackage_levmar Subpackage_vmg ThirdParty_MPQC_rebuilt_buildsystem TremoloParser_IncreasedPrecision TremoloParser_MultipleTimesteps Ubuntu_1604_changes stable
Last change on this file since 6b1a6a was 860145, checked in by Frederik Heber <heber@…>, 8 years ago

Merge commit '0b990dfaa8c6007a996d030163a25f7f5fc8a7e7' as 'ThirdParty/mpqc_open'

  • Property mode set to 100644
File size: 5.8 KB
Line 
1
2/** \page ref The Reference Library
3
4The Reference Library provides a means to automatically free
5memory that is no longer needed.
6
7<ul>
8 <li> \ref refintro
9 <li> \ref refthread
10 <li> \ref refcust
11 <li> \ref refexample
12</ul>
13
14\section refintro Introduction to Reference Counting
15
16It is fairly easy in C++ to create a pointer to an object that
17actually references invalid memory. One common way to do this
18is to create an object with new and store that
19object's pointer. Then the pointer is given to another
20object's member function as an argument which keeps a copy of
21the pointer for future use. After the member function
22returns, the routine that originally created the object
23delete's it, not knowing that another object has since
24created a reference to the object. The result of using the
25delete'ed object is unpredictable and would likely be
26a program crash. It is up to the programmer to provide the
27logic necessary to avoid this problem. The programmer must
28also deal with the problem of calling to delete
29operator on any new'ed memory when it is no longer
30referenced.
31
32Reference counting is one technique that can be applied to
33automate memory management. In this approach, a count of how
34many pointers point to an object is attached to that object.
35This count is managed by a smart pointer class which mimics
36the behavior of C++ pointers by providing
37<tt>operator->()</tt>. This class has a pointer to the
38reference counted object and increments the reference count of
39objects when they are assigned to it while decrementing the
40counts of the objects that are displaced by these assigments.
41The smart pointer class automatically delete's the
42object when its reference count drops to zero.
43
44A deficiency of this method is that unreferenced circular
45lists are not automatically deleted. Circular list
46implementors must provide a mechanism to detect when the list
47is dereferenced and then break the list's circularity to let
48the automated reference mechanism finish the work.
49
50The reference library provides smart pointers and a base class that
51can be used to maintain reference counts to objects. For an
52object to be reference counted its class must inherit from
53the RefCount class. This adds <tt>sizeof(int)</tt> bytes
54of overhead per object and makes the destructor virtual (so a vtable
55will be added to objects of the class, if there wasn't already a virtual
56member in the class).
57
58The smart pointers that maintain the reference counts are
59provided by the Ref class template. A smart pointer to a
60class A which inherits from RefCount would have the
61type Ref<A>.
62
63\section refthread Thread Safety of the Reference Counting Package
64
65The referencing counting package is thread-safe if the CPP macro
66REF_USE_LOCKS is defined to 1. This means that Ref's to a particular
67object can be created and reassigned and destroyed in different
68threads. However, the Ref's themselves are not thread-safe.
69For example, a static Ref cannot be simultaneously modified from
70multiple threads.
71
72Because there is an overhead associated with locking access to an
73object's reference count, locking is not turned on by default,
74and, thus, making and deleting references to an object in
75multiple threads is not thread-safe by default. The
76RefCount::use_locks member is passed a bool value to turn locking
77on and off on a per object basis.
78
79\section refcust Customizing the Reference Counting Package
80
81 The behaviour of the package can be modified at compile time
82with the following five macros, each of which should be undefined, 0, or 1:
83
84<dl>
85<dt><tt>REF_CHECK_STACK</tt><dd>
86 If this is 1, referenced objects are checked to see if they
87 reside on the stack, in which case storage for the object is not managed,
88 if management is enabled.
89<dt><tt>REF_MANAGE</tt><dd>
90 If this is 1, the unmanage member is enabled.
91<dt><tt>REF_CHECK_MAX_NREF</tt><dd>
92 If this is 1, the reference count is checked before
93 it is incremented to make sure it isn't too big.
94<dt><tt>REF_CHECK_MIN_NREF</tt><dd>
95 If this is 1, the reference count is checked before
96 it is decremented to make sure it isn't already zero.
97<dt><tt>REF_USE_LOCKS</tt><dd>
98 If this is 1, modification of the reference count
99 is locked to allow thread-safe execution.
100</dl>
101
102If a macro is undefined, then the behaviour is architecture
103dependent---usually, the macro will be set to 1 in this case.
104For maximum efficiency and for normal operation after the program is
105debugged, compile with all of the above macros defined to zero.
106This can also be done by defining REF_OPTIMIZE.
107
108 An include file can be used to set these options as well. This has
109the advantage that dependency checking will force an automatic
110recompile of all affected files if the options change. This is done
111in the file scconfig.h, which is produced by the automated configuration
112procedure.
113
114 Note that all source code that uses references must be compiled with
115the same value for REF_MANAGE.
116Changing this can change the storage layout and the interpretation of
117the reference count data.
118
119\section refexample A Reference Example
120
121Following is a simple example of how to manage memory with reference
122counts.
123
124<pre>
125\#include <util/container/ref.h>
126
127class A: virtual public RefCount {};
128
129class B: public A {};
130
131int
132main()
133{
134 Ref\<A\> a1(new A);
135 Ref\<A\> a2;
136
137 // Create another reference to the A object pointed to by a1.
138 a2 = a1;
139
140 // Make a2 refer to a new A object.
141 a2 = new A;
142
143 // a2 was the only reference to the second A object, so setting
144 // a2 to the null object will cause the second A object to be
145 // deleted.
146 a2 = 0;
147
148 Ref\<B\> b(new B);
149
150 // An object of type Ref\<X\> can be assigned to an object of type
151 // Ref\<Y\> as long as X* can be assigned to Y*.
152 a1 = b;
153
154 // An automatic dynamic cast can be done by using the left shift
155 // operator.
156 b << a1;
157
158 // The B object will be deleted here because all of the references
159 // to it go out of scope and destroyed.
160 return 0;
161}
162</pre>
163
164*/
Note: See TracBrowser for help on using the repository browser.