| [0b990d] | 1 | 
 | 
|---|
 | 2 | /** \page ref The Reference Library
 | 
|---|
 | 3 | 
 | 
|---|
 | 4 | The Reference Library provides a means to automatically free
 | 
|---|
 | 5 | memory 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 | 
 | 
|---|
 | 16 | It is fairly easy in C++ to create a pointer to an object that
 | 
|---|
 | 17 | actually references invalid memory.  One common way to do this
 | 
|---|
 | 18 | is to create an object with new and store that
 | 
|---|
 | 19 | object's pointer.  Then the pointer is given to another
 | 
|---|
 | 20 | object's member function as an argument which keeps a copy of
 | 
|---|
 | 21 | the pointer for future use.  After the member function
 | 
|---|
 | 22 | returns, the routine that originally created the object
 | 
|---|
 | 23 | delete's it, not knowing that another object has since
 | 
|---|
 | 24 | created a reference to the object.  The result of using the
 | 
|---|
 | 25 | delete'ed object is unpredictable and would likely be
 | 
|---|
 | 26 | a program crash.  It is up to the programmer to provide the
 | 
|---|
 | 27 | logic necessary to avoid this problem.  The programmer must
 | 
|---|
 | 28 | also deal with the problem of calling to delete
 | 
|---|
 | 29 | operator on any new'ed memory when it is no longer
 | 
|---|
 | 30 | referenced.
 | 
|---|
 | 31 | 
 | 
|---|
 | 32 | Reference counting is one technique that can be applied to
 | 
|---|
 | 33 | automate memory management.  In this approach, a count of how
 | 
|---|
 | 34 | many pointers point to an object is attached to that object.
 | 
|---|
 | 35 | This count is managed by a smart pointer class which mimics
 | 
|---|
 | 36 | the behavior of C++ pointers by providing
 | 
|---|
 | 37 | <tt>operator->()</tt>.  This class has a pointer to the
 | 
|---|
 | 38 | reference counted object and increments the reference count of
 | 
|---|
 | 39 | objects when they are assigned to it while decrementing the
 | 
|---|
 | 40 | counts of the objects that are displaced by these assigments.
 | 
|---|
 | 41 | The smart pointer class automatically delete's the
 | 
|---|
 | 42 | object when its reference count drops to zero.
 | 
|---|
 | 43 | 
 | 
|---|
 | 44 | A deficiency of this method is that unreferenced circular
 | 
|---|
 | 45 | lists are not automatically deleted.  Circular list
 | 
|---|
 | 46 | implementors must provide a mechanism to detect when the list
 | 
|---|
 | 47 | is dereferenced and then break the list's circularity to let
 | 
|---|
 | 48 | the automated reference mechanism finish the work.
 | 
|---|
 | 49 | 
 | 
|---|
 | 50 | The reference library provides smart pointers and a base class that
 | 
|---|
 | 51 | can be used to maintain reference counts to objects.  For an
 | 
|---|
 | 52 | object to be reference counted its class must inherit from
 | 
|---|
 | 53 | the RefCount class.  This adds <tt>sizeof(int)</tt> bytes
 | 
|---|
 | 54 | of overhead per object and makes the destructor virtual (so a vtable
 | 
|---|
 | 55 | will be added to objects of the class, if there wasn't already a virtual
 | 
|---|
 | 56 | member in the class).
 | 
|---|
 | 57 | 
 | 
|---|
 | 58 | The smart pointers that maintain the reference counts are
 | 
|---|
 | 59 | provided by the Ref class template.  A smart pointer to a
 | 
|---|
 | 60 | class A which inherits from RefCount would have the
 | 
|---|
 | 61 | type Ref<A>.
 | 
|---|
 | 62 | 
 | 
|---|
 | 63 | \section refthread Thread Safety of the Reference Counting Package
 | 
|---|
 | 64 | 
 | 
|---|
 | 65 | The referencing counting package is thread-safe if the CPP macro
 | 
|---|
 | 66 | REF_USE_LOCKS is defined to 1.  This means that Ref's to a particular
 | 
|---|
 | 67 | object can be created and reassigned and destroyed in different
 | 
|---|
 | 68 | threads.  However, the Ref's themselves are not thread-safe.
 | 
|---|
 | 69 | For example, a static Ref cannot be simultaneously modified from
 | 
|---|
 | 70 | multiple threads.
 | 
|---|
 | 71 | 
 | 
|---|
 | 72 | Because there is an overhead associated with locking access to an
 | 
|---|
 | 73 | object's reference count, locking is not turned on by default,
 | 
|---|
 | 74 | and, thus, making and deleting references to an object in
 | 
|---|
 | 75 | multiple threads is not thread-safe by default.  The
 | 
|---|
 | 76 | RefCount::use_locks member is passed a bool value to turn locking
 | 
|---|
 | 77 | on 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
 | 
|---|
 | 82 | with 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 | 
 | 
|---|
 | 102 | If a macro is undefined, then the behaviour is architecture
 | 
|---|
 | 103 | dependent---usually, the macro will be set to 1 in this case.
 | 
|---|
 | 104 | For maximum efficiency and for normal operation after the program is
 | 
|---|
 | 105 | debugged, compile with all of the above macros defined to zero.
 | 
|---|
 | 106 | This can also be done by defining REF_OPTIMIZE.
 | 
|---|
 | 107 | 
 | 
|---|
 | 108 |   An include file can be used to set these options as well.  This has
 | 
|---|
 | 109 | the advantage that dependency checking will force an automatic
 | 
|---|
 | 110 | recompile of all affected files if the options change.  This is done
 | 
|---|
 | 111 | in the file scconfig.h, which is produced by the automated configuration
 | 
|---|
 | 112 | procedure.
 | 
|---|
 | 113 | 
 | 
|---|
 | 114 |   Note that all source code that uses references must be compiled with
 | 
|---|
 | 115 | the same value for REF_MANAGE.
 | 
|---|
 | 116 | Changing this can change the storage layout and the interpretation of
 | 
|---|
 | 117 | the reference count data.
 | 
|---|
 | 118 | 
 | 
|---|
 | 119 | \section refexample A Reference Example
 | 
|---|
 | 120 | 
 | 
|---|
 | 121 | Following is a simple example of how to manage memory with reference
 | 
|---|
 | 122 | counts.
 | 
|---|
 | 123 | 
 | 
|---|
 | 124 | <pre>
 | 
|---|
 | 125 | \#include <util/container/ref.h>
 | 
|---|
 | 126 | 
 | 
|---|
 | 127 | class A: virtual public RefCount {};
 | 
|---|
 | 128 | 
 | 
|---|
 | 129 | class B: public A {};
 | 
|---|
 | 130 | 
 | 
|---|
 | 131 | int
 | 
|---|
 | 132 | main()
 | 
|---|
 | 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 | */
 | 
|---|