00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017 #ifndef _debug_h
00018 #define _debug_h
00019
00020 #include <stdlib.h>
00021 #include <assert.h>
00022
00023 #if defined(_WIN32) || defined(WIN32) || defined(_WIN64) || defined(WIN64)
00024 #include <spl/configwin32.h>
00025 #else
00026 #include <spl/autoconf/config.h>
00027 #endif
00028
00029 #if defined(_WIN32) || defined(WIN32) || defined(_WIN64) || defined(WIN64)
00030 #ifndef _WIN32
00031 #define _WIN32
00032 #endif
00033 #ifndef WIN32
00034 #define WIN32
00035 #endif
00036 #ifndef _WINDOWS
00037 #define _WINDOWS
00038 #endif
00039 #endif
00040 #include <spl/types.h>
00041
00042 #if defined _DEBUG || defined DEBUG
00043
00044 #ifndef DEBUG
00045 #define DEBUG
00046 #endif
00047
00048 #ifndef _DEBUG
00049 #define _DEBUG
00050 #endif
00051
00058 #ifdef __cplusplus
00059 void *operator new(size_t size);
00060 void operator delete(void *vp);
00061 void *operator new[](size_t size);
00062 void operator delete[](void *vp);
00063 #endif
00064
00065 #undef malloc
00066 #undef free
00067
00068 #ifndef NULL
00069 #define NULL ((void *)0)
00070 #endif
00071
00072 void _unitAssert( const char *msg, const int cond, const char *filename, const int lineno );
00073 void _unitTest( const char *msg, const int cond);
00074
00075 void *_debugMalloc( const int, const char *filename, const int lineno, const bool isarray );
00076 void *_rpl_malloc( const int size, char *filename, const int lineno, const bool isarray );
00077 void _debugFree( void * );
00078 void _debugAssert( const int cond, const char* filename, const int lineno );
00079 int _debugCheckPtr( const void *ptr );
00080 int _debugCheckBlock( const void *ptr, const int size );
00081 void _debugClearMemCheckPoints();
00082 void _debugNoteMemBlock( const void *vp );
00083 int _debugCheckMem();
00084 int _debugAssertMemFree();
00085 void _debugCheckpointHeap();
00086 int _debugAssertCheckPoint();
00087 void _debugTearDown( bool reallyfree );
00088 void _debugDumpMemLeaks();
00089 void _debugValidateHeap();
00090 void _debugLockHeap();
00091 void _debugUnlockHeap();
00092 void _debugFreeHeap();
00093 void _debugEnableHeapLocking();
00094
00095 #define malloc(size) _debugMalloc(size, __FILE__, __LINE__, false)
00096 #define free(ptr) _debugFree(ptr)
00097 void realfree(void *vp);
00098
00099 #undef ASSERT
00100 #define ASSERT(cond) _debugAssert( cond, __FILE__, __LINE__ )
00101 #define ASSERT_PTR(ptr) ASSERT(_debugCheckPtr( ptr ))
00102 #define ASSERT_MEM(ptr, size) ASSERT(_debugCheckBlock( ptr, size ))
00103 #define ASSERT_MEM_FREE() ASSERT(_debugAssertMemFree())
00104
00105 #define DEBUG_CLEAR_MEM_CHECK_POINTS() _debugClearMemCheckPoints()
00106 #define DEBUG_NOTE_MEM_ALLOCATION(a) _debugNoteMemBlock( a )
00107 #define DEBUG_NOTE_MEM(a) _debugNoteMemBlock( a )
00108 #define DEBUG_ASSERT_MEM_NOTED() ASSERT(_debugCheckMem())
00109
00110 #define DEBUG_CHECK_POINT_HEAP() _debugCheckpointHeap()
00111 #define DEBUG_ASSERT_CHECK_POINT() ASSERT(_debugAssertCheckPoint())
00112 #define DEBUG_TEAR_DOWN(a) _debugTearDown(a)
00113 #define DEBUG_DUMP_MEM_LEAKS() _debugDumpMemLeaks()
00114 #define DEBUG_LOCK_HEAP() _debugLockHeap()
00115 #define DEBUG_UNLOCK_HEAP() _debugUnlockHeap()
00116 #define DEBUG_FREE_HEAP() _debugFreeHeap()
00117 #define DEBUG_ENABLE_HEAP_LOCK() _debugEnableHeapLocking()
00118
00119 #ifdef DEBUG2
00120 #define DEBUG_VALIDATE() _debugValidateHeap()
00121 #define DEBUG_PTR2(ptr) ASSERT(_debugCheckPtr( ptr ))
00122 #define ASSERT_MEM2(ptr, size) ASSERT(_debugCheckBlock( ptr, size ))
00123 #else
00124 #define DEBUG_VALIDATE()
00125 #define DEBUG_PTR2(ptr)
00126 #define ASSERT_MEM2(ptr, size)
00127 #endif
00128
00129 #define UNIT_TEST(msg,cond) _unitTest(msg,cond)
00130 #define UNIT_ASSERT(msg,cond) _unitAssert( msg, cond, __FILE__, __LINE__ )
00131 #define UNIT_ASSERT_PTR(msg,ptr) UNIT_ASSERT(msg,_debugCheckPtr( ptr ))
00132 #define UNIT_ASSERT_MEM(msg,ptr,size) UNIT_ASSERT(msg,_debugCheckBlock( ptr, size ))
00133 #define UNIT_ASSERT_MEM_FREE(msg) UNIT_ASSERT(msg,_debugAssertMemFree())
00134 #define UNIT_ASSERT_MEM_NOTED(msg) UNIT_ASSERT(msg,_debugCheckMem())
00135 #define UNIT_ASSERT_CHECK_POINT(msg) UNIT_ASSERT(msg,_debugAssertCheckPoint())
00136
00137 void debugAssertCPP(const int cond, const char *file, const int line);
00138
00139 #undef ASSERT
00140 #define ASSERT(cond) if(!(cond)) debugAssertCPP(false, __FILE__, __LINE__ )
00141
00142 #else
00143
00144 void *rpl_malloc( const int size );
00145
00146 #ifndef ASSERT
00147 #define ASSERT(cond)
00148 #endif
00149 #define ASSERT_PTR(ptr)
00150 #define ASSERT_MEM(ptr, size)
00151 #define ASSERT_MEM_FREE()
00152
00153 #define DEBUG_CLEAR_MEM_CHECK_POINTS()
00154 #define DEBUG_NOTE_MEM_ALLOCATION(a)
00155 #define DEBUG_NOTE_MEM(a)
00156 #define DEBUG_ASSERT_MEM_NOTED()
00157
00158 #define DEBUG_CHECK_POINT_HEAP()
00159 #define DEBUG_ASSERT_CHECK_POINT()
00160 #define DEBUG_TEAR_DOWN(a)
00161 #define DEBUG_DUMP_MEM_LEAKS()
00162
00163 #define UNIT_TEST(cond,msg)
00164 #define UNIT_ASSERT(msg,cond)
00165 #define UNIT_ASSERT_PTR(msg,ptr)
00166 #define UNIT_ASSERT_MEM(msg,ptr,size)
00167 #define UNIT_ASSERT_MEM_FREE(msg)
00168 #define UNIT_ASSERT_MEM_NOTED(msg)
00169 #define UNIT_ASSERT_CHECK_POINT(msg)
00170 #define DEBUG_ENABLE_HEAP_LOCK()
00171
00172 #define DEBUG_VALIDATE()
00173 #define DEBUG_LOCK_HEAP()
00174 #define DEBUG_UNLOCK_HEAP()
00175 #define DEBUG_FREE_HEAP()
00176
00177 #define realfree(a) free(a)
00178
00179 #endif
00180
00182 class Debug
00183 {
00184 private:
00185 inline Debug() {}
00186 inline Debug(const Debug& d) {}
00187 inline Debug& operator =(const Debug& d) {}
00188
00189 public:
00190
00192 inline static void Assert(bool cond)
00193 {
00194 ASSERT(cond);
00195 }
00196
00201 inline static void AssertPtr(const void *vp)
00202 {
00203 ASSERT_PTR(vp);
00204 }
00205
00209 inline static void AssertMem(const void *vp, const int size)
00210 {
00211 ASSERT_MEM(vp, size);
00212 }
00213
00218 inline static void TearDown(bool reallyFree)
00219 {
00220 DEBUG_TEAR_DOWN(reallyFree);
00221 }
00222
00224 inline static void FreeDebugHeap()
00225 {
00226 DEBUG_FREE_HEAP();
00227 }
00228
00232 inline static void EnableHeapLocking()
00233 {
00234 DEBUG_ENABLE_HEAP_LOCK();
00235 }
00236
00242 inline static void HeapLock()
00243 {
00244 DEBUG_LOCK_HEAP();
00245 }
00246
00252 inline static void UnlockHeap()
00253 {
00254 DEBUG_UNLOCK_HEAP();
00255 }
00256
00262 inline void ClearMemCheckPoints()
00263 {
00264 DEBUG_CLEAR_MEM_CHECK_POINTS();
00265 }
00266
00272 inline static void PrintMemoryLeaks()
00273 {
00274 DEBUG_DUMP_MEM_LEAKS();
00275 }
00276
00282 inline static void NoteMem(const void *vp)
00283 {
00284 DEBUG_NOTE_MEM(vp);
00285 }
00286
00290 inline static void AssertMemNoted()
00291 {
00292 DEBUG_ASSERT_MEM_NOTED();
00293 }
00294
00296 inline static void AssertMemFree()
00297 {
00298 ASSERT_MEM_FREE();
00299 }
00300
00304 inline static void UnitAssert(const char *msg, bool cond)
00305 {
00306 UNIT_ASSERT(msg, cond);
00307 }
00308
00313 inline static void UnitAssertPtr(const char *msg, const void *vp)
00314 {
00315 UNIT_ASSERT_PTR(msg, vp);
00316 }
00317
00321 inline static void UnitAssertMem(const char *msg, const void *vp, const int size)
00322 {
00323 UNIT_ASSERT_MEM(msg, vp, size);
00324 }
00325
00329 inline static void UnitAssertMemNoted(const char *msg)
00330 {
00331 UNIT_ASSERT_MEM_FREE(msg);
00332 }
00333
00337 inline static void UnitAssertMemFree(const char *msg)
00338 {
00339 UNIT_ASSERT_MEM_FREE(msg);
00340 }
00341 };
00342
00345 #endif
00346