mirror of
https://github.com/RPCS3/rpcs3.git
synced 2025-07-04 05:51:27 +12:00
Memory: Factorize virtual memory reservation/allocation code in function
This commit is contained in:
parent
241dedef4e
commit
942f26509b
5 changed files with 75 additions and 15 deletions
41
Utilities/VirtualMemory.cpp
Normal file
41
Utilities/VirtualMemory.cpp
Normal file
|
@ -0,0 +1,41 @@
|
||||||
|
#include "stdafx.h"
|
||||||
|
#include "VirtualMemory.h"
|
||||||
|
#ifdef _WIN32
|
||||||
|
#include <Windows.h>
|
||||||
|
#else
|
||||||
|
#include <sys/mman.h>
|
||||||
|
#include <sys/stat.h>
|
||||||
|
#include <fcntl.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
#include <sys/types.h>
|
||||||
|
#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
|
||||||
|
}
|
||||||
|
}
|
22
Utilities/VirtualMemory.h
Normal file
22
Utilities/VirtualMemory.h
Normal file
|
@ -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);
|
||||||
|
}
|
|
@ -12,6 +12,7 @@
|
||||||
#include "Emu/Cell/PPUInterpreter2.h"
|
#include "Emu/Cell/PPUInterpreter2.h"
|
||||||
#include "Emu/Cell/PPULLVMRecompiler.h"
|
#include "Emu/Cell/PPULLVMRecompiler.h"
|
||||||
//#include "Emu/Cell/PPURecompiler.h"
|
//#include "Emu/Cell/PPURecompiler.h"
|
||||||
|
#include "Utilities/VirtualMemory.h"
|
||||||
|
|
||||||
#ifdef _WIN32
|
#ifdef _WIN32
|
||||||
#include <Windows.h>
|
#include <Windows.h>
|
||||||
|
@ -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
|
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()
|
ppu_decoder_cache_t::ppu_decoder_cache_t()
|
||||||
#ifdef _WIN32
|
: pointer(static_cast<decltype(pointer)>(memory_helper::reserve_memory(0x200000000)))
|
||||||
: pointer(static_cast<decltype(pointer)>(VirtualAlloc(NULL, 0x200000000, MEM_RESERVE, PAGE_NOACCESS)))
|
|
||||||
#else
|
|
||||||
: pointer(static_cast<decltype(pointer)>(mmap(nullptr, 0x200000000, PROT_NONE, MAP_ANON | MAP_PRIVATE, -1, 0)))
|
|
||||||
#endif
|
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
ppu_decoder_cache_t::~ppu_decoder_cache_t()
|
ppu_decoder_cache_t::~ppu_decoder_cache_t()
|
||||||
{
|
{
|
||||||
#ifdef _WIN32
|
memory_helper::free_reserved_memory(pointer, 0x200000000);
|
||||||
VirtualFree(pointer, 0, MEM_RELEASE);
|
|
||||||
#else
|
|
||||||
munmap(pointer, 0x200000000);
|
|
||||||
#endif
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void ppu_decoder_cache_t::initialize(u32 addr, u32 size)
|
void ppu_decoder_cache_t::initialize(u32 addr, u32 size)
|
||||||
{
|
{
|
||||||
#ifdef _WIN32
|
memory_helper::commit_page_memory(pointer + addr / 4, size * 2);
|
||||||
VirtualAlloc(pointer + addr / 4, size * 2, MEM_COMMIT, PAGE_READWRITE);
|
|
||||||
#else
|
|
||||||
mprotect(pointer + addr / 4, size * 2, PROT_READ | PROT_WRITE);
|
|
||||||
#endif
|
|
||||||
|
|
||||||
PPUInterpreter2* inter;
|
PPUInterpreter2* inter;
|
||||||
PPUDecoder dec(inter = new PPUInterpreter2);
|
PPUDecoder dec(inter = new PPUInterpreter2);
|
||||||
|
|
|
@ -50,6 +50,7 @@
|
||||||
<ClCompile Include="..\Utilities\SleepQueue.cpp" />
|
<ClCompile Include="..\Utilities\SleepQueue.cpp" />
|
||||||
<ClCompile Include="..\Utilities\StrFmt.cpp" />
|
<ClCompile Include="..\Utilities\StrFmt.cpp" />
|
||||||
<ClCompile Include="..\Utilities\Thread.cpp" />
|
<ClCompile Include="..\Utilities\Thread.cpp" />
|
||||||
|
<ClCompile Include="..\Utilities\VirtualMemory.cpp" />
|
||||||
<ClCompile Include="Emu\Cell\PPUInterpreter.cpp" />
|
<ClCompile Include="Emu\Cell\PPUInterpreter.cpp" />
|
||||||
<ClCompile Include="Emu\Cell\PPULLVMRecompilerCore.cpp" />
|
<ClCompile Include="Emu\Cell\PPULLVMRecompilerCore.cpp" />
|
||||||
<ClCompile Include="Emu\Cell\SPUInterpreter.cpp" />
|
<ClCompile Include="Emu\Cell\SPUInterpreter.cpp" />
|
||||||
|
@ -387,6 +388,7 @@
|
||||||
<ClInclude Include="..\Utilities\StrFmt.h" />
|
<ClInclude Include="..\Utilities\StrFmt.h" />
|
||||||
<ClInclude Include="..\Utilities\Thread.h" />
|
<ClInclude Include="..\Utilities\Thread.h" />
|
||||||
<ClInclude Include="..\Utilities\Timer.h" />
|
<ClInclude Include="..\Utilities\Timer.h" />
|
||||||
|
<ClInclude Include="..\Utilities\VirtualMemory.h" />
|
||||||
<ClInclude Include="Crypto\aes.h" />
|
<ClInclude Include="Crypto\aes.h" />
|
||||||
<ClInclude Include="Crypto\ec.h" />
|
<ClInclude Include="Crypto\ec.h" />
|
||||||
<ClInclude Include="Crypto\key_vault.h" />
|
<ClInclude Include="Crypto\key_vault.h" />
|
||||||
|
|
|
@ -983,6 +983,9 @@
|
||||||
<ClCompile Include="..\Utilities\SleepQueue.cpp">
|
<ClCompile Include="..\Utilities\SleepQueue.cpp">
|
||||||
<Filter>Utilities</Filter>
|
<Filter>Utilities</Filter>
|
||||||
</ClCompile>
|
</ClCompile>
|
||||||
|
<ClCompile Include="..\Utilities\VirtualMemory.cpp">
|
||||||
|
<Filter>Utilities</Filter>
|
||||||
|
</ClCompile>
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<ClInclude Include="Crypto\aes.h">
|
<ClInclude Include="Crypto\aes.h">
|
||||||
|
@ -1867,5 +1870,8 @@
|
||||||
<ClInclude Include="Emu\Cell\Common.h">
|
<ClInclude Include="Emu\Cell\Common.h">
|
||||||
<Filter>Emu\CPU\Cell</Filter>
|
<Filter>Emu\CPU\Cell</Filter>
|
||||||
</ClInclude>
|
</ClInclude>
|
||||||
|
<ClInclude Include="..\Utilities\VirtualMemory.h">
|
||||||
|
<Filter>Utilities</Filter>
|
||||||
|
</ClInclude>
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
</Project>
|
</Project>
|
Loading…
Add table
Add a link
Reference in a new issue