project64/Source/Project64-core/N64System/Recompiler/RecompilerMemory.cpp

88 lines
2.6 KiB
C++
Raw Normal View History

2016-01-27 09:11:59 +00:00
#include "stdafx.h"
#include <Project64-core/N64System/Recompiler/RecompilerMemory.h>
#include <Project64-core/N64System/SystemGlobals.h>
2021-04-14 05:34:15 +00:00
#include <Project64-core/N64System/Recompiler/Recompiler.h>
2016-01-27 09:11:59 +00:00
#include <Common/MemoryManagement.h>
CRecompMemory::CRecompMemory() :
2021-04-12 11:35:39 +00:00
m_RecompCode(nullptr),
2016-01-27 09:11:59 +00:00
m_RecompSize(0)
{
2021-04-12 11:35:39 +00:00
m_RecompPos = nullptr;
2016-01-27 09:11:59 +00:00
}
CRecompMemory::~CRecompMemory()
{
if (m_RecompCode)
{
FreeAddressSpace(m_RecompCode,MaxCompileBufferSize + 4);
2021-04-12 11:35:39 +00:00
m_RecompCode = nullptr;
2016-01-27 09:11:59 +00:00
}
2021-04-12 11:35:39 +00:00
m_RecompPos = nullptr;
2016-01-27 09:11:59 +00:00
}
bool CRecompMemory::AllocateMemory()
{
2016-06-28 11:22:30 +00:00
WriteTrace(TraceRecompiler, TraceDebug, "Start");
2016-01-27 09:11:59 +00:00
uint8_t * RecompCodeBase = (uint8_t *)AllocateAddressSpace(MaxCompileBufferSize + 4);
2016-06-28 11:22:30 +00:00
WriteTrace(TraceRecompiler, TraceDebug, "RecompCodeBase = %X", RecompCodeBase);
2021-04-12 11:35:39 +00:00
if (RecompCodeBase == nullptr)
2016-01-27 09:11:59 +00:00
{
WriteTrace(TraceRecompiler, TraceError, "Failed to allocate RecompCodeBase");
2016-01-27 09:11:59 +00:00
g_Notify->DisplayError(MSG_MEM_ALLOC_ERROR);
return false;
}
m_RecompCode = (uint8_t *)CommitMemory(RecompCodeBase, InitialCompileBufferSize, MEM_EXECUTE_READWRITE);
2021-04-12 11:35:39 +00:00
if (m_RecompCode == nullptr)
2016-01-27 09:11:59 +00:00
{
WriteTrace(TraceRecompiler, TraceError, "Failed to commit initial buffer");
2016-01-27 09:11:59 +00:00
FreeAddressSpace(RecompCodeBase,MaxCompileBufferSize + 4);
g_Notify->DisplayError(MSG_MEM_ALLOC_ERROR);
return false;
}
m_RecompSize = InitialCompileBufferSize;
m_RecompPos = m_RecompCode;
memset(m_RecompCode, 0, InitialCompileBufferSize);
2016-06-28 11:22:30 +00:00
WriteTrace(TraceRecompiler, TraceDebug, "Done");
2016-01-27 09:11:59 +00:00
return true;
}
void CRecompMemory::CheckRecompMem()
{
uint32_t Size = (uint32_t)((uint8_t *)m_RecompPos - (uint8_t *)m_RecompCode);
if ((Size + 0x20000) < m_RecompSize)
{
return;
}
if (m_RecompSize == MaxCompileBufferSize)
{
g_Recompiler->ResetRecompCode(true);
return;
}
void * MemAddr = CommitMemory(m_RecompCode + m_RecompSize, IncreaseCompileBufferSize, MEM_EXECUTE_READWRITE);
2021-04-12 11:35:39 +00:00
if (MemAddr == nullptr)
2016-01-27 09:11:59 +00:00
{
WriteTrace(TraceRecompiler, TraceError, "Failed to increase buffer");
2016-01-27 09:11:59 +00:00
g_Notify->FatalError(MSG_MEM_ALLOC_ERROR);
}
m_RecompSize += IncreaseCompileBufferSize;
}
void CRecompMemory::Reset()
{
m_RecompPos = m_RecompCode;
}
void CRecompMemory::ShowMemUsed()
{
uint32_t Size = m_RecompPos - m_RecompCode;
uint32_t MB = Size / 0x100000;
Size -= MB * 0x100000;
uint32_t KB = Size / 1024;
Size -= KB * 1024;
uint32_t TotalAvaliable = m_RecompSize / 0x100000;
g_Notify->DisplayMessage(0, stdstr_f("Memory used: %d mb %-3d kb %-3d bytes Total Available: %d mb", MB, KB, Size, TotalAvaliable).c_str());
}