libMesh
reference_counter.h
Go to the documentation of this file.
1 // The libMesh Finite Element Library.
2 // Copyright (C) 2002-2017 Benjamin S. Kirk, John W. Peterson, Roy H. Stogner
3 
4 // This library is free software; you can redistribute it and/or
5 // modify it under the terms of the GNU Lesser General Public
6 // License as published by the Free Software Foundation; either
7 // version 2.1 of the License, or (at your option) any later version.
8 
9 // This library is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 // Lesser General Public License for more details.
13 
14 // You should have received a copy of the GNU Lesser General Public
15 // License along with this library; if not, write to the Free Software
16 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 
18 
19 
20 #ifndef LIBMESH_REFERENCE_COUNTER_H
21 #define LIBMESH_REFERENCE_COUNTER_H
22 
23 // Local includes
24 #include "libmesh/libmesh_config.h"
25 #include "libmesh/threads.h"
26 #include "libmesh/libmesh.h" // libMesh::on_command_line
27 
28 // C++ includes
29 #include <iostream>
30 #include <string>
31 #include <map>
32 
33 namespace libMesh
34 {
35 
45 {
46 protected:
47 
55 
56 
57 #ifdef LIBMESH_HAVE_CXX11_MOVE_CONSTRUCTORS
58 
61  ReferenceCounter(ReferenceCounter && other) noexcept;
62 #endif
63 
64 public:
65 
70 
74  static std::string get_info ();
75 
79  static void print_info (std::ostream & out = libMesh::out);
80 
85  static unsigned int n_objects ()
86  { return _n_objects; }
87 
92  static void enable_print_counter_info();
93  static void disable_print_counter_info();
94 
95 
96 protected:
97 
98 #if defined(LIBMESH_ENABLE_REFERENCE_COUNTING) && defined(DEBUG)
99 
105  void increment_constructor_count (const std::string & name);
106 
112  void increment_destructor_count (const std::string & name);
113 
118  typedef std::map<std::string, std::pair<unsigned int,
119  unsigned int>> Counts;
120 
124  static Counts _counts;
125 
126 #endif
127 
133 
138 
144 };
145 
146 
147 
148 // ------------------------------------------------------------
149 // ReferenceCounter class inline methods
151 {
152  ++_n_objects;
153 }
154 
155 
156 
158 {
159  ++_n_objects;
160 }
161 
162 
163 
164 #ifdef LIBMESH_HAVE_CXX11_MOVE_CONSTRUCTORS
166 {
167  ++_n_objects;
168 }
169 #endif
170 
171 
172 
174 {
175  --_n_objects;
176 }
177 
178 
179 
180 
181 
182 
183 #if defined(LIBMESH_ENABLE_REFERENCE_COUNTING) && defined(DEBUG)
184 inline
186 {
187  Threads::spin_mutex::scoped_lock lock(Threads::spin_mtx);
188  std::pair<unsigned int, unsigned int> & p = _counts[name];
189 
190  p.first++;
191 }
192 #endif
193 
194 
195 
196 #if defined(LIBMESH_ENABLE_REFERENCE_COUNTING) && defined(DEBUG)
197 inline
199 {
200  Threads::spin_mutex::scoped_lock lock(Threads::spin_mtx);
201  std::pair<unsigned int, unsigned int> & p = _counts[name];
202 
203  p.second++;
204 }
205 #endif
206 
207 
208 } // namespace libMesh
209 
210 
211 #endif // LIBMESH_REFERENCE_COUNTER_H
std::string name(const ElemQuality q)
This function returns a string containing some name for q.
Definition: elem_quality.C:39
static Threads::atomic< unsigned int > _n_objects
The number of objects.
static unsigned int n_objects()
Prints the number of outstanding (created, but not yet destroyed) objects.
void increment_constructor_count(const std::string &name)
Increments the construction counter.
The libMesh namespace provides an interface to certain functionality in the library.
std::map< std::string, std::pair< unsigned int, unsigned int > > Counts
Data structure to log the information.
static std::string get_info()
Gets a string containing the reference information.
This is the base class for enabling reference counting.
static Threads::spin_mutex _mutex
Mutual exclusion object to enable thread-safe reference counting.
spin_mutex spin_mtx
A convenient spin mutex object which can be used for obtaining locks.
Definition: threads.C:29
void increment_destructor_count(const std::string &name)
Increments the destruction counter.
static bool _enable_print_counter
Flag to control whether reference count information is printed when print_info is called...
static void enable_print_counter_info()
Methods to enable/disable the reference counter output from print_info()
ReferenceCounter()
Constructors.
static void print_info(std::ostream &out=libMesh::out)
Prints the reference information, by default to libMesh::out.
static Counts _counts
Actually holds the data.
OStreamProxy out
static void disable_print_counter_info()
void ErrorVector unsigned int
Definition: adjoints_ex3.C:360