libMesh
reference_counter.C
Go to the documentation of this file.
1 // The libMesh Finite Element Library.
2 // Copyright (C) 2002-2024 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 // C++ includes
21 #include <iostream>
22 #include <sstream>
23 
24 // Local includes
25 #include "libmesh/reference_counter.h"
26 
27 namespace libMesh
28 {
29 
30 
31 
32 // ------------------------------------------------------------
33 // ReferenceCounter class static member initializations
34 #if defined(LIBMESH_ENABLE_REFERENCE_COUNTING) && defined(DEBUG)
35 
37 
38 #endif
39 
41 Threads::atomic<unsigned int> ReferenceCounter::_n_objects;
43 
44 
45 // ------------------------------------------------------------
46 // ReferenceCounter class members
48 {
49 #if defined(LIBMESH_ENABLE_REFERENCE_COUNTING) && defined(DEBUG)
50 
51  std::ostringstream oss;
52 
53  oss << '\n'
54  << " ---------------------------------------------------------------------------- \n"
55  << "| Reference count information |\n"
56  << " ---------------------------------------------------------------------------- \n";
57 
58  for (const auto & [name, cd] : _counts)
59  oss << "| " << name << " reference count information:\n"
60  << "| Creations: " << cd.first << '\n'
61  << "| Destructions: " << cd.second << '\n';
62 
63  oss << " ---------------------------------------------------------------------------- \n";
64 
65  return oss.str();
66 
67 #else
68 
69  return "";
70 
71 #endif
72 }
73 
74 
75 
76 
77 
78 // avoid unused variable warnings
79 #if defined(LIBMESH_ENABLE_REFERENCE_COUNTING) && defined(DEBUG)
80 
81 void ReferenceCounter::print_info (std::ostream & out_stream)
82 {
84  out_stream << ReferenceCounter::get_info();
85 }
86 
87 #else
88 
89 void ReferenceCounter::print_info (std::ostream & /* out_stream */)
90 {}
91 
92 #endif
93 
95 {
96  _enable_print_counter = true;
97  return;
98 }
99 
101 {
102  _enable_print_counter = false;
103  return;
104 }
105 
106 } // namespace libMesh
std::string name(const ElemQuality q)
This function returns a string containing some name for q.
Definition: elem_quality.C:42
static Threads::atomic< unsigned int > _n_objects
The number of objects.
static void print_info(std::ostream &out_stream=libMesh::out)
Prints the reference information, by default to libMesh::out.
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.
static Threads::spin_mutex _mutex
Mutual exclusion object to enable thread-safe reference counting.
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()
tbb::spin_mutex spin_mutex
Spin mutex.
Definition: threads_tbb.h:167
static Counts _counts
Actually holds the data.
static void disable_print_counter_info()