Main Page   Namespace List   Class Hierarchy   Compound List   File List   Namespace Members   Compound Members   File Members  

mdebug.h

Go to the documentation of this file.
00001 
00025 #ifndef __DEBUG_H__
00026 #define __DEBUG_H__
00027 
00028 // Remove new-redefinition
00029 #ifdef new
00030 #undef new
00031 #define new_UNDEFD
00032 #endif
00033 
00034 // Restore new-redefinition
00035 #ifdef new_UNDEFD
00036 #define new DEBUG_NEW
00037 #undef new_UNDEFD
00038 #endif
00039 
00041 //                                                                          //
00042 // ___                               ----               o                   //
00043 // |  \   ___  |                    (      ___   |   |      _          ____ //
00044 // |   | /   ) |---  |   |  ___      ---  /   ) -+- -+- | |/ \   ___  (     //
00045 // |   | |---  |   ) |   | (   \        ) |---   |   |  | |   | (   \  \__  //
00046 // |__/   \__  |__/   \__!  ---/    ___/   \__    \   \ | |   |  ---/ ____) //
00047 //                          __/                                  __/        //
00049 
00052 #define DEBUG_MODE
00053 
00054 // TODO: Handle this somehow...
00055 #ifndef DEBUG_MODE
00056 #define NODEBUG
00057 #endif
00058 
00062 //#define new DEBUG_NEW
00063 
00066 // #define DEBUG_OBJECT_NEW
00067 
00070 #ifndef MEMORY_DEBUG
00071 #define DISABLE_ALL_MEMORY_DEBUGGING
00072 #else
00073 #define new DEBUG_NEW
00074 #endif
00075 
00078 #define LINUX
00079 #define UNIX
00080 
00081 
00083 //                                                                           //
00084 //     ___                                                                   //
00085 //     |  \   ___  |                           ___   ___            ____     //
00086 //     |   | /   ) |---  |   |  ___     |/|/|  ___| |   \ |/\  __  (         //
00087 //     |   | |---  |   ) |   | (   \    | | | (   | |     |   /  \  \__      //
00088 //     |__/   \__  |__/   \__!  ---/    | | |  \__|  \__/ |   \__/ ____)     //
00089 //                              __/                                          //
00091 
00092 #ifndef NODEBUG
00093 
00094 #define TRACELINE {fprintf (dbgout, "File:%s, Line#%d %s\n", __FILE__, __LINE__, __FUNCTION__); fflush (dbgout);}
00095 #define TRACE(expr) {fprintf (dbgout, "File:%s, Line#%d %s: ", __FILE__, __LINE__, __FUNCTION__); fprintf (dbgout, expr); fprintf (dbgout, "\n"); fflush (dbgout);}
00096 #define TRACE1(expr,par1) {fprintf (dbgout, "File:%s, Line#%d %s: ", __FILE__, __LINE__, __FUNCTION__); fprintf (dbgout, expr, par1); fprintf (dbgout, "\n"); fflush (dbgout);}
00097 #define TRACE2(expr,par1,par2) {fprintf (dbgout, "File:%s, Line#%d %s: ", __FILE__, __LINE__, __FUNCTION__); fprintf (dbgout, expr, par1, par2); fprintf (dbgout, "\n"); fflush (dbgout);}
00098 #define TRACE3(expr,par1,par2,par3) {fprintf (dbgout, "File:%s, Line#%d %s: ", __FILE__, __LINE__, __FUNCTION__); fprintf (dbgout, expr, par1, par2, par3); fprintf (dbgout, "\n"); fflush (dbgout);}
00099 #define TRACE4(expr,par1,par2,par3,par4) {fprintf (dbgout, "File:%s, Line#%d %s: ", __FILE__, __LINE__, __FUNCTION__); fprintf (dbgout, expr, par1, par2, par3, par4); fprintf (dbgout, "\n"); fflush (dbgout);}
00100 #define TRACE5(expr,par1,par2,par3,par4,par5) {fprintf (dbgout, "File:%s, Line#%d %s: ", __FILE__, __LINE__, __FUNCTION__); fprintf (dbgout, expr, par1, par2, par3, par4, par5); fprintf (dbgout, "\n"); fflush (dbgout);}
00101 #define TRACE6(expr,par1,par2,par3,par4,par5,par6) {fprintf (dbgout, "File:%s, Line#%d %s: ", __FILE__, __LINE__, __FUNCTION__); fprintf (dbgout, expr, par1, par2, par3, par4, par5, par6); fprintf (dbgout, "\n"); fflush (dbgout);}
00102 
00103 // ASSERT is not available under Qt, which also defines it. Clever.
00104 #ifndef QT_VERSION
00105 #define ASSERT(expr) {if (!(expr)) assertwith (__FILE__, __LINE__, __FUNCTION__, #expr);}
00106 #endif
00107 
00108 #define ASSERTWITH(expr,message) {if (!(expr)) assertwith (__FILE__, __LINE__, __FUNCTION__, #expr, message);}
00109 #define PRE(expr) ASSERTWITH(expr, "Violation of precondition");
00110 #define POST(expr) ASSERTWITH(expr, "Violation of postcondition");
00111 
00112 #define IFDEBUG(expr) {expr;}
00113 
00114 #else
00115 
00116 #define TRACELINE
00117 #define ASSERT(expr)
00118 #define ASSERTWITH(expr,message)
00119 #define PRE(expr)
00120 #define POST(expr)
00121 #define TRACE(msg) {}
00122 #define TRACE1(msg,p1) {}
00123 #define TRACE2(msg,p1,p2) {}
00124 #define TRACE3(msg,p1,p2,p3) {}
00125 #define TRACE4(msg,p1,p2,p3,p4) {}
00126 #define IFDEBUG(expr) {expr;}
00127 #endif
00128 
00129 
00130 
00132 //                                                                           //
00133 //     |   |                                   |                             //
00134 //     |\ /|  ___                              |  ___  |                     //
00135 //     | V | /   ) |/|/|  __  |/\ \   |     ---| /   ) |---  |   |  ___      //
00136 //     | | | |---  | | | /  \ |    \  |    (   | |---  |   ) |   | (   \     //
00137 //     |   |  \__  | | | \__/ |     \_/     ---|  \__  |__/   \__!  ---/     //
00138 //                                 \_/                              __/      //
00140 
00141 // Define may memory debugging extensions, unless disabled in config.h
00142 #ifndef DISABLE_ALL_MEMORY_DEBUGGING
00143 
00144 //#define new DEBUG_NEW
00145 #define DEBUG_NEW new(__FILE__,__LINE__,__FUNCTION__)
00146 #ifdef new
00147 #undef new
00148 #define new_UNDEFD
00149 #define renew(p,ns) _renew (p,ns,__FILE__,__LINE__,__FUNCTION__)
00150 #else
00151 #define renew realloc
00152 #warning "Renew is realloc"
00153 #endif
00154 
00155 extern void* operator new[] (size_t size, const char* filen, int lineno, const char* funcn);
00156 extern void* operator new (size_t size, const char* filen, int lineno, const char* funcn);
00157 extern void operator delete (void* p);
00158 extern void list_allocations (int number=100, FILE* out=stderr);
00159 extern void* _renew (void* p, size_t newsize, const char* filen, int lineno, const char* funcn);
00160 extern "C" void newComment (const char* comment);
00161 
00162 #ifdef new_UNDEFD
00163 #define new DEBUG_NEW
00164 #undef new_UNDEFD
00165 #endif
00166 
00167 struct mallocinfostr {
00168     char            startsym[6];
00169     int             size;
00170     const char*     filename;
00171     int             lineno;
00172     const char*     funcname;
00173     char*           newcomment;
00174     bool            renewed;
00175     bool            isObject;
00176     mallocinfostr*  next;
00177     mallocinfostr*  prev;
00178 
00179     // This is required to corrent double-allocation word frame shift,
00180     // which results in BUS ERROR when writing to a double reference
00181     char            padding[4];
00182 };
00183 
00184 extern mallocinfostr* lastmalloced;     // The last malloced memory block
00185 extern int dbgnewmalloced;              // Total amount of memory malloced
00186 extern int newtrace_disabled;
00187 
00188 #else // DISABLE_ALL_MEMORY_DEBUGGING
00189 // Some dummy function definitions
00190 
00191 #include <stdlib.h>
00192 
00193 inline void* _renew (void* p, size_t newsize, const char* filen, int lineno, const char* funcn) {
00194     return realloc (p, newsize);
00195 }
00196 #define renew(p,ns) _renew (p,ns,__FILE__,__LINE__,__FUNCTION__)
00197 
00198 inline void newComment (const char* comment) {}
00199 
00200 #endif // DISABLE_ALL_MEMORY_DEBUGGING
00201 
00202 #endif
00203 

Generated on Thu Feb 10 20:06:41 2005 for LibMagiC by doxygen1.2.18