28 using namespace DD4hep;
33 typedef std::map<const std::type_info*, COUNT*> TypeCounter;
34 typedef std::map<const std::string*, COUNT*> StringCounter;
35 static bool s_trace_instances = ::getenv(
"DD4HEP_TRACE") != 0;
41 inline TypeCounter& types() {
42 return *(s_typCounts.get());
44 inline StringCounter& strings() {
45 return *(s_strCounts.get());
50 ~_Global() { s_global = 0; }
52 int on_exit_destructors() {
53 static bool first =
true;
54 if ( first && s_global == 0 && s_trace_instances ) {
56 ::printf(
"Static out of order destructors occurred. Reference count table is unreliable.....\n");
63 InstanceCount::InstanceCount() {
67 InstanceCount::~InstanceCount() {
69 if (0 == s_thisCount.
value()) {
70 StringCounter::iterator i;
71 TypeCounter::iterator j;
72 dump(s_trace_instances ? ALL : NONE);
73 for (i = s_strCounts->begin(); i != s_strCounts->end(); ++i)
75 for (j = s_typCounts->begin(); j != s_typCounts->end(); ++j)
82 bool InstanceCount::doTrace() {
83 return s_trace_instances;
86 void InstanceCount::doTracing(
bool value) {
87 s_trace_instances = value;
91 Counter* cnt = s_trace_instances ? types()[&typ] : &s_nullCount;
92 return (0 != cnt) ? cnt : types()[&typ] =
new Counter();
97 Counter* cnt = s_trace_instances ? strings()[&typ] : &s_nullCount;
98 return (0 != cnt) ? cnt : strings()[&typ] =
new Counter();
102 void InstanceCount::increment(
const std::string& typ) {
104 getCounter(typ)->increment();
106 on_exit_destructors();
110 void InstanceCount::decrement(
const std::string& typ) {
112 getCounter(typ)->decrement();
114 on_exit_destructors();
118 void InstanceCount::increment(
const std::type_info& typ) {
120 getCounter(typ)->increment();
122 on_exit_destructors();
126 void InstanceCount::decrement(
const std::type_info& typ) {
128 getCounter(typ)->decrement();
130 on_exit_destructors();
134 void InstanceCount::dump(
int typ) {
135 bool need_footer =
false;
136 if ((typ & STRING) && s_strCounts.get()) {
137 if (s_strCounts->begin() != s_strCounts->end()) {
138 StringCounter::const_iterator i;
139 std::cout <<
"+-----------------------------------------------------------------------+" << std::endl;
140 std::cout <<
"| I n s t a n c e c o u n t e r s b y N A M E |" << std::endl;
141 std::cout <<
"+----------+------+---------+-------------------------------------------+" << std::endl;
142 std::cout <<
"| Total | Max | Leaking | Type identifier |" << std::endl;
143 std::cout <<
"+----------+------+---------+-------------------------------------------+" << std::endl;
144 for (i = s_strCounts->begin(); i != s_strCounts->end(); ++i) {
145 std::cout <<
"|" << std::setw(10) << (*i).second->total()
146 <<
"|" << std::setw(6) << (*i).second->maximum()
147 <<
"|" << std::setw(9) << (*i).second->value()
148 <<
"|" << (*i).first << std::endl;
153 if ((typ & TYPEINFO) && s_typCounts.get()) {
154 if (s_typCounts->begin() != s_typCounts->end()) {
155 TypeCounter::const_iterator i;
156 std::cout <<
"+-----------------------------------------------------------------------+" << std::endl;
157 std::cout <<
"| I n s t a n c e c o u n t e r s b y T Y P E I N F O |" << std::endl;
158 std::cout <<
"+----------+------+---------+-------------------------------------------+" << std::endl;
159 std::cout <<
"| Total | Max | Leaking | Type identifier |" << std::endl;
160 std::cout <<
"+----------+------+---------+-------------------------------------------+" << std::endl;
161 for (i = s_typCounts->begin(); i != s_typCounts->end(); ++i) {
162 std::cout <<
"|" << std::setw(10) << (*i).second->total()
163 <<
"|" << std::setw(6) << (*i).second->maximum()
164 <<
"|" << std::setw(9) << (*i).second->value()
165 <<
"|" <<
typeName(*((*i).first)) << std::endl;
171 std::cout <<
"+----------+-------+-------------------------------------------+" << std::endl;
void decrement()
Decrement counter.
std::string typeName(const std::type_info &type)
ABI information about type names.
Out version of the std auto_ptr implementation base either on auto_ptr or unique_ptr.
void increment()
Increment counter.
Helper to support object counting when debugging memory leaks.
Internal class to could object constructions and destructions.
counter_t value() const
Access counter value.