2012-12-19 09:30:18 +00:00
|
|
|
/****************************************************************************
|
|
|
|
* *
|
2015-11-10 05:21:49 +00:00
|
|
|
* Project64 - A Nintendo 64 emulator. *
|
2012-12-19 09:30:18 +00:00
|
|
|
* http://www.pj64-emu.com/ *
|
|
|
|
* Copyright (C) 2012 Project64. All rights reserved. *
|
|
|
|
* *
|
|
|
|
* License: *
|
|
|
|
* GNU/GPLv2 http://www.gnu.org/licenses/gpl-2.0.html *
|
|
|
|
* *
|
|
|
|
****************************************************************************/
|
2010-05-25 09:15:19 +00:00
|
|
|
#include "stdafx.h"
|
2015-12-06 09:59:58 +00:00
|
|
|
#include "RecompilerMemory.h"
|
|
|
|
#include <Project64-core/N64System/SystemGlobals.h>
|
|
|
|
#include <Project64-core/N64System/Recompiler/RecompilerClass.h>
|
|
|
|
#include <Windows.h>
|
2010-05-25 09:15:19 +00:00
|
|
|
|
|
|
|
CRecompMemory::CRecompMemory() :
|
2015-11-09 20:22:51 +00:00
|
|
|
m_RecompCode(NULL),
|
|
|
|
m_RecompSize(0)
|
2010-05-25 09:15:19 +00:00
|
|
|
{
|
2015-12-13 08:26:41 +00:00
|
|
|
m_RecompPos = NULL;
|
2010-05-25 09:15:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
CRecompMemory::~CRecompMemory()
|
|
|
|
{
|
2015-12-13 08:26:41 +00:00
|
|
|
if (m_RecompCode)
|
|
|
|
{
|
|
|
|
VirtualFree(m_RecompCode, 0, MEM_RELEASE);
|
|
|
|
m_RecompCode = NULL;
|
|
|
|
}
|
|
|
|
m_RecompPos = NULL;
|
2010-05-25 09:15:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool CRecompMemory::AllocateMemory()
|
|
|
|
{
|
2015-12-13 08:26:41 +00:00
|
|
|
uint8_t * RecompCodeBase = (uint8_t *)VirtualAlloc(NULL, MaxCompileBufferSize + 4, MEM_RESERVE | MEM_TOP_DOWN, PAGE_EXECUTE_READWRITE);
|
|
|
|
if (RecompCodeBase == NULL)
|
|
|
|
{
|
2015-12-14 10:51:33 +00:00
|
|
|
WriteTrace(TraceRecompiler, TraceError, "failed to allocate RecompCodeBase");
|
2015-12-13 08:26:41 +00:00
|
|
|
g_Notify->DisplayError(MSG_MEM_ALLOC_ERROR);
|
|
|
|
return false;
|
|
|
|
}
|
2010-06-04 06:25:07 +00:00
|
|
|
|
2015-12-13 08:26:41 +00:00
|
|
|
m_RecompCode = (uint8_t *)VirtualAlloc(RecompCodeBase, InitialCompileBufferSize, MEM_COMMIT, PAGE_EXECUTE_READWRITE);
|
|
|
|
if (m_RecompCode == NULL)
|
|
|
|
{
|
2015-12-14 10:51:33 +00:00
|
|
|
WriteTrace(TraceRecompiler, TraceError, "failed to commit initial buffer");
|
2015-12-13 08:26:41 +00:00
|
|
|
VirtualFree(RecompCodeBase, 0, MEM_RELEASE);
|
|
|
|
g_Notify->DisplayError(MSG_MEM_ALLOC_ERROR);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
m_RecompSize = InitialCompileBufferSize;
|
|
|
|
m_RecompPos = m_RecompCode;
|
|
|
|
memset(m_RecompCode, 0, InitialCompileBufferSize);
|
|
|
|
return true;
|
2010-05-30 01:54:42 +00:00
|
|
|
}
|
|
|
|
|
2015-04-28 22:19:02 +00:00
|
|
|
void CRecompMemory::CheckRecompMem()
|
2010-05-30 01:54:42 +00:00
|
|
|
{
|
2015-12-13 08:26:41 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
LPVOID MemAddr = VirtualAlloc(m_RecompCode + m_RecompSize, IncreaseCompileBufferSize, MEM_COMMIT, PAGE_EXECUTE_READWRITE);
|
|
|
|
if (MemAddr == NULL)
|
|
|
|
{
|
2015-12-14 10:51:33 +00:00
|
|
|
WriteTrace(TraceRecompiler, TraceError, "failed to increase buffer");
|
2015-12-13 08:26:41 +00:00
|
|
|
g_Notify->FatalError(MSG_MEM_ALLOC_ERROR);
|
|
|
|
}
|
|
|
|
m_RecompSize += IncreaseCompileBufferSize;
|
2010-05-30 01:54:42 +00:00
|
|
|
}
|
2010-06-12 02:02:06 +00:00
|
|
|
|
|
|
|
void CRecompMemory::Reset()
|
|
|
|
{
|
2015-12-13 08:26:41 +00:00
|
|
|
m_RecompPos = m_RecompCode;
|
2010-06-16 07:31:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void CRecompMemory::ShowMemUsed()
|
|
|
|
{
|
2015-12-13 08:26:41 +00:00
|
|
|
uint32_t Size = m_RecompPos - m_RecompCode;
|
|
|
|
uint32_t MB = Size / 0x100000;
|
|
|
|
Size -= MB * 0x100000;
|
|
|
|
uint32_t KB = Size / 1024;
|
|
|
|
Size -= KB * 1024;
|
2010-06-16 07:31:47 +00:00
|
|
|
|
2015-12-13 08:26:41 +00:00
|
|
|
uint32_t TotalAvaliable = m_RecompSize / 0x100000;
|
2015-11-09 20:22:51 +00:00
|
|
|
|
2015-12-13 08:26:41 +00:00
|
|
|
g_Notify->DisplayMessage(0, stdstr_f("Memory used: %d mb %-3d kb %-3d bytes Total Available: %d mb", MB, KB, Size, TotalAvaliable).ToUTF16().c_str());
|
|
|
|
}
|