00001 // --------------------------------------------------------------------------------------------------------------------------------- 00002 // Copyright 2000, Paul Nettle. All rights reserved. 00003 // 00004 // You are free to use this source code in any commercial or non-commercial product. 00005 // 00006 // mmgr.h - Memory manager & tracking software 00007 // 00008 // The most recent version of this software can be found at: ftp://ftp.GraphicsPapers.com/pub/ProgrammingTools/MemoryManagers/ 00009 // 00010 // [NOTE: Best when viewed with 8-character tabs] 00011 // --------------------------------------------------------------------------------------------------------------------------------- 00012 00013 #ifndef _H_MMGR 00014 #define _H_MMGR 00015 00016 // --------------------------------------------------------------------------------------------------------------------------------- 00017 // For systems that don't have the __FUNCTION__ variable, we can just define it here 00018 // --------------------------------------------------------------------------------------------------------------------------------- 00019 00020 #define __FUNCTION__ "??" 00021 00022 // --------------------------------------------------------------------------------------------------------------------------------- 00023 // Types 00024 // --------------------------------------------------------------------------------------------------------------------------------- 00025 00026 #include <new> 00027 #include <stddef.h> 00028 00029 typedef struct tag_au 00030 { 00031 size_t actualSize; 00032 size_t reportedSize; 00033 void *actualAddress; 00034 void *reportedAddress; 00035 char sourceFile[40]; 00036 char sourceFunc[40]; 00037 unsigned int sourceLine; 00038 unsigned int allocationType; 00039 bool breakOnDealloc; 00040 bool breakOnRealloc; 00041 unsigned int allocationNumber; 00042 struct tag_au *next; 00043 struct tag_au *prev; 00044 } sAllocUnit; 00045 00046 typedef struct 00047 { 00048 unsigned int totalReportedMemory; 00049 unsigned int totalActualMemory; 00050 unsigned int peakReportedMemory; 00051 unsigned int peakActualMemory; 00052 unsigned int accumulatedReportedMemory; 00053 unsigned int accumulatedActualMemory; 00054 unsigned int accumulatedAllocUnitCount; 00055 unsigned int totalAllocUnitCount; 00056 unsigned int peakAllocUnitCount; 00057 } sMStats; 00058 00059 // --------------------------------------------------------------------------------------------------------------------------------- 00060 // External constants 00061 // --------------------------------------------------------------------------------------------------------------------------------- 00062 00063 extern const unsigned int m_alloc_unknown; 00064 extern const unsigned int m_alloc_new; 00065 extern const unsigned int m_alloc_new_array; 00066 extern const unsigned int m_alloc_malloc; 00067 extern const unsigned int m_alloc_calloc; 00068 extern const unsigned int m_alloc_realloc; 00069 extern const unsigned int m_alloc_delete; 00070 extern const unsigned int m_alloc_delete_array; 00071 extern const unsigned int m_alloc_free; 00072 00073 // --------------------------------------------------------------------------------------------------------------------------------- 00074 // Used by the macros 00075 // --------------------------------------------------------------------------------------------------------------------------------- 00076 00077 void m_setOwner(const char *file, const unsigned int line, const char *func); 00078 00079 // --------------------------------------------------------------------------------------------------------------------------------- 00080 // Allocation breakpoints 00081 // --------------------------------------------------------------------------------------------------------------------------------- 00082 00083 bool &m_breakOnRealloc(void *reportedAddress); 00084 bool &m_breakOnDealloc(void *reportedAddress); 00085 00086 // --------------------------------------------------------------------------------------------------------------------------------- 00087 // The meat of the memory tracking software 00088 // --------------------------------------------------------------------------------------------------------------------------------- 00089 00090 void *m_allocator(const char *sourceFile, const unsigned int sourceLine, const char *sourceFunc, 00091 const unsigned int allocationType, const size_t reportedSize); 00092 void *m_reallocator(const char *sourceFile, const unsigned int sourceLine, const char *sourceFunc, 00093 const unsigned int reallocationType, const size_t reportedSize, void *reportedAddress); 00094 void m_deallocator(const char *sourceFile, const unsigned int sourceLine, const char *sourceFunc, 00095 const unsigned int deallocationType, const void *reportedAddress); 00096 00097 // --------------------------------------------------------------------------------------------------------------------------------- 00098 // Utilitarian functions 00099 // --------------------------------------------------------------------------------------------------------------------------------- 00100 00101 bool m_validateAddress(const void *reportedAddress); 00102 bool m_validateAllocUnit(const sAllocUnit *allocUnit); 00103 bool m_validateAllAllocUnits(); 00104 00105 // --------------------------------------------------------------------------------------------------------------------------------- 00106 // Unused RAM calculations 00107 // --------------------------------------------------------------------------------------------------------------------------------- 00108 00109 unsigned int m_calcUnused(const sAllocUnit *allocUnit); 00110 unsigned int m_calcAllUnused(); 00111 00112 // --------------------------------------------------------------------------------------------------------------------------------- 00113 // Logging and reporting 00114 // --------------------------------------------------------------------------------------------------------------------------------- 00115 00116 void m_dumpAllocUnit(const sAllocUnit *allocUnit, const char *prefix = ""); 00117 void m_dumpMemoryReport(const char *filename = "memreport.log", const bool overwrite = true); 00118 sMStats m_getMemoryStatistics(); 00119 00120 // --------------------------------------------------------------------------------------------------------------------------------- 00121 // Variations of global operators new & delete 00122 // --------------------------------------------------------------------------------------------------------------------------------- 00123 00124 void *operator new(size_t reportedSize) throw (std::bad_alloc); 00125 void *operator new[](size_t reportedSize) throw (std::bad_alloc); 00126 void *operator new(size_t reportedSize, const char *sourceFile, int sourceLine); 00127 void *operator new[](size_t reportedSize, const char *sourceFile, int sourceLine); 00128 void operator delete(void *reportedAddress); 00129 void operator delete[](void *reportedAddress); 00130 00131 #endif // _H_MMGR 00132 00133 // --------------------------------------------------------------------------------------------------------------------------------- 00134 // Macros -- "Kids, please don't try this at home. We're trained professionals here." :) 00135 // --------------------------------------------------------------------------------------------------------------------------------- 00136 00137 #include "nommgr.h" 00138 #define new (m_setOwner (__FILE__,__LINE__,__FUNCTION__),false) ? NULL : new 00139 #define delete (m_setOwner (__FILE__,__LINE__,__FUNCTION__),false) ? m_setOwner("",0,"") : delete 00140 #define malloc(sz) m_allocator (__FILE__,__LINE__,__FUNCTION__,m_alloc_malloc,sz) 00141 #define calloc(sz) m_allocator (__FILE__,__LINE__,__FUNCTION__,m_alloc_calloc,sz) 00142 #define realloc(ptr,sz) m_reallocator(__FILE__,__LINE__,__FUNCTION__,m_alloc_realloc,sz,ptr) 00143 #define free(ptr) m_deallocator(__FILE__,__LINE__,__FUNCTION__,m_alloc_free,ptr) 00144 00145 // --------------------------------------------------------------------------------------------------------------------------------- 00146 // mmgr.h - End of file 00147 // ---------------------------------------------------------------------------------------------------------------------------------