From 6482c582d4b534981dd4bc7fd637629dc5dfb09a Mon Sep 17 00:00:00 2001 From: Exverge Date: Sun, 19 Jul 2026 13:30:56 -0400 Subject: [PATCH] [core/host_mem] replace while loop with VirtualAlloc2 ext params --- src/common/host_memory.cpp | 39 +++++++------------------------------- 1 file changed, 7 insertions(+), 32 deletions(-) diff --git a/src/common/host_memory.cpp b/src/common/host_memory.cpp index e9396b512e..269cf60b2b 100644 --- a/src/common/host_memory.cpp +++ b/src/common/host_memory.cpp @@ -168,41 +168,16 @@ public: } // Allocate virtual address placeholder within a 39-bit address space - SIZE_T cursor = 0; - while (cursor < (1ULL << 39) - virtual_size) { - MEMORY_BASIC_INFORMATION info{}; + MEM_ADDRESS_REQUIREMENTS addr_reqs {}; + addr_reqs.Alignment = HugePageSize; + addr_reqs.HighestEndingAddress = reinterpret_cast(1ULL << 39); - // find the next mapped region of memory - auto res = pfn_VirtualQuery(reinterpret_cast(cursor), &info, sizeof(info)); + MEM_EXTENDED_PARAMETER ext_param {}; + ext_param.Type = MemExtendedParameterAddressRequirements; + ext_param.Pointer = &addr_reqs; - if (res == 0) { - LOG_WARNING(HW_Memory, "Failed to check memory region, error {}", GetLastError()); - break; - } - - auto start_aligned = AlignUp(reinterpret_cast(info.BaseAddress), HugePageSize); - // is this region free? - if (info.State == MEM_FREE && start_aligned < reinterpret_cast(info.BaseAddress) + info.RegionSize) { - // is this region big enough for us to use? - if (info.RegionSize - (start_aligned - reinterpret_cast(info.BaseAddress)) >= virtual_size) { - virtual_base = static_cast(pfn_VirtualAlloc2 - (process, reinterpret_cast(start_aligned), virtual_size, MEM_RESERVE | MEM_RESERVE_PLACEHOLDER, PAGE_NOACCESS, nullptr, 0)); - if (virtual_base) { - break; - } else { - LOG_WARNING(HW_Memory, "Failed to allocate buffer at {:#x} with error {}, trying at at new address", start_aligned, GetLastError()); - } - } - } + virtual_base = static_cast(pfn_VirtualAlloc2(process, nullptr, virtual_size, MEM_RESERVE | MEM_RESERVE_PLACEHOLDER, PAGE_NOACCESS, &ext_param, 1)); - auto new_cursor = reinterpret_cast(info.BaseAddress) + info.RegionSize; - if (new_cursor <= cursor) { - // weird unknown error, let's just continue cursor so this isn't an infinite loop - cursor = cursor + HugePageSize; - continue; - } - cursor = new_cursor; - } // Check if we failed to allocate for direct-mapping, otherwise map normally if (!virtual_base) { LOG_WARNING(HW_Memory, "Failed to allocate within 39-bit address space, direct mapping is not supported");