From 7774aa4f0c05ca3f6ac4828762d95f7f74c3f129 Mon Sep 17 00:00:00 2001 From: Andrej E Baranov Date: Tue, 27 May 2025 00:04:59 +0700 Subject: [PATCH 1/6] rpmalloc --- .gitmodules | 4 ++++ CMakeLists.txt | 8 ++++---- external/rpmalloc | 1 + src/PCH.h | 1 - src/patches/memorymanager.cpp | 21 ++++++++++++--------- 5 files changed, 21 insertions(+), 14 deletions(-) create mode 160000 external/rpmalloc diff --git a/.gitmodules b/.gitmodules index 6b4835a..90b49f5 100644 --- a/.gitmodules +++ b/.gitmodules @@ -2,3 +2,7 @@ path = external/CommonLibSSE-NG url = https://github.com/alandtse/CommonLibVR.git branch = ng +[submodule "external/rpmalloc"] + path = external/rpmalloc + url = https://github.com/mjansson/rpmalloc.git + branch = main diff --git a/CMakeLists.txt b/CMakeLists.txt index e11b8a3..51981f4 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -56,7 +56,6 @@ find_package(TBB CONFIG COMPONENTS tbb - tbbmalloc CONFIG ) find_package(xbyak REQUIRED CONFIG) @@ -95,6 +94,7 @@ target_include_directories( PRIVATE ${CMAKE_CURRENT_BINARY_DIR}/include ${CMAKE_CURRENT_SOURCE_DIR}/src + "external/rpmalloc/rpmalloc" ) target_link_libraries( @@ -104,16 +104,16 @@ target_link_libraries( Boost::regex CommonLibSSE::CommonLibSSE TBB::tbb - TBB::tbbmalloc xbyak::xbyak + ${CMAKE_CURRENT_SOURCE_DIR}/external/rpmalloc/lib/windows/release/x86-64/rpmalloc.lib ) if (MSVC) target_link_options( ${PROJECT_NAME} PRIVATE - "$<$:/INCREMENTAL;/OPT:NOREF;/OPT:NOICF>" - "$<$:/INCREMENTAL:NO;/OPT:REF;/OPT:ICF;/DEBUG:FULL>" + "$<$:/INCREMENTAL;/OPT:NOREF;/OPT:NOICF;/NODEFAULTLIB:LIBCMT>" + "$<$:/INCREMENTAL:NO;/OPT:REF;/OPT:ICF;/DEBUG:FULL;/NODEFAULTLIB:LIBCMT>" ) endif() diff --git a/external/rpmalloc b/external/rpmalloc new file mode 160000 index 0000000..9351765 --- /dev/null +++ b/external/rpmalloc @@ -0,0 +1 @@ +Subproject commit 9351765a98dfe8ed341df0a7f00dc756379c7141 diff --git a/src/PCH.h b/src/PCH.h index 0e1e673..3207588 100644 --- a/src/PCH.h +++ b/src/PCH.h @@ -72,7 +72,6 @@ #include #include -#include #include #ifndef NDEBUG diff --git a/src/patches/memorymanager.cpp b/src/patches/memorymanager.cpp index 035c7e4..ff22634 100644 --- a/src/patches/memorymanager.cpp +++ b/src/patches/memorymanager.cpp @@ -1,6 +1,7 @@ #include "version.h" #include "offsets.h" +#include namespace { @@ -84,8 +85,8 @@ namespace { if (a_size > 0) return a_alignmentRequired ? - scalable_aligned_malloc(a_size, a_alignment) : - scalable_malloc(a_size); + rpaligned_alloc(a_alignment, a_size) : // scalable_aligned_malloc(a_size, a_alignment) + rpmalloc(a_size); // scalable_malloc(a_size) else return g_trash; } @@ -94,8 +95,8 @@ namespace { if (a_mem != g_trash) a_alignmentRequired ? - scalable_aligned_free(a_mem) : - scalable_free(a_mem); + rpfree(a_mem) : // scalable_aligned_free(a_mem) + rpfree(a_mem); // scalable_free(a_mem) } void* Reallocate(RE::MemoryManager* a_self, void* a_oldMem, std::size_t a_newSize, std::uint32_t a_alignment, bool a_alignmentRequired) @@ -104,8 +105,8 @@ namespace return Allocate(a_self, a_newSize, a_alignment, a_alignmentRequired); else return a_alignmentRequired ? - scalable_aligned_realloc(a_oldMem, a_newSize, a_alignment) : - scalable_realloc(a_oldMem, a_newSize); + rpaligned_realloc(a_oldMem, a_alignment, a_newSize, rpmalloc_usable_size(a_oldMem), 0) : //scalable_aligned_realloc(a_oldMem, a_newSize, a_alignment) : + rprealloc(a_oldMem, a_newSize); // scalable_realloc(a_oldMem, a_newSize); } void ReplaceAllocRoutines() @@ -145,7 +146,8 @@ namespace { std::size_t hk_msize(void* a_ptr) { - return scalable_msize(a_ptr); + //return scalable_msize(a_ptr); + return rpmalloc_usable_size(a_ptr); } void Install() @@ -159,7 +161,7 @@ namespace void* Allocate(RE::ScrapHeap*, std::size_t a_size, std::size_t a_alignment) { return a_size > 0 ? - scalable_aligned_malloc(a_size, a_alignment) : + rpaligned_alloc(a_alignment, a_size) : // scalable_aligned_malloc(a_size, a_alignment) : g_trash; } @@ -173,7 +175,8 @@ namespace void Deallocate(RE::ScrapHeap*, void* a_mem) { if (a_mem != g_trash) - scalable_aligned_free(a_mem); + rpfree(a_mem); // scalable_aligned_free(a_mem); + } void WriteHooks() From 2f15b7b7d9bc0651bdceb6e260f8f6e106c548a0 Mon Sep 17 00:00:00 2001 From: Andrej E Baranov Date: Tue, 27 May 2025 18:05:31 +0700 Subject: [PATCH 2/6] add rpmalloc.c to CMake and rpmalloc_dump_statistics --- CMakeLists.txt | 32 ++++++++++++++++++++------------ src/main.cpp | 17 +++++++++++++++++ 2 files changed, 37 insertions(+), 12 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 51981f4..b05f3b2 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,4 +1,8 @@ -cmake_minimum_required(VERSION 3.21) +cmake_minimum_required(VERSION 3.30) + +if(POLICY CMP0167) + cmake_policy(SET CMP0167 NEW) +endif() macro(set_from_environment VARIABLE) if (NOT DEFINED ${VARIABLE} AND DEFINED ENV{${VARIABLE}}) @@ -11,7 +15,7 @@ include(cmake/version.cmake) project( EngineFixes VERSION ${VERSION} - LANGUAGES CXX + LANGUAGES C CXX ) add_compile_definitions(SKYRIM) @@ -45,12 +49,7 @@ add_subdirectory(${CommonLibPath} ${CommonLibName} EXCLUDE_FROM_ALL) find_package(spdlog CONFIG REQUIRED) find_package(AutoTOML REQUIRED CONFIG) -find_package(Boost - MODULE - REQUIRED - COMPONENTS - regex -) +find_package(boost_regex CONFIG REQUIRED) find_package(TBB REQUIRED CONFIG @@ -76,6 +75,7 @@ add_library( ${PROJECT_NAME} SHARED ${SOURCES} + external/rpmalloc/rpmalloc/rpmalloc.c ${CMAKE_CURRENT_BINARY_DIR}/include/Version.h ${CMAKE_CURRENT_BINARY_DIR}/version.rc ${PROJECT_NAME}.toml @@ -105,22 +105,30 @@ target_link_libraries( CommonLibSSE::CommonLibSSE TBB::tbb xbyak::xbyak - ${CMAKE_CURRENT_SOURCE_DIR}/external/rpmalloc/lib/windows/release/x86-64/rpmalloc.lib +# ${CMAKE_CURRENT_SOURCE_DIR}/external/rpmalloc/lib/windows/release/x86-64/rpmalloc.lib +) +target_compile_definitions( + ${PROJECT_NAME} + PRIVATE + ENABLE_PRELOAD + ENABLE_VALIDATE_ARGS + ENABLE_ASSERTS + ENABLE_STATISTICS ) if (MSVC) target_link_options( ${PROJECT_NAME} PRIVATE - "$<$:/INCREMENTAL;/OPT:NOREF;/OPT:NOICF;/NODEFAULTLIB:LIBCMT>" - "$<$:/INCREMENTAL:NO;/OPT:REF;/OPT:ICF;/DEBUG:FULL;/NODEFAULTLIB:LIBCMT>" + "$<$:/INCREMENTAL;/OPT:NOREF;/OPT:NOICF>" + "$<$:/INCREMENTAL:NO;/OPT:REF;/OPT:ICF;/DEBUG:FULL>" ) endif() target_precompile_headers( ${PROJECT_NAME} PRIVATE - src/PCH.h + "$<$:${CMAKE_CURRENT_SOURCE_DIR}/src/PCH.h>" ) option(COPY_BUILD "whether we should copy the outputs to the skyrim dir" OFF) diff --git a/src/main.cpp b/src/main.cpp index 2792aa4..3ecbea4 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -4,6 +4,8 @@ #include "utils.h" #include "version.h" #include "warnings.h" +#include +#include inline constexpr REL::Version RUNTIME_1_6_1170(1, 6, 1170, 0); @@ -39,6 +41,21 @@ void MessageHandler(SKSE::MessagingInterface::Message* a_msg) if (*config::warnRefHandleLimit) warnings::WarnActiveRefrHandleCount(static_cast(*config::warnRefrLoadedGameLimit)); + break; + case SKSE::MessagingInterface::kSaveGame: + if (*config::patchMemoryManager) { + FILE* fout; + auto path = logger::log_directory(); + if (!path) + stl::report_and_fail("failed to get standard log path"sv); + + *path /= "EngineFixes_rpmalloc_stats.log"sv; + if ((fout = fopen(path->string().c_str(), "w")) != NULL) + { + rpmalloc_dump_statistics(fout); + } + } + break; default: break; From 74daed027af8ce0e760b41f258e7db991a9411ce Mon Sep 17 00:00:00 2001 From: Andrej E Baranov Date: Wed, 28 May 2025 21:47:20 +0700 Subject: [PATCH 3/6] experiments --- CMakeLists.txt | 6 +- CMakePresets.json | 6 +- src/main.cpp | 5 +- src/patches.h | 1 + src/patches/memorymanager.cpp | 102 ++++++++++++++++++++++++++++++---- 5 files changed, 104 insertions(+), 16 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index b05f3b2..78e92cb 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -83,6 +83,9 @@ add_library( .editorconfig ) +# TODO: flags +set_source_files_properties( external/rpmalloc/rpmalloc/rpmalloc.c PROPERTIES COMPILE_FLAGS "/D \"_UNICODE\" /D \"UNICODE\" /std:c17 /Zi /Oi /Oy- /GS- /Qpar- /fp:fast /fp:except- /Zc:forScope /Zc:wchar_t /GR- /openmp- /W4 /WX /wd4201 /wd4100 /Gm- /Ob2 /Ot /GT /GL /GF /O2 /D\"BUILD_RELEASE=1\"") + target_compile_features( ${PROJECT_NAME} PRIVATE @@ -113,7 +116,8 @@ target_compile_definitions( ENABLE_PRELOAD ENABLE_VALIDATE_ARGS ENABLE_ASSERTS - ENABLE_STATISTICS + ENABLE_THREAD_CACHE=1 +# ENABLE_STATISTICS ) if (MSVC) diff --git a/CMakePresets.json b/CMakePresets.json index f3f81f0..116dec0 100644 --- a/CMakePresets.json +++ b/CMakePresets.json @@ -23,9 +23,9 @@ } }, { - "environment": { - "CXXFLAGS": "/MP /W4 /WX /external:anglebrackets /external:W0 $penv{CXXFLAGS}" - }, + "environment": { + "CXXFLAGS": "/MP /W4 /WX /external:anglebrackets /external:W0 $penv{CXXFLAGS}" + }, "generator": "Visual Studio 17 2022", "hidden": true, "name": "vs2022" diff --git a/src/main.cpp b/src/main.cpp index 3ecbea4..5a53549 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -5,7 +5,6 @@ #include "version.h" #include "warnings.h" #include -#include inline constexpr REL::Version RUNTIME_1_6_1170(1, 6, 1170, 0); @@ -44,6 +43,7 @@ void MessageHandler(SKSE::MessagingInterface::Message* a_msg) break; case SKSE::MessagingInterface::kSaveGame: if (*config::patchMemoryManager) { + /* FILE* fout; auto path = logger::log_directory(); if (!path) @@ -53,7 +53,10 @@ void MessageHandler(SKSE::MessagingInterface::Message* a_msg) if ((fout = fopen(path->string().c_str(), "w")) != NULL) { rpmalloc_dump_statistics(fout); + fclose(fout); } + */ + patches::WriteMemoryManagerStats(); } break; diff --git a/src/patches.h b/src/patches.h index 1145542..c731ca5 100644 --- a/src/patches.h +++ b/src/patches.h @@ -17,6 +17,7 @@ namespace patches bool PatchWaterflowAnimation(); bool PatchMemoryManager(); + void WriteMemoryManagerStats(); bool PatchSafeExit(); bool PatchSaveGameMaxSize(); bool PatchScaleformAllocator(); diff --git a/src/patches/memorymanager.cpp b/src/patches/memorymanager.cpp index ff22634..78aed1a 100644 --- a/src/patches/memorymanager.cpp +++ b/src/patches/memorymanager.cpp @@ -2,6 +2,7 @@ #include "offsets.h" #include +#include namespace { @@ -79,34 +80,100 @@ namespace } } +#if ENABLE_STATISTICS + namespace MemoryManagerStats + { + struct + { + struct + { + std::chrono::nanoseconds total = 0ns; + unsigned long count = 0; + std::mutex mutex; + } Allocate; + struct + { + std::chrono::nanoseconds total = 0ns; + unsigned long count = 0; + std::mutex mutex; + } Deallocate; + struct + { + std::chrono::nanoseconds total = 0ns; + unsigned long count = 0; + std::mutex mutex; + } Reallocate; + } Stats; + } +#endif + namespace MemoryManager { void* Allocate(RE::MemoryManager*, std::size_t a_size, std::uint32_t a_alignment, bool a_alignmentRequired) { + void* ret = g_trash; +#if ENABLE_STATISTICS + auto start = std::chrono::steady_clock::now(); +#endif if (a_size > 0) - return a_alignmentRequired ? - rpaligned_alloc(a_alignment, a_size) : // scalable_aligned_malloc(a_size, a_alignment) - rpmalloc(a_size); // scalable_malloc(a_size) - else - return g_trash; + { + ret = a_alignmentRequired ? + rpaligned_alloc(a_alignment, a_size) : // rpaligned_alloc(a_alignment, a_size) : // scalable_aligned_malloc(a_size, a_alignment) + rpmalloc(a_size); // rpmalloc(a_size); // scalable_malloc(a_size) + } +#if ENABLE_STATISTICS + std::lock_guard guard(MemoryManagerStats::Stats.Allocate.mutex); + auto end = std::chrono::steady_clock::now(); + if (MemoryManagerStats::Stats.Allocate.count < (ULONG_MAX - 10)) { + MemoryManagerStats::Stats.Allocate.count++; + MemoryManagerStats::Stats.Allocate.total += end - start; + } +#endif + return ret; } void Deallocate(RE::MemoryManager*, void* a_mem, bool a_alignmentRequired) { +#if ENABLE_STATISTICS + auto start = std::chrono::steady_clock::now(); +#endif if (a_mem != g_trash) a_alignmentRequired ? - rpfree(a_mem) : // scalable_aligned_free(a_mem) - rpfree(a_mem); // scalable_free(a_mem) + rpfree(a_mem) : //rpfree(a_mem) : // scalable_aligned_free(a_mem) + rpfree(a_mem); // rpfree(a_mem); // scalable_free(a_mem) +#if ENABLE_STATISTICS + std::lock_guard guard(MemoryManagerStats::Stats.Deallocate.mutex); + auto end = std::chrono::steady_clock::now(); + if (MemoryManagerStats::Stats.Deallocate.count < (ULONG_MAX - 10)) + { + MemoryManagerStats::Stats.Deallocate.count++; + MemoryManagerStats::Stats.Deallocate.total += end - start; + } +#endif } void* Reallocate(RE::MemoryManager* a_self, void* a_oldMem, std::size_t a_newSize, std::uint32_t a_alignment, bool a_alignmentRequired) { + void* ret = g_trash; +#if ENABLE_STATISTICS + auto start = std::chrono::steady_clock::now(); +#endif if (a_oldMem == g_trash) - return Allocate(a_self, a_newSize, a_alignment, a_alignmentRequired); + ret = Allocate(a_self, a_newSize, a_alignment, a_alignmentRequired); else - return a_alignmentRequired ? - rpaligned_realloc(a_oldMem, a_alignment, a_newSize, rpmalloc_usable_size(a_oldMem), 0) : //scalable_aligned_realloc(a_oldMem, a_newSize, a_alignment) : - rprealloc(a_oldMem, a_newSize); // scalable_realloc(a_oldMem, a_newSize); + ret = a_alignmentRequired ? + rpaligned_realloc(a_oldMem, a_alignment, a_newSize, rpmalloc_usable_size(a_oldMem), 0) : //rpaligned_realloc(a_oldMem, a_alignment, a_newSize, rpmalloc_usable_size(a_oldMem), 0) : //scalable_aligned_realloc(a_oldMem, a_newSize, a_alignment) : + rprealloc(a_oldMem, a_newSize); //rprealloc(a_oldMem, a_newSize); // scalable_realloc(a_oldMem, a_newSize); +#if ENABLE_STATISTICS + std::lock_guard guard(MemoryManagerStats::Stats.Reallocate.mutex); + auto end = std::chrono::steady_clock::now(); + if (MemoryManagerStats::Stats.Reallocate.count < (ULONG_MAX - 10)) + { + MemoryManagerStats::Stats.Reallocate.count++; + MemoryManagerStats::Stats.Reallocate.total += end - start; + } +#endif + return ret; } void ReplaceAllocRoutines() @@ -240,4 +307,17 @@ namespace patches logger::trace("success"sv); return true; } + + void WriteMemoryManagerStats() + { +#if ENABLE_STATISTICS + std::lock_guard guard(MemoryManagerStats::Stats.Allocate.mutex); + std::lock_guard guard1(MemoryManagerStats::Stats.Deallocate.mutex); + std::lock_guard guard2(MemoryManagerStats::Stats.Reallocate.mutex); + + logger::trace("Allocate: {} ns. Total: {} ns, Count: {}"sv, (MemoryManagerStats::Stats.Allocate.total / MemoryManagerStats::Stats.Allocate.count).count(), MemoryManagerStats::Stats.Allocate.total.count(), MemoryManagerStats::Stats.Allocate.count); + logger::trace("Deallocate: {} ns. Total: {} ns, Count: {}"sv, (MemoryManagerStats::Stats.Deallocate.total / MemoryManagerStats::Stats.Deallocate.count).count(), MemoryManagerStats::Stats.Deallocate.total.count(), MemoryManagerStats::Stats.Deallocate.count); + logger::trace("Reallocate: {} ns. Total: {} ns, Count: {}"sv, (MemoryManagerStats::Stats.Reallocate.total / MemoryManagerStats::Stats.Reallocate.count).count(), MemoryManagerStats::Stats.Reallocate.total.count(), MemoryManagerStats::Stats.Reallocate.count); +#endif + } } From 7786424dbe48d383c5b6e828577867a478b780b4 Mon Sep 17 00:00:00 2001 From: Andrej E Baranov Date: Sat, 31 May 2025 22:01:48 +0700 Subject: [PATCH 4/6] debug formcaching --- src/patches/formcaching.cpp | 24 ++++++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/src/patches/formcaching.cpp b/src/patches/formcaching.cpp index 6fb4731..e2f7237 100644 --- a/src/patches/formcaching.cpp +++ b/src/patches/formcaching.cpp @@ -48,6 +48,26 @@ namespace patches if (globalFormCacheMap[masterId].find(accessor, baseId)) { formPointer = accessor->second; + if (masterId == 0xFF) + { + RE::TESForm* formPointerGlobalTable = nullptr; + GlobalFormTableLock->LockForRead(); + + if (*GlobalFormTable) + { + auto iter = (*GlobalFormTable)->find(FormId); + formPointerGlobalTable = (iter != (*GlobalFormTable)->end()) ? iter->second : nullptr; + } + + GlobalFormTableLock->UnlockForRead(); + + if (formPointerGlobalTable == nullptr) + { + logger::trace("debug hk_GetFormByID from cache for {:08X} is {}, but in globalTable is {}"sv, FormId, (formPointer == nullptr) ? "nullptr" : "Form", (formPointerGlobalTable == nullptr) ? "nullptr" : "Form"); + //UpdateFormCache(FormId, formPointerGlobalTable, true); + return formPointerGlobalTable; + } + } return formPointer; } } @@ -201,8 +221,8 @@ namespace patches origFunc2HookAddr.address(), reinterpret_cast(UnknownFormFunction2)); } - - logger::trace("done"sv); + bool isTbbScalableAllocator = (globalFormCacheMap[0].get_allocator().allocator_type() == globalFormCacheMap[0].get_allocator().scalable) ? true : false; + logger::trace("done. isTbbScalableAllocator: {}"sv, isTbbScalableAllocator); logger::trace("success"sv); From 184d11f4f3d9810533108de28de1b7c393f8bac1 Mon Sep 17 00:00:00 2001 From: Andrej E Baranov Date: Sat, 31 May 2025 22:03:16 +0700 Subject: [PATCH 5/6] continue experiments --- CMakeLists.txt | 2 ++ src/patches/memorymanager.cpp | 45 ++++++++++++++++++++++++++++++++--- 2 files changed, 44 insertions(+), 3 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 78e92cb..d9212c2 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -55,6 +55,7 @@ find_package(TBB CONFIG COMPONENTS tbb + tbbmalloc CONFIG ) find_package(xbyak REQUIRED CONFIG) @@ -107,6 +108,7 @@ target_link_libraries( Boost::regex CommonLibSSE::CommonLibSSE TBB::tbb + TBB::tbbmalloc xbyak::xbyak # ${CMAKE_CURRENT_SOURCE_DIR}/external/rpmalloc/lib/windows/release/x86-64/rpmalloc.lib ) diff --git a/src/patches/memorymanager.cpp b/src/patches/memorymanager.cpp index 78aed1a..03cbcc3 100644 --- a/src/patches/memorymanager.cpp +++ b/src/patches/memorymanager.cpp @@ -115,12 +115,17 @@ namespace #if ENABLE_STATISTICS auto start = std::chrono::steady_clock::now(); #endif + //logger::info("MemoryManager::Allocate START"); if (a_size > 0) { ret = a_alignmentRequired ? rpaligned_alloc(a_alignment, a_size) : // rpaligned_alloc(a_alignment, a_size) : // scalable_aligned_malloc(a_size, a_alignment) rpmalloc(a_size); // rpmalloc(a_size); // scalable_malloc(a_size) } + //logger::info("MemoryManager::Allocate END"); + if (ret == 0) { + logger::error("MemoryManager::Allocate ret: 0; a_size: {}, a_alignment: {}, a_alignmentRequired: {}"sv, a_size, a_alignment, a_alignmentRequired); + } #if ENABLE_STATISTICS std::lock_guard guard(MemoryManagerStats::Stats.Allocate.mutex); auto end = std::chrono::steady_clock::now(); @@ -137,10 +142,12 @@ namespace #if ENABLE_STATISTICS auto start = std::chrono::steady_clock::now(); #endif + //logger::info("MemoryManager::Deallocate START"); if (a_mem != g_trash) a_alignmentRequired ? rpfree(a_mem) : //rpfree(a_mem) : // scalable_aligned_free(a_mem) rpfree(a_mem); // rpfree(a_mem); // scalable_free(a_mem) + //logger::info("MemoryManager::Deallocate END"); #if ENABLE_STATISTICS std::lock_guard guard(MemoryManagerStats::Stats.Deallocate.mutex); auto end = std::chrono::steady_clock::now(); @@ -158,12 +165,19 @@ namespace #if ENABLE_STATISTICS auto start = std::chrono::steady_clock::now(); #endif + //logger::info("MemoryManager::Reallocate START"); if (a_oldMem == g_trash) ret = Allocate(a_self, a_newSize, a_alignment, a_alignmentRequired); else ret = a_alignmentRequired ? rpaligned_realloc(a_oldMem, a_alignment, a_newSize, rpmalloc_usable_size(a_oldMem), 0) : //rpaligned_realloc(a_oldMem, a_alignment, a_newSize, rpmalloc_usable_size(a_oldMem), 0) : //scalable_aligned_realloc(a_oldMem, a_newSize, a_alignment) : rprealloc(a_oldMem, a_newSize); //rprealloc(a_oldMem, a_newSize); // scalable_realloc(a_oldMem, a_newSize); + + //logger::info("MemoryManager::Reallocate END"); + if (ret == 0) + { + logger::error("MemoryManager::Reallocate ret: 0; a_newSize: {}, a_alignment: {}, a_alignmentRequired: {}"sv, a_newSize, a_alignment, a_alignmentRequired); + } #if ENABLE_STATISTICS std::lock_guard guard(MemoryManagerStats::Stats.Reallocate.mutex); auto end = std::chrono::steady_clock::now(); @@ -213,8 +227,11 @@ namespace { std::size_t hk_msize(void* a_ptr) { + logger::info("msize::hk_msize START"); //return scalable_msize(a_ptr); - return rpmalloc_usable_size(a_ptr); + auto a = rpmalloc_usable_size(a_ptr); + logger::info("msize::hk_msize END"); + return a; } void Install() @@ -227,9 +244,17 @@ namespace { void* Allocate(RE::ScrapHeap*, std::size_t a_size, std::size_t a_alignment) { - return a_size > 0 ? + //logger::info("ScrapHeap::Allocate START"); + void* ret = a_size > 0 ? rpaligned_alloc(a_alignment, a_size) : // scalable_aligned_malloc(a_size, a_alignment) : - g_trash; + g_trash; + //logger::info("ScrapHeap::Allocate END"); + + if (ret == 0) + { + logger::error("ScrapHeap::Allocate ret: 0; a_size: {}, a_alignment: {}"sv, a_size, a_alignment); + } + return ret; } RE::ScrapHeap* Ctor(RE::ScrapHeap* a_this) @@ -241,8 +266,10 @@ namespace void Deallocate(RE::ScrapHeap*, void* a_mem) { + //logger::info("ScrapHeap::Deallocate START"); if (a_mem != g_trash) rpfree(a_mem); // scalable_aligned_free(a_mem); + //logger::info("ScrapHeap::Deallocate END"); } @@ -293,11 +320,23 @@ namespace namespace patches { + + //rpmalloc_config_t config = { 0 }; + + void ErrorCallback(const char* message) + { + logger::error("MemoryManager::Error {}"sv, message); + throw message; + } + bool PatchMemoryManager() { logger::trace("- memory manager patch -"sv); g_trash = new std::byte[1u << 10]{ static_cast(0) }; + //config.error_callback = &ErrorCallback; + //rpmalloc_initialize_config(&config); + rpmalloc_config()->error_callback = &ErrorCallback; AutoScrapBuffer::Install(); MemoryManager::Install(); From bce475f4d9781cdd902a7732452d76ea908877ff Mon Sep 17 00:00:00 2001 From: Andrej E Baranov Date: Sun, 1 Jun 2025 05:37:00 +0700 Subject: [PATCH 6/6] revert hard edit hard edit rpmalloc --- src/patches/memorymanager.cpp | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/src/patches/memorymanager.cpp b/src/patches/memorymanager.cpp index 03cbcc3..b083a8e 100644 --- a/src/patches/memorymanager.cpp +++ b/src/patches/memorymanager.cpp @@ -118,6 +118,7 @@ namespace //logger::info("MemoryManager::Allocate START"); if (a_size > 0) { + //rpmalloc_thread_initialize(); ret = a_alignmentRequired ? rpaligned_alloc(a_alignment, a_size) : // rpaligned_alloc(a_alignment, a_size) : // scalable_aligned_malloc(a_size, a_alignment) rpmalloc(a_size); // rpmalloc(a_size); // scalable_malloc(a_size) @@ -144,6 +145,7 @@ namespace #endif //logger::info("MemoryManager::Deallocate START"); if (a_mem != g_trash) + //rpmalloc_thread_initialize(); a_alignmentRequired ? rpfree(a_mem) : //rpfree(a_mem) : // scalable_aligned_free(a_mem) rpfree(a_mem); // rpfree(a_mem); // scalable_free(a_mem) @@ -169,9 +171,12 @@ namespace if (a_oldMem == g_trash) ret = Allocate(a_self, a_newSize, a_alignment, a_alignmentRequired); else + { + //rpmalloc_thread_initialize(); ret = a_alignmentRequired ? rpaligned_realloc(a_oldMem, a_alignment, a_newSize, rpmalloc_usable_size(a_oldMem), 0) : //rpaligned_realloc(a_oldMem, a_alignment, a_newSize, rpmalloc_usable_size(a_oldMem), 0) : //scalable_aligned_realloc(a_oldMem, a_newSize, a_alignment) : rprealloc(a_oldMem, a_newSize); //rprealloc(a_oldMem, a_newSize); // scalable_realloc(a_oldMem, a_newSize); + } //logger::info("MemoryManager::Reallocate END"); if (ret == 0) @@ -228,6 +233,7 @@ namespace std::size_t hk_msize(void* a_ptr) { logger::info("msize::hk_msize START"); + //rpmalloc_thread_initialize(); //return scalable_msize(a_ptr); auto a = rpmalloc_usable_size(a_ptr); logger::info("msize::hk_msize END"); @@ -245,6 +251,7 @@ namespace void* Allocate(RE::ScrapHeap*, std::size_t a_size, std::size_t a_alignment) { //logger::info("ScrapHeap::Allocate START"); + //rpmalloc_thread_initialize(); void* ret = a_size > 0 ? rpaligned_alloc(a_alignment, a_size) : // scalable_aligned_malloc(a_size, a_alignment) : g_trash; @@ -266,6 +273,7 @@ namespace void Deallocate(RE::ScrapHeap*, void* a_mem) { + //rpmalloc_thread_initialize(); //logger::info("ScrapHeap::Deallocate START"); if (a_mem != g_trash) rpfree(a_mem); // scalable_aligned_free(a_mem); @@ -321,8 +329,6 @@ namespace namespace patches { - //rpmalloc_config_t config = { 0 }; - void ErrorCallback(const char* message) { logger::error("MemoryManager::Error {}"sv, message); @@ -334,9 +340,11 @@ namespace patches logger::trace("- memory manager patch -"sv); g_trash = new std::byte[1u << 10]{ static_cast(0) }; + + //rpmalloc_config_t config{}; //config.error_callback = &ErrorCallback; + //rpmalloc_initialize_config(&config); - rpmalloc_config()->error_callback = &ErrorCallback; AutoScrapBuffer::Install(); MemoryManager::Install();