From 7d160e98fde05e0b9b542dc04ea72dc34994bc5b Mon Sep 17 00:00:00 2001
From: TSRBerry <20988865+TSRBerry@users.noreply.github.com>
Date: Sat, 24 Jun 2023 02:46:04 +0200
Subject: [PATCH] MemoryManagement: Change return types for Commit/Decommit to
 void (#5325)

* Replace return type with void for Commit/Decommit

* Small cleanup
---
 src/ARMeilleure/Memory/IJitMemoryBlock.cs     |  4 ++--
 src/Ryujinx.Cpu/Jit/JitMemoryBlock.cs         |  4 ++--
 src/Ryujinx.Memory/MemoryBlock.cs             | 13 ++++++-------
 src/Ryujinx.Memory/MemoryManagement.cs        | 12 ++++++------
 src/Ryujinx.Memory/MemoryManagementUnix.cs    | 16 +++++-----------
 src/Ryujinx.Memory/MemoryManagementWindows.cs | 16 +++++++++++-----
 6 files changed, 32 insertions(+), 33 deletions(-)

diff --git a/src/ARMeilleure/Memory/IJitMemoryBlock.cs b/src/ARMeilleure/Memory/IJitMemoryBlock.cs
index 670f2862..9b11e07f 100644
--- a/src/ARMeilleure/Memory/IJitMemoryBlock.cs
+++ b/src/ARMeilleure/Memory/IJitMemoryBlock.cs
@@ -6,9 +6,9 @@ namespace ARMeilleure.Memory
     {
         IntPtr Pointer { get; }
 
-        bool Commit(ulong offset, ulong size);
+        void Commit(ulong offset, ulong size);
 
         void MapAsRx(ulong offset, ulong size);
         void MapAsRwx(ulong offset, ulong size);
     }
-}
+}
\ No newline at end of file
diff --git a/src/Ryujinx.Cpu/Jit/JitMemoryBlock.cs b/src/Ryujinx.Cpu/Jit/JitMemoryBlock.cs
index 327fb303..61e27eaf 100644
--- a/src/Ryujinx.Cpu/Jit/JitMemoryBlock.cs
+++ b/src/Ryujinx.Cpu/Jit/JitMemoryBlock.cs
@@ -15,10 +15,10 @@ namespace Ryujinx.Cpu.Jit
             _impl = new MemoryBlock(size, flags);
         }
 
-        public bool Commit(ulong offset, ulong size) => _impl.Commit(offset, size);
+        public void Commit(ulong offset, ulong size) => _impl.Commit(offset, size);
         public void MapAsRx(ulong offset, ulong size) => _impl.Reprotect(offset, size, MemoryPermission.ReadAndExecute);
         public void MapAsRwx(ulong offset, ulong size) => _impl.Reprotect(offset, size, MemoryPermission.ReadWriteExecute);
 
         public void Dispose() => _impl.Dispose();
     }
-}
+}
\ No newline at end of file
diff --git a/src/Ryujinx.Memory/MemoryBlock.cs b/src/Ryujinx.Memory/MemoryBlock.cs
index 885ef456..2cf04628 100644
--- a/src/Ryujinx.Memory/MemoryBlock.cs
+++ b/src/Ryujinx.Memory/MemoryBlock.cs
@@ -1,6 +1,5 @@
 using System;
 using System.Runtime.CompilerServices;
-using System.Runtime.InteropServices;
 using System.Threading;
 
 namespace Ryujinx.Memory
@@ -101,12 +100,12 @@ namespace Ryujinx.Memory
         /// </summary>
         /// <param name="offset">Starting offset of the range to be committed</param>
         /// <param name="size">Size of the range to be committed</param>
-        /// <returns>True if the operation was successful, false otherwise</returns>
+        /// <exception cref="SystemException">Throw when the operation was not successful</exception>
         /// <exception cref="ObjectDisposedException">Throw when the memory block has already been disposed</exception>
         /// <exception cref="InvalidMemoryRegionException">Throw when either <paramref name="offset"/> or <paramref name="size"/> are out of range</exception>
