$extrastylesheet
reference_counter.h
Go to the documentation of this file.
00001 // The libMesh Finite Element Library.
00002 // Copyright (C) 2002-2014 Benjamin S. Kirk, John W. Peterson, Roy H. Stogner
00003 
00004 // This library is free software; you can redistribute it and/or
00005 // modify it under the terms of the GNU Lesser General Public
00006 // License as published by the Free Software Foundation; either
00007 // version 2.1 of the License, or (at your option) any later version.
00008 
00009 // This library is distributed in the hope that it will be useful,
00010 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00011 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012 // Lesser General Public License for more details.
00013 
00014 // You should have received a copy of the GNU Lesser General Public
00015 // License along with this library; if not, write to the Free Software
00016 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00017 
00018 
00019 
00020 #ifndef LIBMESH_REFERENCE_COUNTER_H
00021 #define LIBMESH_REFERENCE_COUNTER_H
00022 
00023 // Local includes
00024 #include "libmesh/libmesh_config.h"
00025 #include "libmesh/threads.h"
00026 #include "libmesh/libmesh.h" // libMesh::on_command_line
00027 
00028 // C++ includes
00029 #include <iostream>
00030 #include <string>
00031 #include <map>
00032 
00033 namespace libMesh
00034 {
00035 
00036 
00037 
00045 // ------------------------------------------------------------
00046 // ReferenceCounter class definition
00047 class ReferenceCounter
00048 {
00049 protected:
00050 
00056   ReferenceCounter ();
00057 
00058 public:
00059 
00063   ~ReferenceCounter ();
00064 
00068   static std::string get_info ();
00069 
00073   static void print_info (std::ostream& out = libMesh::out);
00074 
00079   static unsigned int n_objects ()
00080   { return _n_objects; }
00081 
00086   static void enable_print_counter_info();
00087   static void disable_print_counter_info();
00088 
00089 
00090 protected:
00091 
00092 #if defined(LIBMESH_ENABLE_REFERENCE_COUNTING) && defined(DEBUG)
00093 
00099   void increment_constructor_count (const std::string& name);
00100 
00106   void increment_destructor_count (const std::string& name);
00107 
00112   typedef std::map<std::string, std::pair<unsigned int,
00113                                           unsigned int> > Counts;
00114 
00118   static Counts _counts;
00119 
00120 #endif
00121 
00126   static Threads::atomic<unsigned int> _n_objects;
00127 
00131   static Threads::spin_mutex _mutex;
00132 
00137   static bool _enable_print_counter;
00138 };
00139 
00140 
00141 
00142 // ------------------------------------------------------------
00143 // ReferenceCounter class inline methods
00144 inline ReferenceCounter::ReferenceCounter()
00145 {
00146   ++_n_objects;
00147 }
00148 
00149 
00150 
00151 inline ReferenceCounter::~ReferenceCounter()
00152 {
00153   --_n_objects;
00154 }
00155 
00156 
00157 
00158 
00159 
00160 
00161 #if defined(LIBMESH_ENABLE_REFERENCE_COUNTING) && defined(DEBUG)
00162 inline
00163 void ReferenceCounter::increment_constructor_count (const std::string& name)
00164 {
00165   Threads::spin_mutex::scoped_lock lock(Threads::spin_mtx);
00166   std::pair<unsigned int, unsigned int>& p = _counts[name];
00167 
00168   p.first++;
00169 }
00170 #endif
00171 
00172 
00173 
00174 #if defined(LIBMESH_ENABLE_REFERENCE_COUNTING) && defined(DEBUG)
00175 inline
00176 void ReferenceCounter::increment_destructor_count (const std::string& name)
00177 {
00178   Threads::spin_mutex::scoped_lock lock(Threads::spin_mtx);
00179   std::pair<unsigned int, unsigned int>& p = _counts[name];
00180 
00181   p.second++;
00182 }
00183 #endif
00184 
00185 
00186 } // namespace libMesh
00187 
00188 
00189 #endif // LIBMESH_REFERENCE_COUNTER_H