memdebug.h

00001 /*  _________________________________________________________________________
00002  *
00003  *  UTILIB: A utility library for developing portable C++ codes.
00004  *  Copyright (c) 2001, Sandia National Laboratories.
00005  *  This software is distributed under the GNU Lesser General Public License.
00006  *  For more information, see the README file in the top UTILIB directory.
00007  *  _________________________________________________________________________
00008  */
00009 
00010 //
00011 // memdebug.h
00012 //
00013 // Macros that can be used to debug memory allocation
00014 //
00015 
00016 #ifndef __memdebug_h
00017 #define __memdebug_h 1
00018 
00019 #ifdef  USING_MEMDEBUG
00020 
00021 #ifdef NON_ANSI
00022 #include <iostream.h>
00023 #else
00024 #include <iostream>
00025 using namespace std;
00026 #endif
00027 #include "BasicArray_CharString.h"
00028 #include "IntVector.h"
00029 
00030 
00031 #define MEMDEBUG_START_NEW(this)                                        \
00032         {                                                               \
00033         int i=0;                                                        \
00034         for (; i<memdebug::num; i++)                                    \
00035           if (memdebug::name[i] == this) {                              \
00036              memdebug::last_total[i] = memdebug::nbytes;                \
00037              break;                                                     \
00038              }                                                          \
00039         if (i == memdebug::num) {                                       \
00040            memdebug::num++;                                             \
00041            if (memdebug::name.len() == memdebug::num) {                 \
00042               memdebug::name.resize(memdebug::num+10);                  \
00043               memdebug::num_new.resize(memdebug::num+10);               \
00044               memdebug::num_del.resize(memdebug::num+10);               \
00045               memdebug::memory_allocated.resize(memdebug::num+10);      \
00046               memdebug::memory_deleted.resize(memdebug::num+10);        \
00047               memdebug::last_total.resize(memdebug::num+10);            \
00048               }                                                         \
00049            }                                                            \
00050         memdebug::name[i] = this;                                       \
00051         memdebug::last_total[i] = memdebug::nbytes;                     \
00052         }
00053 
00054 #define MEMDEBUG_END_NEW(this)                                          \
00055         {                                                               \
00056         for (int i=0; i<memdebug::num; i++)                             \
00057           if (memdebug::name[i] == this) {                              \
00058              memdebug::memory_allocated[i] +=                           \
00059                         memdebug::nbytes - memdebug::last_total[i];     \
00060              memdebug::num_new[i]++;                                    \
00061              break;                                                     \
00062              }                                                          \
00063         }
00064 
00065 
00066 #define MEMDEBUG_START_RESIZE(this)                                     \
00067         {                                                               \
00068         int i=0;                                                        \
00069         for (; i<memdebug::num; i++)                                    \
00070           if (memdebug::name[i] == this)                                \
00071              memdebug::last_total[i] = memdebug::nbytes;                \
00072         }
00073 
00074 #define MEMDEBUG_END_RESIZE(this)                                       \
00075         {                                                               \
00076         for (int i=0; i<memdebug::num; i++)                             \
00077           if (memdebug::name[i] == this) {                              \
00078              memdebug::memory_allocated[i] +=                           \
00079                         memdebug::nbytes - memdebug::last_total[i];     \
00080              }                                                          \
00081         }
00082 
00083 
00084 #define MEMDEBUG_START_DEL(this)                                        \
00085         {                                                               \
00086         }
00087 
00088 #define MEMDEBUG_END_DEL(this)                                          \
00089         {                                                               \
00090         for (int i=0; i<memdebug::num; i++)                             \
00091           if (memdebug::name[i] == this) {                              \
00092              memdebug::num_del[i]++;                                    \
00093              }                                                          \
00094         }
00095 
00096 
00097 #define MEMDEBUG_DUMP(os)               memdebug::print_summary(os);
00098 
00099 
00100 class memdebug
00101 {
00102 
00103 public:
00104 
00105   static int nbytes;
00106   static int num;
00107   static int n_news;
00108   static int n_dels;
00109 
00110   static BasicArray_CharString name;
00111   static IntVector             num_new;
00112   static IntVector             memory_allocated;
00113   static IntVector             num_del;
00114   static IntVector             memory_deleted;
00115   static IntVector             last_total;
00116 
00117   static void print_summary(ostream& os);
00118 };
00119 
00120 #else
00121 
00122 #define MEMDEBUG_START_NEW(this)
00123 #define MEMDEBUG_END_NEW(this)  
00124 #define MEMDEBUG_START_RESIZE(this)     
00125 #define MEMDEBUG_END_RESIZE(this)
00126 #define MEMDEBUG_START_DEL(this)
00127 #define MEMDEBUG_END_DEL(this)
00128 #define MEMDEBUG_DUMP(os)
00129 
00130 #endif
00131 
00132 #endif