-        public bool Commit(ulong offset, ulong size)
+        public void Commit(ulong offset, ulong size)
         {
-            return MemoryManagement.Commit(GetPointerInternal(offset, size), size, _forJit);
+            MemoryManagement.Commit(GetPointerInternal(offset, size), size, _forJit);
         }
 
         /// <summary>
@@ -115,12 +114,12 @@ namespace Ryujinx.Memory
         /// </summary>
         /// <param name="offset">Starting offset of the range to be decommitted</param>
         /// <param name="size">Size of the range to be decommitted</param>
-        /// <returns>True if the operation was successful, false otherwise</returns>
+        /// <exception cref="SystemException">Throw when the operation was not successful</exception>
         /// <exception cref="ObjectDisposedException">Throw when the memory block has already been disposed</exception>
         /// <exception cref="InvalidMemoryRegionException">Throw when either <paramref name="offset"/> or <paramref name="size"/> are out of range</exception>
-        public bool Decommit(ulong offset, ulong size)
+        public void Decommit(ulong offset, ulong size)
         {
-            return MemoryManagement.Decommit(GetPointerInternal(offset, size), size);
+            MemoryManagement.Decommit(GetPointerInternal(offset, size), size);
         }
 
         /// <summary>
diff --git a/src/Ryujinx.Memory/MemoryManagement.cs b/src/Ryujinx.Memory/MemoryManagement.cs
index c4b5ac4c..7acf8345 100644
--- a/src/Ryujinx.Memory/MemoryManagement.cs
+++ b/src/Ryujinx.Memory/MemoryManagement.cs
@@ -36,15 +36,15 @@ namespace Ryujinx.Memory
             }
         }
 
-        public static bool Commit(IntPtr address, ulong size, bool forJit)
+        public static void Commit(IntPtr address, ulong size, bool forJit)
         {
             if (OperatingSystem.IsWindows())
             {
-                return MemoryManagementWindows.Commit(address, (IntPtr)size);
+                MemoryManagementWindows.Commit(address, (IntPtr)size);
             }
             else if (OperatingSystem.IsLinux() || OperatingSystem.IsMacOS())
             {
-                return MemoryManagementUnix.Commit(address, size, forJit);
+                MemoryManagementUnix.Commit(address, size, forJit);
             }
             else
             {
@@ -52,15 +52,15 @@ namespace Ryujinx.Memory
             }
         }
 
