Use "in" instead of "ref" where appropriate in SharedRef<T>

This commit is contained in:
Alex Barney 2021-11-01 16:02:00 -07:00
parent 770cd222f1
commit f9f31056ef
26 changed files with 105 additions and 99 deletions

View File

@ -59,7 +59,7 @@ namespace LibHac.Bcat
{
Debug.Assert((uint)type < ServiceTypeCount);
return SharedRef<IServiceCreator>.CreateCopy(ref _serviceCreators[(int)type]);
return SharedRef<IServiceCreator>.CreateCopy(in _serviceCreators[(int)type]);
}
}

View File

@ -20,7 +20,7 @@ namespace LibHac.Bcat.Impl.Ipc
public Result GetServiceObject(ref SharedRef<IDisposable> serviceObject)
{
serviceObject.SetByCopy(ref _serviceCreator);
serviceObject.SetByCopy(in _serviceCreator);
return Result.Success;
}
}

View File

@ -81,7 +81,10 @@ namespace LibHac.Boot
private const int ModernStage1Size = 0x7000;
private const int MarikoWarmBootPlainTextSectionSize = 0x330;
private IStorage BaseStorage { get; set; }
private SharedRef<IStorage> _baseStorage;
private SubStorage _pk11Storage;
private SubStorage _bodyStorage;
private KeySet KeySet { get; set; }
public bool IsModern { get; private set; }
@ -94,8 +97,6 @@ namespace LibHac.Boot
public byte KeyRevision { get; private set; }
public int Pk11Size { get; private set; }
private IStorage Pk11Storage { get; set; }
private IStorage BodyStorage { get; set; }
private Package1MarikoOemHeader _marikoOemHeader;
private Package1MetaData _metaData;
@ -109,13 +110,13 @@ namespace LibHac.Boot
public ref readonly Package1Pk11Header Pk11Header => ref _pk11Header;
public ref readonly Buffer16 Pk11Mac => ref _pk11Mac;
public Result Initialize(KeySet keySet, IStorage storage)
public Result Initialize(KeySet keySet, in SharedRef<IStorage> storage)
{
KeySet = keySet;
BaseStorage = storage;
_baseStorage.SetByCopy(in storage);
// Read what might be a mariko header and check if it actually is a mariko header
Result rc = BaseStorage.Read(0, SpanHelpers.AsByteSpan(ref _marikoOemHeader));
Result rc = _baseStorage.Get.Read(0, SpanHelpers.AsByteSpan(ref _marikoOemHeader));
if (rc.IsFailure()) return rc;
IsMariko = IsMarikoImpl();
@ -127,8 +128,11 @@ namespace LibHac.Boot
}
else
{
BodyStorage = BaseStorage;
rc = BodyStorage.Read(0, SpanHelpers.AsByteSpan(ref _metaData));
rc = _baseStorage.Get.GetSize(out long baseStorageSize);
if (rc.IsFailure()) return rc.Miss();
_bodyStorage = new SubStorage(in _baseStorage, 0, baseStorageSize);
rc = _bodyStorage.Read(0, SpanHelpers.AsByteSpan(ref _metaData));
if (rc.IsFailure()) return rc;
}
@ -169,7 +173,7 @@ namespace LibHac.Boot
return ResultLibHac.InvalidPackage1MarikoBodySize.Log();
// Verify the body storage size is not smaller than the size in the header
Result rc = BaseStorage.GetSize(out long totalSize);
Result rc = _baseStorage.Get.GetSize(out long totalSize);
if (rc.IsFailure()) return rc;
long bodySize = totalSize - Unsafe.SizeOf<Package1MarikoOemHeader>();
@ -177,7 +181,7 @@ namespace LibHac.Boot
return ResultLibHac.InvalidPackage1MarikoBodySize.Log();
// Create body SubStorage and metadata buffers
var bodySubStorage = new SubStorage(BaseStorage, Unsafe.SizeOf<Package1MarikoOemHeader>(), bodySize);
var bodySubStorage = new SubStorage(in _baseStorage, Unsafe.SizeOf<Package1MarikoOemHeader>(), bodySize);
Span<Package1MetaData> metaData = stackalloc Package1MetaData[2];
Span<byte> metaData1 = SpanHelpers.AsByteSpan(ref metaData[0]);
@ -189,7 +193,7 @@ namespace LibHac.Boot
// Set the body storage and decrypted metadata
_metaData = metaData[0];
BodyStorage = bodySubStorage;
_bodyStorage = bodySubStorage;
// The plaintext metadata is followed by an encrypted copy
// If these two match then the body is already decrypted
@ -208,7 +212,8 @@ namespace LibHac.Boot
if (IsDecrypted)
{
var decStorage = new AesCbcStorage(bodySubStorage, KeySet.MarikoBek, _metaData.Iv, true);
BodyStorage = new CachedStorage(decStorage, 0x4000, 1, true);
var cachedStorage = new CachedStorage(decStorage, 0x4000, 1, true);
_bodyStorage = new SubStorage(cachedStorage, 0, bodySize);
}
return Result.Success;
@ -236,7 +241,7 @@ namespace LibHac.Boot
// Read the package1ldr footer
int footerOffset = stage1Size - Unsafe.SizeOf<Package1Stage1Footer>();
Result rc = BodyStorage.Read(footerOffset, SpanHelpers.AsByteSpan(ref _stage1Footer));
Result rc = _bodyStorage.Read(footerOffset, SpanHelpers.AsByteSpan(ref _stage1Footer));
if (rc.IsFailure()) return rc;
// Get the PK11 size from the field in the unencrypted stage 1 footer
@ -249,12 +254,12 @@ namespace LibHac.Boot
{
int pk11Offset = IsModern ? ModernStage1Size : LegacyStage1Size;
return BodyStorage.Read(pk11Offset, SpanHelpers.AsByteSpan(ref _pk11Header));
return _bodyStorage.Read(pk11Offset, SpanHelpers.AsByteSpan(ref _pk11Header));
}
private Result ReadModernEristaMac()
{
return BaseStorage.Read(ModernStage1Size + Pk11Size, _pk11Mac.Bytes);
return _baseStorage.Get.Read(ModernStage1Size + Pk11Size, _pk11Mac.Bytes);
}
private Result SetPk11Storage()
@ -262,7 +267,7 @@ namespace LibHac.Boot
// Read the PK11 header from the body storage
int pk11Offset = IsModern ? ModernStage1Size : LegacyStage1Size;
Result rc = BodyStorage.Read(pk11Offset, SpanHelpers.AsByteSpan(ref _pk11Header));
Result rc = _bodyStorage.Read(pk11Offset, SpanHelpers.AsByteSpan(ref _pk11Header));
if (rc.IsFailure()) return rc;
// Check if PK11 is already decrypted, creating the PK11 storage if it is
@ -270,11 +275,11 @@ namespace LibHac.Boot
if (IsDecrypted)
{
Pk11Storage = new SubStorage(BodyStorage, pk11Offset, Pk11Size);
_pk11Storage = new SubStorage(_bodyStorage, pk11Offset, Pk11Size);
return Result.Success;
}
var encPk11Storage = new SubStorage(BodyStorage, pk11Offset, Pk11Size);
var encPk11Storage = new SubStorage(_bodyStorage, pk11Offset, Pk11Size);
// See if we have an Erista package1 key that can decrypt this PK11
if (!IsMariko && TryFindEristaKeyRevision())
@ -293,13 +298,13 @@ namespace LibHac.Boot
KeySet.Package1Keys[KeyRevision].DataRo.ToArray(), _stage1Footer.Iv.ToArray(), true);
}
Pk11Storage = new CachedStorage(decPk11Storage, 0x4000, 1, true);
_pk11Storage = new SubStorage(new CachedStorage(decPk11Storage, 0x4000, 1, true), 0, Pk11Size);
return Result.Success;
}
// We can't decrypt the PK11. Set Pk11Storage to the encrypted PK11 storage
Pk11Storage = encPk11Storage;
_pk11Storage = encPk11Storage;
return Result.Success;
}
@ -375,15 +380,15 @@ namespace LibHac.Boot
// The metadata at the start of the body is unencrypted, so don't take its data from the decrypted
// body storage
storages.Add(new SubStorage(BaseStorage, 0, Unsafe.SizeOf<Package1MarikoOemHeader>() + metaSize));
storages.Add(new SubStorage(BodyStorage, metaSize, _marikoOemHeader.Size - metaSize));
storages.Add(new SubStorage(in _baseStorage, 0, Unsafe.SizeOf<Package1MarikoOemHeader>() + metaSize));
storages.Add(new SubStorage(_bodyStorage, metaSize, _marikoOemHeader.Size - metaSize));
}
else
{
int stage1Size = IsModern ? ModernStage1Size : LegacyStage1Size;
storages.Add(new SubStorage(BaseStorage, 0, stage1Size));
storages.Add(Pk11Storage);
storages.Add(new SubStorage(in _baseStorage, 0, stage1Size));
storages.Add(_pk11Storage);
if (IsModern)
{
@ -429,7 +434,7 @@ namespace LibHac.Boot
int offset = Unsafe.SizeOf<Package1Pk11Header>() + GetSectionOffset(sectionType);
int size = GetSectionSize(sectionType);
return new SubStorage(Pk11Storage, offset, size);
return new SubStorage(_pk11Storage, offset, size);
}

View File

@ -156,11 +156,11 @@ namespace LibHac.Common
return sharedRef;
}
public static SharedRef<T> CreateCopy<TFrom>(ref SharedRef<TFrom> other) where TFrom : class, T
public static SharedRef<T> CreateCopy<TFrom>(in SharedRef<TFrom> other) where TFrom : class, T
{
var sharedRef = new SharedRef<T>();
sharedRef._value = Unsafe.As<TFrom, T>(ref other._value);
sharedRef._value = Unsafe.As<TFrom, T>(ref other.Ref()._value);
sharedRef._refCount = other._refCount;
sharedRef._refCount?.Increment();
@ -168,9 +168,9 @@ namespace LibHac.Common
return sharedRef;
}
public static SharedRef<T> Create<TFrom>(ref WeakRef<TFrom> other) where TFrom : class, T
public static SharedRef<T> Create<TFrom>(in WeakRef<TFrom> other) where TFrom : class, T
{
ref SharedRef<TFrom> otherShared = ref Unsafe.As<WeakRef<TFrom>, SharedRef<TFrom>>(ref other);
ref SharedRef<TFrom> otherShared = ref Unsafe.As<WeakRef<TFrom>, SharedRef<TFrom>>(ref other.Ref());
var sharedRef = new SharedRef<T>();
@ -244,14 +244,14 @@ namespace LibHac.Common
oldRefCount?.Decrement();
}
public void SetByCopy<TFrom>(ref SharedRef<TFrom> other) where TFrom : class, T
public void SetByCopy<TFrom>(in SharedRef<TFrom> other) where TFrom : class, T
{
RefCount oldRefCount = _refCount;
RefCount otherRef = other._refCount;
otherRef?.Increment();
_value = Unsafe.As<TFrom, T>(ref other._value);
_value = Unsafe.As<TFrom, T>(ref other.Ref()._value);
_refCount = otherRef;
oldRefCount?.Decrement();
@ -309,9 +309,9 @@ namespace LibHac.Common
private T _value;
private RefCount _refCount;
public WeakRef(ref SharedRef<T> other)
public WeakRef(in SharedRef<T> other)
{
this = Create(ref other);
this = Create(in other);
}
[Obsolete("This method should never be manually called. Use the Destroy method instead.", true)]
@ -350,7 +350,7 @@ namespace LibHac.Common
return weakRef;
}
public static WeakRef<T> CreateCopy<TFrom>(ref WeakRef<TFrom> other) where TFrom : class, T
public static WeakRef<T> CreateCopy<TFrom>(in WeakRef<TFrom> other) where TFrom : class, T
{
var weakRef = new WeakRef<T>();
@ -361,7 +361,7 @@ namespace LibHac.Common
if (weakRef._refCount.IncrementIfNotZero())
{
weakRef._value = Unsafe.As<TFrom, T>(ref other._value);
weakRef._value = Unsafe.As<TFrom, T>(ref other.Ref()._value);
weakRef._refCount.Decrement();
}
}
@ -369,9 +369,9 @@ namespace LibHac.Common
return weakRef;
}
public static WeakRef<T> Create<TFrom>(ref SharedRef<TFrom> other) where TFrom : class, T
public static WeakRef<T> Create<TFrom>(in SharedRef<TFrom> other) where TFrom : class, T
{
ref WeakRef<TFrom> otherWeak = ref Unsafe.As<SharedRef<TFrom>, WeakRef<TFrom>>(ref other);
ref WeakRef<TFrom> otherWeak = ref Unsafe.As<SharedRef<TFrom>, WeakRef<TFrom>>(ref other.Ref());
var weakRef = new WeakRef<T>();
@ -411,16 +411,16 @@ namespace LibHac.Common
temp.DisposeInternal();
}
public void SetCopy<TFrom>(ref WeakRef<TFrom> other) where TFrom : class, T
public void SetCopy<TFrom>(in WeakRef<TFrom> other) where TFrom : class, T
{
WeakRef<T> temp = CreateCopy(ref other);
WeakRef<T> temp = CreateCopy(in other);
Swap(ref temp);
temp.DisposeInternal();
}
public void Set<TFrom>(ref SharedRef<TFrom> other) where TFrom : class, T
public void Set<TFrom>(in SharedRef<TFrom> other) where TFrom : class, T
{
WeakRef<T> temp = Create(ref other);
WeakRef<T> temp = Create(in other);
Swap(ref temp);
temp.DisposeInternal();
}

