diff --git a/src/common/host_memory.cpp b/src/common/host_memory.cpp index de000d32e5..99070f02b2 100644 --- a/src/common/host_memory.cpp +++ b/src/common/host_memory.cpp @@ -451,16 +451,14 @@ static void* ChooseVirtualBase(size_t virtual_size) { #else -static void* ChooseVirtualBase(size_t virtual_size) { -#if defined(__OPENORBIS__) - return mmap(nullptr, virtual_size, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS | MAP_SYSTEM, -1, 0); -#elif defined(__FreeBSD__) || defined(__DragonFly__) || defined(__OpenBSD__) || defined(__sun__) || defined(__HAIKU__) || defined(__managarm__) || defined(__AIX__) - void* virtual_base = mmap(nullptr, virtual_size, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS | MAP_NORESERVE | MAP_ALIGNED_SUPER, -1, 0); +static void* ChooseVirtualBase(size_t size) { +#if defined(__FreeBSD__) || defined(__DragonFly__) || defined(__OpenBSD__) || defined(__sun__) || defined(__HAIKU__) || defined(__managarm__) || defined(__AIX__) + void* virtual_base = mmap(nullptr, size, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS | MAP_NORESERVE | MAP_ALIGNED_SUPER, -1, 0); if (virtual_base != MAP_FAILED) return virtual_base; - return mmap(nullptr, virtual_size, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS | MAP_NORESERVE, -1, 0); + return mmap(nullptr, size, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS | MAP_NORESERVE, -1, 0); #else - return mmap(nullptr, virtual_size, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS | MAP_NORESERVE, -1, 0); + return mmap(nullptr, size, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS | MAP_NORESERVE, -1, 0); #endif } @@ -713,6 +711,12 @@ private: #endif // ^^^ POSIX ^^^ HostMemory::HostMemory(size_t backing_size_, size_t virtual_size_) : backing_size(backing_size_), virtual_size(virtual_size_) { +#ifdef __OPENORBIS__ + LOG_WARNING(HW_Memory, "Platform doesn't support fastmem"); + fallback_buffer = std::make_unique>(backing_size); + backing_base = fallback_buffer->data(); + virtual_base = nullptr; +#else // Try to allocate a fastmem arena. // The implementation will fail with std::bad_alloc on errors. impl = std::make_unique(AlignUp(backing_size, PageAlignment), AlignUp(virtual_size, PageAlignment) + HugePageSize); @@ -723,6 +727,7 @@ HostMemory::HostMemory(size_t backing_size_, size_t virtual_size_) : backing_siz virtual_base = reinterpret_cast(Common::AlignUp(uintptr_t(virtual_base), HugePageSize)); virtual_base_offset = virtual_base - impl->virtual_base; } +#endif } HostMemory::~HostMemory() = default; diff --git a/src/common/virtual_buffer.cpp b/src/common/virtual_buffer.cpp index 55ddfc243a..ef9113c557 100644 --- a/src/common/virtual_buffer.cpp +++ b/src/common/virtual_buffer.cpp @@ -6,6 +6,8 @@ #ifdef _WIN32 #include +#elif defined(__OPENORBIS__) +#include #else #include #endif @@ -17,23 +19,38 @@ namespace Common { void* AllocateMemoryPages(std::size_t size) noexcept { #ifdef _WIN32 - void* base = VirtualAlloc(nullptr, size, MEM_COMMIT, PAGE_READWRITE); + void* addr = VirtualAlloc(nullptr, size, MEM_COMMIT, PAGE_READWRITE); + ASSERT(addr != nullptr); +#elif defined(__OPENORBIS__) + u64 align = 16384; + void *addr = nullptr; + off_t direct_mem_off; + int32_t rc; + if ((rc = sceKernelAllocateDirectMemory(0, sceKernelGetDirectMemorySize(), size, align, 3, &direct_mem_off)) < 0) { + ASSERT(false && "sceKernelAllocateDirectMemory"); + return nullptr; + } + if ((rc = sceKernelMapDirectMemory(&addr, size, 0x33, 0, direct_mem_off, align)) < 0) { + ASSERT(false && "sceKernelMapDirectMemory"); + return nullptr; + } + ASSERT(addr != nullptr); #else - void* base = mmap(nullptr, size, PROT_READ | PROT_WRITE, MAP_ANON | MAP_PRIVATE, -1, 0); - if (base == MAP_FAILED) - base = nullptr; + void* addr = mmap(nullptr, size, PROT_READ | PROT_WRITE, MAP_ANON | MAP_PRIVATE, -1, 0); + ASSERT(addr != MAP_FAILED); #endif - ASSERT(base); - return base; + return addr; } -void FreeMemoryPages(void* base, [[maybe_unused]] std::size_t size) noexcept { - if (!base) +void FreeMemoryPages(void* addr, [[maybe_unused]] std::size_t size) noexcept { + if (!addr) return; #ifdef _WIN32 - ASSERT(VirtualFree(base, 0, MEM_RELEASE)); + VirtualFree(addr, 0, MEM_RELEASE) +#elif defined(__OPENORBIS__) #else - ASSERT(munmap(base, size) == 0); + int rc = munmap(addr, size); + ASSERT(rc == 0); #endif }