diff --git a/Utilities/VirtualMemory.cpp b/Utilities/VirtualMemory.cpp index 0d21858c2c..6214e938d4 100644 --- a/Utilities/VirtualMemory.cpp +++ b/Utilities/VirtualMemory.cpp @@ -63,6 +63,17 @@ namespace utils #ifdef _WIN32 return ::VirtualAlloc(use_addr, size, MEM_RESERVE, PAGE_NOACCESS); #else + if (use_addr && reinterpret_cast(use_addr) % 0x10000) + { + return nullptr; + } + + if (!use_addr) + { + // Hack: Ensure aligned 64k allocations + size += 0x10000; + } + auto ptr = ::mmap(use_addr, size, PROT_NONE, MAP_ANON | MAP_PRIVATE, -1, 0); if (ptr == reinterpret_cast(-1)) @@ -76,6 +87,20 @@ namespace utils return nullptr; } + if (!use_addr && ptr) + { + // Continuation of the hack above + const auto misalign = reinterpret_cast(ptr) % 0x10000; + ::munmap(ptr, 0x10000 - misalign); + + if (misalign) + { + ::munmap(static_cast(ptr) + size - misalign, misalign); + } + + ptr = static_cast(ptr) + (0x10000 - misalign); + } + return ptr; #endif }