View File

@ -4,6 +4,7 @@ using LibHac.FsSrv.Sf;
namespace LibHac.Fs.Shim
{
[NonCopyable]
internal struct FileSystemProxyServiceObjectGlobals : IDisposable
{
public nint FileSystemProxyServiceObjectInitGuard;
@ -46,7 +47,7 @@ namespace LibHac.Fs.Shim
g.FileSystemProxyServiceObject.SetByMove(ref createdObject.Ref());
}
return SharedRef<IFileSystemProxy>.CreateCopy(ref g.FileSystemProxyServiceObject);
return SharedRef<IFileSystemProxy>.CreateCopy(in g.FileSystemProxyServiceObject);
}
private static SharedRef<IFileSystemProxy> GetFileSystemProxyServiceObjectImpl(FileSystemClientImpl fs)
@ -56,7 +57,7 @@ namespace LibHac.Fs.Shim
if (dfcServiceObject.HasValue)
{
return SharedRef<IFileSystemProxy>.CreateCopy(ref dfcServiceObject);
return SharedRef<IFileSystemProxy>.CreateCopy(in dfcServiceObject);
}
using var fileSystemProxy = new SharedRef<IFileSystemProxy>();
@ -84,7 +85,7 @@ namespace LibHac.Fs.Shim
g.FileSystemProxyForLoaderServiceObject.SetByMove(ref createdObject.Ref());
}
return SharedRef<IFileSystemProxyForLoader>.CreateCopy(ref g.FileSystemProxyForLoaderServiceObject);
return SharedRef<IFileSystemProxyForLoader>.CreateCopy(in g.FileSystemProxyForLoaderServiceObject);
}
private static SharedRef<IFileSystemProxyForLoader> GetFileSystemProxyForLoaderServiceObjectImpl(
@ -114,7 +115,7 @@ namespace LibHac.Fs.Shim
g.ProgramRegistryServiceObject.SetByMove(ref createdObject.Ref());
}
return SharedRef<IProgramRegistry>.CreateCopy(ref g.ProgramRegistryServiceObject);
return SharedRef<IProgramRegistry>.CreateCopy(in g.ProgramRegistryServiceObject);
}
private static SharedRef<IProgramRegistry> GetProgramRegistryServiceObjectImpl(FileSystemClientImpl fs)

