Use MAP_NORESERVE when available in mmap (Linux)

Used in: memory_reserve, memory_decommit (vm_native.cpp)
This commit is contained in:
Nekotekina 2020-12-27 10:18:59 +03:00
parent 92e5bb88e8
commit 1e32311a65

View File

@ -33,6 +33,12 @@ static int memfd_create_(const char *name, uint flags)
namespace utils
{
#ifdef MAP_NORESERVE
constexpr auto c_map_noreserve = MAP_NORESERVE;
#else
constexpr int c_map_noreserve = 0;
#endif
#ifdef _WIN32
DYNAMIC_IMPORT("KernelBase.dll", VirtualAlloc2, PVOID(HANDLE Process, PVOID Base, SIZE_T Size, ULONG AllocType, ULONG Prot, MEM_EXTENDED_PARAMETER*, ULONG));
DYNAMIC_IMPORT("KernelBase.dll", MapViewOfFile3, PVOID(HANDLE Handle, HANDLE Process, PVOID Base, ULONG64 Off, SIZE_T ViewSize, ULONG AllocType, ULONG Prot, MEM_EXTENDED_PARAMETER*, ULONG));
@ -84,7 +90,7 @@ namespace utils
size += 0x10000;
}
auto ptr = ::mmap(use_addr, size, PROT_NONE, MAP_ANON | MAP_PRIVATE, -1, 0);
auto ptr = ::mmap(use_addr, size, PROT_NONE, MAP_ANON | MAP_PRIVATE | c_map_noreserve, -1, 0);
if (ptr == reinterpret_cast<void*>(-1))
{
@ -137,7 +143,7 @@ namespace utils
ensure(::VirtualFree(pointer, size, MEM_DECOMMIT));
#else
const u64 ptr64 = reinterpret_cast<u64>(pointer);
ensure(::mmap(pointer, size, PROT_NONE, MAP_FIXED | MAP_ANON | MAP_PRIVATE, -1, 0) != reinterpret_cast<void*>(-1));
ensure(::mmap(pointer, size, PROT_NONE, MAP_FIXED | MAP_ANON | MAP_PRIVATE | c_map_noreserve, -1, 0) != reinterpret_cast<void*>(-1));
#ifdef MADV_FREE
ensure(::madvise(reinterpret_cast<void*>(ptr64 & -4096), size + (ptr64 & 4095), MADV_FREE) != -1);
#else