/
de_memory.cpp
226 lines (174 loc) · 3.21 KB
/
de_memory.cpp
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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
#include "bdefs.h"
#ifdef _MSC_VER
#include <new.h>
#else
#include <new>
#endif
#include "de_memory.h"
#include "build_options.h"
#include "sysdebugging.h"
#include "dsys_interface.h"
#include "syscounter.h"
#include "ltmem.h"
//#define TRACK_MEMORY_USAGE
class MemTrack
{
public:
uint32 m_AllocSize;
uint32 m_AllocNumber;
};
static uint32 g_MemoryUsage;
static uint32 g_nAllocations;
uint32 g_nTotalAllocations, g_nTotalFrees;
static int g_MemRefCount=0;
#ifdef _MSC_VER
static _PNH g_OldNewHandler;
#else
static std::new_handler g_OldNewHandler;
#define _set_new_handler std::set_new_handler
#endif
// PlayDemo profile info.
uint32 g_PD_Malloc=0;
uint32 g_PD_Free=0;
// Hook Stdlith's base allocators.
void* DefStdlithAlloc(uint32 size)
{
return dalloc(size);
}
void DefStdlithFree(void *ptr)
{
dfree(ptr);
}
void dm_HeapCompact()
{
HANDLE hHeap;
if(hHeap = GetProcessHeap())
{
HeapCompact(hHeap, 0);
}
}
#ifdef _MSC_VER
static int dm_NewHandler(size_t size)
{
dsi_OnMemoryFailure();
return 0;
}
#else
static void dm_NewHandler()
{
dsi_OnMemoryFailure();
}
#endif
void dm_Init()
{
if(g_MemRefCount == 0)
{
g_OldNewHandler = _set_new_handler(dm_NewHandler);
g_MemoryUsage = 0;
g_nAllocations = 0;
g_nTotalAllocations = g_nTotalFrees = 0;
}
g_MemRefCount++;
}
void dm_Term()
{
g_MemRefCount--;
if(g_MemRefCount == 0)
{
// Restore the old new handler.
_set_new_handler(g_OldNewHandler);
g_MemoryUsage = 0;
g_nAllocations = 0;
}
}
uint32 dm_GetBytesAllocated()
{
return g_MemoryUsage;
}
uint32 dm_GetNumAllocations()
{
return g_nAllocations;
}
void* dalloc(size_t size)
{
//[DLK] Removed to avoid allocation errors with D3DXEffectCompiler
/*
if(size == 0)
{
return NULL;
}
*/
unsigned long fullAllocSize;
// Add 4 bytes if we're tracking memory usage.
#ifdef TRACK_MEMORY_USAGE
MemTrack *pMemTrack;
fullAllocSize = size + sizeof(MemTrack);
#else
fullAllocSize = size;
#endif
char *ptr;
// Try to allocate the memory.
{
CountAdder cntAdd(&g_PD_Malloc);
LT_MEM_TRACK_ALLOC(ptr = (char*)LTMemAlloc((size_t)fullAllocSize),LT_MEM_TYPE_UNKNOWN);
}
if(!ptr)
{
dsi_OnMemoryFailure();
}
// Store the size in there if we're tracking memory usage.
#ifdef TRACK_MEMORY_USAGE
g_MemoryUsage += size;
pMemTrack = (MemTrack*)ptr;
pMemTrack->m_AllocSize = size;
pMemTrack->m_AllocNumber = g_nAllocations;
ptr += sizeof(MemTrack);
#endif
++g_nAllocations;
++g_nTotalAllocations;
return ptr;
}
void* dalloc_z(size_t size)
{
void *ret;
ret = dalloc(size);
if(ret)
{
memset(ret, 0, size);
}
return ret;
}
void dfree(void *ptr)
{
if(!ptr)
return;
char *pCharPtr = (char*)ptr;
#ifdef TRACK_MEMORY_USAGE
MemTrack *pMemTrack;
pCharPtr -= sizeof(MemTrack);
pMemTrack = (MemTrack*)pCharPtr;
if(pMemTrack->m_AllocSize > g_MemoryUsage)
{
dsi_PrintToConsole("Error: engine freed more memory than allocated!");
}
g_MemoryUsage -= pMemTrack->m_AllocSize;
#endif
--g_nAllocations;
++g_nTotalFrees;
{
CountAdder cntAdd(&g_PD_Free);
LTMemFree(pCharPtr);
}
}
void* operator new(size_t size, void *ptr, char z)
{
return dalloc(size);
}
void* operator new(size_t size)
{
return dalloc(size);
}
void operator delete(void *ptr)
{
dfree(ptr);
}