2 Copyright (C) 2003, 2010 - Wolfire Games
4 This file is part of Lugaru.
6 Lugaru is free software; you can redistribute it and/or
7 modify it under the terms of the GNU General Public License
8 as published by the Free Software Foundation; either version 2
9 of the License, or (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
15 See the GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
22 // ---------------------------------------------------------------------------------------------------------------------------------
25 // _ __ ___ _ __ ___ __ _ _ __ | |__
26 // | '_ ` _ \| '_ ` _ \ / _` | '__| | '_ \
27 // | | | | | | | | | | | (_| | | _ | | | |
28 // |_| |_| |_|_| |_| |_|\__, |_| (_)|_| |_|
32 // Memory manager & tracking software
34 // Best viewed with 8-character tabs and (at least) 132 columns
36 // ---------------------------------------------------------------------------------------------------------------------------------
38 // Restrictions & freedoms pertaining to usage and redistribution of this software:
40 // * This software is 100% free
41 // * If you use this software (in part or in whole) you must credit the author.
42 // * This software may not be re-distributed (in part or in whole) in a modified
43 // form without clear documentation on how to obtain a copy of the original work.
44 // * You may not use this software to directly or indirectly cause harm to others.
45 // * This software is provided as-is and without warrantee. Use at your own risk.
47 // For more information, visit HTTP://www.FluidStudios.com
49 // ---------------------------------------------------------------------------------------------------------------------------------
50 // Originally created on 12/22/2000 by Paul Nettle
52 // Copyright 2000, Fluid Studios, Inc., all rights reserved.
53 // ---------------------------------------------------------------------------------------------------------------------------------
58 // ---------------------------------------------------------------------------------------------------------------------------------
59 // For systems that don't have the __FUNCTION__ variable, we can just define it here
60 // ---------------------------------------------------------------------------------------------------------------------------------
62 #define __FUNCTION__ "??"
64 // ---------------------------------------------------------------------------------------------------------------------------------
66 // ---------------------------------------------------------------------------------------------------------------------------------
73 void *reportedAddress;
76 unsigned int sourceLine;
77 unsigned int allocationType;
80 unsigned int allocationNumber;
87 unsigned int totalReportedMemory;
88 unsigned int totalActualMemory;
89 unsigned int peakReportedMemory;
90 unsigned int peakActualMemory;
91 unsigned int accumulatedReportedMemory;
92 unsigned int accumulatedActualMemory;
93 unsigned int accumulatedAllocUnitCount;
94 unsigned int totalAllocUnitCount;
95 unsigned int peakAllocUnitCount;
98 // ---------------------------------------------------------------------------------------------------------------------------------
100 // ---------------------------------------------------------------------------------------------------------------------------------
102 extern const unsigned int m_alloc_unknown;
103 extern const unsigned int m_alloc_new;
104 extern const unsigned int m_alloc_new_array;
105 extern const unsigned int m_alloc_malloc;
106 extern const unsigned int m_alloc_calloc;
107 extern const unsigned int m_alloc_realloc;
108 extern const unsigned int m_alloc_delete;
109 extern const unsigned int m_alloc_delete_array;
110 extern const unsigned int m_alloc_free;
112 // ---------------------------------------------------------------------------------------------------------------------------------
113 // Used by the macros
114 // ---------------------------------------------------------------------------------------------------------------------------------
116 void m_setOwner(const char *file, const unsigned int line, const char *func);
118 // ---------------------------------------------------------------------------------------------------------------------------------
119 // Allocation breakpoints
120 // ---------------------------------------------------------------------------------------------------------------------------------
122 bool &m_breakOnRealloc(void *reportedAddress);
123 bool &m_breakOnDealloc(void *reportedAddress);
125 // ---------------------------------------------------------------------------------------------------------------------------------
126 // The meat of the memory tracking software
127 // ---------------------------------------------------------------------------------------------------------------------------------
129 void *m_allocator(const char *sourceFile, const unsigned int sourceLine, const char *sourceFunc,
130 const unsigned int allocationType, const size_t reportedSize);
131 void *m_reallocator(const char *sourceFile, const unsigned int sourceLine, const char *sourceFunc,
132 const unsigned int reallocationType, const size_t reportedSize, void *reportedAddress);
133 void m_deallocator(const char *sourceFile, const unsigned int sourceLine, const char *sourceFunc,
134 const unsigned int deallocationType, const void *reportedAddress);
136 // ---------------------------------------------------------------------------------------------------------------------------------
137 // Utilitarian functions
138 // ---------------------------------------------------------------------------------------------------------------------------------
140 bool m_validateAddress(const void *reportedAddress);
141 bool m_validateAllocUnit(const sAllocUnit *allocUnit);
142 bool m_validateAllAllocUnits();
144 // ---------------------------------------------------------------------------------------------------------------------------------
145 // Unused RAM calculations
146 // ---------------------------------------------------------------------------------------------------------------------------------
148 unsigned int m_calcUnused(const sAllocUnit *allocUnit);
149 unsigned int m_calcAllUnused();
151 // ---------------------------------------------------------------------------------------------------------------------------------
152 // Logging and reporting
153 // ---------------------------------------------------------------------------------------------------------------------------------
155 void m_dumpAllocUnit(const sAllocUnit *allocUnit, const char *prefix = "");
156 void m_dumpMemoryReport(const char *filename = "memreport.log", const bool overwrite = true);
157 sMStats m_getMemoryStatistics();
159 // ---------------------------------------------------------------------------------------------------------------------------------
160 // Variations of global operators new & delete
161 // ---------------------------------------------------------------------------------------------------------------------------------
163 void *operator new(size_t reportedSize);
164 void *operator new[](size_t reportedSize);
165 void *operator new(size_t reportedSize, const char *sourceFile, int sourceLine);
166 void *operator new[](size_t reportedSize, const char *sourceFile, int sourceLine);
167 void operator delete(void *reportedAddress);
168 void operator delete[](void *reportedAddress);
172 // ---------------------------------------------------------------------------------------------------------------------------------
173 // Macros -- "Kids, please don't try this at home. We're trained professionals here." :)
174 // ---------------------------------------------------------------------------------------------------------------------------------
177 #define new (m_setOwner (__FILE__,__LINE__,__FUNCTION__),false) ? NULL : new
178 #define delete (m_setOwner (__FILE__,__LINE__,__FUNCTION__),false) ? m_setOwner("",0,"") : delete
179 #define malloc(sz) m_allocator (__FILE__,__LINE__,__FUNCTION__,m_alloc_malloc,sz)
180 #define calloc(sz) m_allocator (__FILE__,__LINE__,__FUNCTION__,m_alloc_calloc,sz)
181 #define realloc(ptr,sz) m_reallocator(__FILE__,__LINE__,__FUNCTION__,m_alloc_realloc,sz,ptr)
182 #define free(ptr) m_deallocator(__FILE__,__LINE__,__FUNCTION__,m_alloc_free,ptr)
184 // ---------------------------------------------------------------------------------------------------------------------------------
185 // mmgr.h - End of file
186 // ---------------------------------------------------------------------------------------------------------------------------------