Skip to content

Commit

Permalink
Memory: Factorize virtual memory reservation/allocation code in function
Browse files Browse the repository at this point in the history
  • Loading branch information
vlj committed Aug 26, 2015
1 parent 241dede commit 942f265
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 15 deletions.
41 changes: 41 additions & 0 deletions Utilities/VirtualMemory.cpp
Original file line number Diff line number Diff line change
@@ -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 changes: 22 additions & 0 deletions Utilities/VirtualMemory.h
Original file line number Diff line number Diff line change
@@ -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);
}
19 changes: 4 additions & 15 deletions rpcs3/Emu/Cell/PPUThread.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 <Windows.h>
Expand All @@ -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<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
: pointer(static_cast<decltype(pointer)>(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);
Expand Down
2 changes: 2 additions & 0 deletions rpcs3/emucore.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
<ClCompile Include="..\Utilities\SleepQueue.cpp" />
<ClCompile Include="..\Utilities\StrFmt.cpp" />
<ClCompile Include="..\Utilities\Thread.cpp" />
<ClCompile Include="..\Utilities\VirtualMemory.cpp" />
<ClCompile Include="Emu\Cell\PPUInterpreter.cpp" />
<ClCompile Include="Emu\Cell\PPULLVMRecompilerCore.cpp" />
<ClCompile Include="Emu\Cell\SPUInterpreter.cpp" />
Expand Down Expand Up @@ -387,6 +388,7 @@
<ClInclude Include="..\Utilities\StrFmt.h" />
<ClInclude Include="..\Utilities\Thread.h" />
<ClInclude Include="..\Utilities\Timer.h" />
<ClInclude Include="..\Utilities\VirtualMemory.h" />
<ClInclude Include="Crypto\aes.h" />
<ClInclude Include="Crypto\ec.h" />
<ClInclude Include="Crypto\key_vault.h" />
Expand Down
6 changes: 6 additions & 0 deletions rpcs3/emucore.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -983,6 +983,9 @@
<ClCompile Include="..\Utilities\SleepQueue.cpp">
<Filter>Utilities</Filter>
</ClCompile>
<ClCompile Include="..\Utilities\VirtualMemory.cpp">
<Filter>Utilities</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="Crypto\aes.h">
Expand Down Expand Up @@ -1867,5 +1870,8 @@
<ClInclude Include="Emu\Cell\Common.h">
<Filter>Emu\CPU\Cell</Filter>
</ClInclude>
<ClInclude Include="..\Utilities\VirtualMemory.h">
<Filter>Utilities</Filter>
</ClInclude>
</ItemGroup>
</Project>

0 comments on commit 942f265

Please sign in to comment.