From 2bcbfe8747b5f525523b6a0713cc2a8b463977c8 Mon Sep 17 00:00:00 2001 From: Ryan Houdek Date: Sun, 19 Oct 2025 14:05:53 -0700 Subject: [PATCH] Windows: rpmalloc --- CMakeLists.txt | 2 +- FEXCore/Source/Utils/AllocatorHooks.cpp | 28 +++---------------------- Source/Windows/ARM64EC/Module.cpp | 1 + Source/Windows/Common/CRT/CRT.cpp | 5 +++++ 4 files changed, 10 insertions(+), 26 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index c7453c5a5..f3480b6ee 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -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" diff --git a/FEXCore/Source/Utils/AllocatorHooks.cpp b/FEXCore/Source/Utils/AllocatorHooks.cpp index b800c8a9b..87e38d51f 100644 --- a/FEXCore/Source/Utils/AllocatorHooks.cpp +++ b/FEXCore/Source/Utils/AllocatorHooks.cpp @@ -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" diff --git a/Source/Windows/ARM64EC/Module.cpp b/Source/Windows/ARM64EC/Module.cpp index 49d3f1bce..2384efaff 100644 --- a/Source/Windows/ARM64EC/Module.cpp +++ b/Source/Windows/ARM64EC/Module.cpp @@ -1027,6 +1027,7 @@ NTSTATUS ThreadTerm(HANDLE Thread, LONG ExitCode) { if (ThreadTID == GetCurrentThreadId()) { FEX::Windows::DeinitCRTThread(); } + return STATUS_SUCCESS; } diff --git a/Source/Windows/Common/CRT/CRT.cpp b/Source/Windows/Common/CRT/CRT.cpp index bf87f1b0f..addea79d8 100644 --- a/Source/Windows/Common/CRT/CRT.cpp +++ b/Source/Windows/Common/CRT/CRT.cpp @@ -3,6 +3,7 @@ #include #include #include +#include #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