From e01c143379cac68dccdcb00db195b218e500b1c8 Mon Sep 17 00:00:00 2001 From: Lioncash Date: Sun, 7 Aug 2016 13:03:07 -0400 Subject: [PATCH 1/2] Common: namespace MemoryUtil --- Source/Core/Common/CodeBlock.h | 6 +++--- Source/Core/Common/MemoryUtil.cpp | 5 +++++ Source/Core/Common/MemoryUtil.h | 5 +++++ Source/Core/Core/Core.cpp | 2 +- Source/Core/Core/DSP/DSPCore.cpp | 18 +++++++++--------- Source/Core/Core/DSP/DSPHWInterface.cpp | 4 ++-- Source/Core/Core/HW/DSP.cpp | 4 ++-- Source/Core/Core/HW/DSPLLE/DSPLLE.cpp | 4 ++-- Source/Core/Core/HW/EXI_DeviceIPL.cpp | 6 +++--- Source/Core/Core/PowerPC/Jit64/Jit.cpp | 10 +++++----- Source/Core/VideoBackends/OGL/StreamBuffer.cpp | 6 +++--- Source/Core/VideoCommon/Fifo.cpp | 4 ++-- Source/Core/VideoCommon/HiresTextures.cpp | 2 +- Source/Core/VideoCommon/TextureCacheBase.cpp | 8 ++++---- Source/UnitTests/Core/PageFaultTest.cpp | 6 +++--- 15 files changed, 50 insertions(+), 40 deletions(-) diff --git a/Source/Core/Common/CodeBlock.h b/Source/Core/Common/CodeBlock.h index 1666f270a4..40b8db3acf 100644 --- a/Source/Core/Common/CodeBlock.h +++ b/Source/Core/Common/CodeBlock.h @@ -48,7 +48,7 @@ public: void AllocCodeSpace(int size, bool need_low = true) { region_size = size; - region = (u8*)AllocateExecutableMemory(region_size, need_low); + region = static_cast(Common::AllocateExecutableMemory(region_size, need_low)); T::SetCodePtr(region); } @@ -63,7 +63,7 @@ public: // Call this when shutting down. Don't rely on the destructor, even though it'll do the job. void FreeCodeSpace() { - FreeMemoryPages(region, region_size); + Common::FreeMemoryPages(region, region_size); region = nullptr; region_size = 0; parent_region_size = 0; @@ -77,7 +77,7 @@ public: bool IsInSpace(u8* ptr) const { return (ptr >= region) && (ptr < (region + region_size)); } // Cannot currently be undone. Will write protect the entire code region. // Start over if you need to change the code (call FreeCodeSpace(), AllocCodeSpace()). - void WriteProtect() { WriteProtectMemory(region, region_size, true); } + void WriteProtect() { Common::WriteProtectMemory(region, region_size, true); } void ResetCodePtr() { T::SetCodePtr(region); } size_t GetSpaceLeft() const { diff --git a/Source/Core/Common/MemoryUtil.cpp b/Source/Core/Common/MemoryUtil.cpp index 1941f8e35b..ce0d38cbcb 100644 --- a/Source/Core/Common/MemoryUtil.cpp +++ b/Source/Core/Common/MemoryUtil.cpp @@ -31,6 +31,9 @@ // Uncomment the following line to be able to run Dolphin in Valgrind. //#undef MAP_32BIT +namespace Common +{ + #if !defined(_WIN32) #include static uintptr_t RoundPage(uintptr_t addr) @@ -273,3 +276,5 @@ size_t MemPhysical() return (size_t)memInfo.totalram * memInfo.mem_unit; #endif } + +} // namespace Common diff --git a/Source/Core/Common/MemoryUtil.h b/Source/Core/Common/MemoryUtil.h index 2224324fa2..56ef604def 100644 --- a/Source/Core/Common/MemoryUtil.h +++ b/Source/Core/Common/MemoryUtil.h @@ -7,6 +7,9 @@ #include #include +namespace Common +{ + void* AllocateExecutableMemory(size_t size, bool low = true); void* AllocateMemoryPages(size_t size); void FreeMemoryPages(void* ptr, size_t size); @@ -25,3 +28,5 @@ inline int GetPageSize() { return 4096; } + +} // namespace Common diff --git a/Source/Core/Core/Core.cpp b/Source/Core/Core/Core.cpp index 9d495f8d02..c18802622e 100644 --- a/Source/Core/Core/Core.cpp +++ b/Source/Core/Core/Core.cpp @@ -162,7 +162,7 @@ void FrameUpdateOnCPUThread() std::string StopMessage(bool main_thread, const std::string& message) { return StringFromFormat("Stop [%s %i]\t%s\t%s", main_thread ? "Main Thread" : "Video Thread", - Common::CurrentThreadId(), MemUsage().c_str(), message.c_str()); + Common::CurrentThreadId(), Common::MemUsage().c_str(), message.c_str()); } void DisplayMessage(const std::string& message, int time_in_ms) diff --git a/Source/Core/Core/DSP/DSPCore.cpp b/Source/Core/Core/DSP/DSPCore.cpp index 8c69e4e82d..7c6a3beb52 100644 --- a/Source/Core/Core/DSP/DSPCore.cpp +++ b/Source/Core/Core/DSP/DSPCore.cpp @@ -90,10 +90,10 @@ static bool VerifyRoms() static void DSPCore_FreeMemoryPages() { - FreeMemoryPages(g_dsp.irom, DSP_IROM_BYTE_SIZE); - FreeMemoryPages(g_dsp.iram, DSP_IRAM_BYTE_SIZE); - FreeMemoryPages(g_dsp.dram, DSP_DRAM_BYTE_SIZE); - FreeMemoryPages(g_dsp.coef, DSP_COEF_BYTE_SIZE); + Common::FreeMemoryPages(g_dsp.irom, DSP_IROM_BYTE_SIZE); + Common::FreeMemoryPages(g_dsp.iram, DSP_IRAM_BYTE_SIZE); + Common::FreeMemoryPages(g_dsp.dram, DSP_DRAM_BYTE_SIZE); + Common::FreeMemoryPages(g_dsp.coef, DSP_COEF_BYTE_SIZE); g_dsp.irom = g_dsp.iram = g_dsp.dram = g_dsp.coef = nullptr; } @@ -103,10 +103,10 @@ bool DSPCore_Init(const DSPInitOptions& opts) g_cycles_left = 0; g_init_hax = false; - g_dsp.irom = (u16*)AllocateMemoryPages(DSP_IROM_BYTE_SIZE); - g_dsp.iram = (u16*)AllocateMemoryPages(DSP_IRAM_BYTE_SIZE); - g_dsp.dram = (u16*)AllocateMemoryPages(DSP_DRAM_BYTE_SIZE); - g_dsp.coef = (u16*)AllocateMemoryPages(DSP_COEF_BYTE_SIZE); + g_dsp.irom = static_cast(Common::AllocateMemoryPages(DSP_IROM_BYTE_SIZE)); + g_dsp.iram = static_cast(Common::AllocateMemoryPages(DSP_IRAM_BYTE_SIZE)); + g_dsp.dram = static_cast(Common::AllocateMemoryPages(DSP_DRAM_BYTE_SIZE)); + g_dsp.coef = static_cast(Common::AllocateMemoryPages(DSP_COEF_BYTE_SIZE)); memcpy(g_dsp.irom, opts.irom_contents.data(), DSP_IROM_BYTE_SIZE); memcpy(g_dsp.coef, opts.coef_contents.data(), DSP_COEF_BYTE_SIZE); @@ -142,7 +142,7 @@ bool DSPCore_Init(const DSPInitOptions& opts) gdsp_ifx_init(); // Mostly keep IRAM write protected. We unprotect only when DMA-ing // in new ucodes. - WriteProtectMemory(g_dsp.iram, DSP_IRAM_BYTE_SIZE, false); + Common::WriteProtectMemory(g_dsp.iram, DSP_IRAM_BYTE_SIZE, false); // Initialize JIT, if necessary if (opts.core_type == DSPInitOptions::CORE_JIT) diff --git a/Source/Core/Core/DSP/DSPHWInterface.cpp b/Source/Core/Core/DSP/DSPHWInterface.cpp index b1bf47f3d7..06005f6191 100644 --- a/Source/Core/Core/DSP/DSPHWInterface.cpp +++ b/Source/Core/Core/DSP/DSPHWInterface.cpp @@ -225,7 +225,7 @@ u16 gdsp_ifx_read(u16 addr) static const u8* gdsp_idma_in(u16 dsp_addr, u32 addr, u32 size) { - UnWriteProtectMemory(g_dsp.iram, DSP_IRAM_BYTE_SIZE, false); + Common::UnWriteProtectMemory(g_dsp.iram, DSP_IRAM_BYTE_SIZE, false); u8* dst = ((u8*)g_dsp.iram); for (u32 i = 0; i < size; i += 2) @@ -233,7 +233,7 @@ static const u8* gdsp_idma_in(u16 dsp_addr, u32 addr, u32 size) *(u16*)&dst[dsp_addr + i] = Common::swap16(*(const u16*)&g_dsp.cpu_ram[(addr + i) & 0x0fffffff]); } - WriteProtectMemory(g_dsp.iram, DSP_IRAM_BYTE_SIZE, false); + Common::WriteProtectMemory(g_dsp.iram, DSP_IRAM_BYTE_SIZE, false); DSPHost::CodeLoaded((const u8*)g_dsp.iram + dsp_addr, size); diff --git a/Source/Core/Core/HW/DSP.cpp b/Source/Core/Core/HW/DSP.cpp index fe37eb3cd2..9fdd22bcf4 100644 --- a/Source/Core/Core/HW/DSP.cpp +++ b/Source/Core/Core/HW/DSP.cpp @@ -244,7 +244,7 @@ void Init(bool hle) g_ARAM.wii_mode = false; g_ARAM.size = ARAM_SIZE; g_ARAM.mask = ARAM_MASK; - g_ARAM.ptr = (u8*)AllocateMemoryPages(g_ARAM.size); + g_ARAM.ptr = static_cast(Common::AllocateMemoryPages(g_ARAM.size)); } memset(&g_audioDMA, 0, sizeof(g_audioDMA)); @@ -270,7 +270,7 @@ void Shutdown() { if (!g_ARAM.wii_mode) { - FreeMemoryPages(g_ARAM.ptr, g_ARAM.size); + Common::FreeMemoryPages(g_ARAM.ptr, g_ARAM.size); g_ARAM.ptr = nullptr; } diff --git a/Source/Core/Core/HW/DSPLLE/DSPLLE.cpp b/Source/Core/Core/HW/DSPLLE/DSPLLE.cpp index fad25fabb3..dcd77db7d5 100644 --- a/Source/Core/Core/HW/DSPLLE/DSPLLE.cpp +++ b/Source/Core/Core/HW/DSPLLE/DSPLLE.cpp @@ -68,9 +68,9 @@ void DSPLLE::DoState(PointerWrap& p) p.DoArray(g_dsp.ifx_regs); p.Do(g_dsp.mbox[0]); p.Do(g_dsp.mbox[1]); - UnWriteProtectMemory(g_dsp.iram, DSP_IRAM_BYTE_SIZE, false); + Common::UnWriteProtectMemory(g_dsp.iram, DSP_IRAM_BYTE_SIZE, false); p.DoArray(g_dsp.iram, DSP_IRAM_SIZE); - WriteProtectMemory(g_dsp.iram, DSP_IRAM_BYTE_SIZE, false); + Common::WriteProtectMemory(g_dsp.iram, DSP_IRAM_BYTE_SIZE, false); if (p.GetMode() == PointerWrap::MODE_READ) DSPHost::CodeLoaded((const u8*)g_dsp.iram, DSP_IRAM_BYTE_SIZE); p.DoArray(g_dsp.dram, DSP_DRAM_SIZE); diff --git a/Source/Core/Core/HW/EXI_DeviceIPL.cpp b/Source/Core/Core/HW/EXI_DeviceIPL.cpp index 4cae293f0b..29d3624e13 100644 --- a/Source/Core/Core/HW/EXI_DeviceIPL.cpp +++ b/Source/Core/Core/HW/EXI_DeviceIPL.cpp @@ -93,7 +93,7 @@ CEXIIPL::CEXIIPL() : m_uPosition(0), m_uAddress(0), m_uRWOffset(0), m_FontsLoade m_bNTSC = SConfig::GetInstance().bNTSC; // Create the IPL - m_pIPL = (u8*)AllocateMemoryPages(ROM_SIZE); + m_pIPL = static_cast(Common::AllocateMemoryPages(ROM_SIZE)); if (SConfig::GetInstance().bHLE_BS2) { @@ -122,13 +122,13 @@ CEXIIPL::CEXIIPL() : m_uPosition(0), m_uAddress(0), m_uRWOffset(0), m_FontsLoade g_SRAM.lang = SConfig::GetInstance().SelectedLanguage; FixSRAMChecksums(); - WriteProtectMemory(m_pIPL, ROM_SIZE); + Common::WriteProtectMemory(m_pIPL, ROM_SIZE); m_uAddress = 0; } CEXIIPL::~CEXIIPL() { - FreeMemoryPages(m_pIPL, ROM_SIZE); + Common::FreeMemoryPages(m_pIPL, ROM_SIZE); m_pIPL = nullptr; // SRAM diff --git a/Source/Core/Core/PowerPC/Jit64/Jit.cpp b/Source/Core/Core/PowerPC/Jit64/Jit.cpp index a1ef3c409c..96159d9154 100644 --- a/Source/Core/Core/PowerPC/Jit64/Jit.cpp +++ b/Source/Core/Core/PowerPC/Jit64/Jit.cpp @@ -151,9 +151,9 @@ enum void Jit64::AllocStack() { #ifndef _WIN32 - m_stack = (u8*)AllocateMemoryPages(STACK_SIZE); - ReadProtectMemory(m_stack, GUARD_SIZE); - ReadProtectMemory(m_stack + GUARD_OFFSET, GUARD_SIZE); + m_stack = static_cast(Common::AllocateMemoryPages(STACK_SIZE)); + Common::ReadProtectMemory(m_stack, GUARD_SIZE); + Common::ReadProtectMemory(m_stack + GUARD_OFFSET, GUARD_SIZE); #else // For windows we just keep using the system stack and reserve a large amount of memory at the end // of the stack. @@ -167,7 +167,7 @@ void Jit64::FreeStack() #ifndef _WIN32 if (m_stack) { - FreeMemoryPages(m_stack, STACK_SIZE); + Common::FreeMemoryPages(m_stack, STACK_SIZE); m_stack = nullptr; } #endif @@ -186,7 +186,7 @@ bool Jit64::HandleStackFault() m_enable_blr_optimization = false; #ifndef _WIN32 // Windows does this automatically. - UnWriteProtectMemory(m_stack + GUARD_OFFSET, GUARD_SIZE); + Common::UnWriteProtectMemory(m_stack + GUARD_OFFSET, GUARD_SIZE); #endif // We're going to need to clear the whole cache to get rid of the bad // CALLs, but we can't yet. Fake the downcount so we're forced to the diff --git a/Source/Core/VideoBackends/OGL/StreamBuffer.cpp b/Source/Core/VideoBackends/OGL/StreamBuffer.cpp index 1ed598b043..f0ede2d7d0 100644 --- a/Source/Core/VideoBackends/OGL/StreamBuffer.cpp +++ b/Source/Core/VideoBackends/OGL/StreamBuffer.cpp @@ -261,8 +261,8 @@ public: PinnedMemory(u32 type, u32 size) : StreamBuffer(type, size) { CreateFences(); - m_pointer = - (u8*)AllocateAlignedMemory(ROUND_UP(m_size, ALIGN_PINNED_MEMORY), ALIGN_PINNED_MEMORY); + m_pointer = static_cast( + Common::AllocateAlignedMemory(ROUND_UP(m_size, ALIGN_PINNED_MEMORY), ALIGN_PINNED_MEMORY)); glBindBuffer(GL_EXTERNAL_VIRTUAL_MEMORY_BUFFER_AMD, m_buffer); glBufferData(GL_EXTERNAL_VIRTUAL_MEMORY_BUFFER_AMD, ROUND_UP(m_size, ALIGN_PINNED_MEMORY), m_pointer, GL_STREAM_COPY); @@ -275,7 +275,7 @@ public: DeleteFences(); glBindBuffer(m_buffertype, 0); glFinish(); // ogl pipeline must be flushed, else this buffer can be in use - FreeAlignedMemory(m_pointer); + Common::FreeAlignedMemory(m_pointer); m_pointer = nullptr; } diff --git a/Source/Core/VideoCommon/Fifo.cpp b/Source/Core/VideoCommon/Fifo.cpp index 504a274157..fbf22faea7 100644 --- a/Source/Core/VideoCommon/Fifo.cpp +++ b/Source/Core/VideoCommon/Fifo.cpp @@ -106,7 +106,7 @@ void PauseAndLock(bool doLock, bool unpauseOnUnlock) void Init() { // Padded so that SIMD overreads in the vertex loader are safe - s_video_buffer = (u8*)AllocateMemoryPages(FIFO_SIZE + 4); + s_video_buffer = static_cast(Common::AllocateMemoryPages(FIFO_SIZE + 4)); ResetVideoBuffer(); if (SConfig::GetInstance().bCPUThread) s_gpu_mainloop.Prepare(); @@ -118,7 +118,7 @@ void Shutdown() if (s_gpu_mainloop.IsRunning()) PanicAlert("Fifo shutting down while active"); - FreeMemoryPages(s_video_buffer, FIFO_SIZE + 4); + Common::FreeMemoryPages(s_video_buffer, FIFO_SIZE + 4); s_video_buffer = nullptr; s_video_buffer_write_ptr = nullptr; s_video_buffer_pp_read_ptr = nullptr; diff --git a/Source/Core/VideoCommon/HiresTextures.cpp b/Source/Core/VideoCommon/HiresTextures.cpp index 400596121b..56798d006e 100644 --- a/Source/Core/VideoCommon/HiresTextures.cpp +++ b/Source/Core/VideoCommon/HiresTextures.cpp @@ -141,7 +141,7 @@ void HiresTexture::Prefetch() Common::SetCurrentThreadName("Prefetcher"); size_t size_sum = 0; - size_t sys_mem = MemPhysical(); + size_t sys_mem = Common::MemPhysical(); size_t recommended_min_mem = 2 * size_t(1024 * 1024 * 1024); // keep 2GB memory for system stability if system RAM is 4GB+ - use half of memory in other cases size_t max_mem = diff --git a/Source/Core/VideoCommon/TextureCacheBase.cpp b/Source/Core/VideoCommon/TextureCacheBase.cpp index 94095ecf95..1cc0e3635e 100644 --- a/Source/Core/VideoCommon/TextureCacheBase.cpp +++ b/Source/Core/VideoCommon/TextureCacheBase.cpp @@ -63,15 +63,15 @@ void TextureCacheBase::CheckTempSize(size_t required_size) return; temp_size = required_size; - FreeAlignedMemory(temp); - temp = (u8*)AllocateAlignedMemory(temp_size, 16); + Common::FreeAlignedMemory(temp); + temp = static_cast(Common::AllocateAlignedMemory(temp_size, 16)); } TextureCacheBase::TextureCacheBase() { temp_size = 2048 * 2048 * 4; if (!temp) - temp = (u8*)AllocateAlignedMemory(temp_size, 16); + temp = static_cast(Common::AllocateAlignedMemory(temp_size, 16)); TexDecoder_SetTexFmtOverlayOptions(g_ActiveConfig.bTexFmtOverlayEnable, g_ActiveConfig.bTexFmtOverlayCenter); @@ -103,7 +103,7 @@ TextureCacheBase::~TextureCacheBase() { HiresTexture::Shutdown(); Invalidate(); - FreeAlignedMemory(temp); + Common::FreeAlignedMemory(temp); temp = nullptr; } diff --git a/Source/UnitTests/Core/PageFaultTest.cpp b/Source/UnitTests/Core/PageFaultTest.cpp index 63990f7a6f..7a1e25885a 100644 --- a/Source/UnitTests/Core/PageFaultTest.cpp +++ b/Source/UnitTests/Core/PageFaultTest.cpp @@ -38,7 +38,7 @@ public: virtual bool HandleFault(uintptr_t access_address, SContext* ctx) override { m_pre_unprotect_time = std::chrono::high_resolution_clock::now(); - UnWriteProtectMemory(m_data, PAGE_GRAN, /*allowExecute*/ false); + Common::UnWriteProtectMemory(m_data, PAGE_GRAN, /*allowExecute*/ false); m_post_unprotect_time = std::chrono::high_resolution_clock::now(); return true; } @@ -51,9 +51,9 @@ public: TEST(PageFault, PageFault) { EMM::InstallExceptionHandler(); - void* data = AllocateMemoryPages(PAGE_GRAN); + void* data = Common::AllocateMemoryPages(PAGE_GRAN); EXPECT_NE(data, nullptr); - WriteProtectMemory(data, PAGE_GRAN, false); + Common::WriteProtectMemory(data, PAGE_GRAN, false); PageFaultFakeJit pfjit; jit = &pfjit; From fbc0aaf79672cd20c580a28ba765e4a07c2c8c67 Mon Sep 17 00:00:00 2001 From: Lioncash Date: Sun, 7 Aug 2016 13:04:02 -0400 Subject: [PATCH 2/2] MemoryUtil: Remove unimplemented/unused functions GuardMemoryMake/GuardMemoryUnmake are unimplemented prototypes. GetPageSize is an unused function. --- Source/Core/Common/MemoryUtil.h | 8 -------- 1 file changed, 8 deletions(-) diff --git a/Source/Core/Common/MemoryUtil.h b/Source/Core/Common/MemoryUtil.h index 56ef604def..fa41da297c 100644 --- a/Source/Core/Common/MemoryUtil.h +++ b/Source/Core/Common/MemoryUtil.h @@ -21,12 +21,4 @@ void UnWriteProtectMemory(void* ptr, size_t size, bool allowExecute = false); std::string MemUsage(); size_t MemPhysical(); -void GuardMemoryMake(void* ptr, size_t size); -void GuardMemoryUnmake(void* ptr, size_t size); - -inline int GetPageSize() -{ - return 4096; -} - } // namespace Common