00001 // --------------------------------------------------------------------------------------------------------------------------------- 00002 // _ 00003 // | | 00004 // _ __ ___ _ __ ___ __ _ _ __ | |__ 00005 // | '_ ` _ \| '_ ` _ \ / _` | '__| | '_ \ 00006 // | | | | | | | | | | | (_| | | _ | | | | 00007 // |_| |_| |_|_| |_| |_|\__, |_| (_)|_| |_| 00008 // __/ | 00009 // |___/ 00010 // 00011 // Memory manager & tracking software 00012 // 00013 // Best viewed with 8-character tabs and (at least) 132 columns 00014 // 00015 // --------------------------------------------------------------------------------------------------------------------------------- 00016 // 00017 // Restrictions & freedoms pertaining to usage and redistribution of this software: 00018 // 00019 // * This software is 100% free 00020 // * If you use this software (in part or in whole) you must credit the author. 00021 // * This software may not be re-distributed (in part or in whole) in a modified 00022 // form without clear documentation on how to obtain a copy of the original work. 00023 // * You may not use this software to directly or indirectly cause harm to others. 00024 // * This software is provided as-is and without warrantee. Use at your own risk. 00025 // 00026 // For more information, visit HTTP://www.FluidStudios.com 00027 // 00028 // --------------------------------------------------------------------------------------------------------------------------------- 00029 // Originally created on 12/22/2000 by Paul Nettle 00030 // 00031 // Copyright 2000, Fluid Studios, Inc., all rights reserved. 00032 // --------------------------------------------------------------------------------------------------------------------------------- 00033 00034 #ifdef _DEBUG 00035 #define USE_MMGR 00036 #endif 00037 00038 00039 #ifdef USE_MMGR 00040 00041 #ifndef _H_MMGR 00042 #define _H_MMGR 00043 00044 // --------------------------------------------------------------------------------------------------------------------------------- 00045 // For systems that don't have the __FUNCTION__ variable, we can just define it here 00046 // --------------------------------------------------------------------------------------------------------------------------------- 00047 00048 #ifndef __FUNCTION__ 00049 #define __FUNCTION__ "??" 00050 #endif 00051 00052 // --------------------------------------------------------------------------------------------------------------------------------- 00053 // Types 00054 // --------------------------------------------------------------------------------------------------------------------------------- 00055 00056 typedef struct tag_au 00057 { 00058 size_t actualSize; 00059 size_t reportedSize; 00060 void *actualAddress; 00061 void *reportedAddress; 00062 char sourceFile[40]; 00063 char sourceFunc[40]; 00064 unsigned int sourceLine; 00065 unsigned int allocationType; 00066 bool breakOnDealloc; 00067 bool breakOnRealloc; 00068 unsigned int allocationNumber; 00069 struct tag_au *next; 00070 struct tag_au *prev; 00071 } sAllocUnit; 00072 00073 typedef struct 00074 { 00075 unsigned int totalReportedMemory; 00076 unsigned int totalActualMemory; 00077 unsigned int peakReportedMemory; 00078 unsigned int peakActualMemory; 00079 unsigned int accumulatedReportedMemory; 00080 unsigned int accumulatedActualMemory; 00081 unsigned int accumulatedAllocUnitCount; 00082 unsigned int totalAllocUnitCount; 00083 unsigned int peakAllocUnitCount; 00084 } sMStats; 00085 00086 // --------------------------------------------------------------------------------------------------------------------------------- 00087 // External constants 00088 // --------------------------------------------------------------------------------------------------------------------------------- 00089 00090 extern const unsigned int m_alloc_unknown; 00091 extern const unsigned int m_alloc_new; 00092 extern const unsigned int m_alloc_new_array; 00093 extern const unsigned int m_alloc_malloc; 00094 extern const unsigned int m_alloc_calloc; 00095 extern const unsigned int m_alloc_realloc; 00096 extern const unsigned int m_alloc_delete; 00097 extern const unsigned int m_alloc_delete_array; 00098 extern const unsigned int m_alloc_free; 00099 00100 // --------------------------------------------------------------------------------------------------------------------------------- 00101 // Used by the macros 00102 // --------------------------------------------------------------------------------------------------------------------------------- 00103 00104 void m_setOwner(const char *file, const unsigned int line, const char *func); 00105 00106 // --------------------------------------------------------------------------------------------------------------------------------- 00107 // Allocation breakpoints 00108 // --------------------------------------------------------------------------------------------------------------------------------- 00109 00110 bool &m_breakOnRealloc(void *reportedAddress); 00111 bool &m_breakOnDealloc(void *reportedAddress); 00112 00113 // --------------------------------------------------------------------------------------------------------------------------------- 00114 // The meat of the memory tracking software 00115 // --------------------------------------------------------------------------------------------------------------------------------- 00116 00117 void *m_allocator(const char *sourceFile, const unsigned int sourceLine, const char *sourceFunc, 00118 const unsigned int allocationType, const size_t reportedSize); 00119 void *m_reallocator(const char *sourceFile, const unsigned int sourceLine, const char *sourceFunc, 00120 const unsigned int reallocationType, const size_t reportedSize, void *reportedAddress); 00121 void m_deallocator(const char *sourceFile, const unsigned int sourceLine, const char *sourceFunc, 00122 const unsigned int deallocationType, const void *reportedAddress); 00123 00124 // --------------------------------------------------------------------------------------------------------------------------------- 00125 // Utilitarian functions 00126 // --------------------------------------------------------------------------------------------------------------------------------- 00127 00128 bool m_validateAddress(const void *reportedAddress); 00129 bool m_validateAllocUnit(const sAllocUnit *allocUnit); 00130 bool m_validateAllAllocUnits(); 00131 00132 // --------------------------------------------------------------------------------------------------------------------------------- 00133 // Unused RAM calculations 00134 // --------------------------------------------------------------------------------------------------------------------------------- 00135 00136 unsigned int m_calcUnused(const sAllocUnit *allocUnit); 00137 unsigned int m_calcAllUnused(); 00138 00139 // --------------------------------------------------------------------------------------------------------------------------------- 00140 // Logging and reporting 00141 // --------------------------------------------------------------------------------------------------------------------------------- 00142 00143 void m_dumpAllocUnit(const sAllocUnit *allocUnit, const char *prefix = ""); 00144 void m_dumpMemoryReport(const char *filename = "memreport.log", const bool overwrite = true); 00145 sMStats m_getMemoryStatistics(); 00146 00147 // --------------------------------------------------------------------------------------------------------------------------------- 00148 // Variations of global operators new & delete 00149 // --------------------------------------------------------------------------------------------------------------------------------- 00150 00151 void *operator new(size_t reportedSize); 00152 void *operator new[](size_t reportedSize); 00153 void *operator new(size_t reportedSize, const char *sourceFile, int sourceLine); 00154 void *operator new[](size_t reportedSize, const char *sourceFile, int sourceLine); 00155 void operator delete(void *reportedAddress); 00156 void operator delete[](void *reportedAddress); 00157 00158 #endif // _H_MMGR 00159 00160 // --------------------------------------------------------------------------------------------------------------------------------- 00161 // Macros -- "Kids, please don't try this at home. We're trained professionals here." :) 00162 // --------------------------------------------------------------------------------------------------------------------------------- 00163 00164 #include "nommgr.h" 00165 #define new (m_setOwner (__FILE__,__LINE__,__FUNCTION__),false) ? NULL : new 00166 #define delete (m_setOwner (__FILE__,__LINE__,__FUNCTION__),false) ? m_setOwner("",0,"") : delete 00167 #define malloc(sz) m_allocator (__FILE__,__LINE__,__FUNCTION__,m_alloc_malloc,sz) 00168 #define calloc(sz) m_allocator (__FILE__,__LINE__,__FUNCTION__,m_alloc_calloc,sz) 00169 #define realloc(ptr,sz) m_reallocator(__FILE__,__LINE__,__FUNCTION__,m_alloc_realloc,sz,ptr) 00170 #define free(ptr) m_deallocator(__FILE__,__LINE__,__FUNCTION__,m_alloc_free,ptr) 00171 00172 #endif 00173 00174 // --------------------------------------------------------------------------------------------------------------------------------- 00175 // mmgr.h - End of file 00176 // ---------------------------------------------------------------------------------------------------------------------------------