]> git.jsancho.org Git - lugaru.git/blob - Source/mmgr.h
41e0c634bfcf62b611290da28e441ce6f0d222ea
[lugaru.git] / Source / mmgr.h
1 // ---------------------------------------------------------------------------------------------------------------------------------
2 //                                     _     
3 //                                    | |    
4 //  _ __ ___  _ __ ___   __ _ _ __    | |__  
5 // | '_ ` _ \| '_ ` _ \ / _` | '__|   | '_ \ 
6 // | | | | | | | | | | | (_| | |    _ | | | |
7 // |_| |_| |_|_| |_| |_|\__, |_|   (_)|_| |_|
8 //                       __/ |               
9 //                      |___/                
10 //
11 // Memory manager & tracking software
12 //
13 // Best viewed with 8-character tabs and (at least) 132 columns
14 //
15 // ---------------------------------------------------------------------------------------------------------------------------------
16 //
17 // Restrictions & freedoms pertaining to usage and redistribution of this software:
18 //
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.
25 //
26 // For more information, visit HTTP://www.FluidStudios.com
27 //
28 // ---------------------------------------------------------------------------------------------------------------------------------
29 // Originally created on 12/22/2000 by Paul Nettle
30 //
31 // Copyright 2000, Fluid Studios, Inc., all rights reserved.
32 // ---------------------------------------------------------------------------------------------------------------------------------
33
34 #ifndef _H_MMGR
35 #define _H_MMGR
36
37 // ---------------------------------------------------------------------------------------------------------------------------------
38 // For systems that don't have the __FUNCTION__ variable, we can just define it here
39 // ---------------------------------------------------------------------------------------------------------------------------------
40 #ifndef __FUNCTION__
41 #define __FUNCTION__ "??"
42 #endif
43 // ---------------------------------------------------------------------------------------------------------------------------------
44 // Types
45 // ---------------------------------------------------------------------------------------------------------------------------------
46
47 typedef struct tag_au
48 {
49         size_t          actualSize;
50         size_t          reportedSize;
51         void            *actualAddress;
52         void            *reportedAddress;
53         char            sourceFile[40];
54         char            sourceFunc[40];
55         unsigned int    sourceLine;
56         unsigned int    allocationType;
57         bool            breakOnDealloc;
58         bool            breakOnRealloc;
59         unsigned int    allocationNumber;
60         struct tag_au   *next;
61         struct tag_au   *prev;
62 } sAllocUnit;
63
64 typedef struct
65 {
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;
75 } sMStats;
76
77 // ---------------------------------------------------------------------------------------------------------------------------------
78 // External constants
79 // ---------------------------------------------------------------------------------------------------------------------------------
80
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;
90
91 // ---------------------------------------------------------------------------------------------------------------------------------
92 // Used by the macros
93 // ---------------------------------------------------------------------------------------------------------------------------------
94
95 void            m_setOwner(const char *file, const unsigned int line, const char *func);
96
97 // ---------------------------------------------------------------------------------------------------------------------------------
98 // Allocation breakpoints
99 // ---------------------------------------------------------------------------------------------------------------------------------
100
101 bool            &m_breakOnRealloc(void *reportedAddress);
102 bool            &m_breakOnDealloc(void *reportedAddress);
103
104 // ---------------------------------------------------------------------------------------------------------------------------------
105 // The meat of the memory tracking software
106 // ---------------------------------------------------------------------------------------------------------------------------------
107
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);
114
115 // ---------------------------------------------------------------------------------------------------------------------------------
116 // Utilitarian functions
117 // ---------------------------------------------------------------------------------------------------------------------------------
118
119 bool            m_validateAddress(const void *reportedAddress);
120 bool            m_validateAllocUnit(const sAllocUnit *allocUnit);
121 bool            m_validateAllAllocUnits();
122
123 // ---------------------------------------------------------------------------------------------------------------------------------
124 // Unused RAM calculations
125 // ---------------------------------------------------------------------------------------------------------------------------------
126
127 unsigned int    m_calcUnused(const sAllocUnit *allocUnit);
128 unsigned int    m_calcAllUnused();
129
130 // ---------------------------------------------------------------------------------------------------------------------------------
131 // Logging and reporting
132 // ---------------------------------------------------------------------------------------------------------------------------------
133
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();
137
138 // ---------------------------------------------------------------------------------------------------------------------------------
139 // Variations of global operators new & delete
140 // ---------------------------------------------------------------------------------------------------------------------------------
141
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);
148
149 #endif // _H_MMGR
150
151 // ---------------------------------------------------------------------------------------------------------------------------------
152 // Macros -- "Kids, please don't try this at home. We're trained professionals here." :)
153 // ---------------------------------------------------------------------------------------------------------------------------------
154
155 #include "nommgr.h"
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)
162
163 // ---------------------------------------------------------------------------------------------------------------------------------
164 // mmgr.h - End of file
165 // ---------------------------------------------------------------------------------------------------------------------------------
166