From b9236b973a11f481e1847fbd185e96ef077e9511 Mon Sep 17 00:00:00 2001 From: Alex Barney Date: Fri, 10 May 2019 14:49:23 -0500 Subject: [PATCH] Fix bug in CachedStorage that would drop writes If block 0 of the storage was written to before the cache was filled and then another block was accessed, any writes to block 0 would be lost. --- src/LibHac/IO/CachedStorage.cs | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/src/LibHac/IO/CachedStorage.cs b/src/LibHac/IO/CachedStorage.cs index 33376251..e2c42ea2 100644 --- a/src/LibHac/IO/CachedStorage.cs +++ b/src/LibHac/IO/CachedStorage.cs @@ -1,5 +1,4 @@ using System; -using System.Buffers; using System.Collections.Generic; namespace LibHac.IO @@ -23,15 +22,14 @@ namespace LibHac.IO for (int i = 0; i < cacheSize; i++) { - // todo why is this rented? - var block = new CacheBlock { Buffer = ArrayPool.Shared.Rent(blockSize) }; + var block = new CacheBlock { Buffer = new byte[blockSize], Index = -1 }; Blocks.AddLast(block); } } public CachedStorage(SectorStorage baseStorage, int cacheSize, bool leaveOpen) : this(baseStorage, baseStorage.SectorSize, cacheSize, leaveOpen) { } - + protected override void ReadImpl(Span destination, long offset) { long remaining = destination.Length; @@ -124,7 +122,11 @@ namespace LibHac.IO CacheBlock block = node.Value; Blocks.RemoveLast(); - BlockDict.Remove(block.Index); + + if (block.Index != -1) + { + BlockDict.Remove(block.Index); + } FlushBlock(block); ReadBlock(block, blockIndex);