Hotfix for shm->map_self()

Make sure mmap returns 64K-aligned results, as on Windows.
This commit is contained in:
Nekotekina 2020-11-08 22:15:59 +03:00
parent 733b46d51a
commit b05d12df0e

View File

@ -254,8 +254,32 @@ namespace utils
return nullptr;
#else
const u64 ptr64 = reinterpret_cast<u64>(ptr);
return static_cast<u8*>(::mmap(reinterpret_cast<void*>(ptr64 & -0x10000), m_size, +prot, MAP_SHARED | (ptr ? MAP_FIXED : 0), m_file, 0));
const u64 ptr64 = reinterpret_cast<u64>(ptr) & -0x10000;
if (ptr64)
{
return reinterpret_cast<u8*>(reinterpret_cast<u64>(::mmap(reinterpret_cast<void*>(ptr64), m_size, +prot, MAP_SHARED | MAP_FIXED, m_file, 0)));
}
else
{
const u64 res64 = reinterpret_cast<u64>(::mmap(reinterpret_cast<void*>(ptr64), m_size + 0xe000, PROT_NONE, MAP_ANON | MAP_PRIVATE, -1, 0));
const u64 aligned = ::align(res64, 0x10000);
const auto result = ::mmap(reinterpret_cast<void*>(aligned), m_size, +prot, MAP_SHARED | MAP_FIXED, m_file, 0);
// Now cleanup remnants
if (aligned > res64)
{
verify(HERE), ::munmap(reinterpret_cast<void*>(res64), aligned - res64) == 0;
}
if (aligned < res64 + 0xe000)
{
verify(HERE), ::munmap(reinterpret_cast<void*>(aligned + m_size), (res64 + 0xe000) - (aligned)) == 0;
}
return reinterpret_cast<u8*>(result);
}
#endif
}