DEV9: Deduplicate sparse file assert code

This commit is contained in:
TheLastRar 2023-06-23 12:35:36 +01:00 committed by refractionpcsx2
parent cd4d829f9f
commit cb224789e2
2 changed files with 43 additions and 51 deletions

View File

@ -222,6 +222,9 @@ private:
bool IO_SparseZero(u64 byteOffset, u64 byteSize);
void IO_SparseCacheUpdateLocation(u64 Offset);
void IO_SparseCacheLoad();
#if defined(PCSX2_DEBUG) || defined(PCSX2_DEVBUILD)
void IO_SparseCacheAssertFileZeros(u64 hddSparseBlockSizeReadable);
#endif
bool IsAllZero(const void* data, size_t len);
void HDD_ReadAsync(void (ATA::*drqCMD)());
void HDD_ReadSync(void (ATA::*drqCMD)());

View File

@ -214,7 +214,6 @@ void ATA::IO_SparseCacheLoad()
// Store file pointer.
const s64 orgPos = FileSystem::FTell64(hddImage);
pxAssert(orgPos != -1);
#ifdef _WIN32
// FlushFileBuffers is required, hddSparseBlock differs from actual file without it.
@ -235,31 +234,7 @@ void ATA::IO_SparseCacheLoad()
memset(hddSparseBlock.get(), 0, hddSparseBlockSize);
hddSparseBlockValid = true;
#if defined(PCSX2_DEBUG) || defined(PCSX2_DEVBUILD)
//Load into check buffer.
FileSystem::FSeek64(hddImage, HddSparseStart, SEEK_SET);
std::unique_ptr<u8[]> temp = std::make_unique<u8[]>(hddSparseBlockSize);
memset(temp.get(), 0, hddSparseBlockSize);
if (FileSystem::FSeek64(hddImage, HddSparseStart, SEEK_SET) != 0 ||
std::fread((char*)hddSparseBlock.get(), readSize, 1, hddImage) != 1)
pxAssert(false);
// Restore file pointer.
if (FileSystem::FSeek64(hddImage, orgPos, SEEK_SET) != 0)
pxAssert(false);
// Check if file is actully zeros.
if (memcmp(hddSparseBlock.get(), temp.get(), hddSparseBlockSize) != 0)
{
Console.WriteLn("DEV9: ATA: Sparse area not sparse, BlockStart: %s, BlockEnd: %s",
std::to_string(HddSparseStart).c_str(), std::to_string(HddSparseStart + hddSparseBlockSize).c_str());
}
else
Console.WriteLn("DEV9: ATA: Sparse area is sparse (Yay), BlockStart: %s, BlockEnd: %s",
std::to_string(HddSparseStart).c_str(), std::to_string(HddSparseStart + hddSparseBlockSize).c_str());
pxAssert(memcmp(hddSparseBlock.get(), temp.get(), hddSparseBlockSize) == 0);
ATA::IO_SparseCacheAssertFileZeros(readSize);
#endif
return;
}
@ -277,31 +252,7 @@ void ATA::IO_SparseCacheLoad()
memset(hddSparseBlock.get(), 0, hddSparseBlockSize);
hddSparseBlockValid = true;
#if defined(PCSX2_DEBUG) || defined(PCSX2_DEVBUILD)
// Load into check buffer.
FileSystem::FSeek64(hddImage, HddSparseStart, SEEK_SET);
std::unique_ptr<u8[]> temp = std::make_unique<u8[]>(hddSparseBlockSize);
memset(temp.get(), 0, hddSparseBlockSize);
if (FileSystem::FSeek64(hddImage, HddSparseStart, SEEK_SET) != 0 ||
std::fread((char*)hddSparseBlock.get(), readSize, 1, hddImage) != 1)
pxAssert(false);
// Restore file pointer.
if (FileSystem::FSeek64(hddImage, orgPos, SEEK_SET) != 0)
pxAssert(false);
// Check if file is actully zeros.
if (memcmp(hddSparseBlock.get(), temp.get(), hddSparseBlockSize) != 0)
{
Console.WriteLn("DEV9: ATA: Sparse area not sparse, BlockStart: %s, BlockEnd: %s",
std::to_string(HddSparseStart).c_str(), std::to_string(HddSparseStart + hddSparseBlockSize).c_str());
}
else
Console.WriteLn("DEV9: ATA: Sparse area is sparse (Yay), BlockStart: %s, BlockEnd: %s",
std::to_string(HddSparseStart).c_str(), std::to_string(HddSparseStart + hddSparseBlockSize).c_str());
pxAssert(memcmp(hddSparseBlock.get(), temp.get(), hddSparseBlockSize) == 0);
ATA::IO_AssertSparseFileZeros(readSize);
#endif
return;
}
@ -323,6 +274,44 @@ void ATA::IO_SparseCacheLoad()
hddSparseBlockValid = true;
}
#if defined(PCSX2_DEBUG) || defined(PCSX2_DEVBUILD)
// Asserts that the region of file indicated by HddSparseStart & hddSparseBlockSizeReadable is all zeros
// Used by IO_SparseCacheLoad to ensure the sparse/allocated apis and FileSystem apis are in sync
void ATA::IO_SparseCacheAssertFileZeros(u64 hddSparseBlockSizeReadable)
{
const s64 orgPos = FileSystem::FTell64(hddImage);
pxAssert(orgPos != -1);
// Load into check buffer.
FileSystem::FSeek64(hddImage, HddSparseStart, SEEK_SET);
std::unique_ptr<u8[]> temp = std::make_unique<u8[]>(hddSparseBlockSize);
memset(temp.get(), 0, hddSparseBlockSize);
if (FileSystem::FSeek64(hddImage, HddSparseStart, SEEK_SET) != 0 ||
std::fread((char*)hddSparseBlock.get(), hddSparseBlockSizeReadable, 1, hddImage) != 1)
pxAssert(false);
// Restore file pointer.
if (FileSystem::FSeek64(hddImage, orgPos, SEEK_SET) != 0)
pxAssert(false);
bool regionIsZeros = memcmp(hddSparseBlock.get(), temp.get(), hddSparseBlockSize) == 0;
// Check if file is actully zeros.
if (!regionIsZeros)
{
Console.WriteLn("DEV9: ATA: Sparse area not sparse, BlockStart: %s, BlockEnd: %s",
std::to_string(HddSparseStart).c_str(), std::to_string(HddSparseStart + hddSparseBlockSize).c_str());
}
else
Console.WriteLn("DEV9: ATA: Sparse area is sparse (Yay), BlockStart: %s, BlockEnd: %s",
std::to_string(HddSparseStart).c_str(), std::to_string(HddSparseStart + hddSparseBlockSize).c_str());
pxAssert(regionIsZeros);
}
#endif
void ATA::IO_SparseCacheUpdateLocation(u64 byteOffset)
{
const u64 currentBlockStart = (byteOffset / hddSparseBlockSize) * hddSparseBlockSize;