From 7773e11c2d2e3e7874c3682adef83d037bbafd04 Mon Sep 17 00:00:00 2001 From: georgemoralis Date: Wed, 2 Aug 2023 13:51:10 +0300 Subject: [PATCH] refactoring --- CMakeLists.txt | 4 +-- src/Core/PS4/HLE/Kernel/MemoryManagement.cpp | 33 ++++++++++--------- ...PhysicalMemory.cpp => physical_memory.cpp} | 10 +++--- .../{PhysicalMemory.h => physical_memory.h} | 10 +++--- src/Core/PS4/HLE/LibKernel.cpp | 2 +- 5 files changed, 31 insertions(+), 28 deletions(-) rename src/Core/PS4/HLE/Kernel/Objects/{PhysicalMemory.cpp => physical_memory.cpp} (85%) rename src/Core/PS4/HLE/Kernel/Objects/{PhysicalMemory.h => physical_memory.h} (81%) diff --git a/CMakeLists.txt b/CMakeLists.txt index a254e801..53c4ac01 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -36,8 +36,8 @@ add_executable(shadps4 src/Core/PS4/Linker.h src/Lib/Threads.cpp src/Lib/Threads.h - src/Core/PS4/HLE/Kernel/Objects/PhysicalMemory.h - src/Core/PS4/HLE/Kernel/Objects/PhysicalMemory.cpp + src/Core/PS4/HLE/Kernel/Objects/physical_memory.h + src/Core/PS4/HLE/Kernel/Objects/physical_memory.cpp "src/Util/Singleton.h" "src/Util/Disassembler.cpp" "src/Util/Disassembler.h" "src/Util/StringUtil.h" "src/Core/PS4/Util/aerolib.h" "src/Core/PS4/Loader/SymbolsResolver.h" "src/Core/PS4/Loader/SymbolsResolver.cpp" "src/Core/PS4/HLE/Libs.cpp" "src/Core/PS4/HLE/Libs.h" "src/Core/PS4/HLE/LibC.cpp" "src/Core/PS4/HLE/LibC.h" "src/Lib/Timer.cpp" "src/Lib/Timer.h" "src/Core/PS4/HLE/LibKernel.cpp" "src/Core/PS4/HLE/LibKernel.h" "src/Core/PS4/HLE/LibSceVideoOut.cpp" "src/Core/PS4/HLE/LibSceVideoOut.h" "src/Core/PS4/HLE/LibSceGnmDriver.cpp" "src/Core/PS4/HLE/LibSceGnmDriver.h" "src/Core/PS4/HLE/Kernel/ThreadManagement.cpp" "src/Core/PS4/HLE/Kernel/ThreadManagement.h" "src/Core/PS4/HLE/ErrorCodes.h" "src/Debug.h" "src/Core/PS4/HLE/Kernel/MemoryManagement.cpp" "src/Core/PS4/HLE/Kernel/MemoryManagement.h" "src/Core/PS4/HLE/Kernel/MemMngCodes.h" "src/Util/StringUtil.cpp") find_package(OpenGL REQUIRED) diff --git a/src/Core/PS4/HLE/Kernel/MemoryManagement.cpp b/src/Core/PS4/HLE/Kernel/MemoryManagement.cpp index 40cbd847..8950105c 100644 --- a/src/Core/PS4/HLE/Kernel/MemoryManagement.cpp +++ b/src/Core/PS4/HLE/Kernel/MemoryManagement.cpp @@ -1,12 +1,14 @@ #include "MemoryManagement.h" + +#include + #include "../../../../Debug.h" #include "../../../../Util/Log.h" -#include "../Libs.h" -#include "../ErrorCodes.h" -#include "MemMngCodes.h" -#include #include "../../../../Util/Singleton.h" -#include "Objects/PhysicalMemory.h" +#include "../ErrorCodes.h" +#include "../Libs.h" +#include "MemMngCodes.h" +#include "Objects/physical_memory.h" namespace HLE::Libs::LibKernel::MemoryManagement { @@ -19,20 +21,19 @@ u64 PS4_SYSV_ABI sceKernelGetDirectMemorySize() { return SCE_KERNEL_MAIN_DMEM_SIZE; } -int PS4_SYSV_ABI sceKernelAllocateDirectMemory(s64 searchStart, s64 searchEnd, u64 len, u64 alignment, int memoryType,s64* physAddrOut){ - +int PS4_SYSV_ABI sceKernelAllocateDirectMemory(s64 searchStart, s64 searchEnd, u64 len, u64 alignment, int memoryType, s64* physAddrOut) { PRINT_FUNCTION_NAME(); - - if (searchStart < 0 || searchEnd <= searchStart) { - //TODO debug logging - return SCE_KERNEL_ERROR_EINVAL; - } - bool isInRange = (searchStart < len && searchEnd > len); - if (len <= 0 || !is16KBAligned(len) || !isInRange){ + + if (searchStart < 0 || searchEnd <= searchStart) { // TODO debug logging return SCE_KERNEL_ERROR_EINVAL; } - if ((alignment != 0 || is16KBAligned(alignment)) && !isPowerOfTwo(alignment)){ + bool isInRange = (searchStart < len && searchEnd > len); + if (len <= 0 || !is16KBAligned(len) || !isInRange) { + // TODO debug logging + return SCE_KERNEL_ERROR_EINVAL; + } + if ((alignment != 0 || is16KBAligned(alignment)) && !isPowerOfTwo(alignment)) { // TODO debug logging return SCE_KERNEL_ERROR_EINVAL; } @@ -49,7 +50,7 @@ int PS4_SYSV_ABI sceKernelAllocateDirectMemory(s64 searchStart, s64 searchEnd, u u64 physical_addr = 0; auto* physical_memory = Singleton::Instance(); if (!physical_memory->Alloc(searchStart, searchEnd, len, alignment, &physical_addr, memoryType)) { - //TODO debug logging + // TODO debug logging return SCE_KERNEL_ERROR_EAGAIN; } *physAddrOut = static_cast(physical_addr); diff --git a/src/Core/PS4/HLE/Kernel/Objects/PhysicalMemory.cpp b/src/Core/PS4/HLE/Kernel/Objects/physical_memory.cpp similarity index 85% rename from src/Core/PS4/HLE/Kernel/Objects/PhysicalMemory.cpp rename to src/Core/PS4/HLE/Kernel/Objects/physical_memory.cpp index c1c6a91d..5e9e5d5d 100644 --- a/src/Core/PS4/HLE/Kernel/Objects/PhysicalMemory.cpp +++ b/src/Core/PS4/HLE/Kernel/Objects/physical_memory.cpp @@ -1,4 +1,4 @@ -#include "PhysicalMemory.h" +#include "physical_memory.h" namespace HLE::Kernel::Objects { @@ -11,12 +11,12 @@ bool PhysicalMemory::Alloc(u64 searchStart, u64 searchEnd, u64 len, u64 alignmen for (const auto& block : m_allocatedBlocks) { u64 n = block.start_addr + block.size; if (n > find_free_pos) { - find_free_pos = n; + find_free_pos = n; } - } + } // align free position - find_free_pos = AlignUp(find_free_pos, alignment); + find_free_pos = AlignUp(find_free_pos, alignment); // if the new position is between searchStart - searchEnd , allocate a new block if (find_free_pos >= searchStart && find_free_pos + len <= searchEnd) { @@ -33,4 +33,4 @@ bool PhysicalMemory::Alloc(u64 searchStart, u64 searchEnd, u64 len, u64 alignmen return false; } -} +} // namespace HLE::Kernel::Objects diff --git a/src/Core/PS4/HLE/Kernel/Objects/PhysicalMemory.h b/src/Core/PS4/HLE/Kernel/Objects/physical_memory.h similarity index 81% rename from src/Core/PS4/HLE/Kernel/Objects/PhysicalMemory.h rename to src/Core/PS4/HLE/Kernel/Objects/physical_memory.h index 480dec5b..116f86e1 100644 --- a/src/Core/PS4/HLE/Kernel/Objects/PhysicalMemory.h +++ b/src/Core/PS4/HLE/Kernel/Objects/physical_memory.h @@ -1,5 +1,6 @@ #pragma once #include + #include "../../../../../types.h" namespace HLE::Kernel::Objects { @@ -11,13 +12,14 @@ class PhysicalMemory { u64 size; int memoryType; }; - PhysicalMemory() { } - virtual ~PhysicalMemory() { } + PhysicalMemory() {} + virtual ~PhysicalMemory() {} + public: bool Alloc(u64 searchStart, u64 searchEnd, u64 len, u64 alignment, u64* physAddrOut, int memoryType); + private: std::vector m_allocatedBlocks; }; - -} \ No newline at end of file +} // namespace HLE::Kernel::Objects \ No newline at end of file diff --git a/src/Core/PS4/HLE/LibKernel.cpp b/src/Core/PS4/HLE/LibKernel.cpp index 9776d2e5..0c633937 100644 --- a/src/Core/PS4/HLE/LibKernel.cpp +++ b/src/Core/PS4/HLE/LibKernel.cpp @@ -5,7 +5,7 @@ #include "../../../Util/Log.h" #include "Kernel/MemoryManagement.h" #include "../../../Util/Singleton.h" -#include "Kernel/Objects/PhysicalMemory.h" +#include "Kernel/Objects/physical_memory.h" namespace HLE::Libs::LibKernel {