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 ++++++++++++++++++-------------------- 1 file changed, 18 insertions(+), 20 deletions(-) (limited to 'core/fxcrt/cfx_memorystream.cpp') 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; } -- cgit v1.2.3