View File

@ -305,7 +305,7 @@ namespace LibHac.Fs.Impl
public SharedRef<IFileSystemSf> GetFileSystem()
{
return SharedRef<IFileSystemSf>.CreateCopy(ref _baseFs);
return SharedRef<IFileSystemSf>.CreateCopy(in _baseFs);
}
public SharedRef<IFileSystemSf> GetMultiCommitTarget()

View File

@ -49,7 +49,7 @@ namespace LibHac.Fs
_offset = other._offset;
_size = other._size;
_isResizable = other._isResizable;
_sharedBaseStorage = SharedRef<IStorage>.CreateCopy(ref other._sharedBaseStorage);
_sharedBaseStorage = SharedRef<IStorage>.CreateCopy(in other._sharedBaseStorage);
}
/// <summary>
@ -90,7 +90,7 @@ namespace LibHac.Fs
_offset = other._offset + offset;
_size = size;
_isResizable = false;
_sharedBaseStorage = SharedRef<IStorage>.CreateCopy(ref other._sharedBaseStorage);
_sharedBaseStorage = SharedRef<IStorage>.CreateCopy(in other._sharedBaseStorage);
Assert.SdkRequiresLessEqual(0, offset);
Assert.SdkRequiresLessEqual(0, size);
@ -105,13 +105,13 @@ namespace LibHac.Fs
/// <param name="baseStorage">The base <see cref="IStorage"/>.</param>
/// <param name="offset">The offset in the base storage at which to begin 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(in SharedRef<IStorage> baseStorage, long offset, long size)
{
BaseStorage = baseStorage.Get;
_offset = offset;
_size = size;
_isResizable = false;
_sharedBaseStorage = SharedRef<IStorage>.CreateCopy(ref baseStorage);
_sharedBaseStorage = SharedRef<IStorage>.CreateCopy(in baseStorage);
Assert.SdkRequiresNotNull(baseStorage.Get);
Assert.SdkRequiresLessEqual(0, _offset);
@ -137,7 +137,7 @@ namespace LibHac.Fs
_offset = other._offset;
_size = other._size;
_isResizable = other._isResizable;
_sharedBaseStorage.SetByCopy(ref other._sharedBaseStorage);
_sharedBaseStorage.SetByCopy(in other._sharedBaseStorage);
}
}

