⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ref.dox

📁 大型并行量子化学软件;支持密度泛函(DFT)。可以进行各种量子化学计算。支持CHARMM并行计算。非常具有应用价值。
💻 DOX
字号:
/** \page ref The Reference LibraryThe Reference Library provides a means to automatically freememory that is no longer needed.<ul>  <li> \ref refintro  <li> \ref refthread  <li> \ref refcust  <li> \ref refexample</ul>\section refintro Introduction to Reference CountingIt is fairly easy in C++ to create a pointer to an object thatactually references invalid memory.  One common way to do thisis to create an object with new and store thatobject's pointer.  Then the pointer is given to anotherobject's member function as an argument which keeps a copy ofthe pointer for future use.  After the member functionreturns, the routine that originally created the objectdelete's it, not knowing that another object has sincecreated a reference to the object.  The result of using thedelete'ed object is unpredictable and would likely bea program crash.  It is up to the programmer to provide thelogic necessary to avoid this problem.  The programmer mustalso deal with the problem of calling to deleteoperator on any new'ed memory when it is no longerreferenced.Reference counting is one technique that can be applied toautomate memory management.  In this approach, a count of howmany pointers point to an object is attached to that object.This count is managed by a smart pointer class which mimicsthe behavior of C++ pointers by providing<tt>operator->()</tt>.  This class has a pointer to thereference counted object and increments the reference count ofobjects when they are assigned to it while decrementing thecounts of the objects that are displaced by these assigments.The smart pointer class automatically delete's theobject when its reference count drops to zero.A deficiency of this method is that unreferenced circularlists are not automatically deleted.  Circular listimplementors must provide a mechanism to detect when the listis dereferenced and then break the list's circularity to letthe automated reference mechanism finish the work.The reference library provides smart pointers and a base class thatcan be used to maintain reference counts to objects.  For anobject to be reference counted its class must inherit fromthe RefCount class.  This adds <tt>sizeof(int)</tt> bytesof overhead per object and makes the destructor virtual (so a vtablewill be added to objects of the class, if there wasn't already a virtualmember in the class).The smart pointers that maintain the reference counts areprovided by the Ref class template.  A smart pointer to aclass A which inherits from RefCount would have thetype Ref<A>.\section refthread Thread Safety of the Reference Counting PackageThe referencing counting package is thread-safe if the CPP macroREF_USE_LOCKS is defined to 1.  This means that Ref's to a particularobject can be created and reassigned and destroyed in differentthreads.  However, the Ref's themselves are not thread-safe.For example, a static Ref cannot be simultaneously modified frommultiple threads.Because there is an overhead associated with locking access to anobject's reference count, locking is not turned on by default,and, thus, making and deleting references to an object inmultiple threads is not thread-safe by default.  TheRefCount::use_locks member is passed a bool value to turn lockingon and off on a per object basis.\section refcust Customizing the Reference Counting Package  The behaviour of the package can be modified at compile timewith the following five macros, each of which should be undefined, 0, or 1:<dl><dt><tt>REF_CHECK_STACK</tt><dd>  If this is 1, referenced objects are checked to see if they  reside on the stack, in which case storage for the object is not managed,  if management is enabled.<dt><tt>REF_MANAGE</tt><dd>  If this is 1, the unmanage member is enabled.<dt><tt>REF_CHECK_MAX_NREF</tt><dd>  If this is 1, the reference count is checked before  it is incremented to make sure it isn't too big.<dt><tt>REF_CHECK_MIN_NREF</tt><dd>  If this is 1, the reference count is checked before  it is decremented to make sure it isn't already zero.<dt><tt>REF_USE_LOCKS</tt><dd>  If this is 1, modification of the reference count  is locked to allow thread-safe execution.</dl>If a macro is undefined, then the behaviour is architecturedependent---usually, the macro will be set to 1 in this case.For maximum efficiency and for normal operation after the program isdebugged, compile with all of the above macros defined to zero.This can also be done by defining REF_OPTIMIZE.  An include file can be used to set these options as well.  This hasthe advantage that dependency checking will force an automaticrecompile of all affected files if the options change.  This is donein the file scconfig.h, which is produced by the automated configurationprocedure.  Note that all source code that uses references must be compiled withthe same value for REF_MANAGE.Changing this can change the storage layout and the interpretation ofthe reference count data.\section refexample A Reference ExampleFollowing is a simple example of how to manage memory with referencecounts.<pre>\#include <util/container/ref.h>class A: virtual public RefCount {};class B: public A {};intmain(){  Ref\<A\> a1(new A);  Ref\<A\> a2;  // Create another reference to the A object pointed to by a1.  a2 = a1;  // Make a2 refer to a new A object.  a2 = new A;  // a2 was the only reference to the second A object, so setting  // a2 to the null object will cause the second A object to be  // deleted.  a2 = 0;  Ref\<B\> b(new B);  // An object of type Ref\<X\> can be assigned to an object of type  // Ref\<Y\> as long as X* can be assigned to Y*.  a1 = b;  // An automatic dynamic cast can be done by using the left shift  // operator.  b << a1;  // The B object will be deleted here because all of the references  // to it go out of scope and destroyed.  return 0;}</pre>*/

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -