Merge pull request #3386 from lioncash/memory

Common: Namespace MemoryUtil
This commit is contained in:
JosJuice 2016-08-19 11:04:45 +02:00 committed by GitHub
commit 31c530c7b3
15 changed files with 49 additions and 47 deletions

View File

@ -42,7 +42,7 @@ public:
void AllocCodeSpace(int size, bool need_low = true)
{
region_size = size;
region = (u8*)AllocateExecutableMemory(region_size, need_low);
region = static_cast<u8*>(Common::AllocateExecutableMemory(region_size, need_low));
T::SetCodePtr(region);
}
@ -57,7 +57,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;
@ -71,7 +71,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
{

View File

@ -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 <unistd.h>
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

View File

@ -7,6 +7,9 @@
#include <cstddef>
#include <string>
namespace Common
{
void* AllocateExecutableMemory(size_t size, bool low = true);
void* AllocateMemoryPages(size_t size);
void FreeMemoryPages(void* ptr, size_t size);
@ -18,10 +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

View File

@ -163,7 +163,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)

View File

@ -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<u16*>(Common::AllocateMemoryPages(DSP_IROM_BYTE_SIZE));
g_dsp.iram = static_cast<u16*>(Common::AllocateMemoryPages(DSP_IRAM_BYTE_SIZE));
g_dsp.dram = static_cast<u16*>(Common::AllocateMemoryPages(DSP_DRAM_BYTE_SIZE));
g_dsp.coef = static_cast<u16*>(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)

View File

@ -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);

View File

@ -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<u8*>(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;
}

View File

@ -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);

View File

@ -94,7 +94,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<u8*>(Common::AllocateMemoryPages(ROM_SIZE));
if (SConfig::GetInstance().bHLE_BS2)
{
@ -123,13 +123,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

View File

@ -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<u8*>(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

View File

@ -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<u8*>(
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;
}

View File

@ -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<u8*>(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;

View File

@ -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 =

View File

@ -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<u8*>(Common::AllocateAlignedMemory(temp_size, 16));
}
TextureCacheBase::TextureCacheBase()
{
temp_size = 2048 * 2048 * 4;
if (!temp)
temp = (u8*)AllocateAlignedMemory(temp_size, 16);
temp = static_cast<u8*>(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;
}

View File

@ -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;