Fix a new SubStorage bug and add NonCopyable attributes

This commit is contained in:
Alex Barney 2021-10-28 13:26:49 -07:00
parent 1f14a1c360
commit f07e515048
7 changed files with 22 additions and 6 deletions

View file

@ -0,0 +1,10 @@
using System;
namespace LibHac.Common
{
[AttributeUsage(AttributeTargets.Struct)]
public sealed class NonCopyableAttribute : System.Attribute { }
[AttributeUsage(AttributeTargets.Struct)]
public sealed class NonCopyableDisposableAttribute : System.Attribute { }
}

View file

@ -4,6 +4,8 @@ using System.Threading;
using InlineIL; using InlineIL;
using LibHac.Diag; using LibHac.Diag;
#pragma warning disable LH0001, LH0002, LH0003, LH0004, LH0005
namespace LibHac.Common namespace LibHac.Common
{ {
public static class SharedRefExtensions public static class SharedRefExtensions
@ -105,6 +107,7 @@ namespace LibHac.Common
} }
} }
[NonCopyableDisposable]
public struct SharedRef<T> : IDisposable where T : class, IDisposable public struct SharedRef<T> : IDisposable where T : class, IDisposable
{ {
// SharedRef and WeakRef should share a base type, but struct inheritance doesn't exist in C#. // SharedRef and WeakRef should share a base type, but struct inheritance doesn't exist in C#.
@ -300,6 +303,7 @@ namespace LibHac.Common
} }
} }
[NonCopyableDisposable]
public struct WeakRef<T> : IDisposable where T : class, IDisposable public struct WeakRef<T> : IDisposable where T : class, IDisposable
{ {
private T _value; private T _value;

View file

@ -15,6 +15,7 @@ namespace LibHac.Common
} }
} }
[NonCopyableDisposable]
public struct UniqueRef<T> : IDisposable where T : class, IDisposable public struct UniqueRef<T> : IDisposable where T : class, IDisposable
{ {
private T _value; private T _value;

View file

@ -83,6 +83,7 @@ namespace LibHac.Fs
/// is normalized before passing it to <see cref="SetShallowBuffer"/>.</para> /// is normalized before passing it to <see cref="SetShallowBuffer"/>.</para>
/// <para>Based on FS 12.1.0 (nnSdk 12.3.1)</para></remarks> /// <para>Based on FS 12.1.0 (nnSdk 12.3.1)</para></remarks>
[DebuggerDisplay("{" + nameof(ToString) + "(),nq}")] [DebuggerDisplay("{" + nameof(ToString) + "(),nq}")]
[NonCopyableDisposable]
public ref struct Path public ref struct Path
{ {
/// <summary> /// <summary>

View file

@ -107,7 +107,7 @@ namespace LibHac.Fs
/// <param name="size">The size of the created SubStorage.</param> /// <param name="size">The size of the created SubStorage.</param>
public SubStorage(ref SharedRef<IStorage> baseStorage, long offset, long size) public SubStorage(ref SharedRef<IStorage> baseStorage, long offset, long size)
{ {
BaseStorage = _sharedBaseStorage.Get; BaseStorage = baseStorage.Get;
_offset = offset; _offset = offset;
_size = size; _size = size;
_isResizable = false; _isResizable = false;

View file

@ -268,7 +268,7 @@ namespace LibHac.FsSrv.Impl
var adapter = new FileSystemInterfaceAdapter(ref baseFileSystem, allowAllOperations); var adapter = new FileSystemInterfaceAdapter(ref baseFileSystem, allowAllOperations);
using var sharedAdapter = new SharedRef<FileSystemInterfaceAdapter>(adapter); using var sharedAdapter = new SharedRef<FileSystemInterfaceAdapter>(adapter);
adapter._selfReference = new WeakRef<FileSystemInterfaceAdapter>(ref sharedAdapter.Ref()); adapter._selfReference.Set(ref sharedAdapter.Ref());
return SharedRef<IFileSystemSf>.CreateMove(ref sharedAdapter.Ref()); return SharedRef<IFileSystemSf>.CreateMove(ref sharedAdapter.Ref());
} }
@ -279,7 +279,7 @@ namespace LibHac.FsSrv.Impl
var adapter = new FileSystemInterfaceAdapter(ref baseFileSystem, flags, allowAllOperations); var adapter = new FileSystemInterfaceAdapter(ref baseFileSystem, flags, allowAllOperations);
using var sharedAdapter = new SharedRef<FileSystemInterfaceAdapter>(adapter); using var sharedAdapter = new SharedRef<FileSystemInterfaceAdapter>(adapter);
adapter._selfReference = new WeakRef<FileSystemInterfaceAdapter>(ref sharedAdapter.Ref()); adapter._selfReference.Set(ref sharedAdapter.Ref());
return SharedRef<IFileSystemSf>.CreateMove(ref sharedAdapter.Ref()); return SharedRef<IFileSystemSf>.CreateMove(ref sharedAdapter.Ref());
} }

View file

@ -455,7 +455,7 @@ namespace LibHac.FsSystem.Save
public bool IsListStart() public bool IsListStart()
{ {
return Prev == int.MinValue; return Prev == unchecked((int)0x80000000);
} }
public bool IsListEnd() public bool IsListEnd()
@ -485,12 +485,12 @@ namespace LibHac.FsSystem.Save
public void MakeListStart() public void MakeListStart()
{ {
Prev = int.MinValue; Prev = unchecked((int)0x80000000);
} }
public bool IsRangeEntry() public bool IsRangeEntry()
{ {
return Prev != int.MinValue && Prev < 0; return Prev != unchecked((int)0x80000000) && Prev < 0;
} }
public void MakeRangeEntry() public void MakeRangeEntry()