Main Page | Modules | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

LeaLeakChecker.cxx

Go to the documentation of this file.
00001 
00002 // $Id: LeaLeakChecker.cxx,v 1.8 2005/02/01 18:55:04 tagg Exp $
00003 //
00004 // LeaLeakChecker
00005 //
00006 // Package: Lea (Leak Checker).
00007 
00008 // Begin_Html<img src="../../pedestrians.gif" align=center>
00009 // <a href="../source_warning.html">Warning for beginners</a>.<br>
00010 // Also see <a href="../../root_crib/index.html">The ROOT Crib</a> and
00011 // <a href="../index.html">The MINOS Class User Guide</a>End_Html
00012 //
00013 // N. West 06/00
00014 //
00015 //   
00016 // Purpose: 
00017 //
00018 //
00020 
00021 #include <string>
00022 
00023 #include "LeakChecker/LeaClassMonitor.h"
00024 #include "LeakChecker/LeaLeakChecker.h"
00025 #include "MessageService/MsgService.h"
00026 
00027 std::ostream& operator<<(std::ostream& os, LeaLeakChecker* l) 
00028                                                  {return l->Print(os);}
00029 
00030 ClassImp(LeaLeakChecker)
00031 
00032 //   Definition of static data members
00033 //   *********************************
00034 
00035 CVSID("$Id: LeaLeakChecker.cxx,v 1.8 2005/02/01 18:55:04 tagg Exp $");
00036 
00037 LeaLeakChecker* LeaLeakChecker::fgInstance = 0;
00038 
00039 
00040 // Definition of member functions (alphabetical order)
00041 // ***************************************************
00042 
00043 //.....................................................................
00044 
00045 Bool_t LeaLeakChecker::Add(const Char_t* name, const void* addr) {
00046 
00047 
00048 //  Purpose:  Add named object to class monitor.
00049 
00050 //  Arguments: 
00051 //    name         in    String containing class name (can be file name).
00052 //    addr         in    Address of object.
00053 
00054 //  Return:    kTRUE always (reserved for future use).
00055 
00056 //  Contact:   N. West
00057 
00058 //  Specification:-
00059 //  =============
00060 
00061 //  o Get monitor for class and record object creation.
00062 
00063 //  Program Notes:-
00064 //  =============
00065 
00066 //  None.
00067 
00068   return GetMonitor(name)->Add(addr);
00069 
00070 }
00071 
00072 
00073 //.....................................................................
00074 
00075 LeaClassMonitor* LeaLeakChecker::GetExistingMonitor(const Char_t* name
00076                                                     ) const {
00077 
00078 
00079 //  Purpose:  Return monitor for this existing class.
00080 
00081 //  Arguments: 
00082 //    name         in    class name.
00083 
00084 //  Return:    Class Monitor addr, or 0 if none.
00085 
00086 //  Contact:   N. West
00087 
00088 //  Specification:-
00089 //  =============
00090 
00091 //  o  Return the LeaClassMonitor for this class if any.
00092 
00093 //  Program Notes:-
00094 //  =============
00095 
00096 //  None.
00097 
00098 
00099   std::string className = name;
00100 
00101 //  Locate the associated class monitor.
00102   MapConstItr element = fMonitors.find(className);
00103   return ( element != fMonitors.end() ) ? (*element).second : 0;
00104 
00105 }
00106 //.....................................................................
00107 
00108 LeaClassMonitor* LeaLeakChecker::GetMonitor(const Char_t* name) {
00109 
00110 
00111 //  Purpose:  Return monitor for this class, creating if necessary
00112 
00113 //  Arguments: 
00114 //    name         in    class name or file name.
00115 
00116 //  Return:    Class Monitor add.
00117 
00118 //  Contact:   N. West
00119 
00120 //  Specification:-
00121 //  =============
00122 
00123 //  o  Determine the class name if necessary from the file name.
00124 
00125 //  o  Return, creating if necessary and required, the LeaClassMonitor
00126 //     for this class.
00127 
00128 //  Program Notes:-
00129 //  =============
00130 
00131 //  None.
00132 
00133 //  Determine the class name from the file name by removing 
00134 //  leading directory and any trailing extension.
00135 
00136   std::string className = name;
00137   if ( className.find(".") < className.size() ) {
00138     UInt_t locSlash = className.rfind("/");
00139     if ( locSlash < className.size() ) className.erase(0,locSlash+1);
00140     className.erase(className.find("."),className.size());
00141   }
00142 
00143 
00144 //  Locate the associated class monitor and set its name if new.
00145   LeaClassMonitor* mon = fMonitors[className];
00146   if ( ! mon ) {
00147     mon = new LeaClassMonitor;
00148     fMonitors[className] = mon;
00149     mon->SetName(className.c_str());
00150   }
00151 
00152   MSG("Lea", Msg::kDebug) << "ClassMonitor: "
00153                          << className.c_str() << " found at "
00154                          << reinterpret_cast<void*> (mon)
00155                          << " set size " << fMonitors.size() << endl;
00156    
00157   return mon;
00158    
00159 
00160 }
00161 
00162 //.....................................................................
00163 
00164 UInt_t LeaLeakChecker::GetNumActive(const Char_t* name) const{
00165 
00166 
00167 //  Purpose:  Return the number of active objects of one or all
00168 //            classes.
00169 
00170 //  Arguments: 
00171 //    name         in    Class name string or 0 (default) all classes.
00172 
00173 //  Return:   Return the number of active objects of one or all
00174 //            classes. 
00175 
00176 //  Contact:   N. West
00177 
00178 //  Specification:-
00179 //  =============
00180 
00181 //  o  Return the number of active objects of one or all classes.
00182 
00183 //  Program Notes:-
00184 //  =============
00185 
00186 //  None.
00187 
00188 //  If name supplied, just use, but don't create, it.
00189 
00190   if ( name ) {
00191     LeaClassMonitor* mon = GetExistingMonitor(name);
00192     return mon ? mon->GetNumActive() : 0;
00193   }
00194 
00195 //  No name supplied, use all classs
00196 
00197   UInt_t num = 0;
00198   for ( MapConstItr itr = fMonitors.begin();
00199         itr != fMonitors.end();
00200         itr++) num += (*itr).second->GetNumActive();
00201   return num;
00202 
00203 }
00204 //.....................................................................
00205 
00206 UInt_t LeaLeakChecker::GetNumCreated(const Char_t* name) const {
00207 
00208 
00209 //  Purpose:  Return the number of created objects of one or all
00210 //            classes.
00211 
00212 //  Arguments: 
00213 //    name         in    Class name string or 0 (default) all classes.
00214 
00215 //  Return:   Return the number of created objects of one or all
00216 //            classes. 
00217 
00218 //  Contact:   N. West
00219 
00220 //  Specification:-
00221 //  =============
00222 
00223 //  o  Return the number of created objects of one or all classes.
00224 
00225 //  Program Notes:-
00226 //  =============
00227 
00228 //  None.
00229 
00230 //  If name supplied, just use, but don't create, it.
00231 
00232   if ( name ) {
00233     LeaClassMonitor* mon = GetExistingMonitor(name);
00234     return mon ? mon->GetNumCreated() : 0;
00235   }
00236 
00237 //  No name supplied, use all classs
00238 
00239   UInt_t num = 0;
00240   for ( MapConstItr itr = fMonitors.begin();
00241         itr != fMonitors.end();
00242         itr++)  num += (*itr).second->GetNumCreated();
00243   return num;
00244 
00245 }
00246 //.....................................................................
00247 
00248 LeaLeakChecker* LeaLeakChecker::Instance() {
00249 
00250 
00251 //  Purpose:  Return a pointer to the sole instance of the LeaLeakChecker. 
00252 
00253 //  Arguments: 
00254 //   None
00255 
00256 //  Return:  A pointer to the LeaLeakChecker. 
00257 
00258 //  Contact:   N. West
00259 
00260 //  Specification:-
00261 //  =============
00262 
00263 //  o Return a pointer to the sole instance of the LeaLeakChecker. 
00264 
00265 //  Program Notes:-
00266 //  =============
00267 
00268 //  None.
00269 
00270   if ( ! fgInstance ) {
00271 
00272 //  Helper class to handle delete
00273     static LeaLeakChecker::Cleaner c;
00274     c.ClassIsUsed();
00275 
00276     fgInstance = new LeaLeakChecker();
00277 
00278     MSG("Lea",Msg::kWarning) << "\n\n\n"
00279      << " WARNING:  The LeakChecker package has been activated.\n"
00280      << " *******   This can seriously degrade performance and should\n"
00281      << "           only be used for development and never production !!!\n\n\n";
00282 
00283   }
00284 
00285   return fgInstance;
00286 
00287 }
00288 //.....................................................................
00289 
00290 LeaLeakChecker::LeaLeakChecker() {
00291 
00292 
00293 //  Purpose:  Default constructor.
00294 
00295 //  Arguments: 
00296 //    None.
00297 
00298 //  Return:   n/a
00299 
00300 //  Contact:   N. West
00301 
00302 //  Specification:-
00303 //  =============
00304 
00305 //  o Create LeaLeakChecker.
00306 
00307 //  Program Notes:-
00308 //  =============
00309 
00310 //  None.
00311 
00312    MSG("Lea", Msg::kDebug) << "Creating LeaLeakChecker at "
00313                           << reinterpret_cast<void*>(this) << endl;
00314 
00315 }
00316 //.....................................................................
00317 
00318 LeaLeakChecker::~LeaLeakChecker() {
00319 
00320 
00321 //  Purpose:  Destructor.
00322 
00323 //  Arguments: 
00324 //    None.
00325 
00326 //  Return:   n/a
00327 
00328 //  Contact:   N. West
00329 
00330 //  Specification:-
00331 //  =============
00332 
00333 //  o Destroy LeaLeakChecker.
00334 
00335 //  Program Notes:-
00336 //  =============
00337 
00338 //  None.
00339 
00340   cout << "LeakChecker shutting down ..." << endl;
00341    for ( MapItr itr = fMonitors.begin();
00342          itr != fMonitors.end();
00343          itr++) {
00344      LeaClassMonitor* mon = (*itr).second;
00345      delete mon;
00346    }
00347   cout << "LeakChecker shutdown complete." << endl;
00348   LeaLeakChecker::fgInstance = 0;
00349    
00350 }
00351 
00352 //.....................................................................
00353 
00354 ostream& LeaLeakChecker::Print(ostream& os) {
00355 
00356 
00357 //  Purpose: Display current status of Leak Checker on ostream. 
00358 
00359 //  Arguments: 
00360 //    os           in    ostream to display on.
00361 
00362 //  Return:  Update ostream.
00363 
00364 //  Contact:   N. West
00365 
00366 //  Specification:-
00367 //  =============
00368 
00369 //  o Display current status of Leak Checker on ostream.
00370 
00371 //  Program Notes:-
00372 //  =============
00373 
00374 //  None.
00375 
00376   os << "Currently " << fMonitors.size() 
00377      << " classes are being monitored. " << endl
00378      << "There are "
00379      << GetNumActive() << " active objects of a total "
00380      << GetNumCreated() << " created." << endl
00381      << "Breakdown by class:-" << endl;
00382 
00383    for ( MapItr itr = fMonitors.begin();
00384          itr != fMonitors.end();
00385          itr++) {
00386      LeaClassMonitor* mon = (*itr).second;
00387      mon->Print(os);
00388    }
00389 
00390   return os;
00391 
00392 }
00393 //.....................................................................
00394 
00395 void LeaLeakChecker::Reset(const Char_t* name) {
00396 
00397 
00398 //  Purpose:  Clear the number of objects of one or all classes.
00399 
00400 //  Arguments: 
00401 //    name         in    Class name string or 0 (default) all classes.
00402 
00403 //  Return:   None. 
00404 
00405 //  Contact:   N. West
00406 
00407 //  Specification:-
00408 //  =============
00409 
00410 //  o  Reseet the number of objects of one or all classes.
00411 
00412 //  Program Notes:-
00413 //  =============
00414 
00415 //  None.
00416 
00417 //  If name supplied, just use, but don't create, it.
00418 
00419   if ( name ) {
00420     LeaClassMonitor* mon = GetExistingMonitor(name);
00421     if ( mon ) mon->Reset();
00422   }
00423 
00424 //  No name supplied, use all classs
00425 
00426   for ( MapConstItr itr = fMonitors.begin();
00427         itr != fMonitors.end();
00428         itr++) (*itr).second->Reset();
00429 
00430 }
00431 
00432 //.....................................................................
00433 
00434 Bool_t LeaLeakChecker::Remove(const Char_t* name, const void* addr) {
00435 
00436 
00437 //  Purpose:  Remove named object to class monitor.
00438 
00439 //  Arguments: 
00440 //    name         in    String containing class name (can be file name).
00441 //    addr         in    Address of object.
00442 
00443 //  Return:    kTRUE always (reserved for future use).
00444 
00445 //  Contact:   N. West
00446 
00447 //  Specification:-
00448 //  =============
00449 
00450 //  o Get monitor for class and record object destruction.
00451 
00452 //  Program Notes:-
00453 //  =============
00454 
00455 //  None.
00456 
00457   return GetMonitor(name)->Remove(addr);
00458 
00459 }
00460 
00461 //.....................................................................
00462 
00463 
00464 /*    Template for New Member Function
00465 
00466 //.....................................................................
00467 
00468 LeaLeakChecker:: {
00469 
00470 
00471 //  Purpose:  
00472 
00473 //  Arguments: 
00474 //    xxxxx        in    xxxxxxxxxxxxxxxxxx
00475 
00476 //  Return:   
00477 
00478 //  Contact:   N. West
00479 
00480 //  Specification:-
00481 //  =============
00482 
00483 //  o 
00484 
00485 //  Program Notes:-
00486 //  =============
00487 
00488 //  None.
00489 
00490 
00491 }
00492 
00493 */
00494 

Generated on Mon Feb 15 11:06:51 2010 for loon by  doxygen 1.3.9.1