View File

@ -236,7 +236,7 @@ namespace LibHac.FsSrv
internal Result OpenDeviceOperator(ref SharedRef<IDeviceOperator> outDeviceOperator,
AccessControl accessControl)
{
outDeviceOperator.SetByCopy(ref _deviceOperator);
outDeviceOperator.SetByCopy(in _deviceOperator);
return Result.Success;
}
}

View File

@ -24,7 +24,7 @@ namespace LibHac.FsSrv
using var sharedRootFileSystem = new SharedRef<IFileSystem>(rootFileSystem);
using SharedRef<IFileSystem> sharedRootFileSystemCopy =
SharedRef<IFileSystem>.CreateCopy(ref sharedRootFileSystem.Ref());
SharedRef<IFileSystem>.CreateCopy(in sharedRootFileSystem);
creators.RomFileSystemCreator = new RomFileSystemCreator();
creators.PartitionFileSystemCreator = new PartitionFileSystemCreator();

View File

@ -50,7 +50,7 @@ namespace LibHac.FsSrv.FsCreator
public bool TryGetRootFileSystem(ref SharedRef<IFileSystem> outFileSystem)
{
outFileSystem.SetByCopy(ref _rootFileSystem);
outFileSystem.SetByCopy(in _rootFileSystem);
return outFileSystem.HasValue;
}
@ -60,7 +60,7 @@ namespace LibHac.FsSrv.FsCreator
if (!IsValidPartitionId(partitionId))
return false;
outFileSystem.SetByCopy(ref PartitionFileSystems[GetArrayIndex(partitionId)]);
outFileSystem.SetByCopy(in PartitionFileSystems[GetArrayIndex(partitionId)]);
return outFileSystem.HasValue;
}

