From 4d655cdd198d7cf7798f6831b1f641d832d3de7c Mon Sep 17 00:00:00 2001 From: Alex Barney Date: Mon, 25 Feb 2019 12:37:30 -0600 Subject: [PATCH] Use the Unsafe class in RomFsDictionary --- src/LibHac/IO/RomFs/RomFsDictionary.cs | 13 ++++++------- src/LibHac/LibHac.csproj | 6 +++++- 2 files changed, 11 insertions(+), 8 deletions(-) diff --git a/src/LibHac/IO/RomFs/RomFsDictionary.cs b/src/LibHac/IO/RomFs/RomFsDictionary.cs index 9c6a3cfd..6326ee4b 100644 --- a/src/LibHac/IO/RomFs/RomFsDictionary.cs +++ b/src/LibHac/IO/RomFs/RomFsDictionary.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.Runtime.CompilerServices; using System.Runtime.InteropServices; namespace LibHac.IO.RomFs @@ -13,8 +14,7 @@ namespace LibHac.IO.RomFs private int[] Buckets { get; set; } private byte[] Entries { get; set; } - // Hack around not being able to get the size of generic structures - private readonly int _sizeOfEntry = 12 + Marshal.SizeOf(); + private readonly int _sizeOfEntry = Unsafe.SizeOf(); public RomFsDictionary(IStorage bucketStorage, IStorage entryStorage) { @@ -68,13 +68,12 @@ namespace LibHac.IO.RomFs public ref T GetValueReference(int offset) { - ref RomFsEntry entry = ref MemoryMarshal.Cast(Entries.AsSpan(offset))[0]; - return ref entry.Value; + return ref Unsafe.As(ref Entries[offset]).Value; } public ref T GetValueReference(int offset, out Span name) { - ref RomFsEntry entry = ref MemoryMarshal.Cast(Entries.AsSpan(offset))[0]; + ref RomFsEntry entry = ref Unsafe.As(ref Entries[offset]); name = Entries.AsSpan(offset + _sizeOfEntry, entry.KeyLength); return ref entry.Value; @@ -249,7 +248,7 @@ namespace LibHac.IO.RomFs private ref RomFsEntry GetEntryReference(int offset, out Span name) { - ref RomFsEntry entry = ref MemoryMarshal.Cast(Entries.AsSpan(offset))[0]; + ref RomFsEntry entry = ref Unsafe.As(ref Entries[offset]); name = Entries.AsSpan(offset + _sizeOfEntry, entry.KeyLength); return ref entry; @@ -257,7 +256,7 @@ namespace LibHac.IO.RomFs private ref RomFsEntry GetEntryReference(int offset, out Span name, int nameLength) { - ref RomFsEntry entry = ref MemoryMarshal.Cast(Entries.AsSpan(offset))[0]; + ref RomFsEntry entry = ref Unsafe.As(ref Entries[offset]); name = Entries.AsSpan(offset + _sizeOfEntry, nameLength); return ref entry; diff --git a/src/LibHac/LibHac.csproj b/src/LibHac/LibHac.csproj index 29038b06..8f04d210 100644 --- a/src/LibHac/LibHac.csproj +++ b/src/LibHac/LibHac.csproj @@ -35,8 +35,12 @@ - + + + + +