Combine guest memory and function table into one virtual allocation.

This commit is contained in:
Skyth
2025-01-02 14:45:42 +03:00
parent 038edfdebd
commit 967a0ce17f
27 changed files with 46 additions and 199 deletions

View File

@@ -1,48 +1,30 @@
#include <stdafx.h>
#include "memory.h"
Memory::Memory(void* address, size_t size) : size(size)
Memory::Memory()
{
#ifdef _WIN32
base = (char*)VirtualAlloc(address, size, MEM_RESERVE, PAGE_READWRITE);
base = (uint8_t*)VirtualAlloc((void*)0x100000000ull, PPC_MEMORY_SIZE + PPC_FUNC_TABLE_SIZE, MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE);
if (base == nullptr)
base = (char*)VirtualAlloc(nullptr, size, MEM_RESERVE, PAGE_READWRITE);
#else
base = (char*)mmap(address, size, PROT_READ | PROT_WRITE, MAP_ANON | MAP_PRIVATE, -1, 0);
base = (uint8_t*)VirtualAlloc(nullptr, PPC_MEMORY_SIZE + PPC_FUNC_TABLE_SIZE, MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE);
if (base == (char*)MAP_FAILED)
base = (char*)mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_ANON | MAP_PRIVATE, -1, 0);
DWORD oldProtect;
VirtualProtect(base, 4096, PAGE_NOACCESS, &oldProtect);
#else
base = (uint8_t*)mmap((void*)0x100000000ull, PPC_MEMORY_SIZE + PPC_FUNC_TABLE_SIZE, PROT_READ | PROT_WRITE, MAP_ANON | MAP_PRIVATE, -1, 0);
if (base == (uint8_t*)MAP_FAILED)
base = (uint8_t*)mmap(NULL, PPC_MEMORY_SIZE + PPC_FUNC_TABLE_SIZE, PROT_READ | PROT_WRITE, MAP_ANON | MAP_PRIVATE, -1, 0);
mprotect(base, 4096, PROT_NONE);
#endif
}
void* Memory::Alloc(size_t offset, size_t size, uint32_t type)
{
#ifdef _WIN32
return VirtualAlloc(base + offset, size, type, PAGE_READWRITE);
#else
return base + offset;
#endif
}
void* Memory::Commit(size_t offset, size_t size)
{
#ifdef _WIN32
return Alloc(offset, size, MEM_COMMIT);
#else
return base + offset;
#endif
}
void* Memory::Reserve(size_t offset, size_t size)
{
#ifdef _WIN32
return Alloc(offset, size, MEM_RESERVE);
#else
return base + offset;
#endif
for (size_t i = 0; PPCFuncMappings[i].guest != 0; i++)
{
if (PPCFuncMappings[i].host != nullptr)
InsertFunction(PPCFuncMappings[i].guest, PPCFuncMappings[i].host);
}
}
void* MmGetHostAddress(uint32_t ptr)