From 2a45e5f7929fe2a1fad9aec178b6a9c64870275b Mon Sep 17 00:00:00 2001 From: unknown Date: Sat, 18 Jul 2015 12:45:55 -0400 Subject: [PATCH 1/4] Declare a couple constants to help clean the code rewrites. --- Source/Project64/N64 System/Mips/Memory Virtual Mem.cpp | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/Source/Project64/N64 System/Mips/Memory Virtual Mem.cpp b/Source/Project64/N64 System/Mips/Memory Virtual Mem.cpp index b5feb9b2f..f7cacbbfc 100644 --- a/Source/Project64/N64 System/Mips/Memory Virtual Mem.cpp +++ b/Source/Project64/N64 System/Mips/Memory Virtual Mem.cpp @@ -5372,11 +5372,14 @@ void CMipsMemoryVM::TLB_Unmaped( DWORD Vaddr, DWORD Len ) void CMipsMemoryVM::RdramChanged ( CMipsMemoryVM * _this ) { - if (_this->m_AllocatedRdramSize == g_Settings->LoadDword(Game_RDRamSize)) + const size_t new_size = g_Settings -> LoadDword(Game_RDRamSize); + const size_t old_size = _this -> m_AllocatedRdramSize; + + if (old_size == new_size) { return; } - if (_this->m_AllocatedRdramSize == 0x400000) + if (old_size == 0x400000) { if (VirtualAlloc(_this->m_RDRAM + 0x400000, 0x400000, MEM_COMMIT, PAGE_READWRITE)==NULL) { From 62d316f7ae89661f4d0ba807c8d701eb01825f7d Mon Sep 17 00:00:00 2001 From: unknown Date: Sat, 18 Jul 2015 12:50:00 -0400 Subject: [PATCH 2/4] centralized m_AllocatedRdramSize writeback outside the if/else --- Source/Project64/N64 System/Mips/Memory Virtual Mem.cpp | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/Source/Project64/N64 System/Mips/Memory Virtual Mem.cpp b/Source/Project64/N64 System/Mips/Memory Virtual Mem.cpp index f7cacbbfc..06a85da6d 100644 --- a/Source/Project64/N64 System/Mips/Memory Virtual Mem.cpp +++ b/Source/Project64/N64 System/Mips/Memory Virtual Mem.cpp @@ -5386,14 +5386,12 @@ void CMipsMemoryVM::RdramChanged ( CMipsMemoryVM * _this ) WriteTrace(TraceError,__FUNCTION__ ": failed to allocate extended memory"); g_Notify->FatalError(GS(MSG_MEM_ALLOC_ERROR)); } - _this->m_AllocatedRdramSize = 0x800000; } else { VirtualFree(_this->m_RDRAM + 0x400000, 0x400000,MEM_DECOMMIT); - _this->m_AllocatedRdramSize = 0x400000; } - + _this->m_AllocatedRdramSize = new_size; } void CMipsMemoryVM::ChangeSpStatus() From 188091e32a2536dc4dad05d392908f976ba70487 Mon Sep 17 00:00:00 2001 From: unknown Date: Sat, 18 Jul 2015 12:51:44 -0400 Subject: [PATCH 3/4] flipped the if/else blocks for better branch prediction --- .../Project64/N64 System/Mips/Memory Virtual Mem.cpp | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/Source/Project64/N64 System/Mips/Memory Virtual Mem.cpp b/Source/Project64/N64 System/Mips/Memory Virtual Mem.cpp index 06a85da6d..afabe3497 100644 --- a/Source/Project64/N64 System/Mips/Memory Virtual Mem.cpp +++ b/Source/Project64/N64 System/Mips/Memory Virtual Mem.cpp @@ -5379,18 +5379,18 @@ void CMipsMemoryVM::RdramChanged ( CMipsMemoryVM * _this ) { return; } - if (old_size == 0x400000) + if (old_size == 0x800000) + { + VirtualFree(_this->m_RDRAM + 0x400000, 0x400000, MEM_DECOMMIT); + } + else { if (VirtualAlloc(_this->m_RDRAM + 0x400000, 0x400000, MEM_COMMIT, PAGE_READWRITE)==NULL) { WriteTrace(TraceError,__FUNCTION__ ": failed to allocate extended memory"); - g_Notify->FatalError(GS(MSG_MEM_ALLOC_ERROR)); + g_Notify -> FatalError(GS(MSG_MEM_ALLOC_ERROR)); } } - else - { - VirtualFree(_this->m_RDRAM + 0x400000, 0x400000,MEM_DECOMMIT); - } _this->m_AllocatedRdramSize = new_size; } From 51975fb00dd1e8012c03e8e1ede191977f71a053 Mon Sep 17 00:00:00 2001 From: unknown Date: Sat, 18 Jul 2015 13:12:40 -0400 Subject: [PATCH 4/4] unbiased the algorithm from just checking if only 4 or 8 MB --- .../N64 System/Mips/Memory Virtual Mem.cpp | 22 ++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/Source/Project64/N64 System/Mips/Memory Virtual Mem.cpp b/Source/Project64/N64 System/Mips/Memory Virtual Mem.cpp index afabe3497..58156b8a4 100644 --- a/Source/Project64/N64 System/Mips/Memory Virtual Mem.cpp +++ b/Source/Project64/N64 System/Mips/Memory Virtual Mem.cpp @@ -5379,15 +5379,27 @@ void CMipsMemoryVM::RdramChanged ( CMipsMemoryVM * _this ) { return; } - if (old_size == 0x800000) + if (old_size > new_size) { - VirtualFree(_this->m_RDRAM + 0x400000, 0x400000, MEM_DECOMMIT); + VirtualFree( + _this->m_RDRAM + new_size, + old_size - new_size, + MEM_DECOMMIT + ); } else - { - if (VirtualAlloc(_this->m_RDRAM + 0x400000, 0x400000, MEM_COMMIT, PAGE_READWRITE)==NULL) + { + void * result; + + result = VirtualAlloc( + _this->m_RDRAM + old_size, + new_size - old_size, + MEM_COMMIT, + PAGE_READWRITE + ); + if (result == NULL) { - WriteTrace(TraceError,__FUNCTION__ ": failed to allocate extended memory"); + WriteTrace(TraceError, __FUNCTION__": failed to allocate extended memory"); g_Notify -> FatalError(GS(MSG_MEM_ALLOC_ERROR)); } }