View File

@ -25,7 +25,7 @@ namespace LibHac.FsSrv.FsCreator
Result rc = GameCard.GetCardInfo(out GameCardInfo cardInfo, handle);
if (rc.IsFailure()) return rc;
outStorage.Reset(new SubStorage(ref baseStorage.Ref(), 0, cardInfo.SecureAreaOffset));
outStorage.Reset(new SubStorage(in baseStorage, 0, cardInfo.SecureAreaOffset));
return Result.Success;
}
@ -51,7 +51,7 @@ namespace LibHac.FsSrv.FsCreator
rc = GameCard.GetCardInfo(out GameCardInfo cardInfo, handle);
if (rc.IsFailure()) return rc;
outStorage.Reset(new SubStorage(ref baseStorage.Ref(), cardInfo.SecureAreaOffset, cardInfo.SecureAreaSize));
outStorage.Reset(new SubStorage(in baseStorage, cardInfo.SecureAreaOffset, cardInfo.SecureAreaSize));
return Result.Success;
}

View File

@ -44,7 +44,7 @@ namespace LibHac.FsSrv.FsCreator
if (_sdCardFileSystem.HasValue)
{
outFileSystem.SetByCopy(ref _sdCardFileSystem);
outFileSystem.SetByCopy(in _sdCardFileSystem);
return Result.Success;
}
@ -67,11 +67,11 @@ namespace LibHac.FsSrv.FsCreator
// Todo: Add ProxyFileSystem?
using SharedRef<IFileSystem> fileSystem = SharedRef<IFileSystem>.CreateCopy(ref _rootFileSystem);
using SharedRef<IFileSystem> fileSystem = SharedRef<IFileSystem>.CreateCopy(in _rootFileSystem);
rc = Utility.WrapSubDirectory(ref _sdCardFileSystem, ref fileSystem.Ref(), in sdCardPath, true);
if (rc.IsFailure()) return rc;
outFileSystem.SetByCopy(ref _sdCardFileSystem);
outFileSystem.SetByCopy(in _sdCardFileSystem);
return Result.Success;
}

View File

@ -83,8 +83,8 @@ namespace LibHac.FsSrv.FsCreator
isMultiCommitSupported, !openReadOnly);
if (rc.IsFailure()) return rc;
outFileSystem.SetByCopy(ref saveDirFs.Ref());
outExtraDataAccessor.SetByCopy(ref saveDirFs.Ref());
outFileSystem.SetByCopy(in saveDirFs);
outExtraDataAccessor.SetByCopy(in saveDirFs);
return Result.Success;
}

View File

@ -23,7 +23,7 @@ namespace LibHac.FsSrv.Impl
using var retryFileSystem = new SharedRef<DeepRetryFileSystem>(
new DeepRetryFileSystem(ref baseFileSystem, ref accessFailureManager));
retryFileSystem.Get._selfReference = new WeakRef<DeepRetryFileSystem>(ref retryFileSystem.Ref());
retryFileSystem.Get._selfReference.Set(in retryFileSystem.Ref());
return SharedRef<IFileSystem>.CreateMove(ref retryFileSystem.Ref());
}

View File

