hopefully fix macos

This commit is contained in:
RSDuck 2024-11-18 21:31:56 +01:00
parent f0503a6a28
commit 9ad3d42252
2 changed files with 13 additions and 4 deletions

View File

@ -248,6 +248,7 @@ Compiler::Compiler(melonDS::NDS& nds) : Arm64Gen::ARM64XEmitter(), NDS(nds)
ARMJIT_Global::Init(); ARMJIT_Global::Init();
CodeMemBase = ARMJIT_Global::AllocateCodeMem(); CodeMemBase = ARMJIT_Global::AllocateCodeMem();
nds.JIT.JitEnableWrite();
SetCodeBase(reinterpret_cast<u8*>(CodeMemBase), reinterpret_cast<u8*>(CodeMemBase)); SetCodeBase(reinterpret_cast<u8*>(CodeMemBase), reinterpret_cast<u8*>(CodeMemBase));
JitMemMainSize = ARMJIT_Global::CodeMemorySliceSize; JitMemMainSize = ARMJIT_Global::CodeMemorySliceSize;

View File

@ -21,7 +21,11 @@ namespace ARMJIT_Global
std::mutex globalMutex; std::mutex globalMutex;
#ifndef __APPLE__ #if defined(__APPLE__) && defined(__aarch64__)
#define APPLE_AARCH64
#endif
#ifndef APPLE_AARCH64
static constexpr size_t NumCodeMemSlices = 4; static constexpr size_t NumCodeMemSlices = 4;
static constexpr size_t CodeMemoryAlignedSize = NumCodeMemSlices * CodeMemorySliceSize; static constexpr size_t CodeMemoryAlignedSize = NumCodeMemSlices * CodeMemorySliceSize;
@ -42,7 +46,7 @@ void* AllocateCodeMem()
{ {
std::lock_guard guard(globalMutex); std::lock_guard guard(globalMutex);
#ifndef __APPLE__ #ifndef APPLE_AARCH64
if (AvailableCodeMemSlices) if (AvailableCodeMemSlices)
{ {
int slice = __builtin_ctz(AvailableCodeMemSlices); int slice = __builtin_ctz(AvailableCodeMemSlices);
@ -55,6 +59,8 @@ void* AllocateCodeMem()
// allocate // allocate
#ifdef _WIN32 #ifdef _WIN32
return VirtualAlloc(nullptr, CodeMemorySliceSize, MEM_RESERVE|MEM_COMMIT, PAGE_EXECUTE_READWRITE); return VirtualAlloc(nullptr, CodeMemorySliceSize, MEM_RESERVE|MEM_COMMIT, PAGE_EXECUTE_READWRITE);
#elif defined(APPLE_AARCH64)
return mmap(NULL, CodeMemorySliceSize, PROT_READ | PROT_WRITE | PROT_EXEC, MAP_PRIVATE | MAP_ANONYMOUS | MAP_JIT,-1, 0);
#else #else
//printf("mmaping...\n"); //printf("mmaping...\n");
return mmap(nullptr, CodeMemorySliceSize, PROT_READ | PROT_WRITE | PROT_EXEC, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); return mmap(nullptr, CodeMemorySliceSize, PROT_READ | PROT_WRITE | PROT_EXEC, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
@ -65,6 +71,7 @@ void FreeCodeMem(void* codeMem)
{ {
std::lock_guard guard(globalMutex); std::lock_guard guard(globalMutex);
#ifndef APPLE_AARCH64
for (int i = 0; i < NumCodeMemSlices; i++) for (int i = 0; i < NumCodeMemSlices; i++)
{ {
if (codeMem == &GetAlignedCodeMemoryStart()[CodeMemorySliceSize * i]) if (codeMem == &GetAlignedCodeMemoryStart()[CodeMemorySliceSize * i])
@ -74,6 +81,7 @@ void FreeCodeMem(void* codeMem)
return; return;
} }
} }
#endif
#ifdef _WIN32 #ifdef _WIN32
VirtualFree(codeMem, CodeMemorySliceSize, MEM_RELEASE|MEM_DECOMMIT); VirtualFree(codeMem, CodeMemorySliceSize, MEM_RELEASE|MEM_DECOMMIT);
@ -92,8 +100,8 @@ void Init()
#ifdef _WIN32 #ifdef _WIN32
DWORD dummy; DWORD dummy;
VirtualProtect(GetAlignedCodeMemoryStart(), CodeMemoryAlignedSize, PAGE_EXECUTE_READWRITE, &dummy); VirtualProtect(GetAlignedCodeMemoryStart(), CodeMemoryAlignedSize, PAGE_EXECUTE_READWRITE, &dummy);
#elif defined(__APPLE__) #elif defined(APPLE_AARCH64)
// Apple always uses dynamic allocation // Apple aarch64 always uses dynamic allocation
#else #else
mprotect(GetAlignedCodeMemoryStart(), CodeMemoryAlignedSize, PROT_EXEC | PROT_READ | PROT_WRITE); mprotect(GetAlignedCodeMemoryStart(), CodeMemoryAlignedSize, PROT_EXEC | PROT_READ | PROT_WRITE);
#endif #endif