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
// ---------------------------------------------------------------------------------------------------------------------------------
40
typedef unsigned long size_t;
44
// ---------------------------------------------------------------------------------------------------------------------------------
45
// For systems that don't have the __FUNCTION__ variable, we can just define it here
46
// ---------------------------------------------------------------------------------------------------------------------------------
48
#define __FUNCTION__ "??"
50
// ---------------------------------------------------------------------------------------------------------------------------------
52
// ---------------------------------------------------------------------------------------------------------------------------------
59
void *reportedAddress;
62
unsigned int sourceLine;
63
unsigned int allocationType;
66
unsigned int allocationNumber;
73
unsigned int totalReportedMemory;
74
unsigned int totalActualMemory;
75
unsigned int peakReportedMemory;
76
unsigned int peakActualMemory;
77
unsigned int accumulatedReportedMemory;
78
unsigned int accumulatedActualMemory;
79
unsigned int accumulatedAllocUnitCount;
80
unsigned int totalAllocUnitCount;
81
unsigned int peakAllocUnitCount;
84
// ---------------------------------------------------------------------------------------------------------------------------------
86
// ---------------------------------------------------------------------------------------------------------------------------------
88
extern const unsigned int m_alloc_unknown;
89
extern const unsigned int m_alloc_new;
90
extern const unsigned int m_alloc_new_array;
91
extern const unsigned int m_alloc_malloc;
92
extern const unsigned int m_alloc_calloc;
93
extern const unsigned int m_alloc_realloc;
94
extern const unsigned int m_alloc_delete;
95
extern const unsigned int m_alloc_delete_array;
96
extern const unsigned int m_alloc_free;
98
// ---------------------------------------------------------------------------------------------------------------------------------
100
// ---------------------------------------------------------------------------------------------------------------------------------
102
void m_setOwner(const char *file, const unsigned int line, const char *func);
104
// ---------------------------------------------------------------------------------------------------------------------------------
105
// Allocation breakpoints
106
// ---------------------------------------------------------------------------------------------------------------------------------
108
bool &m_breakOnRealloc(void *reportedAddress);
109
bool &m_breakOnDealloc(void *reportedAddress);
111
// ---------------------------------------------------------------------------------------------------------------------------------
112
// The meat of the memory tracking software
113
// ---------------------------------------------------------------------------------------------------------------------------------
115
void *m_allocator(const char *sourceFile, const unsigned int sourceLine, const char *sourceFunc,
116
const unsigned int allocationType, const size_t reportedSize);
117
void *m_reallocator(const char *sourceFile, const unsigned int sourceLine, const char *sourceFunc,
118
const unsigned int reallocationType, const size_t reportedSize, void *reportedAddress);
119
void m_deallocator(const char *sourceFile, const unsigned int sourceLine, const char *sourceFunc,
120
const unsigned int deallocationType, const void *reportedAddress);
122
// ---------------------------------------------------------------------------------------------------------------------------------
123
// Utilitarian functions
124
// ---------------------------------------------------------------------------------------------------------------------------------
126
bool m_validateAddress(const void *reportedAddress);
127
bool m_validateAllocUnit(const sAllocUnit *allocUnit);
128
bool m_validateAllAllocUnits();
130
// ---------------------------------------------------------------------------------------------------------------------------------
131
// Unused RAM calculations
132
// ---------------------------------------------------------------------------------------------------------------------------------
134
unsigned int m_calcUnused(const sAllocUnit *allocUnit);
135
unsigned int m_calcAllUnused();
137
// ---------------------------------------------------------------------------------------------------------------------------------
138
// Logging and reporting
139
// ---------------------------------------------------------------------------------------------------------------------------------
141
void m_dumpAllocUnit(const sAllocUnit *allocUnit, const char *prefix = "");
142
void m_dumpMemoryReport(const char *filename = "memreport.log", const bool overwrite = true);
143
sMStats m_getMemoryStatistics();
145
// ---------------------------------------------------------------------------------------------------------------------------------
146
// Variations of global operators new & delete
147
// ---------------------------------------------------------------------------------------------------------------------------------
149
void *operator new(size_t reportedSize);
150
void *operator new[](size_t reportedSize);
151
void *operator new(size_t reportedSize, const char *sourceFile, int sourceLine);
152
void *operator new[](size_t reportedSize, const char *sourceFile, int sourceLine);
153
void operator delete(void *reportedAddress);
154
void operator delete[](void *reportedAddress);
158
// ---------------------------------------------------------------------------------------------------------------------------------
159
// Macros -- "Kids, please don't try this at home. We're trained professionals here." :)
160
// ---------------------------------------------------------------------------------------------------------------------------------
163
#define new (m_setOwner (__FILE__,__LINE__,__FUNCTION__),false) ? NULL : new
164
#define delete (m_setOwner (__FILE__,__LINE__,__FUNCTION__),false) ? m_setOwner("",0,"") : delete
165
#define malloc(sz) m_allocator (__FILE__,__LINE__,__FUNCTION__,m_alloc_malloc,sz)
166
#define calloc(sz) m_allocator (__FILE__,__LINE__,__FUNCTION__,m_alloc_calloc,sz)
167
#define realloc(ptr,sz) m_reallocator(__FILE__,__LINE__,__FUNCTION__,m_alloc_realloc,sz,ptr)
168
#define free(ptr) m_deallocator(__FILE__,__LINE__,__FUNCTION__,m_alloc_free,ptr)
170
// ---------------------------------------------------------------------------------------------------------------------------------
171
// mmgr.h - End of file
172
// ---------------------------------------------------------------------------------------------------------------------------------