1 // ---------------------------------------------------------------------------------------------------------------------------------
4 // _ __ ___ _ __ ___ __ _ _ __ | |__
5 // | '_ ` _ \| '_ ` _ \ / _` | '__| | '_ \
6 // | | | | | | | | | | | (_| | | _ | | | |
7 // |_| |_| |_|_| |_| |_|\__, |_| (_)|_| |_|
11 // Memory manager & tracking software
13 // Best viewed with 8-character tabs and (at least) 132 columns
15 // ---------------------------------------------------------------------------------------------------------------------------------
17 // Restrictions & freedoms pertaining to usage and redistribution of this software:
19 // * This software is 100% free
20 // * If you use this software (in part or in whole) you must credit the author.
21 // * This software may not be re-distributed (in part or in whole) in a modified
22 // form without clear documentation on how to obtain a copy of the original work.
23 // * You may not use this software to directly or indirectly cause harm to others.
24 // * This software is provided as-is and without warrantee. Use at your own risk.
26 // For more information, visit HTTP://www.FluidStudios.com
28 // ---------------------------------------------------------------------------------------------------------------------------------
29 // Originally created on 12/22/2000 by Paul Nettle
31 // Copyright 2000, Fluid Studios, Inc., all rights reserved.
32 // ---------------------------------------------------------------------------------------------------------------------------------
37 // ---------------------------------------------------------------------------------------------------------------------------------
38 // For systems that don't have the __FUNCTION__ variable, we can just define it here
39 // ---------------------------------------------------------------------------------------------------------------------------------
41 #define __FUNCTION__ "??"
43 // ---------------------------------------------------------------------------------------------------------------------------------
45 // ---------------------------------------------------------------------------------------------------------------------------------
52 void *reportedAddress;
55 unsigned int sourceLine;
56 unsigned int allocationType;
59 unsigned int allocationNumber;
66 unsigned int totalReportedMemory;
67 unsigned int totalActualMemory;
68 unsigned int peakReportedMemory;
69 unsigned int peakActualMemory;
70 unsigned int accumulatedReportedMemory;
71 unsigned int accumulatedActualMemory;
72 unsigned int accumulatedAllocUnitCount;
73 unsigned int totalAllocUnitCount;
74 unsigned int peakAllocUnitCount;
77 // ---------------------------------------------------------------------------------------------------------------------------------
79 // ---------------------------------------------------------------------------------------------------------------------------------
81 extern const unsigned int m_alloc_unknown;
82 extern const unsigned int m_alloc_new;
83 extern const unsigned int m_alloc_new_array;
84 extern const unsigned int m_alloc_malloc;
85 extern const unsigned int m_alloc_calloc;
86 extern const unsigned int m_alloc_realloc;
87 extern const unsigned int m_alloc_delete;
88 extern const unsigned int m_alloc_delete_array;
89 extern const unsigned int m_alloc_free;
91 // ---------------------------------------------------------------------------------------------------------------------------------
93 // ---------------------------------------------------------------------------------------------------------------------------------
95 void m_setOwner(const char *file, const unsigned int line, const char *func);
97 // ---------------------------------------------------------------------------------------------------------------------------------
98 // Allocation breakpoints
99 // ---------------------------------------------------------------------------------------------------------------------------------
101 bool &m_breakOnRealloc(void *reportedAddress);
102 bool &m_breakOnDealloc(void *reportedAddress);
104 // ---------------------------------------------------------------------------------------------------------------------------------
105 // The meat of the memory tracking software
106 // ---------------------------------------------------------------------------------------------------------------------------------
108 void *m_allocator(const char *sourceFile, const unsigned int sourceLine, const char *sourceFunc,
109 const unsigned int allocationType, const size_t reportedSize);
110 void *m_reallocator(const char *sourceFile, const unsigned int sourceLine, const char *sourceFunc,
111 const unsigned int reallocationType, const size_t reportedSize, void *reportedAddress);
112 void m_deallocator(const char *sourceFile, const unsigned int sourceLine, const char *sourceFunc,
113 const unsigned int deallocationType, const void *reportedAddress);
115 // ---------------------------------------------------------------------------------------------------------------------------------
116 // Utilitarian functions
117 // ---------------------------------------------------------------------------------------------------------------------------------
119 bool m_validateAddress(const void *reportedAddress);
120 bool m_validateAllocUnit(const sAllocUnit *allocUnit);
121 bool m_validateAllAllocUnits();
123 // ---------------------------------------------------------------------------------------------------------------------------------
124 // Unused RAM calculations
125 // ---------------------------------------------------------------------------------------------------------------------------------
127 unsigned int m_calcUnused(const sAllocUnit *allocUnit);
128 unsigned int m_calcAllUnused();
130 // ---------------------------------------------------------------------------------------------------------------------------------
131 // Logging and reporting
132 // ---------------------------------------------------------------------------------------------------------------------------------
134 void m_dumpAllocUnit(const sAllocUnit *allocUnit, const char *prefix = "");
135 void m_dumpMemoryReport(const char *filename = "memreport.log", const bool overwrite = true);
136 sMStats m_getMemoryStatistics();
138 // ---------------------------------------------------------------------------------------------------------------------------------
139 // Variations of global operators new & delete
140 // ---------------------------------------------------------------------------------------------------------------------------------
142 void *operator new(size_t reportedSize);
143 void *operator new[](size_t reportedSize);
144 void *operator new(size_t reportedSize, const char *sourceFile, int sourceLine);
145 void *operator new[](size_t reportedSize, const char *sourceFile, int sourceLine);
146 void operator delete(void *reportedAddress);
147 void operator delete[](void *reportedAddress);
151 // ---------------------------------------------------------------------------------------------------------------------------------
152 // Macros -- "Kids, please don't try this at home. We're trained professionals here." :)
153 // ---------------------------------------------------------------------------------------------------------------------------------
156 #define new (m_setOwner (__FILE__,__LINE__,__FUNCTION__),false) ? NULL : new
157 #define delete (m_setOwner (__FILE__,__LINE__,__FUNCTION__),false) ? m_setOwner("",0,"") : delete
158 #define malloc(sz) m_allocator (__FILE__,__LINE__,__FUNCTION__,m_alloc_malloc,sz)
159 #define calloc(sz) m_allocator (__FILE__,__LINE__,__FUNCTION__,m_alloc_calloc,sz)
160 #define realloc(ptr,sz) m_reallocator(__FILE__,__LINE__,__FUNCTION__,m_alloc_realloc,sz,ptr)
161 #define free(ptr) m_deallocator(__FILE__,__LINE__,__FUNCTION__,m_alloc_free,ptr)
163 // ---------------------------------------------------------------------------------------------------------------------------------
164 // mmgr.h - End of file
165 // ---------------------------------------------------------------------------------------------------------------------------------