-        public static bool Decommit(IntPtr address, ulong size)
+        public static void Decommit(IntPtr address, ulong size)
         {
             if (OperatingSystem.IsWindows())
             {
-                return MemoryManagementWindows.Decommit(address, (IntPtr)size);
+                MemoryManagementWindows.Decommit(address, (IntPtr)size);
             }
             else if (OperatingSystem.IsLinux() || OperatingSystem.IsMacOS())
             {
-                return MemoryManagementUnix.Decommit(address, size);
+                MemoryManagementUnix.Decommit(address, size);
             }
             else
             {
diff --git a/src/Ryujinx.Memory/MemoryManagementUnix.cs b/src/Ryujinx.Memory/MemoryManagementUnix.cs
index 30baf035..d665b229 100644
--- a/src/Ryujinx.Memory/MemoryManagementUnix.cs
+++ b/src/Ryujinx.Memory/MemoryManagementUnix.cs
@@ -2,8 +2,6 @@
 using System.Collections.Concurrent;
 using System.Runtime.InteropServices;
 using System.Runtime.Versioning;
-using System.Text;
-
 using static Ryujinx.Memory.MemoryManagerUnixHelper;
 
 namespace Ryujinx.Memory
@@ -12,7 +10,7 @@ namespace Ryujinx.Memory
     [SupportedOSPlatform("macos")]
     static class MemoryManagementUnix
     {
-        private static readonly ConcurrentDictionary<IntPtr, ulong> _allocations = new ConcurrentDictionary<IntPtr, ulong>();
+        private static readonly ConcurrentDictionary<IntPtr, ulong> _allocations = new();
 
         public static IntPtr Allocate(ulong size, bool forJit)
         {
@@ -68,7 +66,7 @@ namespace Ryujinx.Memory
             return ptr;
         }
 
-        public static bool Commit(IntPtr address, ulong size, bool forJit)
+        public static void Commit(IntPtr address, ulong size, bool forJit)
         {
             MmapProts prot = MmapProts.PROT_READ | MmapProts.PROT_WRITE;
 
@@ -81,11 +79,9 @@ namespace Ryujinx.Memory
             {
                 throw new SystemException(Marshal.GetLastPInvokeErrorMessage());
             }
-
-            return true;
         }
 
-        public static bool Decommit(IntPtr address, ulong size)
+        public static void Decommit(IntPtr address, ulong size)
         {
             // Must be writable for madvise to work properly.
             if (mprotect(address, size, MmapProts.PROT_READ | MmapProts.PROT_WRITE) != 0)
@@ -102,8 +98,6 @@ namespace Ryujinx.Memory
             {
                 throw new SystemException(Marshal.GetLastPInvokeErrorMessage());
             }
-
-            return true;
         }
 
         public static bool Reprotect(IntPtr address, ulong size, MemoryPermission permission)
@@ -146,7 +140,7 @@ namespace Ryujinx.Memory
 
             if (OperatingSystem.IsMacOS())
             {
-                byte[] memName = Encoding.ASCII.GetBytes("Ryujinx-XXXXXX");
+                byte[] memName = "Ryujinx-XXXXXX"u8.ToArray();
 
                 fixed (byte* pMemName = memName)
                 {
@@ -164,7 +158,7 @@ namespace Ryujinx.Memory
             }
             else
             {
-                byte[] fileName = Encoding.ASCII.GetBytes("/dev/shm/Ryujinx-XXXXXX");
+                byte[] fileName = "/dev/shm/Ryujinx-XXXXXX"u8.ToArray();
 
                 fixed (byte* pFileName = fileName)
                 {
diff --git a/src/Ryujinx.Memory/MemoryManagementWindows.cs b/src/Ryujinx.Memory/MemoryManagementWindows.cs
index cbf3ecba..d7d78bd8 100644
--- a/src/Ryujinx.Memory/MemoryManagementWindows.cs
+++ b/src/Ryujinx.Memory/MemoryManagementWindows.cs
@@ -10,7 +10,7 @@ namespace Ryujinx.Memory
     {
         public const int PageSize = 0x1000;
 
-        private static readonly PlaceholderManager _placeholders = new PlaceholderManager();
+        private static readonly PlaceholderManager _placeholders = new();
 
         public static IntPtr Allocate(IntPtr size)
         {
@@ -55,14 +55,20 @@ namespace Ryujinx.Memory
             return ptr;
         }
 
-        public static bool Commit(IntPtr location, IntPtr size)
+        public static void Commit(IntPtr location, IntPtr size)
         {
-            return WindowsApi.VirtualAlloc(location, size, AllocationType.Commit, MemoryProtection.ReadWrite) != IntPtr.Zero;
+            if (WindowsApi.VirtualAlloc(location, size, AllocationType.Commit, MemoryProtection.ReadWrite) == IntPtr.Zero)
+            {
+                throw new SystemException(Marshal.GetLastPInvokeErrorMessage());
+            }
         }
 
-        public static bool Decommit(IntPtr location, IntPtr size)
+        public static void Decommit(IntPtr location, IntPtr size)
         {
-            return WindowsApi.VirtualFree(location, size, AllocationType.Decommit);
+            if (!WindowsApi.VirtualFree(location, size, AllocationType.Decommit))
+            {
+                throw new SystemException(Marshal.GetLastPInvokeErrorMessage());
+            }
         }
 
         public static void MapView(IntPtr sharedMemory, ulong srcOffset, IntPtr location, IntPtr size, MemoryBlock owner)