From e9679f83091072ca6d3e4ed9c5ce066ba1ed7c65 Mon Sep 17 00:00:00 2001 From: raphaelthegreat <47210458+raphaelthegreat@users.noreply.github.com> Date: Sat, 8 Jun 2024 19:10:32 +0300 Subject: [PATCH] address_space: Don't reserve space again --- src/core/address_space.cpp | 21 ++++++++++++++++++--- src/core/memory.cpp | 5 +---- 2 files changed, 19 insertions(+), 7 deletions(-) diff --git a/src/core/address_space.cpp b/src/core/address_space.cpp index 409b453b..fe9780f5 100644 --- a/src/core/address_space.cpp +++ b/src/core/address_space.cpp @@ -116,6 +116,7 @@ struct AddressSpace::Impl { void* MapPrivate(VAddr virtual_addr, size_t size, u64 alignment, ULONG prot, bool no_commit = false) { // Map a private allocation + PVOID addr = reinterpret_cast(virtual_addr); MEM_ADDRESS_REQUIREMENTS req{}; MEM_EXTENDED_PARAMETER param{}; // req.LowestStartingAddress = @@ -129,9 +130,23 @@ struct AddressSpace::Impl { if (!no_commit) { alloc_type |= MEM_COMMIT; } - void* const ptr = VirtualAlloc2(process, reinterpret_cast(virtual_addr), size, - alloc_type, prot, ¶m, 1); - ASSERT_MSG(ptr, "{}", Common::GetLastErrorMsg()); + // Check if the area has been reserved beforehand (typically for tesselation buffer) + // and in that case don't reserve it again as Windows complains. + if (virtual_addr) { + MEMORY_BASIC_INFORMATION info; + VirtualQuery(addr, &info, sizeof(info)); + if (info.State == MEM_RESERVE) { + alloc_type &= ~MEM_RESERVE; + } + } + void* ptr{}; + if (virtual_addr) { + ptr = VirtualAlloc2(process, addr, size, alloc_type, prot, NULL, 0); + ASSERT_MSG(ptr && VAddr(ptr) == virtual_addr, "{}", Common::GetLastErrorMsg()); + } else { + ptr = VirtualAlloc2(process, nullptr, size, alloc_type, prot, ¶m, 1); + ASSERT_MSG(ptr, "{}", Common::GetLastErrorMsg()); + } return ptr; } diff --git a/src/core/memory.cpp b/src/core/memory.cpp index acae3b52..5180a633 100644 --- a/src/core/memory.cpp +++ b/src/core/memory.cpp @@ -92,10 +92,7 @@ int MemoryManager::MapMemory(void** out_addr, VAddr virtual_addr, size_t size, M it++; } ASSERT(it != vma_map.end()); - if (alignment > 0) { - ASSERT_MSG(it->second.base % alignment == 0, "Free region base is not aligned"); - } - mapped_addr = it->second.base; + mapped_addr = alignment > 0 ? Common::AlignUp(it->second.base, alignment) : it->second.base; } // Perform the mapping.