@ -268,7 +268,7 @@ namespace LibHac.FsSrv.Impl
var adapter = new FileSystemInterfaceAdapter(ref baseFileSystem, allowAllOperations);
using var sharedAdapter = new SharedRef<FileSystemInterfaceAdapter>(adapter);
adapter._selfReference.Set(ref sharedAdapter.Ref());
adapter._selfReference.Set(in sharedAdapter);
return SharedRef<IFileSystemSf>.CreateMove(ref sharedAdapter.Ref());
}
@ -279,7 +279,7 @@ namespace LibHac.FsSrv.Impl
var adapter = new FileSystemInterfaceAdapter(ref baseFileSystem, flags, allowAllOperations);
using var sharedAdapter = new SharedRef<FileSystemInterfaceAdapter>(adapter);
adapter._selfReference.Set(ref sharedAdapter.Ref());
adapter._selfReference.Set(in sharedAdapter);
return SharedRef<IFileSystemSf>.CreateMove(ref sharedAdapter.Ref());
}
@ -498,7 +498,7 @@ namespace LibHac.FsSrv.Impl
if (rc.IsFailure()) return rc;
using SharedRef<FileSystemInterfaceAdapter> selfReference =
SharedRef<FileSystemInterfaceAdapter>.Create(ref _selfReference);
SharedRef<FileSystemInterfaceAdapter>.Create(in _selfReference);
var adapter = new FileInterfaceAdapter(ref file.Ref(), ref selfReference.Ref(), _allowAllOperations);
outFile.Reset(adapter);
@ -529,7 +529,7 @@ namespace LibHac.FsSrv.Impl
if (rc.IsFailure()) return rc;
using SharedRef<FileSystemInterfaceAdapter> selfReference =
SharedRef<FileSystemInterfaceAdapter>.Create(ref _selfReference);
SharedRef<FileSystemInterfaceAdapter>.Create(in _selfReference);
var adapter = new DirectoryInterfaceAdapter(ref directory.Ref(), ref selfReference.Ref());
outDirectory.Reset(adapter);
@ -590,7 +590,7 @@ namespace LibHac.FsSrv.Impl
public Result GetImpl(ref SharedRef<IFileSystem> fileSystem)
{
fileSystem.SetByCopy(ref _baseFileSystem);
fileSystem.SetByCopy(in _baseFileSystem);
return Result.Success;
}
}

View File

