From 19b70815b4d1ce20b6eb865580435a11d8b3c8d0 Mon Sep 17 00:00:00 2001 From: Lei Zhang Date: Mon, 13 Aug 2018 18:34:58 +0000 Subject: Split CFX_MemoryStream::m_dwFlags into two bools. Make them const, and remove m_nGrowSize which is always the same value. Also remove unused method and make GetBuffer() non-const. Change-Id: I272aa4c268c4a887e155800b7c8c10892b343465 Reviewed-on: https://pdfium-review.googlesource.com/39877 Reviewed-by: Tom Sepez Commit-Queue: Lei Zhang --- core/fxcrt/cfx_memorystream.cpp | 38 ++++++++++++++++++-------------------- core/fxcrt/cfx_memorystream.h | 12 ++++-------- 2 files changed, 22 insertions(+), 28 deletions(-) diff --git a/core/fxcrt/cfx_memorystream.cpp b/core/fxcrt/cfx_memorystream.cpp index 0115e37b6f..8073d375da 100644 --- a/core/fxcrt/cfx_memorystream.cpp +++ b/core/fxcrt/cfx_memorystream.cpp @@ -12,30 +12,28 @@ namespace { -const int32_t kBlockSize = 64 * 1024; +constexpr size_t kBlockSize = 64 * 1024; } // namespace CFX_MemoryStream::CFX_MemoryStream(bool bConsecutive) : m_nTotalSize(0), m_nCurSize(0), - m_nCurPos(0), - m_nGrowSize(kBlockSize), - m_dwFlags(Type::kTakeOver | (bConsecutive ? Type::kConsecutive : 0)) {} + m_bConsecutive(bConsecutive), + m_bTakeOver(true) {} CFX_MemoryStream::CFX_MemoryStream(uint8_t* pBuffer, size_t nSize, bool bTakeOver) : m_nTotalSize(nSize), m_nCurSize(nSize), - m_nCurPos(0), - m_nGrowSize(kBlockSize), - m_dwFlags(Type::kConsecutive | (bTakeOver ? Type::kTakeOver : 0)) { + m_bConsecutive(true), + m_bTakeOver(bTakeOver) { m_Blocks.push_back(pBuffer); } CFX_MemoryStream::~CFX_MemoryStream() { - if (m_dwFlags & Type::kTakeOver) { + if (m_bTakeOver) { for (uint8_t* pBlock : m_Blocks) FX_Free(pBlock); } @@ -71,15 +69,15 @@ bool CFX_MemoryStream::ReadBlock(void* buffer, } m_nCurPos = newPos.ValueOrDie(); - if (m_dwFlags & Type::kConsecutive) { + if (m_bConsecutive) { memcpy(buffer, m_Blocks[0] + static_cast(offset), size); return true; } - size_t nStartBlock = static_cast(offset) / m_nGrowSize; - offset -= static_cast(nStartBlock * m_nGrowSize); + size_t nStartBlock = static_cast(offset) / kBlockSize; + offset -= static_cast(nStartBlock * kBlockSize); while (size) { - size_t nRead = std::min(size, m_nGrowSize - static_cast(offset)); + size_t nRead = std::min(size, kBlockSize - static_cast(offset)); memcpy(buffer, m_Blocks[nStartBlock] + offset, nRead); buffer = static_cast(buffer) + nRead; size -= nRead; @@ -106,7 +104,7 @@ bool CFX_MemoryStream::WriteBlock(const void* buffer, if (!buffer || !size) return false; - if (m_dwFlags & Type::kConsecutive) { + if (m_bConsecutive) { FX_SAFE_SIZE_T newPos = size; newPos += offset; if (!newPos.IsValid()) @@ -114,7 +112,7 @@ bool CFX_MemoryStream::WriteBlock(const void* buffer, m_nCurPos = newPos.ValueOrDie(); if (m_nCurPos > m_nTotalSize) { - m_nTotalSize = (m_nCurPos + m_nGrowSize - 1) / m_nGrowSize * m_nGrowSize; + m_nTotalSize = (m_nCurPos + kBlockSize - 1) / kBlockSize * kBlockSize; if (m_Blocks.empty()) m_Blocks.push_back(FX_Alloc(uint8_t, m_nTotalSize)); else @@ -135,10 +133,10 @@ bool CFX_MemoryStream::WriteBlock(const void* buffer, return false; m_nCurPos = newPos.ValueOrDie(); - size_t nStartBlock = static_cast(offset) / m_nGrowSize; - offset -= static_cast(nStartBlock * m_nGrowSize); + size_t nStartBlock = static_cast(offset) / kBlockSize; + offset -= static_cast(nStartBlock * kBlockSize); while (size) { - size_t nWrite = std::min(size, m_nGrowSize - static_cast(offset)); + size_t nWrite = std::min(size, kBlockSize - static_cast(offset)); memcpy(m_Blocks[nStartBlock] + offset, buffer, nWrite); buffer = static_cast(buffer) + nWrite; size -= nWrite; @@ -161,12 +159,12 @@ bool CFX_MemoryStream::ExpandBlocks(size_t size) { if (size <= m_nTotalSize) return true; - size = (size - m_nTotalSize + m_nGrowSize - 1) / m_nGrowSize; + size = (size - m_nTotalSize + kBlockSize - 1) / kBlockSize; size_t iCount = m_Blocks.size(); m_Blocks.resize(iCount + size); while (size--) { - m_Blocks[iCount++] = FX_Alloc(uint8_t, m_nGrowSize); - m_nTotalSize += m_nGrowSize; + m_Blocks[iCount++] = FX_Alloc(uint8_t, kBlockSize); + m_nTotalSize += kBlockSize; } return true; } diff --git a/core/fxcrt/cfx_memorystream.h b/core/fxcrt/cfx_memorystream.h index 64ea2cd5b5..3726b897c4 100644 --- a/core/fxcrt/cfx_memorystream.h +++ b/core/fxcrt/cfx_memorystream.h @@ -14,8 +14,6 @@ class CFX_MemoryStream : public IFX_SeekableStream { public: - enum Type { kConsecutive = 1 << 0, kTakeOver = 1 << 1 }; - template friend RetainPtr pdfium::MakeRetain(Args&&... args); @@ -31,9 +29,7 @@ class CFX_MemoryStream : public IFX_SeekableStream { // Sets the cursor position to |pos| if possible bool Seek(size_t pos); - bool IsConsecutive() const { return !!(m_dwFlags & Type::kConsecutive); } - - uint8_t* GetBuffer() const { + uint8_t* GetBuffer() { return !m_Blocks.empty() ? m_Blocks.front() : nullptr; } @@ -47,9 +43,9 @@ class CFX_MemoryStream : public IFX_SeekableStream { std::vector m_Blocks; size_t m_nTotalSize; size_t m_nCurSize; - size_t m_nCurPos; - size_t m_nGrowSize; - uint32_t m_dwFlags; + size_t m_nCurPos = 0; + const bool m_bConsecutive; + const bool m_bTakeOver; // Owns the data in |m_Blocks|. }; #endif // CORE_FXCRT_CFX_MEMORYSTREAM_H_ -- cgit v1.2.3