diff --git a/Utilities/VirtualMemory.cpp b/Utilities/VirtualMemory.cpp new file mode 100644 index 0000000000..54e6c6adb4 --- /dev/null +++ b/Utilities/VirtualMemory.cpp @@ -0,0 +1,41 @@ +#include "stdafx.h" +#include "VirtualMemory.h" +#ifdef _WIN32 +#include +#else +#include +#include +#include +#include +#include +#endif + +namespace memory_helper +{ + void* reserve_memory(size_t size) + { +#ifdef _WIN32 + return VirtualAlloc(NULL, size, MEM_RESERVE, PAGE_NOACCESS); +#else + return mmap(nullptr, size, PROT_NONE, MAP_ANON | MAP_PRIVATE, -1, 0); +#endif + } + + void commit_page_memory(void* pointer, size_t page_size) + { +#ifdef _WIN32 + VirtualAlloc((u8*)pointer, page_size, MEM_COMMIT, PAGE_READWRITE); +#else + mprotect((u8*)pointer, page_size, PROT_READ | PROT_WRITE); +#endif + } + + void free_reserved_memory(void* pointer, size_t size) + { +#ifdef _WIN32 + VirtualFree(pointer, 0, MEM_RELEASE); +#else + munmap(pointer, size); +#endif + } +} \ No newline at end of file diff --git a/Utilities/VirtualMemory.h b/Utilities/VirtualMemory.h new file mode 100644 index 0000000000..ecce48db2e --- /dev/null +++ b/Utilities/VirtualMemory.h @@ -0,0 +1,22 @@ +#pragma once + +namespace memory_helper +{ + /** + * Reserve size bytes of virtual memory and returns it. + * The memory should be commited before usage. + */ + void* reserve_memory(size_t size); + + /** + * Commit page_size bytes of virtual memory starting at pointer. + * That is, bake reserved memory with physical memory. + * pointer should belong to a range of reserved memory. + */ + void commit_page_memory(void* pointer, size_t page_size); + + /** + * Free memory alloced via reserve_memory. + */ + void free_reserved_memory(void* pointer, size_t size); +} \ No newline at end of file diff --git a/rpcs3/Emu/Cell/PPUThread.cpp b/rpcs3/Emu/Cell/PPUThread.cpp index cf2d6cf8c6..e9a5fa6660 100644 --- a/rpcs3/Emu/Cell/PPUThread.cpp +++ b/rpcs3/Emu/Cell/PPUThread.cpp @@ -12,6 +12,7 @@ #include "Emu/Cell/PPUInterpreter2.h" #include "Emu/Cell/PPULLVMRecompiler.h" //#include "Emu/Cell/PPURecompiler.h" +#include "Utilities/VirtualMemory.h" #ifdef _WIN32 #include @@ -29,30 +30,18 @@ extern void ppu_free_tls(u32 thread); thread_local const ppu_decoder_cache_t* g_tls_ppu_decoder_cache = nullptr; // temporarily, because thread_local is not fully available ppu_decoder_cache_t::ppu_decoder_cache_t() -#ifdef _WIN32 - : pointer(static_cast(VirtualAlloc(NULL, 0x200000000, MEM_RESERVE, PAGE_NOACCESS))) -#else - : pointer(static_cast(mmap(nullptr, 0x200000000, PROT_NONE, MAP_ANON | MAP_PRIVATE, -1, 0))) -#endif + : pointer(static_cast(memory_helper::reserve_memory(0x200000000))) { } ppu_decoder_cache_t::~ppu_decoder_cache_t() { -#ifdef _WIN32 - VirtualFree(pointer, 0, MEM_RELEASE); -#else - munmap(pointer, 0x200000000); -#endif + memory_helper::free_reserved_memory(pointer, 0x200000000); } void ppu_decoder_cache_t::initialize(u32 addr, u32 size) { -#ifdef _WIN32 - VirtualAlloc(pointer + addr / 4, size * 2, MEM_COMMIT, PAGE_READWRITE); -#else - mprotect(pointer + addr / 4, size * 2, PROT_READ | PROT_WRITE); -#endif + memory_helper::commit_page_memory(pointer + addr / 4, size * 2); PPUInterpreter2* inter; PPUDecoder dec(inter = new PPUInterpreter2); diff --git a/rpcs3/emucore.vcxproj b/rpcs3/emucore.vcxproj index adbc5db34d..806833b1d6 100644 --- a/rpcs3/emucore.vcxproj +++ b/rpcs3/emucore.vcxproj @@ -50,6 +50,7 @@ + @@ -387,6 +388,7 @@ + diff --git a/rpcs3/emucore.vcxproj.filters b/rpcs3/emucore.vcxproj.filters index 27f6a4808c..fe0c57f077 100644 --- a/rpcs3/emucore.vcxproj.filters +++ b/rpcs3/emucore.vcxproj.filters @@ -983,6 +983,9 @@ Utilities + + Utilities + @@ -1867,5 +1870,8 @@ Emu\CPU\Cell + + Utilities + \ No newline at end of file