diff --git a/UnleashedRecomp/cpu/code_cache.cpp b/UnleashedRecomp/cpu/code_cache.cpp index 2175f0b8..1c8775e0 100644 --- a/UnleashedRecomp/cpu/code_cache.cpp +++ b/UnleashedRecomp/cpu/code_cache.cpp @@ -4,13 +4,22 @@ CodeCache::CodeCache() { +#ifdef _WIN32 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() { +#ifdef _WIN32 VirtualFree(bucket, 0, MEM_RELEASE); +#else + munmap(bucket, 0x200000000); +#endif } void CodeCache::Init() @@ -19,7 +28,9 @@ void CodeCache::Init() { if (PPCFuncMappings[i].host != nullptr) { +#ifdef _WIN32 VirtualAlloc(bucket + PPCFuncMappings[i].guest * 2, sizeof(void*), MEM_COMMIT, PAGE_READWRITE); +#endif *(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) { +#ifdef _WIN32 VirtualAlloc(bucket + static_cast(guest) * 2, sizeof(void*), MEM_COMMIT, PAGE_READWRITE); +#endif *reinterpret_cast(bucket + static_cast(guest) * 2) = host; } diff --git a/UnleashedRecomp/kernel/memory.cpp b/UnleashedRecomp/kernel/memory.cpp index 1e7b12e9..85b11710 100644 --- a/UnleashedRecomp/kernel/memory.cpp +++ b/UnleashedRecomp/kernel/memory.cpp @@ -3,25 +3,44 @@ Memory::Memory(void* address, size_t size) : size(size) { +#ifdef _WIN32 base = (char*)VirtualAlloc(address, size, 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); + + 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) { +#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 } void* MmGetHostAddress(uint32_t ptr) diff --git a/UnleashedRecomp/stdafx.h b/UnleashedRecomp/stdafx.h index b8800fb7..3a79732b 100644 --- a/UnleashedRecomp/stdafx.h +++ b/UnleashedRecomp/stdafx.h @@ -49,3 +49,7 @@ using Microsoft::WRL::ComPtr; #include "framework.h" #include "mutex.h" + +#ifndef _WIN32 +#include +#endif