reference_counter.h
Go to the documentation of this file.00001 // $Id: reference_counter.h 3391 2009-05-26 03:50:35Z benkirk $ 00002 00003 // The libMesh Finite Element Library. 00004 // Copyright (C) 2002-2008 Benjamin S. Kirk, John W. Peterson, Roy H. Stogner 00005 00006 // This library is free software; you can redistribute it and/or 00007 // modify it under the terms of the GNU Lesser General Public 00008 // License as published by the Free Software Foundation; either 00009 // version 2.1 of the License, or (at your option) any later version. 00010 00011 // This library is distributed in the hope that it will be useful, 00012 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00014 // Lesser General Public License for more details. 00015 00016 // You should have received a copy of the GNU Lesser General Public 00017 // License along with this library; if not, write to the Free Software 00018 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 00019 00020 00021 00022 #ifndef __reference_counter_h__ 00023 #define __reference_counter_h__ 00024 00025 // Local includes 00026 #include "libmesh_config.h" 00027 #include "threads.h" 00028 00029 // C++ includes 00030 #include <string> 00031 #include <map> 00032 00033 00034 00042 // ------------------------------------------------------------ 00043 // ReferenceCounter class definition 00044 class ReferenceCounter 00045 { 00046 protected: 00047 00053 ReferenceCounter (); 00054 00055 public: 00056 00060 virtual ~ReferenceCounter (); 00061 00065 static std::string get_info (); 00066 00070 static void print_info (); 00071 00076 static unsigned int n_objects () 00077 { return _n_objects; } 00078 00079 00080 protected: 00081 00082 00088 void increment_constructor_count (const std::string& name); 00089 00095 void increment_destructor_count (const std::string& name); 00096 00097 00098 #if defined(LIBMESH_ENABLE_REFERENCE_COUNTING) && defined(DEBUG) 00099 00104 typedef std::map<std::string, std::pair<unsigned int, 00105 unsigned int> > Counts; 00106 00110 static Counts _counts; 00111 00112 #endif 00113 00118 static Threads::atomic<unsigned int> _n_objects; 00119 00123 static Threads::spin_mutex _mutex; 00124 }; 00125 00126 00127 00128 // ------------------------------------------------------------ 00129 // ReferenceCounter class inline methods 00130 inline ReferenceCounter::ReferenceCounter() 00131 { 00132 _n_objects++; 00133 } 00134 00135 00136 00137 inline ReferenceCounter::~ReferenceCounter() 00138 { 00139 _n_objects--; 00140 } 00141 00142 00143 00144 00145 00146 00147 #if defined(LIBMESH_ENABLE_REFERENCE_COUNTING) && defined(DEBUG) 00148 inline 00149 void ReferenceCounter::increment_constructor_count (const std::string& name) 00150 { 00151 Threads::spin_mutex::scoped_lock lock(Threads::spin_mtx); 00152 std::pair<unsigned int, unsigned int>& p = _counts[name]; 00153 00154 p.first++; 00155 } 00156 #else 00157 inline 00158 void ReferenceCounter::increment_constructor_count (const std::string&) 00159 { 00160 } 00161 #endif 00162 00163 00164 00165 #if defined(LIBMESH_ENABLE_REFERENCE_COUNTING) && defined(DEBUG) 00166 inline 00167 void ReferenceCounter::increment_destructor_count (const std::string& name) 00168 { 00169 Threads::spin_mutex::scoped_lock lock(Threads::spin_mtx); 00170 std::pair<unsigned int, unsigned int>& p = _counts[name]; 00171 00172 p.second++; 00173 } 00174 #else 00175 inline 00176 void ReferenceCounter::increment_destructor_count (const std::string&) 00177 { 00178 } 00179 #endif 00180 00181 00182 00183 #endif // end #ifndef __reference_counter_h__ 00184 00185 00186 00187