-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMC_Mem.h
208 lines (170 loc) · 5.87 KB
/
MC_Mem.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
// Massgate
// Copyright (C) 2017 Ubisoft Entertainment
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
#pragma once
#ifndef MC_MEM_H
#define MC_MEM_H
#include "mc_globaldefines.h"
void __cdecl FatalError(const char* aMessage, ...);
bool MC_EnableDeadlockFinder();
extern bool ourGlobalIsCrashingFlag;
void MC_MemRegisterAdditionalExceptionHandler(void (*aFunction)());
void MC_TestMemory();
void MC_SetBoomFilename(const char* aFilename);
const char* MC_GetBoomFilename();
void MC_SetBoomBuildPrefix(const char* aString);
void MC_WriteBoomFile(const char* aString);
void MC_GetFreeMemoryDescription(char* writeHere);
#ifdef assert
#error DON'T INCLUDE BOTH assert.h AND mc_mem.h
#endif // assert
#ifndef _RELEASE_
bool __cdecl MC_Assert(const char* aFile, int aLine, const char* aString, bool* anIgnoreFlag);
#ifndef MC_NO_ASSERTS
#define MC_ASSERT(X) do { \
static bool ignoreAlwaysFlag = false; \
if(!(X) && !ignoreAlwaysFlag && MC_Assert(__FILE__, __LINE__, #X, &ignoreAlwaysFlag)) \
__debugbreak(); \
} while(0)
#else
#define MC_ASSERT(X) do {} while(0)
#endif
#else // _RELEASE_
#define MC_ASSERT(X) do {} while(0)
#endif // _RELEASE_
#if _WIN64
#define MC_SAFECAST(aValue) MC_Safecast(aValue)
inline u32 MC_Safecast(size_t aValue)
{
MC_ASSERT(aValue <= 0xFFFFFFFF);
return(u32)aValue;
}
#else
#define MC_SAFECAST(aValue) aValue
#endif
// If this string is present in your exe the mapfileparser tool will not append the pdb to the exe.
#define MC_MEM_DONT_ADD_PDB_MARKER "!!--!![[[DONT ADD PDB TO ME]]]!!--!!"
//
// Temp memory must be allocated and freed by the SAME THREAD.
//
struct MC_TempMemStats
{
int mySavedAllocsCount;
int myTooBigRequestCount;
int myCurrentUsageInBytes;
int myMaxEverUsageInBytes;
int myBufferSizeInBytes;
};
void* MC_TempAlloc(size_t aSize);
void* MC_TempAllocIfOwnerOnStack(void* anOwner, size_t aSize, const char* aFile, int aLine);
bool MC_IsTempAlloc(void* aPointer);
void MC_TempFree(void* aPointer);
void MC_GetStats(MC_TempMemStats& stats);
bool MC_IsProbablyOnStack(const void* aPointer);
void MC_TempMemCheckAllocations();
void MC_TempMemSetDebugLevel(int aLevel);
void MC_TempMemEnable();
bool MC_TempMemIsEnabled();
class MC_TempBuffer
{
public:
MC_TempBuffer::MC_TempBuffer(size_t aSize) { myData = MC_TempAlloc(aSize); }
MC_TempBuffer::~MC_TempBuffer() { MC_TempFree(myData); }
void* GetData() const { return myData; }
private:
void* myData;
};
#ifdef MC_ENABLE_MC_MEM
#define MC_MEM_CRTDBG
//#define MC_MEM_INTERNAL
#ifdef MC_MEM_CRTDBG
#ifdef MC_MEM_INTERNAL
#error CAN'T SPECIFY BOTH MC_MEM_CRTDBG AND MC_MEM_INTERNAL
#endif // MC_MEM_INTERNAL
#endif // MC_MEM_CRTDBG
#ifdef MC_MEM_CRTDBG
#ifndef DEBUG_NEW
#include <crtdbg.h>
#define new new(_NORMAL_BLOCK, __FILE__, __LINE__)
#endif // not DEBUG_NEW
#endif // MC_MEM_CRTDBG
#ifdef MC_MEM_INTERNAL
//#ifdef malloc
//#undef malloc
//#endif // malloc
#ifdef __cplusplus
extern "C" {
#endif
void* MC_MemMalloc(size_t);
void* MC_MemMalloc(size_t aSize);
void* MC_MemCalloc(size_t aNumElements, size_t aSizePerElement);
void* MC_MemRealloc(void* aPointer, size_t aSize);
void MC_MemFree(void* aPointer);
#define malloc MC_MemMalloc
#define calloc MC_MemCalloc
#define realloc MC_MemRealloc
#define free MC_MemFree
#ifdef __cplusplus
}
#endif
MC_FORCEINLINE void* __cdecl operator new(size_t aSize) { return MC_MemMalloc(aSize); }
MC_FORCEINLINE void __cdecl operator delete(void* aPointer) { if(aPointer) MC_MemFree(aPointer); }
void MC_MemDumpMemoryLeaks(bool aDumpToDebugWindowFlag);
void MC_MemDumpMemoryLeaksExtended(); // only available if MC_MEM_EXTENDED_MEM_DUMP is #defined in mc_mem.cpp
void MC_VerifyHeap();
#endif // MC_MEM_INTERNAL
#endif // MC_ENABLE_MC_MEM
#ifdef MC_OVERLOAD_NEW_DELETE
void * operator new( size_t aSize, const char * aFileName, int aLine );
void * operator new[]( size_t aSize, const char * aFileName, int aLine );
#define MC_NEW new(__FILE__, __LINE__)
#else
#define MC_NEW new
#endif
#define MC_MAKE_EXTRA_MEMORYLEAK_NAME(x) __noop
#define MC_SET_EXTRA_MEMORYLEAK_INFO MC_MemLeakExtraInfo MC_MAKE_EXTRA_MEMORYLEAK_NAME(extraLeakInfoInstance)
#ifdef MC_MEMORYLEAK_STACKTRACE
struct MC_MemLeakExtraInfo
{
MC_MemLeakExtraInfo(const char* aFormat, ...);
~MC_MemLeakExtraInfo();
char myInfo[256];
static int GetStackDepth();
static const char* GetInfoAt(int aDepth);
};
#else // MC_MEMORYLEAK_STACKTRACE
struct MC_MemLeakExtraInfo
{
inline MC_MemLeakExtraInfo(const char* /*aFormat*/, ...)
{
}
};
#endif //MC_MEMORYLEAK_STACKTRACE
#else // MC_MEM_H
#ifndef _RELEASE_
bool __cdecl MC_Assert(const char* aFile, int aLine, const char* aString, bool* anIgnoreFlag);
#ifndef MC_NO_ASSERTS
#define MC_ASSERT(X) do { \
static bool ignoreAlwaysFlag = false; \
if(!(X) && !ignoreAlwaysFlag && MC_Assert(__FILE__, __LINE__, #X, &ignoreAlwaysFlag)) \
__debugbreak(); \
} while(0)
#else
#define MC_ASSERT(X) do {} while(0)
#endif
#else // _RELEASE_
#define MC_ASSERT(X) do {} while(0)
#endif // _RELEASE_
#endif // MC_MEM_H