From 0cf6f463302cd291565ea510b152fb3fd33bfaae Mon Sep 17 00:00:00 2001 From: M4xw Date: Mon, 26 Jun 2023 16:17:45 +0200 Subject: [PATCH] [MemoryUtil] Platform changes for Nintendo Switch --- CMakeLists.txt | 2 + Common/MemoryUtil.cpp | 4 +- Common/MemoryUtil.h | 4 ++ Common/MemoryUtilHorizon.cpp | 89 ++++++++++++++++++++++++++++++++++++ 4 files changed, 98 insertions(+), 1 deletion(-) create mode 100644 Common/MemoryUtilHorizon.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index fd40d84a34a9..503047ad0b67 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -801,8 +801,10 @@ add_library(Common STATIC Common/MemArenaDarwin.cpp Common/MemArenaPosix.cpp Common/MemArenaWin32.cpp + Common/MemArenaHorizon.cpp Common/MemArena.h Common/MemoryUtil.cpp + Common/MemoryUtilHorizon.cpp Common/MemoryUtil.h Common/OSVersion.cpp Common/OSVersion.h diff --git a/Common/MemoryUtil.cpp b/Common/MemoryUtil.cpp index cee1e33ecaf7..4ea9ce5d5474 100644 --- a/Common/MemoryUtil.cpp +++ b/Common/MemoryUtil.cpp @@ -17,6 +17,7 @@ #include "ppsspp_config.h" +#if !PPSSPP_PLATFORM(SWITCH) #include #include @@ -257,7 +258,7 @@ void *AllocateAlignedMemory(size_t size, size_t alignment) { #endif #endif - _assert_msg_(ptr != nullptr, "Failed to allocate aligned memory of size %llu", size); + _assert_msg_(ptr != nullptr, "Failed to allocate aligned memory of size %lu", size); return ptr; } @@ -355,3 +356,4 @@ int GetMemoryProtectPageSize() { #endif return MEM_PAGE_SIZE; } +#endif // !PPSSPP_PLATFORM(SWITCH) diff --git a/Common/MemoryUtil.h b/Common/MemoryUtil.h index 9d72de9aba2e..aa7cfd5dffe8 100644 --- a/Common/MemoryUtil.h +++ b/Common/MemoryUtil.h @@ -18,7 +18,11 @@ #pragma once #ifndef _WIN32 +#ifndef __SWITCH__ #include +#else +#include +#endif // !__SWITCH__ #endif #include diff --git a/Common/MemoryUtilHorizon.cpp b/Common/MemoryUtilHorizon.cpp new file mode 100644 index 000000000000..e0ae8090ad2a --- /dev/null +++ b/Common/MemoryUtilHorizon.cpp @@ -0,0 +1,89 @@ +// Copyright (C) 2003 Dolphin Project. + +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, version 2.0 or later versions. + +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License 2.0 for more details. + +// A copy of the GPL 2.0 should have been included with the program. +// If not, see http://www.gnu.org/licenses/ + +// Official SVN repository and contact information can be found at +// http://code.google.com/p/dolphin-emu/ + +#include "ppsspp_config.h" + +#if PPSSPP_PLATFORM(SWITCH) +#include +#include + +#include "Common/CommonTypes.h" +#include "Common/Log.h" +#include "Common/MemoryUtil.h" +#include "Common/StringUtils.h" +#include "Common/SysError.h" + +#include +#include + +#include // memalign + +#define MEM_PAGE_SIZE (0x1000) +#define MEM_PAGE_MASK ((MEM_PAGE_SIZE)-1) +#define ppsspp_round_page(x) ((((uintptr_t)(x)) + MEM_PAGE_MASK) & ~(MEM_PAGE_MASK)) + +// On Switch we dont support allocating executable memory here +// See CodeBlock.h +void *AllocateExecutableMemory(size_t size) { + return nullptr; +} + +void *AllocateMemoryPages(size_t size, uint32_t memProtFlags) { + void* ptr = nullptr; + size = ppsspp_round_page(size); + ptr = memalign(MEM_PAGE_SIZE, size); + return ptr; +} + +void *AllocateAlignedMemory(size_t size, size_t alignment) { + void* ptr = memalign(alignment, size); + + _assert_msg_(ptr != nullptr, "Failed to allocate aligned memory of size %lu", size); + return ptr; +} + +void FreeMemoryPages(void *ptr, size_t size) { + if (!ptr) + return; + + free(ptr); + return; +} + +void FreeExecutableMemory(void *ptr, size_t size) { + return; // Not supported on Switch +} + +void FreeAlignedMemory(void* ptr) { + if (!ptr) + return; + + free(ptr); +} + +bool PlatformIsWXExclusive() { + return false; // Switch technically is W^X but we use dual mappings instead of reprotecting the pages to allow a W and X mapping +} + +bool ProtectMemoryPages(const void* ptr, size_t size, uint32_t memProtFlags) { + return true; +} + +int GetMemoryProtectPageSize() { + return MEM_PAGE_SIZE; +} +#endif // PPSSPP_PLATFORM(SWITCH)