From 0154a594c92a4db6b79e35d7406fe29ba2ae35d9 Mon Sep 17 00:00:00 2001 From: Connor McLaughlin Date: Thu, 18 Aug 2022 21:16:07 +1000 Subject: [PATCH] ByteStream: Devirtualize where unnecessary --- src/common/byte_stream.cpp | 55 +++++++--------- src/common/byte_stream.h | 128 ++++++++++++++++++------------------- 2 files changed, 86 insertions(+), 97 deletions(-) diff --git a/src/common/byte_stream.cpp b/src/common/byte_stream.cpp index 53ba7791b..98e8e3139 100644 --- a/src/common/byte_stream.cpp +++ b/src/common/byte_stream.cpp @@ -33,9 +33,9 @@ class FileByteStream : public ByteStream public: FileByteStream(FILE* pFile) : m_pFile(pFile) { DebugAssert(m_pFile != nullptr); } - virtual ~FileByteStream() { fclose(m_pFile); } + virtual ~FileByteStream() override { fclose(m_pFile); } - virtual bool ReadByte(u8* pDestByte) override + bool ReadByte(u8* pDestByte) override { if (m_errorState) return false; @@ -49,7 +49,7 @@ public: return true; } - virtual u32 Read(void* pDestination, u32 ByteCount) override + u32 Read(void* pDestination, u32 ByteCount) override { if (m_errorState) return 0; @@ -61,7 +61,7 @@ public: return readCount; } - virtual bool Read2(void* pDestination, u32 ByteCount, u32* pNumberOfBytesRead /* = nullptr */) override + bool Read2(void* pDestination, u32 ByteCount, u32* pNumberOfBytesRead /* = nullptr */) override { if (m_errorState) return false; @@ -80,7 +80,7 @@ public: return true; } - virtual bool WriteByte(u8 SourceByte) override + bool WriteByte(u8 SourceByte) override { if (m_errorState) return false; @@ -94,7 +94,7 @@ public: return true; } - virtual u32 Write(const void* pSource, u32 ByteCount) override + u32 Write(const void* pSource, u32 ByteCount) override { if (m_errorState) return 0; @@ -106,7 +106,7 @@ public: return writeCount; } - virtual bool Write2(const void* pSource, u32 ByteCount, u32* pNumberOfBytesWritten /* = nullptr */) override + bool Write2(const void* pSource, u32 ByteCount, u32* pNumberOfBytesWritten /* = nullptr */) override { if (m_errorState) return false; @@ -127,7 +127,7 @@ public: #if defined(_WIN32) - virtual bool SeekAbsolute(u64 Offset) override + bool SeekAbsolute(u64 Offset) override { if (m_errorState) return false; @@ -141,7 +141,7 @@ public: return true; } - virtual bool SeekRelative(s64 Offset) override + bool SeekRelative(s64 Offset) override { if (m_errorState) return false; @@ -155,7 +155,7 @@ public: return true; } - virtual bool SeekToEnd() override + bool SeekToEnd() override { if (m_errorState) return false; @@ -169,9 +169,9 @@ public: return true; } - virtual u64 GetPosition() const override { return _ftelli64(m_pFile); } + u64 GetPosition() const override { return _ftelli64(m_pFile); } - virtual u64 GetSize() const override + u64 GetSize() const override { s64 OldPos = _ftelli64(m_pFile); _fseeki64(m_pFile, 0, SEEK_END); @@ -182,7 +182,7 @@ public: #else - virtual bool SeekAbsolute(u64 Offset) override + bool SeekAbsolute(u64 Offset) override { if (m_errorState) return false; @@ -196,7 +196,7 @@ public: return true; } - virtual bool SeekRelative(s64 Offset) override + bool SeekRelative(s64 Offset) override { if (m_errorState) return false; @@ -210,7 +210,7 @@ public: return true; } - virtual bool SeekToEnd() override + bool SeekToEnd() override { if (m_errorState) return false; @@ -224,9 +224,9 @@ public: return true; } - virtual u64 GetPosition() const override { return static_cast(ftello(m_pFile)); } + u64 GetPosition() const override { return static_cast(ftello(m_pFile)); } - virtual u64 GetSize() const override + u64 GetSize() const override { off_t OldPos = ftello(m_pFile); fseeko(m_pFile, 0, SEEK_END); @@ -237,7 +237,7 @@ public: #endif - virtual bool Flush() override + bool Flush() override { if (m_errorState) return false; @@ -259,7 +259,7 @@ protected: FILE* m_pFile; }; -class AtomicUpdatedFileByteStream : public FileByteStream +class AtomicUpdatedFileByteStream final : public FileByteStream { public: AtomicUpdatedFileByteStream(FILE* pFile, std::string originalFileName, std::string temporaryFileName) @@ -268,7 +268,7 @@ public: { } - virtual ~AtomicUpdatedFileByteStream() + ~AtomicUpdatedFileByteStream() override { if (m_discarded) { @@ -304,18 +304,7 @@ public: // fclose called by FileByteStream destructor } - virtual bool Flush() override - { - if (fflush(m_pFile) != 0) - { - m_errorState = true; - return false; - } - - return true; - } - - virtual bool Commit() override + bool Commit() override { Assert(!m_discarded); if (m_committed) @@ -364,7 +353,7 @@ public: return (!m_discarded); } - virtual bool Discard() override + bool Discard() override { Assert(!m_committed); m_discarded = true; diff --git a/src/common/byte_stream.h b/src/common/byte_stream.h index b4f163101..94ffa4268 100644 --- a/src/common/byte_stream.h +++ b/src/common/byte_stream.h @@ -144,51 +144,51 @@ protected: ByteStream& operator=(const ByteStream&) = delete; }; -class NullByteStream : public ByteStream +class NullByteStream final : public ByteStream { public: NullByteStream(); - ~NullByteStream(); + ~NullByteStream() override; - virtual bool ReadByte(u8* pDestByte) override final; - virtual u32 Read(void* pDestination, u32 ByteCount) override final; - virtual bool Read2(void* pDestination, u32 ByteCount, u32* pNumberOfBytesRead /* = nullptr */) override final; - virtual bool WriteByte(u8 SourceByte) override final; - virtual u32 Write(const void* pSource, u32 ByteCount) override final; - virtual bool Write2(const void* pSource, u32 ByteCount, u32* pNumberOfBytesWritten /* = nullptr */) override final; - virtual bool SeekAbsolute(u64 Offset) override final; - virtual bool SeekRelative(s64 Offset) override final; - virtual bool SeekToEnd() override final; - virtual u64 GetSize() const override final; - virtual u64 GetPosition() const override final; - virtual bool Flush() override final; - virtual bool Commit() override final; - virtual bool Discard() override final; + bool ReadByte(u8* pDestByte) override; + u32 Read(void* pDestination, u32 ByteCount) override; + bool Read2(void* pDestination, u32 ByteCount, u32* pNumberOfBytesRead /* = nullptr */) override; + bool WriteByte(u8 SourceByte) override; + u32 Write(const void* pSource, u32 ByteCount) override; + bool Write2(const void* pSource, u32 ByteCount, u32* pNumberOfBytesWritten /* = nullptr */) override; + bool SeekAbsolute(u64 Offset) override; + bool SeekRelative(s64 Offset) override; + bool SeekToEnd() override; + u64 GetSize() const override; + u64 GetPosition() const override; + bool Flush() override; + bool Commit() override; + bool Discard() override; }; -class MemoryByteStream : public ByteStream +class MemoryByteStream final : public ByteStream { public: MemoryByteStream(void* pMemory, u32 MemSize); - virtual ~MemoryByteStream(); + ~MemoryByteStream() override; u8* GetMemoryPointer() const { return m_pMemory; } u32 GetMemorySize() const { return m_iSize; } - virtual bool ReadByte(u8* pDestByte) override; - virtual u32 Read(void* pDestination, u32 ByteCount) override; - virtual bool Read2(void* pDestination, u32 ByteCount, u32* pNumberOfBytesRead /* = nullptr */) override; - virtual bool WriteByte(u8 SourceByte) override; - virtual u32 Write(const void* pSource, u32 ByteCount) override; - virtual bool Write2(const void* pSource, u32 ByteCount, u32* pNumberOfBytesWritten /* = nullptr */) override; - virtual bool SeekAbsolute(u64 Offset) override; - virtual bool SeekRelative(s64 Offset) override; - virtual bool SeekToEnd() override; - virtual u64 GetSize() const override; - virtual u64 GetPosition() const override; - virtual bool Flush() override; - virtual bool Commit() override; - virtual bool Discard() override; + bool ReadByte(u8* pDestByte) override; + u32 Read(void* pDestination, u32 ByteCount) override; + bool Read2(void* pDestination, u32 ByteCount, u32* pNumberOfBytesRead /* = nullptr */) override; + bool WriteByte(u8 SourceByte) override; + u32 Write(const void* pSource, u32 ByteCount) override; + bool Write2(const void* pSource, u32 ByteCount, u32* pNumberOfBytesWritten /* = nullptr */) override; + bool SeekAbsolute(u64 Offset) override; + bool SeekRelative(s64 Offset) override; + bool SeekToEnd() override; + u64 GetSize() const override; + u64 GetPosition() const override; + bool Flush() override; + bool Commit() override; + bool Discard() override; private: u8* m_pMemory; @@ -196,29 +196,29 @@ private: u32 m_iSize; }; -class ReadOnlyMemoryByteStream : public ByteStream +class ReadOnlyMemoryByteStream final : public ByteStream { public: ReadOnlyMemoryByteStream(const void* pMemory, u32 MemSize); - virtual ~ReadOnlyMemoryByteStream(); + ~ReadOnlyMemoryByteStream() override; const u8* GetMemoryPointer() const { return m_pMemory; } u32 GetMemorySize() const { return m_iSize; } - virtual bool ReadByte(u8* pDestByte) override; - virtual u32 Read(void* pDestination, u32 ByteCount) override; - virtual bool Read2(void* pDestination, u32 ByteCount, u32* pNumberOfBytesRead /* = nullptr */) override; - virtual bool WriteByte(u8 SourceByte) override; - virtual u32 Write(const void* pSource, u32 ByteCount) override; - virtual bool Write2(const void* pSource, u32 ByteCount, u32* pNumberOfBytesWritten /* = nullptr */) override; - virtual bool SeekAbsolute(u64 Offset) override; - virtual bool SeekRelative(s64 Offset) override; - virtual bool SeekToEnd() override; - virtual u64 GetSize() const override; - virtual u64 GetPosition() const override; - virtual bool Flush() override; - virtual bool Commit() override; - virtual bool Discard() override; + bool ReadByte(u8* pDestByte) override; + u32 Read(void* pDestination, u32 ByteCount) override; + bool Read2(void* pDestination, u32 ByteCount, u32* pNumberOfBytesRead /* = nullptr */) override; + bool WriteByte(u8 SourceByte) override; + u32 Write(const void* pSource, u32 ByteCount) override; + bool Write2(const void* pSource, u32 ByteCount, u32* pNumberOfBytesWritten /* = nullptr */) override; + bool SeekAbsolute(u64 Offset) override; + bool SeekRelative(s64 Offset) override; + bool SeekToEnd() override; + u64 GetSize() const override; + u64 GetPosition() const override; + bool Flush() override; + bool Commit() override; + bool Discard() override; private: const u8* m_pMemory; @@ -226,11 +226,11 @@ private: u32 m_iSize; }; -class GrowableMemoryByteStream : public ByteStream +class GrowableMemoryByteStream final : public ByteStream { public: GrowableMemoryByteStream(void* pInitialMem, u32 InitialMemSize); - virtual ~GrowableMemoryByteStream(); + ~GrowableMemoryByteStream() override; u8* GetMemoryPointer() const { return m_pMemory; } u32 GetMemorySize() const { return m_iMemorySize; } @@ -240,20 +240,20 @@ public: void EnsureSpace(u32 space); void ShrinkToFit(); - virtual bool ReadByte(u8* pDestByte) override; - virtual u32 Read(void* pDestination, u32 ByteCount) override; - virtual bool Read2(void* pDestination, u32 ByteCount, u32* pNumberOfBytesRead /* = nullptr */) override; - virtual bool WriteByte(u8 SourceByte) override; - virtual u32 Write(const void* pSource, u32 ByteCount) override; - virtual bool Write2(const void* pSource, u32 ByteCount, u32* pNumberOfBytesWritten /* = nullptr */) override; - virtual bool SeekAbsolute(u64 Offset) override; - virtual bool SeekRelative(s64 Offset) override; - virtual bool SeekToEnd() override; - virtual u64 GetSize() const override; - virtual u64 GetPosition() const override; - virtual bool Flush() override; - virtual bool Commit() override; - virtual bool Discard() override; + bool ReadByte(u8* pDestByte) override; + u32 Read(void* pDestination, u32 ByteCount) override; + bool Read2(void* pDestination, u32 ByteCount, u32* pNumberOfBytesRead /* = nullptr */) override; + bool WriteByte(u8 SourceByte) override; + u32 Write(const void* pSource, u32 ByteCount) override; + bool Write2(const void* pSource, u32 ByteCount, u32* pNumberOfBytesWritten /* = nullptr */) override; + bool SeekAbsolute(u64 Offset) override; + bool SeekRelative(s64 Offset) override; + bool SeekToEnd() override; + u64 GetSize() const override; + u64 GetPosition() const override; + bool Flush() override; + bool Commit() override; + bool Discard() override; private: void Grow(u32 MinimumGrowth);