@ -12,16 +12,17 @@ namespace LibHac.FsSrv.Impl
/// </summary>
public class SaveDataExtraDataAccessorCacheManager : ISaveDataExtraDataAccessorCacheObserver
{
[NonCopyable]
private struct Cache : IDisposable
{
private WeakRef<ISaveDataExtraDataAccessor> _accessor;
private readonly SaveDataSpaceId _spaceId;
private readonly ulong _saveDataId;
public Cache(ref SharedRef<ISaveDataExtraDataAccessor> accessor, SaveDataSpaceId spaceId,
public Cache(in SharedRef<ISaveDataExtraDataAccessor> accessor, SaveDataSpaceId spaceId,
ulong saveDataId)
{
_accessor = new WeakRef<ISaveDataExtraDataAccessor>(ref accessor);
_accessor = new WeakRef<ISaveDataExtraDataAccessor>(in accessor);
_spaceId = spaceId;
_saveDataId = saveDataId;
}
@ -69,10 +70,10 @@ namespace LibHac.FsSrv.Impl
_accessorList.Clear();
}
public Result Register(ref SharedRef<ISaveDataExtraDataAccessor> accessor, SaveDataSpaceId spaceId,
public Result Register(in SharedRef<ISaveDataExtraDataAccessor> accessor, SaveDataSpaceId spaceId,
ulong saveDataId)
{
var node = new LinkedListNode<Cache>(new Cache(ref accessor, spaceId, saveDataId));
var node = new LinkedListNode<Cache>(new Cache(in accessor, spaceId, saveDataId));
using (ScopedLock.Lock(ref _mutex))
{

View File

@ -43,7 +43,7 @@ namespace LibHac.FsSrv
// Wrap the service in a ref-counter and give the service a weak self-reference
using var sharedService = new SharedRef<NcaFileSystemService>(ncaService);
ncaService._selfReference = new WeakRef<NcaFileSystemService>(ref sharedService.Ref());
ncaService._selfReference.Set(in sharedService);
return SharedRef<NcaFileSystemService>.CreateMove(ref sharedService.Ref());
}
@ -154,7 +154,7 @@ namespace LibHac.FsSrv
new SharedRef<IFileSystem>(new AsynchronousAccessFileSystem(ref typeSetFileSystem.Ref()));
using SharedRef<IRomFileSystemAccessFailureManager> accessFailureManager =
SharedRef<IRomFileSystemAccessFailureManager>.Create(ref _selfReference);
SharedRef<IRomFileSystemAccessFailureManager>.Create(in _selfReference);
using SharedRef<IFileSystem> retryFileSystem =
DeepRetryFileSystem.CreateShared(ref asyncFileSystem.Ref(), ref accessFailureManager.Ref());

View File

@ -145,7 +145,7 @@ namespace LibHac.FsSrv
}
using var nspFileSystem = new SharedRef<IFileSystem>();
using SharedRef<IFileSystem> tempFileSystem = SharedRef<IFileSystem>.CreateCopy(ref baseFileSystem.Ref());
using SharedRef<IFileSystem> tempFileSystem = SharedRef<IFileSystem>.CreateCopy(in baseFileSystem);
rc = ParseNsp(ref currentPath, ref nspFileSystem.Ref(), ref baseFileSystem.Ref());
if (rc.IsSuccess())

View File

@ -44,10 +44,10 @@ namespace LibHac.FsSrv
private HorizonClient Hos => _serviceImpl.Hos;
private SharedRef<SaveDataFileSystemService> GetSharedFromThis() =>
SharedRef<SaveDataFileSystemService>.Create(ref _selfReference);
SharedRef<SaveDataFileSystemService>.Create(in _selfReference);
private SharedRef<ISaveDataMultiCommitCoreInterface> GetSharedMultiCommitInterfaceFromThis() =>
SharedRef<ISaveDataMultiCommitCoreInterface>.Create(ref _selfReference);
SharedRef<ISaveDataMultiCommitCoreInterface>.Create(in _selfReference);
public SaveDataFileSystemService(SaveDataFileSystemServiceImpl serviceImpl, ulong processId)
{
@ -64,7 +64,7 @@ namespace LibHac.FsSrv
// Wrap the service in a ref-counter and give the service a weak self-reference
using var sharedService = new SharedRef<SaveDataFileSystemService>(saveService);
saveService._selfReference = WeakRef<SaveDataFileSystemService>.Create(ref sharedService.Ref());
saveService._selfReference.Set(in sharedService);
return SharedRef<SaveDataFileSystemService>.CreateMove(ref sharedService.Ref());
}
@ -1088,7 +1088,7 @@ namespace LibHac.FsSrv
Result rc = TryAcquireSaveDataMountCountSemaphore(ref mountCountSemaphore.Ref());
if (rc.IsFailure()) return rc;
Path saveDataRootPath = _saveDataRootPath.DangerousGetPath();
using Path saveDataRootPath = _saveDataRootPath.DangerousGetPath();
bool useAsyncFileSystem = !_serviceImpl.IsAllowedDirectorySaveData(spaceId, in saveDataRootPath);
using var fileSystem = new SharedRef<IFileSystem>();
@ -1103,7 +1103,7 @@ namespace LibHac.FsSrv
Result ReadExtraData(out SaveDataExtraData data)
{
Path savePath = _saveDataRootPath.DangerousGetPath();
using Path savePath = _saveDataRootPath.DangerousGetPath();
return _serviceImpl.ReadSaveDataFileSystemExtraData(out data, spaceId, saveDataId, type,
in savePath);
}
@ -1278,7 +1278,7 @@ namespace LibHac.FsSrv
rc = accessor.Get.Indexer.GetKey(out SaveDataAttribute key, saveDataId);
if (rc.IsFailure()) return rc;
Path saveDataRootPath = _saveDataRootPath.DangerousGetPath();
using Path saveDataRootPath = _saveDataRootPath.DangerousGetPath();
return _serviceImpl.ReadSaveDataFileSystemExtraData(out extraData, spaceId, saveDataId, key.Type,
in saveDataRootPath);
}
@ -1444,7 +1444,7 @@ namespace LibHac.FsSrv
{
using var scopedContext = new ScopedStorageLayoutTypeSetter(StorageType.NonGameCard);
Path saveDataRootPath = _saveDataRootPath.DangerousGetPath();
using Path saveDataRootPath = _saveDataRootPath.DangerousGetPath();
return _serviceImpl.WriteSaveDataFileSystemExtraData(spaceId, saveDataId, in extraData, in saveDataRootPath,
saveType, updateTimeStamp);
}
@ -1472,7 +1472,7 @@ namespace LibHac.FsSrv
ReadExtraData);
if (rc.IsFailure()) return rc;
Path saveDataRootPath = _saveDataRootPath.DangerousGetPath();
using Path saveDataRootPath = _saveDataRootPath.DangerousGetPath();
rc = _serviceImpl.ReadSaveDataFileSystemExtraData(out SaveDataExtraData extraDataModify, spaceId,
saveDataId, key.Type, in saveDataRootPath);
if (rc.IsFailure()) return rc;
@ -1893,7 +1893,7 @@ namespace LibHac.FsSrv
Result rc = FindCacheStorage(out SaveDataInfo saveInfo, out SaveDataSpaceId spaceId, index);
if (rc.IsFailure()) return rc;
Path saveDataRootPath = _saveDataRootPath.DangerousGetPath();
using Path saveDataRootPath = _saveDataRootPath.DangerousGetPath();
rc = _serviceImpl.ReadSaveDataFileSystemExtraData(out SaveDataExtraData extraData, spaceId,
saveInfo.SaveDataId, saveInfo.Type, in saveDataRootPath);
if (rc.IsFailure()) return rc;

View File

@ -175,7 +175,7 @@ namespace LibHac.FsSrv
{
extraDataAccessor.Get.RegisterCacheObserver(_extraDataCacheManager, spaceId, saveDataId);
rc = _extraDataCacheManager.Register(ref extraDataAccessor.Ref(), spaceId, saveDataId);
rc = _extraDataCacheManager.Register(in extraDataAccessor, spaceId, saveDataId);
if (rc.IsFailure()) return rc.Miss();
}
}

