mirror of
https://github.com/hedge-dev/UnleashedRecomp.git
synced 2026-04-27 04:41:39 +00:00
Add mmap implementations for virtual allocation. (#38)
This commit is contained in:
parent
cc4953584f
commit
fe15f154fe
3 changed files with 37 additions and 1 deletions
|
|
@ -4,13 +4,22 @@
|
||||||
|
|
||||||
CodeCache::CodeCache()
|
CodeCache::CodeCache()
|
||||||
{
|
{
|
||||||
|
#ifdef _WIN32
|
||||||
bucket = (char*)VirtualAlloc(nullptr, 0x200000000, MEM_RESERVE, PAGE_READWRITE);
|
bucket = (char*)VirtualAlloc(nullptr, 0x200000000, MEM_RESERVE, PAGE_READWRITE);
|
||||||
assert(bucket);
|
assert(bucket != nullptr);
|
||||||
|
#else
|
||||||
|
bucket = (char*)mmap(NULL, 0x200000000, PROT_READ | PROT_WRITE, MAP_ANON | MAP_PRIVATE, -1, 0);
|
||||||
|
assert(bucket != (char *)MAP_FAILED)
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
CodeCache::~CodeCache()
|
CodeCache::~CodeCache()
|
||||||
{
|
{
|
||||||
|
#ifdef _WIN32
|
||||||
VirtualFree(bucket, 0, MEM_RELEASE);
|
VirtualFree(bucket, 0, MEM_RELEASE);
|
||||||
|
#else
|
||||||
|
munmap(bucket, 0x200000000);
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
void CodeCache::Init()
|
void CodeCache::Init()
|
||||||
|
|
@ -19,7 +28,9 @@ void CodeCache::Init()
|
||||||
{
|
{
|
||||||
if (PPCFuncMappings[i].host != nullptr)
|
if (PPCFuncMappings[i].host != nullptr)
|
||||||
{
|
{
|
||||||
|
#ifdef _WIN32
|
||||||
VirtualAlloc(bucket + PPCFuncMappings[i].guest * 2, sizeof(void*), MEM_COMMIT, PAGE_READWRITE);
|
VirtualAlloc(bucket + PPCFuncMappings[i].guest * 2, sizeof(void*), MEM_COMMIT, PAGE_READWRITE);
|
||||||
|
#endif
|
||||||
*(void**)(bucket + PPCFuncMappings[i].guest * 2) = (void*)PPCFuncMappings[i].host;
|
*(void**)(bucket + PPCFuncMappings[i].guest * 2) = (void*)PPCFuncMappings[i].host;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -27,7 +38,9 @@ void CodeCache::Init()
|
||||||
|
|
||||||
void CodeCache::Insert(uint32_t guest, const void* host)
|
void CodeCache::Insert(uint32_t guest, const void* host)
|
||||||
{
|
{
|
||||||
|
#ifdef _WIN32
|
||||||
VirtualAlloc(bucket + static_cast<uint64_t>(guest) * 2, sizeof(void*), MEM_COMMIT, PAGE_READWRITE);
|
VirtualAlloc(bucket + static_cast<uint64_t>(guest) * 2, sizeof(void*), MEM_COMMIT, PAGE_READWRITE);
|
||||||
|
#endif
|
||||||
*reinterpret_cast<const void**>(bucket + static_cast<uint64_t>(guest) * 2) = host;
|
*reinterpret_cast<const void**>(bucket + static_cast<uint64_t>(guest) * 2) = host;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -3,25 +3,44 @@
|
||||||
|
|
||||||
Memory::Memory(void* address, size_t size) : size(size)
|
Memory::Memory(void* address, size_t size) : size(size)
|
||||||
{
|
{
|
||||||
|
#ifdef _WIN32
|
||||||
base = (char*)VirtualAlloc(address, size, MEM_RESERVE, PAGE_READWRITE);
|
base = (char*)VirtualAlloc(address, size, MEM_RESERVE, PAGE_READWRITE);
|
||||||
|
|
||||||
if (base == nullptr)
|
if (base == nullptr)
|
||||||
base = (char*)VirtualAlloc(nullptr, size, MEM_RESERVE, PAGE_READWRITE);
|
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);
|
||||||
|
|
||||||
|
if (base == (char*)MAP_FAILED)
|
||||||
|
base = (char*)mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_ANON | MAP_PRIVATE, -1, 0);
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
void* Memory::Alloc(size_t offset, size_t size, uint32_t type)
|
void* Memory::Alloc(size_t offset, size_t size, uint32_t type)
|
||||||
{
|
{
|
||||||
|
#ifdef _WIN32
|
||||||
return VirtualAlloc(base + offset, size, type, PAGE_READWRITE);
|
return VirtualAlloc(base + offset, size, type, PAGE_READWRITE);
|
||||||
|
#else
|
||||||
|
return base + offset;
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
void* Memory::Commit(size_t offset, size_t size)
|
void* Memory::Commit(size_t offset, size_t size)
|
||||||
{
|
{
|
||||||
|
#ifdef _WIN32
|
||||||
return Alloc(offset, size, MEM_COMMIT);
|
return Alloc(offset, size, MEM_COMMIT);
|
||||||
|
#else
|
||||||
|
return base + offset;
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
void* Memory::Reserve(size_t offset, size_t size)
|
void* Memory::Reserve(size_t offset, size_t size)
|
||||||
{
|
{
|
||||||
|
#ifdef _WIN32
|
||||||
return Alloc(offset, size, MEM_RESERVE);
|
return Alloc(offset, size, MEM_RESERVE);
|
||||||
|
#else
|
||||||
|
return base + offset;
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
void* MmGetHostAddress(uint32_t ptr)
|
void* MmGetHostAddress(uint32_t ptr)
|
||||||
|
|
|
||||||
|
|
@ -49,3 +49,7 @@ using Microsoft::WRL::ComPtr;
|
||||||
|
|
||||||
#include "framework.h"
|
#include "framework.h"
|
||||||
#include "mutex.h"
|
#include "mutex.h"
|
||||||
|
|
||||||
|
#ifndef _WIN32
|
||||||
|
#include <sys/mman.h>
|
||||||
|
#endif
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue