Update BufferedStorage for 13.1.0

This commit is contained in:
Alex Barney 2021-12-21 12:34:42 -07:00
parent d2d73827ba
commit c9a2056844

View file

@ -16,6 +16,7 @@ namespace LibHac.FsSystem;
/// <summary> /// <summary>
/// An <see cref="IStorage"/> that provides buffered access to a base <see cref="IStorage"/>. /// An <see cref="IStorage"/> that provides buffered access to a base <see cref="IStorage"/>.
/// </summary> /// </summary>
/// <remarks>Based on FS 13.1.0 (nnSdk 13.4.0)</remarks>
public class BufferedStorage : IStorage public class BufferedStorage : IStorage
{ {
private const long InvalidOffset = long.MaxValue; private const long InvalidOffset = long.MaxValue;
@ -673,6 +674,8 @@ public class BufferedStorage : IStorage
ref Cache end = ref Unsafe.Add(ref MemoryMarshal.GetArrayDataReference(BufferedStorage._caches), ref Cache end = ref Unsafe.Add(ref MemoryMarshal.GetArrayDataReference(BufferedStorage._caches),
BufferedStorage._cacheCount); BufferedStorage._cacheCount);
using var lk = new ScopedLock<SdkMutexType>(ref BufferedStorage._mutex);
// Validate the range. // Validate the range.
Assert.SdkAssert(!Unsafe.IsAddressLessThan(ref start, Assert.SdkAssert(!Unsafe.IsAddressLessThan(ref start,
ref MemoryMarshal.GetArrayDataReference(BufferedStorage._caches))); ref MemoryMarshal.GetArrayDataReference(BufferedStorage._caches)));
@ -716,6 +719,8 @@ public class BufferedStorage : IStorage
ref Cache end = ref Unsafe.Add(ref MemoryMarshal.GetArrayDataReference(BufferedStorage._caches), ref Cache end = ref Unsafe.Add(ref MemoryMarshal.GetArrayDataReference(BufferedStorage._caches),
BufferedStorage._cacheCount); BufferedStorage._cacheCount);
using var lk = new ScopedLock<SdkMutexType>(ref BufferedStorage._mutex);
// Validate the range. // Validate the range.
Assert.SdkAssert(!Unsafe.IsAddressLessThan(ref start, Assert.SdkAssert(!Unsafe.IsAddressLessThan(ref start,
ref MemoryMarshal.GetArrayDataReference(BufferedStorage._caches))); ref MemoryMarshal.GetArrayDataReference(BufferedStorage._caches)));
@ -1161,10 +1166,7 @@ public class BufferedStorage : IStorage
// Invalidate caches if needed. // Invalidate caches if needed.
if (operationId == OperationId.InvalidateCache) if (operationId == OperationId.InvalidateCache)
{ {
using var cache = new SharedCache(this); InvalidateCaches();
while (cache.AcquireNextOverlappedCache(offset, size))
cache.Invalidate();
} }
return _baseStorage.OperateRange(outBuffer, operationId, offset, size, inBuffer); return _baseStorage.OperateRange(outBuffer, operationId, offset, size, inBuffer);
@ -1351,13 +1353,13 @@ public class BufferedStorage : IStorage
while (true) while (true)
{ {
if (!cache.AcquireFetchableCache()) if (!cache.AcquireFetchableCache())
return ResultFs.OutOfResource.Value; return ResultFs.OutOfResource.Log();
// Try to upgrade out SharedCache to a UniqueCache // Try to upgrade out SharedCache to a UniqueCache
using var fetchCache = new UniqueCache(this); using var fetchCache = new UniqueCache(this);
(Result Result, bool wasUpgradeSuccessful) upgradeResult = fetchCache.Upgrade(in cache); (Result Result, bool wasUpgradeSuccessful) upgradeResult = fetchCache.Upgrade(in cache);
if (upgradeResult.Result.IsFailure()) if (upgradeResult.Result.IsFailure())
return upgradeResult.Result; return upgradeResult.Result.Miss();
// Fetch the data from the base storage into the cache buffer if successful // Fetch the data from the base storage into the cache buffer if successful
if (upgradeResult.wasUpgradeSuccessful) if (upgradeResult.wasUpgradeSuccessful)
@ -1676,12 +1678,12 @@ public class BufferedStorage : IStorage
while (true) while (true)
{ {
if (!cache.AcquireFetchableCache()) if (!cache.AcquireFetchableCache())
return ResultFs.OutOfResource.Value; return ResultFs.OutOfResource.Log();
using var fetchCache = new UniqueCache(this); using var fetchCache = new UniqueCache(this);
(Result Result, bool wasUpgradeSuccessful) upgradeResult = fetchCache.Upgrade(in cache); (Result Result, bool wasUpgradeSuccessful) upgradeResult = fetchCache.Upgrade(in cache);
if (upgradeResult.Result.IsFailure()) if (upgradeResult.Result.IsFailure())
return upgradeResult.Result; return upgradeResult.Result.Miss();
if (upgradeResult.wasUpgradeSuccessful) if (upgradeResult.wasUpgradeSuccessful)
{ {