]> git.jsancho.org Git - lugaru.git/blob - Source/mmgr.h
Added GPL license and headers.
[lugaru.git] / Source / mmgr.h
1 /*
2 Copyright (C) 2003, 2010 - Wolfire Games
3
4 This file is part of Lugaru.
5
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.
10
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.  
14
15 See the GNU General Public License for more details.
16
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.
20 */
21
22 // ---------------------------------------------------------------------------------------------------------------------------------
23 //                                     _     
24 //                                    | |    
25 //  _ __ ___  _ __ ___   __ _ _ __    | |__  
26 // | '_ ` _ \| '_ ` _ \ / _` | '__|   | '_ \ 
27 // | | | | | | | | | | | (_| | |    _ | | | |
28 // |_| |_| |_|_| |_| |_|\__, |_|   (_)|_| |_|
29 //                       __/ |               
30 //                      |___/                
31 //
32 // Memory manager & tracking software
33 //
34 // Best viewed with 8-character tabs and (at least) 132 columns
35 //
36 // ---------------------------------------------------------------------------------------------------------------------------------
37 //
38 // Restrictions & freedoms pertaining to usage and redistribution of this software:
39 //
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.
46 //
47 // For more information, visit HTTP://www.FluidStudios.com
48 //
49 // ---------------------------------------------------------------------------------------------------------------------------------
50 // Originally created on 12/22/2000 by Paul Nettle
51 //
52 // Copyright 2000, Fluid Studios, Inc., all rights reserved.
53 // ---------------------------------------------------------------------------------------------------------------------------------
54
55 #ifndef _H_MMGR
56 #define _H_MMGR
57
58 // ---------------------------------------------------------------------------------------------------------------------------------
59 // For systems that don't have the __FUNCTION__ variable, we can just define it here
60 // ---------------------------------------------------------------------------------------------------------------------------------
61 #ifndef __FUNCTION__
62 #define __FUNCTION__ "??"
63 #endif
64 // ---------------------------------------------------------------------------------------------------------------------------------
65 // Types
66 // ---------------------------------------------------------------------------------------------------------------------------------
67
68 typedef struct tag_au
69 {
70         size_t          actualSize;
71         size_t          reportedSize;
72         void            *actualAddress;
73         void            *reportedAddress;
74         char            sourceFile[40];
75         char            sourceFunc[40];
76         unsigned int    sourceLine;
77         unsigned int    allocationType;
78         bool            breakOnDealloc;
79         bool            breakOnRealloc;
80         unsigned int    allocationNumber;
81         struct tag_au   *next;
82         struct tag_au   *prev;
83 } sAllocUnit;
84
85 typedef struct
86 {
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;
96 } sMStats;
97
98 // ---------------------------------------------------------------------------------------------------------------------------------
99 // External constants
100 // ---------------------------------------------------------------------------------------------------------------------------------
101
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;
111
112 // ---------------------------------------------------------------------------------------------------------------------------------
113 // Used by the macros
114 // ---------------------------------------------------------------------------------------------------------------------------------
115
116 void            m_setOwner(const char *file, const unsigned int line, const char *func);
117
118 // ---------------------------------------------------------------------------------------------------------------------------------
119 // Allocation breakpoints
120 // ---------------------------------------------------------------------------------------------------------------------------------
121
122 bool            &m_breakOnRealloc(void *reportedAddress);
123 bool            &m_breakOnDealloc(void *reportedAddress);
124
125 // ---------------------------------------------------------------------------------------------------------------------------------
126 // The meat of the memory tracking software
127 // ---------------------------------------------------------------------------------------------------------------------------------
128
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);
135
136 // ---------------------------------------------------------------------------------------------------------------------------------
137 // Utilitarian functions
138 // ---------------------------------------------------------------------------------------------------------------------------------
139
140 bool            m_validateAddress(const void *reportedAddress);
141 bool            m_validateAllocUnit(const sAllocUnit *allocUnit);
142 bool            m_validateAllAllocUnits();
143
144 // ---------------------------------------------------------------------------------------------------------------------------------
145 // Unused RAM calculations
146 // ---------------------------------------------------------------------------------------------------------------------------------
147
148 unsigned int    m_calcUnused(const sAllocUnit *allocUnit);
149 unsigned int    m_calcAllUnused();
150
151 // ---------------------------------------------------------------------------------------------------------------------------------
152 // Logging and reporting
153 // ---------------------------------------------------------------------------------------------------------------------------------
154
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();
158
159 // ---------------------------------------------------------------------------------------------------------------------------------
160 // Variations of global operators new & delete
161 // ---------------------------------------------------------------------------------------------------------------------------------
162
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);
169
170 #endif // _H_MMGR
171
172 // ---------------------------------------------------------------------------------------------------------------------------------
173 // Macros -- "Kids, please don't try this at home. We're trained professionals here." :)
174 // ---------------------------------------------------------------------------------------------------------------------------------
175
176 #include "nommgr.h"
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)
183
184 // ---------------------------------------------------------------------------------------------------------------------------------
185 // mmgr.h - End of file
186 // ---------------------------------------------------------------------------------------------------------------------------------
187