diff --git a/rpcs3/util/vm_native.cpp b/rpcs3/util/vm_native.cpp index 8dd069d553..156af86548 100644 --- a/rpcs3/util/vm_native.cpp +++ b/rpcs3/util/vm_native.cpp @@ -969,20 +969,24 @@ namespace utils { void* ptr = m_ptr; - while (!ptr) + for (void* mapped = nullptr; !ptr;) { - const auto mapped = this->map(nullptr, prot); + if (!mapped) + { + mapped = this->map(nullptr, prot); + } // Install mapped memory - if (!m_ptr.compare_exchange(ptr, mapped)) - { - // Mapped already, nothing to do. - this->unmap(mapped); - } - else + if (m_ptr.compare_exchange(ptr, mapped)) { ptr = mapped; } + else if (ptr) + { + // Mapped already, nothing to do. + ensure(ptr != mapped); + this->unmap(mapped); + } } return static_cast(ptr);