win32: Be a bit more optimistic with allocated ranges; we get contiguous ranges more often than not

This commit is contained in:
kd-11 2021-01-10 20:26:17 +03:00 committed by kd-11
parent 58d367d704
commit ead180aa60

View File

@ -221,12 +221,18 @@ namespace utils
void memory_protect(void* pointer, usz size, protection prot)
{
#ifdef _WIN32
DWORD old;
if (::VirtualProtect(pointer, size, +prot, &old))
{
return;
}
for (u64 addr = reinterpret_cast<u64>(pointer), end = addr + size; addr < end;)
{
const u64 boundary = (addr + 0x10000) & -0x10000;
const u64 block_size = std::min(boundary, end) - addr;
DWORD old;
if (!::VirtualProtect(reinterpret_cast<LPVOID>(addr), block_size, +prot, &old))
{
fmt::throw_exception("VirtualProtect failed (%p, 0x%x, addr=0x%x, error=%#x)", pointer, size, addr, GetLastError());