View File

@ -521,7 +521,7 @@ namespace LibHac.FsSrv
rc = RegisterReader(ref reader.Ref());
if (rc.IsFailure()) return rc;
outInfoReader.SetByCopy(ref reader.Ref());
outInfoReader.SetByCopy(in reader.Ref());
return Result.Success;
}
}
@ -785,7 +785,7 @@ namespace LibHac.FsSrv
public ReaderAccessor(ref SharedRef<Reader> reader)
{
_reader = new WeakRef<Reader>(ref reader);
_reader = new WeakRef<Reader>(in reader);
}
public void Dispose() => _reader.Destroy();

View File

@ -271,6 +271,7 @@ namespace LibHac.FsSrv
/// <remarks>Based on FS 12.1.0 (nnSdk 12.3.1).</remarks>
public class SaveDataFileStorageHolder
{
[NonCopyable]
private struct Entry
{
private SharedRef<SaveDataOpenTypeSetFileStorage> _storage;
@ -297,7 +298,7 @@ namespace LibHac.FsSrv
public SharedRef<SaveDataOpenTypeSetFileStorage> GetStorage()
{
return SharedRef<SaveDataOpenTypeSetFileStorage>.CreateCopy(ref _storage);
return SharedRef<SaveDataOpenTypeSetFileStorage>.CreateCopy(in _storage);
}
}
@ -370,7 +371,7 @@ namespace LibHac.FsSrv
if (rc.IsFailure()) return rc;
using SharedRef<SaveDataOpenTypeSetFileStorage> baseFileStorageCopy =
SharedRef<SaveDataOpenTypeSetFileStorage>.CreateCopy(ref baseFileStorage.Ref());
SharedRef<SaveDataOpenTypeSetFileStorage>.CreateCopy(in baseFileStorage);
rc = Register(ref baseFileStorageCopy.Ref(), spaceId, saveDataId);
if (rc.IsFailure()) return rc;

View File

@ -7,6 +7,7 @@ namespace LibHac.FsSystem
{
public class SaveDataFileSystemCacheManager : ISaveDataFileSystemCacheManager
{
[NonCopyable]
private struct Cache
{
// Note: Nintendo only supports caching SaveDataFileSystem. We support DirectorySaveDataFileSystem too,

View File

@ -62,7 +62,7 @@ namespace LibHac.FsSystem
using (var sharedStorage = new SharedRef<IStorage>(storage))
{
return new SubStorage(ref sharedStorage.Ref(), start, length);
return new SubStorage(in sharedStorage, start, length);
}
}

View File

@ -15,10 +15,10 @@ namespace hactoolnet
{
public static void ProcessPk11(Context ctx)
{
using (var file = new LocalStorage(ctx.Options.InFile, FileAccess.Read))
using (var file = new SharedRef<IStorage>(new LocalStorage(ctx.Options.InFile, FileAccess.Read)))
{
var package1 = new LibHac.Boot.Package1();
package1.Initialize(ctx.KeySet, file).ThrowIfFailure();
package1.Initialize(ctx.KeySet, in file).ThrowIfFailure();
ctx.Logger.LogMessage(package1.Print());

View File

@ -443,11 +443,8 @@ namespace LibHac.Tests.Fs
rc = fileSystem.OpenFile(ref file.Ref(), path, OpenMode.ReadWrite);
if (rc.IsFailure()) return rc;
using (file)
{
rc = file.Get.Write(0, SpanHelpers.AsByteSpan(ref extraData), WriteOption.Flush);
if (rc.IsFailure()) return rc;
}
rc = file.Get.Write(0, SpanHelpers.AsByteSpan(ref extraData), WriteOption.Flush);
if (rc.IsFailure()) return rc;
return Result.Success;
}