forked from focus-creative-games/il2cpp_plus
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMemory.cpp
More file actions
96 lines (83 loc) · 2.46 KB
/
Memory.cpp
File metadata and controls
96 lines (83 loc) · 2.46 KB
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
#include "il2cpp-config.h"
#include "os/Memory.h"
#include "utils/Memory.h"
#include <cstdlib>
namespace il2cpp
{
namespace utils
{
struct MonoMemoryCallbacks
{
int version;
void *(*malloc_func)(size_t size);
void *(*realloc_func)(void *mem, size_t count);
void(*free_func)(void *mem);
void *(*calloc_func)(size_t count, size_t size);
};
extern "C"
{
int32_t mono_set_allocator_vtable(MonoMemoryCallbacks* callbacks);
}
static MonoMemoryCallbacks s_MonoCallbacks =
{
1, //MONO_ALLOCATOR_VTABLE_VERSION
NULL,
NULL,
NULL,
NULL
};
static Il2CppMemoryCallbacks s_Callbacks =
{
malloc,
os::Memory::AlignedAlloc,
free,
os::Memory::AlignedFree,
calloc,
realloc,
os::Memory::AlignedReAlloc
};
void Memory::SetMemoryCallbacks(Il2CppMemoryCallbacks* callbacks)
{
memcpy(&s_Callbacks, callbacks, sizeof(Il2CppMemoryCallbacks));
#if IL2CPP_MONO_DEBUGGER
// The debugger uses Mono code, so we need to remap the callbacks
// for Mono allocations and frees to the same ones IL2CPP is using.
s_MonoCallbacks.malloc_func = callbacks->malloc_func;
s_MonoCallbacks.realloc_func = callbacks->realloc_func;
s_MonoCallbacks.free_func = callbacks->free_func;
s_MonoCallbacks.calloc_func = callbacks->calloc_func;
int32_t installed = mono_set_allocator_vtable(&s_MonoCallbacks);
IL2CPP_ASSERT(installed != 0);
NO_UNUSED_WARNING(installed);
#endif
}
void* Memory::Malloc(size_t size)
{
return s_Callbacks.malloc_func(size);
}
void* Memory::AlignedMalloc(size_t size, size_t alignment)
{
return s_Callbacks.aligned_malloc_func(size, alignment);
}
void Memory::Free(void* memory)
{
return s_Callbacks.free_func(memory);
}
void Memory::AlignedFree(void* memory)
{
return s_Callbacks.aligned_free_func(memory);
}
void* Memory::Calloc(size_t count, size_t size)
{
return s_Callbacks.calloc_func(count, size);
}
void* Memory::Realloc(void* memory, size_t newSize)
{
return s_Callbacks.realloc_func(memory, newSize);
}
void* Memory::AlignedRealloc(void* memory, size_t newSize, size_t alignment)
{
return s_Callbacks.aligned_realloc_func(memory, newSize, alignment);
}
} /* namespace utils */
} /* namespace il2cpp */