From 833619b4441915c7c55085d44b3221eaef0d9800 Mon Sep 17 00:00:00 2001 From: tsepez Date: Wed, 7 Dec 2016 09:21:17 -0800 Subject: Refcount all the IFX_ stream classes all the time. We can remove a lot of "bOwnsStream" logic in the process. Always pass these by const reference, in case the called method wants to hang on to the stream (one exception is where we stick a raw pointer into a void* slot in a context from another layer). Review-Url: https://codereview.chromium.org/2451493002 --- core/fxcrt/fx_basic.h | 7 ++-- core/fxcrt/fx_basic_buffer.cpp | 5 +-- core/fxcrt/fx_ext.h | 6 ---- core/fxcrt/fx_extension.cpp | 72 ++++++++++++++---------------------------- core/fxcrt/fx_stream.h | 39 +++++++++++------------ core/fxcrt/fx_xml.h | 9 +++--- core/fxcrt/fx_xml_parser.cpp | 70 ++++++++++++++++++---------------------- core/fxcrt/xml_int.h | 9 +++--- 8 files changed, 86 insertions(+), 131 deletions(-) (limited to 'core/fxcrt') diff --git a/core/fxcrt/fx_basic.h b/core/fxcrt/fx_basic.h index 9034398983..2f581b0d5e 100644 --- a/core/fxcrt/fx_basic.h +++ b/core/fxcrt/fx_basic.h @@ -10,6 +10,7 @@ #include #include +#include "core/fxcrt/cfx_retain_ptr.h" #include "core/fxcrt/fx_memory.h" #include "core/fxcrt/fx_stream.h" #include "core/fxcrt/fx_string.h" @@ -119,16 +120,14 @@ class CFX_FileBufferArchive { int32_t AppendByte(uint8_t byte); int32_t AppendDWord(uint32_t i); int32_t AppendString(const CFX_ByteStringC& lpsz); - - // |pFile| must outlive the CFX_FileBufferArchive. - void AttachFile(IFX_WriteStream* pFile); + void AttachFile(const CFX_RetainPtr& pFile); private: static const size_t kBufSize = 32768; size_t m_Length; std::unique_ptr m_pBuffer; - IFX_WriteStream* m_pFile; + CFX_RetainPtr m_pFile; }; class CFX_CharMap { diff --git a/core/fxcrt/fx_basic_buffer.cpp b/core/fxcrt/fx_basic_buffer.cpp index 8466e88c56..e6d05528ae 100644 --- a/core/fxcrt/fx_basic_buffer.cpp +++ b/core/fxcrt/fx_basic_buffer.cpp @@ -234,7 +234,7 @@ CFX_FileBufferArchive::~CFX_FileBufferArchive() {} void CFX_FileBufferArchive::Clear() { m_Length = 0; m_pBuffer.reset(); - m_pFile = nullptr; + m_pFile.Reset(); } bool CFX_FileBufferArchive::Flush() { @@ -285,7 +285,8 @@ int32_t CFX_FileBufferArchive::AppendString(const CFX_ByteStringC& lpsz) { return AppendBlock(lpsz.raw_str(), lpsz.GetLength()); } -void CFX_FileBufferArchive::AttachFile(IFX_WriteStream* pFile) { +void CFX_FileBufferArchive::AttachFile( + const CFX_RetainPtr& pFile) { ASSERT(pFile); m_pFile = pFile; } diff --git a/core/fxcrt/fx_ext.h b/core/fxcrt/fx_ext.h index 7d8529e333..737ea849c9 100644 --- a/core/fxcrt/fx_ext.h +++ b/core/fxcrt/fx_ext.h @@ -15,12 +15,6 @@ #define FX_INVALID_OFFSET static_cast(-1) -// TODO(thestig) Using unique_ptr with ReleaseDeleter is still not ideal. -// Come up or wait for something better. This appears in this file rather -// than fx_stream.h due to include ordering restrictions. -using ScopedFileStream = - std::unique_ptr>; - FX_FLOAT FXSYS_tan(FX_FLOAT a); FX_FLOAT FXSYS_logb(FX_FLOAT b, FX_FLOAT x); FX_FLOAT FXSYS_strtof(const FX_CHAR* pcsStr, diff --git a/core/fxcrt/fx_extension.cpp b/core/fxcrt/fx_extension.cpp index 1bb9a3a5f6..cbbb86f26b 100644 --- a/core/fxcrt/fx_extension.cpp +++ b/core/fxcrt/fx_extension.cpp @@ -32,7 +32,7 @@ class CFX_CRTFileAccess : public IFX_FileAccess { void Release() override; IFX_FileAccess* Retain() override; void GetPath(CFX_WideString& wsPath) override; - IFX_SeekableStream* CreateFileStream(uint32_t dwModes) override; + CFX_RetainPtr CreateFileStream(uint32_t dwModes) override; bool Init(const CFX_WideStringC& wsPath); @@ -59,7 +59,8 @@ void CFX_CRTFileAccess::GetPath(CFX_WideString& wsPath) { wsPath = m_path; } -IFX_SeekableStream* CFX_CRTFileAccess::CreateFileStream(uint32_t dwModes) { +CFX_RetainPtr CFX_CRTFileAccess::CreateFileStream( + uint32_t dwModes) { return IFX_SeekableStream::CreateFromFilename(m_path.c_str(), dwModes); } @@ -77,8 +78,6 @@ class CFX_CRTFileStream final : public IFX_SeekableStream { ~CFX_CRTFileStream() override; // IFX_SeekableStream: - IFX_SeekableStream* Retain() override; - void Release() override; FX_FILESIZE GetSize() override; bool IsEOF() override; FX_FILESIZE GetPosition() override; @@ -89,23 +88,12 @@ class CFX_CRTFileStream final : public IFX_SeekableStream { private: std::unique_ptr m_pFile; - uint32_t m_dwCount; }; CFX_CRTFileStream::CFX_CRTFileStream(std::unique_ptr pFA) - : m_pFile(std::move(pFA)), m_dwCount(1) {} + : m_pFile(std::move(pFA)) {} CFX_CRTFileStream::~CFX_CRTFileStream() {} -IFX_SeekableStream* CFX_CRTFileStream::Retain() { - m_dwCount++; - return this; -} - -void CFX_CRTFileStream::Release() { - uint32_t nCount = --m_dwCount; - if (!nCount) - delete this; -} FX_FILESIZE CFX_CRTFileStream::GetSize() { return m_pFile->GetSize(); @@ -150,8 +138,6 @@ class CFX_MemoryStream final : public IFX_MemoryStream { ~CFX_MemoryStream() override; // IFX_MemoryStream - IFX_SeekableStream* Retain() override; - void Release() override; FX_FILESIZE GetSize() override; bool IsEOF() override; FX_FILESIZE GetPosition() override; @@ -168,19 +154,18 @@ class CFX_MemoryStream final : public IFX_MemoryStream { void DetachBuffer() override; private: + bool ExpandBlocks(size_t size); + CFX_ArrayTemplate m_Blocks; - uint32_t m_dwCount; size_t m_nTotalSize; size_t m_nCurSize; size_t m_nCurPos; size_t m_nGrowSize; uint32_t m_dwFlags; - bool ExpandBlocks(size_t size); }; CFX_MemoryStream::CFX_MemoryStream(bool bConsecutive) - : m_dwCount(1), - m_nTotalSize(0), + : m_nTotalSize(0), m_nCurSize(0), m_nCurPos(0), m_nGrowSize(FX_MEMSTREAM_BlockSize) { @@ -191,8 +176,7 @@ CFX_MemoryStream::CFX_MemoryStream(bool bConsecutive) CFX_MemoryStream::CFX_MemoryStream(uint8_t* pBuffer, size_t nSize, bool bTakeOver) - : m_dwCount(1), - m_nTotalSize(nSize), + : m_nTotalSize(nSize), m_nCurSize(nSize), m_nCurPos(0), m_nGrowSize(FX_MEMSTREAM_BlockSize) { @@ -210,19 +194,6 @@ CFX_MemoryStream::~CFX_MemoryStream() { m_Blocks.RemoveAll(); } -IFX_SeekableStream* CFX_MemoryStream::Retain() { - m_dwCount++; - return this; -} - -void CFX_MemoryStream::Release() { - uint32_t nCount = --m_dwCount; - if (nCount) { - return; - } - delete this; -} - FX_FILESIZE CFX_MemoryStream::GetSize() { return (FX_FILESIZE)m_nCurSize; } @@ -419,41 +390,44 @@ IFX_FileAccess* IFX_FileAccess::CreateDefault(const CFX_WideStringC& wsPath) { #endif // PDF_ENABLE_XFA // static -IFX_SeekableStream* IFX_SeekableStream::CreateFromFilename( +CFX_RetainPtr IFX_SeekableStream::CreateFromFilename( const FX_CHAR* filename, uint32_t dwModes) { std::unique_ptr pFA(IFXCRT_FileAccess::Create()); if (!pFA->Open(filename, dwModes)) return nullptr; - return new CFX_CRTFileStream(std::move(pFA)); + return CFX_RetainPtr( + new CFX_CRTFileStream(std::move(pFA))); } // static -IFX_SeekableStream* IFX_SeekableStream::CreateFromFilename( +CFX_RetainPtr IFX_SeekableStream::CreateFromFilename( const FX_WCHAR* filename, uint32_t dwModes) { std::unique_ptr pFA(IFXCRT_FileAccess::Create()); if (!pFA->Open(filename, dwModes)) return nullptr; - return new CFX_CRTFileStream(std::move(pFA)); + return CFX_RetainPtr( + new CFX_CRTFileStream(std::move(pFA))); } // static -IFX_SeekableReadStream* IFX_SeekableReadStream::CreateFromFilename( - const FX_CHAR* filename) { +CFX_RetainPtr +IFX_SeekableReadStream::CreateFromFilename(const FX_CHAR* filename) { return IFX_SeekableStream::CreateFromFilename(filename, FX_FILEMODE_ReadOnly); } // static -IFX_MemoryStream* IFX_MemoryStream::Create(uint8_t* pBuffer, - size_t dwSize, - bool bTakeOver) { - return new CFX_MemoryStream(pBuffer, dwSize, bTakeOver); +CFX_RetainPtr IFX_MemoryStream::Create(uint8_t* pBuffer, + size_t dwSize, + bool bTakeOver) { + return CFX_RetainPtr( + new CFX_MemoryStream(pBuffer, dwSize, bTakeOver)); } // static -IFX_MemoryStream* IFX_MemoryStream::Create(bool bConsecutive) { - return new CFX_MemoryStream(bConsecutive); +CFX_RetainPtr IFX_MemoryStream::Create(bool bConsecutive) { + return CFX_RetainPtr(new CFX_MemoryStream(bConsecutive)); } FX_FLOAT FXSYS_tan(FX_FLOAT a) { diff --git a/core/fxcrt/fx_stream.h b/core/fxcrt/fx_stream.h index 711b66472b..b998761742 100644 --- a/core/fxcrt/fx_stream.h +++ b/core/fxcrt/fx_stream.h @@ -7,6 +7,7 @@ #ifndef CORE_FXCRT_FX_STREAM_H_ #define CORE_FXCRT_FX_STREAM_H_ +#include "core/fxcrt/cfx_retain_ptr.h" #include "core/fxcrt/fx_string.h" #include "core/fxcrt/fx_system.h" @@ -48,18 +49,13 @@ FX_WCHAR FX_GetFolderSeparator(); #define FX_FILEMODE_ReadOnly 1 #define FX_FILEMODE_Truncate 2 -class IFX_WriteStream { +class IFX_WriteStream : virtual public CFX_Retainable { public: - virtual ~IFX_WriteStream() {} - virtual void Release() = 0; virtual bool WriteBlock(const void* pData, size_t size) = 0; }; -class IFX_ReadStream { +class IFX_ReadStream : virtual public CFX_Retainable { public: - virtual ~IFX_ReadStream() {} - - virtual void Release() = 0; virtual bool IsEOF() = 0; virtual FX_FILESIZE GetPosition() = 0; virtual size_t ReadBlock(void* buffer, size_t size) = 0; @@ -69,6 +65,7 @@ class IFX_SeekableWriteStream : public IFX_WriteStream { public: // IFX_WriteStream: bool WriteBlock(const void* pData, size_t size) override; + virtual FX_FILESIZE GetSize() = 0; virtual bool Flush() = 0; virtual bool WriteBlock(const void* pData, @@ -78,10 +75,10 @@ class IFX_SeekableWriteStream : public IFX_WriteStream { class IFX_SeekableReadStream : public IFX_ReadStream { public: - static IFX_SeekableReadStream* CreateFromFilename(const FX_CHAR* filename); + static CFX_RetainPtr CreateFromFilename( + const FX_CHAR* filename); // IFX_ReadStream: - void Release() override = 0; bool IsEOF() override; FX_FILESIZE GetPosition() override; size_t ReadBlock(void* buffer, size_t size) override; @@ -93,15 +90,15 @@ class IFX_SeekableReadStream : public IFX_ReadStream { class IFX_SeekableStream : public IFX_SeekableReadStream, public IFX_SeekableWriteStream { public: - static IFX_SeekableStream* CreateFromFilename(const FX_CHAR* filename, - uint32_t dwModes); - static IFX_SeekableStream* CreateFromFilename(const FX_WCHAR* filename, - uint32_t dwModes); + static CFX_RetainPtr CreateFromFilename( + const FX_CHAR* filename, + uint32_t dwModes); - virtual IFX_SeekableStream* Retain() = 0; + static CFX_RetainPtr CreateFromFilename( + const FX_WCHAR* filename, + uint32_t dwModes); // IFX_SeekableReadStream: - void Release() override = 0; bool IsEOF() override = 0; FX_FILESIZE GetPosition() override = 0; size_t ReadBlock(void* buffer, size_t size) override = 0; @@ -118,10 +115,10 @@ class IFX_SeekableStream : public IFX_SeekableReadStream, class IFX_MemoryStream : public IFX_SeekableStream { public: - static IFX_MemoryStream* Create(uint8_t* pBuffer, - size_t nSize, - bool bTakeOver = false); - static IFX_MemoryStream* Create(bool bConsecutive = false); + static CFX_RetainPtr Create(uint8_t* pBuffer, + size_t nSize, + bool bTakeOver = false); + static CFX_RetainPtr Create(bool bConsecutive = false); virtual bool IsConsecutive() const = 0; virtual void EstimateSize(size_t nInitSize, size_t nGrowSize) = 0; @@ -135,7 +132,6 @@ class IFX_MemoryStream : public IFX_SeekableStream { class IFX_BufferedReadStream : public IFX_ReadStream { public: // IFX_ReadStream: - void Release() override = 0; bool IsEOF() override = 0; FX_FILESIZE GetPosition() override = 0; size_t ReadBlock(void* buffer, size_t size) override = 0; @@ -155,7 +151,8 @@ class IFX_FileAccess { virtual void Release() = 0; virtual IFX_FileAccess* Retain() = 0; virtual void GetPath(CFX_WideString& wsPath) = 0; - virtual IFX_SeekableStream* CreateFileStream(uint32_t dwModes) = 0; + virtual CFX_RetainPtr CreateFileStream( + uint32_t dwModes) = 0; }; #endif // PDF_ENABLE_XFA diff --git a/core/fxcrt/fx_xml.h b/core/fxcrt/fx_xml.h index 03337d0142..7f42a7fa9b 100644 --- a/core/fxcrt/fx_xml.h +++ b/core/fxcrt/fx_xml.h @@ -58,12 +58,13 @@ class CXML_Element { size_t size, bool bSaveSpaceChars = false, FX_FILESIZE* pParsedSize = nullptr); - static CXML_Element* Parse(IFX_SeekableReadStream* pFile, - bool bSaveSpaceChars = false, - FX_FILESIZE* pParsedSize = nullptr); - static CXML_Element* Parse(IFX_BufferedReadStream* pBuffer, + static CXML_Element* Parse(const CFX_RetainPtr& pFile, bool bSaveSpaceChars = false, FX_FILESIZE* pParsedSize = nullptr); + static CXML_Element* Parse( + const CFX_RetainPtr& pBuffer, + bool bSaveSpaceChars = false, + FX_FILESIZE* pParsedSize = nullptr); CXML_Element(const CFX_ByteStringC& qSpace, const CFX_ByteStringC& tagName); explicit CXML_Element(const CFX_ByteStringC& qTagName); diff --git a/core/fxcrt/fx_xml_parser.cpp b/core/fxcrt/fx_xml_parser.cpp index 803ba597e5..ab30926387 100644 --- a/core/fxcrt/fx_xml_parser.cpp +++ b/core/fxcrt/fx_xml_parser.cpp @@ -74,7 +74,6 @@ class CXML_DataBufAcc : public IFX_BufferedReadStream { ~CXML_DataBufAcc() override; // IFX_BufferedReadStream - void Release() override; bool IsEOF() override; FX_FILESIZE GetPosition() override; size_t ReadBlock(void* buffer, size_t size) override; @@ -94,10 +93,6 @@ CXML_DataBufAcc::CXML_DataBufAcc(const uint8_t* pBuffer, size_t size) CXML_DataBufAcc::~CXML_DataBufAcc() {} -void CXML_DataBufAcc::Release() { - delete this; -} - bool CXML_DataBufAcc::IsEOF() { return m_dwCurPos >= m_dwSize; } @@ -135,11 +130,11 @@ FX_FILESIZE CXML_DataBufAcc::GetBlockOffset() { class CXML_DataStmAcc : public IFX_BufferedReadStream { public: - explicit CXML_DataStmAcc(IFX_SeekableReadStream* pFileRead); + explicit CXML_DataStmAcc( + const CFX_RetainPtr& pFileRead); ~CXML_DataStmAcc() override; // IFX_BufferedReadStream - void Release() override; bool IsEOF() override; FX_FILESIZE GetPosition() override; size_t ReadBlock(void* buffer, size_t size) override; @@ -149,13 +144,14 @@ class CXML_DataStmAcc : public IFX_BufferedReadStream { FX_FILESIZE GetBlockOffset() override; private: - IFX_SeekableReadStream* m_pFileRead; + CFX_RetainPtr m_pFileRead; uint8_t* m_pBuffer; FX_FILESIZE m_nStart; size_t m_dwSize; }; -CXML_DataStmAcc::CXML_DataStmAcc(IFX_SeekableReadStream* pFileRead) +CXML_DataStmAcc::CXML_DataStmAcc( + const CFX_RetainPtr& pFileRead) : m_pFileRead(pFileRead), m_pBuffer(nullptr), m_nStart(0), m_dwSize(0) { ASSERT(m_pFileRead); } @@ -164,10 +160,6 @@ CXML_DataStmAcc::~CXML_DataStmAcc() { FX_Free(m_pBuffer); } -void CXML_DataStmAcc::Release() { - delete this; -} - bool CXML_DataStmAcc::IsEOF() { return m_nStart + (FX_FILESIZE)m_dwSize >= m_pFileRead->GetSize(); } @@ -213,41 +205,34 @@ FX_FILESIZE CXML_DataStmAcc::GetBlockOffset() { } // namespace CXML_Parser::CXML_Parser() - : m_pDataAcc(nullptr), - m_bOwnedStream(false), - m_nOffset(0), + : m_nOffset(0), m_bSaveSpaceChars(false), m_pBuffer(nullptr), m_dwBufferSize(0), m_nBufferOffset(0), m_dwIndex(0) {} -CXML_Parser::~CXML_Parser() { - if (m_bOwnedStream) { - m_pDataAcc->Release(); - } -} +CXML_Parser::~CXML_Parser() {} bool CXML_Parser::Init(uint8_t* pBuffer, size_t size) { - m_pDataAcc = new CXML_DataBufAcc(pBuffer, size); - return Init(true); + m_pDataAcc.Reset(new CXML_DataBufAcc(pBuffer, size)); + return Init(); } -bool CXML_Parser::Init(IFX_SeekableReadStream* pFileRead) { - m_pDataAcc = new CXML_DataStmAcc(pFileRead); - return Init(true); +bool CXML_Parser::Init(const CFX_RetainPtr& pFileRead) { + m_pDataAcc.Reset(new CXML_DataStmAcc(pFileRead)); + return Init(); } -bool CXML_Parser::Init(IFX_BufferedReadStream* pBuffer) { +bool CXML_Parser::Init(const CFX_RetainPtr& pBuffer) { if (!pBuffer) return false; m_pDataAcc = pBuffer; - return Init(false); + return Init(); } -bool CXML_Parser::Init(bool bOwndedStream) { - m_bOwnedStream = bOwndedStream; +bool CXML_Parser::Init() { m_nOffset = 0; return ReadNextBlock(); } @@ -701,24 +686,29 @@ CXML_Element* CXML_Element::Parse(const void* pBuffer, } return XML_ContinueParse(parser, bSaveSpaceChars, pParsedSize); } -CXML_Element* CXML_Element::Parse(IFX_SeekableReadStream* pFile, - bool bSaveSpaceChars, - FX_FILESIZE* pParsedSize) { + +CXML_Element* CXML_Element::Parse( + const CFX_RetainPtr& pFile, + bool bSaveSpaceChars, + FX_FILESIZE* pParsedSize) { CXML_Parser parser; - if (!parser.Init(pFile)) { + if (!parser.Init(pFile)) return nullptr; - } + return XML_ContinueParse(parser, bSaveSpaceChars, pParsedSize); } -CXML_Element* CXML_Element::Parse(IFX_BufferedReadStream* pBuffer, - bool bSaveSpaceChars, - FX_FILESIZE* pParsedSize) { + +CXML_Element* CXML_Element::Parse( + const CFX_RetainPtr& pBuffer, + bool bSaveSpaceChars, + FX_FILESIZE* pParsedSize) { CXML_Parser parser; - if (!parser.Init(pBuffer)) { + if (!parser.Init(pBuffer)) return nullptr; - } + return XML_ContinueParse(parser, bSaveSpaceChars, pParsedSize); } + CXML_Element::CXML_Element() : m_QSpaceName(), m_TagName(), m_AttrMap() {} CXML_Element::CXML_Element(const CFX_ByteStringC& qSpace, const CFX_ByteStringC& tagName) diff --git a/core/fxcrt/xml_int.h b/core/fxcrt/xml_int.h index f5beaef9ae..e617a777f3 100644 --- a/core/fxcrt/xml_int.h +++ b/core/fxcrt/xml_int.h @@ -20,9 +20,9 @@ class CXML_Parser { ~CXML_Parser(); bool Init(uint8_t* pBuffer, size_t size); - bool Init(IFX_SeekableReadStream* pFileRead); - bool Init(IFX_BufferedReadStream* pBuffer); - bool Init(bool bOwndedStream); + bool Init(const CFX_RetainPtr& pFileRead); + bool Init(const CFX_RetainPtr& pBuffer); + bool Init(); bool ReadNextBlock(); bool IsEOF(); bool HaveAvailData(); @@ -41,8 +41,7 @@ class CXML_Parser { CXML_Element* pElement); void InsertCDATASegment(CFX_UTF8Decoder& decoder, CXML_Element* pElement); - IFX_BufferedReadStream* m_pDataAcc; - bool m_bOwnedStream; + CFX_RetainPtr m_pDataAcc; FX_FILESIZE m_nOffset; bool m_bSaveSpaceChars; const uint8_t* m_pBuffer; -- cgit v1.2.3