Polaris: HeapStats.h Source File

HeapStats.h

Go to the documentation of this file.
00001 ///
00002 ///
00003 #ifndef HEAPSTATS_H
00004 #define HEAPSTATS_H
00005 ///
00006 /// \class HeapStats 
00007 /// \brief A module for detecting and finding memory leaks
00008 /// \defgroup Polaris
00009 /// \ingroup Polaris
00010 ///  Base
00011 /// \see HeapStats.h
00012 /// \see HeapStats.h
00013 /// \see HeapStats.cc
00014 ///
00015 /// \endcode
00016 /// \section Overview Overview
00017 /// This module keeps tracks of the number of new\'s and delete\'s a
00018 /// program has generated.  The module also keeps track of the
00019 /// addresses of objects that have been allocated but not deleted.
00020 /// This module is primarily used for spotting memory leaks.
00021 ///
00022 /// \endcode
00023 /// \section Description Description
00024 /// The tracking the number of new\'s and delete\`s in a program can
00025 /// be started and stopped by the functions HeapStats::start() and
00026 /// HeapStats::stop().  These numbers can be printed out by the
00027 /// HeapStats::report() function.  They can also be set back to zero
00028 /// by the HeapStats::reset() function.
00029 ///
00030 /// In addition to keeping track of the numbers of new\`s and
00031 /// delete\`s in a program, HeapStats also keeps track of the
00032 /// addresses and sizes of objects that have been allocated but not
00033 /// deleted.  The function HeapStats::print_memory_leaks() can be
00034 /// used to print out these undeleted objects.  Like the tracking
00035 /// of the numbers of new\`s and delete\`s, the collection of these
00036 /// statistics is controlled by the start(), stop(), and reset()
00037 /// functions.
00038 ///
00039 #ifdef POLARIS_GNU_PRAGMAS
00040 #pragma interface
00041 #endif
00042 ///
00043 #include <new>
00044 #include <iostream.h>
00045 #include <stdlib.h>
00046 #include "Listable.h"
00047 #include "Collection/Map.h"
00048 #include "Collection/Zombie.h"
00049 ///
00050 typedef int      HeapObject;
00051 ///
00052 class HeapElem : public Listable {
00053  public:
00054     void       *ptr;
00055 ///<    size_t      size;
00056     unsigned int size;
00057 
00058     Listable* listable_clone() const;
00059     void      print(ostream &o) const;
00060 };
00061 
00062 class HeapStats {
00063 ///<    friend void* operator new(size_t);
00064     friend void* operator new(unsigned int) throw (std::bad_alloc);
00065     friend void  operator delete(void *) throw ();
00066 ///<    friend void  remember_memory( void *ptr, size_t size );
00067     friend void  remember_memory( void *ptr, unsigned int size );
00068 ///<    friend void* recall_memory( size_t size );
00069     friend void* recall_memory( unsigned int size );
00070 
00071  public:
00072     static void start();
00073     ///< Start collecting statistics.
00074 
00075     static void stop();
00076     ///< Stop collecting statistics.
00077 
00078     static void start_news();
00079     ///< Start collecting "new" statistics
00080     
00081     static void start_dels();
00082     ///< Start collecting delete statistics
00083     
00084     static void stop_news();
00085     ///< Stop collecting "new" statistics
00086     
00087     static void stop_dels();
00088     ///< Stop collecting delete statistics
00089     
00090     static void restart();
00091     ///< Start collecting statistics again.  Functions identically
00092     ///< to HeapStats::start().
00093 
00094     static void report(ostream &o = cout);
00095     ///< Print out number of new\'s and delete\'s
00096 
00097     static void reset(void);
00098     ///< Reset the counters
00099 
00100     static int memory_leak(void);
00101     ///< Return TRUE if number of new\'s do not match number of
00102     ///< delete\'s
00103 
00104     static void print_memory_leaks(ostream &o = cout);
00105     ///< Print all memory leaks that has been detected.
00106 
00107  private:
00108     static int collect_dels;   ///< True if HeapStats should collect statistics on deletes
00109     static int collect_news;   ///< True if HeapStats should collect statistics on news
00110     static int num_news;        ///< # of calls to operator new
00111     static int num_deletes;     ///< # of calls to operator delete
00112 
00113     static Map<HeapObject,HeapElem> *objs; 
00114     ///< List of objects allocated on heap
00115 
00116 ///<    static void *_hs_new(size_t);
00117     static void *_hs_new(unsigned int);
00118 ///<    static void *_hs_record( void *new_obj, size_t sz );
00119     static void *_hs_record( void *new_obj, unsigned int sz );
00120     static void  _hs_delete(void *);
00121     static void  _hs_remove(void *);
00122 };
00123 
00124 #endif
 © 1995-2005 University of Illinois, Urbana-Champaign. All rights reserved.  Fri Mar 25 23:05:54 2005