mirror of
https://github.com/FEX-Emu/FEX.git
synced 2026-10-06 07:00:20 +02:00
Windows: rpmalloc
This commit is contained in:
4 files changed
+10
-26
No files matched your search
+1
-1
@@ -322,7 +322,7 @@ endif()
|
||||
if (ENABLE_FEX_ALLOCATOR)
|
||||
# The rpmalloc subproject that all FEXCore fextl objects allocate through.
|
||||
add_subdirectory(External/rpmalloc/)
|
||||
elseif (NOT MINGW_BUILD)
|
||||
elseif (NOT MINGW)
|
||||
message (STATUS
|
||||
" FEX allocator is disabled!\n"
|
||||
" This is not a recommended configuration!\n"
|
||||
|
||||
@@ -82,6 +82,7 @@ void InitializeThread() {
|
||||
rpmalloc_thread_initialize();
|
||||
}
|
||||
|
||||
#ifndef _WIN32
|
||||
[[nodiscard]]
|
||||
constexpr uint64_t AlignUp(uint64_t value, uint64_t size) {
|
||||
return value + (size - value % size) % size;
|
||||
@@ -95,9 +96,6 @@ static void* FEX_rp_mmap(size_t size, size_t alignment, size_t* offset, size_t*
|
||||
}
|
||||
|
||||
size_t map_size = AlignUp(size + alignment, global_config.page_size);
|
||||
#ifdef _WIN32
|
||||
auto ptr = VirtualAlloc(0, map_size, MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE);
|
||||
#else
|
||||
auto ptr = fex_mmap_hook(0, map_size, PROT_READ | PROT_WRITE, MAP_ANONYMOUS | MAP_PRIVATE, -1, 0);
|
||||
|
||||
if (ptr == MAP_FAILED) {
|
||||
@@ -112,7 +110,6 @@ static void* FEX_rp_mmap(size_t size, size_t alignment, size_t* offset, size_t*
|
||||
#endif
|
||||
prctl(PR_SET_VMA, PR_SET_VMA_ANON_NAME, ptr, map_size, global_config.page_name);
|
||||
}
|
||||
#endif
|
||||
|
||||
if (ptr == nullptr) {
|
||||
fprintf(stderr, "Failed to map VMA region.");
|
||||
@@ -132,17 +129,7 @@ static void* FEX_rp_mmap(size_t size, size_t alignment, size_t* offset, size_t*
|
||||
}
|
||||
|
||||
static void FEX_rp_memory_commit(void* address, size_t size) {
|
||||
if (global_config.disable_decommit) {
|
||||
return;
|
||||
}
|
||||
|
||||
#ifdef _WIN32
|
||||
if (!VirtualAlloc(address, size, MEM_COMMIT, PAGE_READWRITE)) {
|
||||
#else
|
||||
if (false) {
|
||||
#endif
|
||||
fprintf(stderr, "Failed to commit VMA region.");
|
||||
}
|
||||
// NOP-implementation.
|
||||
}
|
||||
|
||||
static void FEX_rp_memory_decommit(void* address, size_t size) {
|
||||
@@ -150,33 +137,23 @@ static void FEX_rp_memory_decommit(void* address, size_t size) {
|
||||
return;
|
||||
}
|
||||
|
||||
#ifdef _WIN32
|
||||
if (!VirtualFree(address, size, MEM_DECOMMIT)) {
|
||||
#else
|
||||
if (madvise(address, size, MADV_DONTNEED)) {
|
||||
#endif
|
||||
fprintf(stderr, "Failed to decommit VMA region.");
|
||||
}
|
||||
}
|
||||
|
||||
static void FEX_rp_memory_unmap(void* address, size_t offset, size_t mapped_size) {
|
||||
address = pointer_offset(address, -(int32_t)offset);
|
||||
#ifdef _WIN32
|
||||
::VirtualFree(address, mapped_size, MEM_RELEASE);
|
||||
#else
|
||||
int Result = fex_munmap_hook(address, mapped_size);
|
||||
if (Result == -1) {
|
||||
fprintf(stderr, "Failed to unmap VMA region.");
|
||||
}
|
||||
#endif
|
||||
#undef pointer_offset
|
||||
}
|
||||
|
||||
void SetupAllocatorHooks(mmap_hook_type MMapHook, munmap_hook_type MunmapHook) {
|
||||
#ifndef _WIN32
|
||||
fex_mmap_hook = MMapHook;
|
||||
fex_munmap_hook = MunmapHook;
|
||||
#endif
|
||||
}
|
||||
|
||||
static rpmalloc_interface_t global_interface {
|
||||
@@ -194,6 +171,7 @@ void InitializeAllocator(size_t PageSize) {
|
||||
rp_mmap_hook = FEX_rp_mmap;
|
||||
rp_munmap_hook = FEX_rp_memory_unmap;
|
||||
}
|
||||
#endif
|
||||
|
||||
#elif defined(_WIN32)
|
||||
#error "Tried building _WIN32 without jemalloc"
|
||||
|
||||
@@ -1027,6 +1027,7 @@ NTSTATUS ThreadTerm(HANDLE Thread, LONG ExitCode) {
|
||||
if (ThreadTID == GetCurrentThreadId()) {
|
||||
FEX::Windows::DeinitCRTThread();
|
||||
}
|
||||
|
||||
return STATUS_SUCCESS;
|
||||
}
|
||||
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
#include <windef.h>
|
||||
#include <winternl.h>
|
||||
#include <wine/debug.h>
|
||||
#include <rpmalloc/rpmalloc.h>
|
||||
#include "CRT.h"
|
||||
|
||||
extern "C" {
|
||||
@@ -39,6 +40,8 @@ void RunFuncArray(TFuncIt Begin, TFuncIt End, TArgs... Args) {
|
||||
|
||||
namespace FEX::Windows {
|
||||
void InitCRTProcess() {
|
||||
rpmalloc_initialize(nullptr);
|
||||
|
||||
auto GNUCtorBegin = &__CTOR_LIST__[1];
|
||||
auto GNUCtorEnd = GNUCtorBegin;
|
||||
while (*GNUCtorEnd != nullptr) {
|
||||
@@ -53,10 +56,12 @@ void InitCRTProcess() {
|
||||
}
|
||||
|
||||
void InitCRTThread() {
|
||||
rpmalloc_thread_initialize();
|
||||
RunFuncArray(&XLA, &XLZ, nullptr, DLL_THREAD_ATTACH, nullptr);
|
||||
}
|
||||
|
||||
void DeinitCRTThread() {
|
||||
RunFuncArray(&XLA, &XLZ, nullptr, DLL_THREAD_DETACH, nullptr);
|
||||
rpmalloc_thread_finalize();
|
||||
}
|
||||
} // namespace FEX::Windows
|
||||
Reference in new issue
Block a user