-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathOutOfMemoryHelper.cpp
60 lines (50 loc) · 1.28 KB
/
OutOfMemoryHelper.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
#include "Utilities.h"
void CreateCrashSave();
namespace OutOfMemoryHelper
{
namespace EmergencyBuffer
{
constexpr UInt32 BUFFER_SIZE = 0x2000;
constexpr UInt32 NUM_BUFFERS = 0x80;
void* pBuffer[NUM_BUFFERS];
void Allocate()
{
if (!pBuffer)
{
for (int i = 0; i < NUM_BUFFERS; ++i)
{
pBuffer[i] = malloc(BUFFER_SIZE);
}
}
}
void Free()
{
for (int i = 0; i < NUM_BUFFERS; ++i)
{
if (pBuffer[i])
{
free(pBuffer[i]);
pBuffer[i] = nullptr;
}
}
}
}
void* __stdcall OnMemoryAllocationFailure(int size)
{
EmergencyBuffer::Free();
MessageBoxA(nullptr, "Geck was unable to allocate requested memory, attempting to clear buffers...", "Out of memory", MB_ICONERROR);
// PurgeCellBuffers - the problem is without knowing where the crash was, we can't really safely free the cells...
CdeclCall(0x44F180);
CreateCrashSave();
EmergencyBuffer::Allocate();
return malloc(size);
}
void Init()
{
// allocate some memory to be freed in emergencies to provide a small buffer for handling issues once out of memory
EmergencyBuffer::Allocate();
// try and free up memory if the geck runs out
SafeWriteBuf(0x854283, "\x85\xC0\x74\x03\xC2\x04\x00", 7);
WriteRelJump(0x854283 + 7, UInt32(OnMemoryAllocationFailure));
}
}