diff options
author | Mary-nyan <mary@mary.zone> | 2022-11-24 15:26:29 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-11-24 15:26:29 +0100 |
commit | f4e879a1e6ad810aa38c1c020467a2589441871b (patch) | |
tree | 274c6ee1f55129f01bbd28a6dcd92ce3f84237fb /Ryujinx.Cpu/MemoryHelper.cs | |
parent | a1ddaa2736b188de928564af56aa787a25831ff7 (diff) |
Reduce usage of Marshal.PtrToStructure and Marshal.StructureToPtr (#3805)1.1.381
* common: Make BinaryReaderExtensions Read & Write take unamanged types
This allows us to not rely on Marshal.PtrToStructure and Marshal.StructureToPtr for those.
* common: Make MemoryHelper Read & Write takes unamanged types
* Update Marshal.SizeOf => Unsafe.SizeOf when appropriate and start moving software applet to unmanaged types
Diffstat (limited to 'Ryujinx.Cpu/MemoryHelper.cs')
-rw-r--r-- | Ryujinx.Cpu/MemoryHelper.cs | 27 |
1 files changed, 6 insertions, 21 deletions
diff --git a/Ryujinx.Cpu/MemoryHelper.cs b/Ryujinx.Cpu/MemoryHelper.cs index 6194d5b2..64ff360e 100644 --- a/Ryujinx.Cpu/MemoryHelper.cs +++ b/Ryujinx.Cpu/MemoryHelper.cs @@ -1,6 +1,7 @@ using Ryujinx.Memory; using System; using System.IO; +using System.Runtime.CompilerServices; using System.Runtime.InteropServices; using System.Text; @@ -23,34 +24,18 @@ namespace Ryujinx.Cpu } } - public unsafe static T Read<T>(IVirtualMemoryManager memory, ulong position) where T : struct + public unsafe static T Read<T>(IVirtualMemoryManager memory, ulong position) where T : unmanaged { - long size = Marshal.SizeOf<T>(); - - byte[] data = new byte[size]; - - memory.Read(position, data); - - fixed (byte* ptr = data) - { - return Marshal.PtrToStructure<T>((IntPtr)ptr); - } + return MemoryMarshal.Cast<byte, T>(memory.GetSpan(position, Unsafe.SizeOf<T>()))[0]; } - public unsafe static ulong Write<T>(IVirtualMemoryManager memory, ulong position, T value) where T : struct + public unsafe static ulong Write<T>(IVirtualMemoryManager memory, ulong position, T value) where T : unmanaged { - long size = Marshal.SizeOf<T>(); - - byte[] data = new byte[size]; - - fixed (byte* ptr = data) - { - Marshal.StructureToPtr<T>(value, (IntPtr)ptr, false); - } + ReadOnlySpan<byte> data = MemoryMarshal.Cast<T, byte>(MemoryMarshal.CreateReadOnlySpan(ref value, 1)); memory.Write(position, data); - return (ulong)size; + return (ulong)data.Length; } public static string ReadAsciiString(IVirtualMemoryManager memory, ulong position, long maxSize = -1) |