29 #include <agrum/agrum.h> 39 #ifndef DOXYGEN_SHOULD_SKIP_THIS 45 # ifdef GUM_DEBUG_MODE 48 typedef std::map< std::string,
int > DEBUG_MAP;
50 static std::mutex debug_mutex;
53 static DEBUG_MAP& _sizeof_() {
55 static DEBUG_MAP* sizeOf =
new DEBUG_MAP();
60 static DEBUG_MAP& _creation_() {
62 static DEBUG_MAP* creation =
new DEBUG_MAP();
66 static DEBUG_MAP& _deletion_() {
68 static DEBUG_MAP* deletion =
new DEBUG_MAP();
72 std::string _getFile_(
const char* f) {
74 return s.erase(0, s.rfind(
"/") + 1);
77 void _show_trace_(
const char* zeKey,
82 # ifdef GUM_DEEP_TRACE_ON 83 std::cerr << std::setw(40) << std::setfill(
' ') << _getFile_(zeFile) <<
"#" 84 << std::setfill(
'0') << std::setw(5) << std::dec << zeLine <<
" : " << zeMsg <<
" <" 85 << zeKey <<
"> [" << std::hex << zePtr <<
"]" << std::dec << std::endl;
89 void _inc_creation_(
const char* zeKey,
96 _show_trace_(zeKey, zeFile, zeLine, zeMsg, zePtr);
97 _creation_()[zeKey]++;
98 _sizeof_()[zeKey] = zeSize;
103 void _dec_creation_(
const char* zeKey,
109 _show_trace_(zeKey, zeFile, zeLine, zeMsg, zePtr);
110 _creation_()[zeKey]--;
111 debug_mutex.unlock();
114 void _inc_deletion_(
const char* zeKey,
120 _show_trace_(zeKey, zeFile, zeLine, zeMsg, zePtr);
121 _deletion_()[zeKey]++;
122 debug_mutex.unlock();
125 void _dumpObjects_() {
127 double total_size = 0.0;
130 int widthColLibelle = 50;
131 int widthColSizeOf = 5;
132 int widthColItemsNumber = 8;
134 std::cout << std::setfill(
'=') <<
"|" << std::setw(widthColLibelle + 2) <<
"" 135 <<
"|" << std::setw(widthColSizeOf + 4) <<
"" 136 <<
"|" << std::setw(widthColItemsNumber + 2) <<
"" 137 <<
"|" << std::setw(widthColItemsNumber + 2) <<
"" 139 std::cout << std::setfill(
' ') <<
"| " << std::left << std::setw(widthColLibelle)
140 <<
"Class Name" << std::right <<
" | " << std::setw(widthColSizeOf) <<
"Size" 141 <<
" | " << std::setw(widthColItemsNumber) <<
"#Const" 142 <<
" | " << std::setw(widthColItemsNumber) <<
"#Dest" 143 <<
" |" << std::endl;
144 std::cout << std::setfill(
'-') <<
"|" << std::setw(widthColLibelle + 2) <<
"" 145 <<
"|" << std::setw(widthColSizeOf + 4) <<
"" 146 <<
"|" << std::setw(widthColItemsNumber + 2) <<
"" 147 <<
"|" << std::setw(widthColItemsNumber + 2) <<
"" 150 std::map< std::string, std::string > res;
152 for (DEBUG_MAP::const_iterator xx = _creation_().begin(); xx != _creation_().end(); ++xx) {
153 std::stringstream stream;
154 int zeCreatedObjs = xx->second;
155 int zeDeletedObjts = -1;
156 int size = _sizeof_()[xx->first];
158 stream << std::setfill(fillChar = (fillChar ==
'_') ?
' ' :
'_') <<
"| " 159 << std::setw(widthColLibelle) << std::left << xx->first <<
" | " << std::right
160 << std::setw(widthColSizeOf) << size <<
" o | " << std::setw(widthColItemsNumber)
161 << zeCreatedObjs <<
" | ";
163 if (size > 0) total_size += zeCreatedObjs * (size / 1024.0);
166 zeDeletedObjts = _deletion_()[xx->first];
167 stream << std::setfill(fillChar) << std::setw(widthColItemsNumber) << zeDeletedObjts;
168 }
catch (NotFound&) {
169 stream << std::setfill(fillChar) << std::setw(widthColItemsNumber) <<
"?????";
175 if (zeCreatedObjs != zeDeletedObjts) {
176 nb_err += std::abs(zeDeletedObjts - zeCreatedObjs);
177 stream <<
"<--- failed";
180 res.insert(make_pair(xx->first, stream.str()));
185 for (DEBUG_MAP::const_iterator xx = _deletion_().begin(); xx != _deletion_().end(); ++xx) {
187 _creation_()[xx->first];
188 }
catch (NotFound&) {
189 std::stringstream stream;
190 fillChar = (fillChar ==
'_') ?
' ' :
'_';
191 stream << std::setfill(fillChar = (fillChar ==
'_') ?
' ' :
'_') <<
"| " 192 << std::setw(widthColLibelle) << std::left << xx->first +
" " 193 <<
" | " << std::right << std::setw(widthColSizeOf) << _sizeof_()[xx->first]
194 <<
" o | " << std::setw(widthColItemsNumber) <<
"?????" 195 <<
" | " << std::setw(widthColItemsNumber) << xx->second <<
" |<--- failed";
196 res.insert(make_pair(xx->first, stream.str()));
198 nb_err += xx->second;
202 for (
const auto iter: res) {
203 std::cout << iter.second << std::endl;
206 std::cout << std::setfill(
'-');
208 std::cout <<
"|-" << std::setw(widthColLibelle) <<
"" 209 <<
"-|-" << std::setw(widthColSizeOf + 2) <<
"" 210 <<
"-|-" << std::setw(widthColItemsNumber) <<
"" 211 <<
"-|-" << std::setw(widthColItemsNumber) <<
"" 212 <<
"-|" << std::endl;
214 std::cout << std::setfill(
' ');
217 std::cout <<
"| " << std::setw(widthColLibelle) <<
"NO MEMORY LEAK !" 218 <<
" | " << std::setw(widthColSizeOf + widthColItemsNumber * 2 + 9) <<
"" 221 std::cout <<
"| " << std::setw(widthColLibelle) <<
"Memory leaks found " 223 <<
" | " << std::setw(widthColSizeOf + widthColItemsNumber * 2 - 6) << nb_err
228 std::cout <<
"| " << std::setw(widthColLibelle) <<
"total " 229 <<
" | " << std::fixed << std::setw(widthColSizeOf + widthColItemsNumber * 2 - 4)
230 << std::setprecision(2) << total_size <<
" Ko " 233 std::cout << std::setfill(
'=') <<
"|" << std::setw(widthColLibelle + 2) <<
"" 234 <<
"|" << std::setw(widthColSizeOf + widthColItemsNumber * 2 + 10) <<
"" 240 void _staticCorrections_() {}
243 _staticCorrections_();
245 _creation_().clear();
246 _deletion_().clear();