diff options
author | tsepez <tsepez@chromium.org> | 2016-04-26 12:13:16 -0700 |
---|---|---|
committer | Commit bot <commit-bot@chromium.org> | 2016-04-26 12:13:16 -0700 |
commit | 518fd4c5ababbfbf28e010a9c27098e8f6669e4b (patch) | |
tree | 79e20cf56d24f2aaf9463056ccb52803426be082 /core/fxcrt/fx_basic_wstring.cpp | |
parent | 5cc24654fb345189140acb4711ff981e1c720951 (diff) | |
download | pdfium-518fd4c5ababbfbf28e010a9c27098e8f6669e4b.tar.xz |
CFX_ByteString::Reserve(), ReleaseBuffer() fixes.
Also identical fixes for CFX_WideString.
Reserve() on an empty string would not actually reserve a
buffer. Currently unused, but there are places where this
would really help.
ReleaseBuffer() would rarely return memory to the system, since
it would short-circuit thinking it could operate in place. Tune
the algorithm slightly so that we hold on when the savings is
small.
Bounds check release buffer args rather than just asserting.
Add tests for all of these.
Review URL: https://codereview.chromium.org/1916303004
Diffstat (limited to 'core/fxcrt/fx_basic_wstring.cpp')
-rw-r--r-- | core/fxcrt/fx_basic_wstring.cpp | 11 |
1 files changed, 8 insertions, 3 deletions
diff --git a/core/fxcrt/fx_basic_wstring.cpp b/core/fxcrt/fx_basic_wstring.cpp index f3b430c7bf..38fd15e13f 100644 --- a/core/fxcrt/fx_basic_wstring.cpp +++ b/core/fxcrt/fx_basic_wstring.cpp @@ -224,20 +224,25 @@ void CFX_WideString::ReleaseBuffer(FX_STRSIZE nNewLength) { if (nNewLength == -1) nNewLength = FXSYS_wcslen(m_pData->m_String); + nNewLength = std::min(nNewLength, m_pData->m_nAllocLength); if (nNewLength == 0) { clear(); return; } - FXSYS_assert(nNewLength <= m_pData->m_nAllocLength); - ReallocBeforeWrite(nNewLength); + FXSYS_assert(m_pData->m_nRefs == 1); m_pData->m_nDataLength = nNewLength; m_pData->m_String[nNewLength] = 0; + if (m_pData->m_nAllocLength - nNewLength >= 32) { + // Over arbitrary threshold, so pay the price to relocate. Force copy to + // always occur by holding a second reference to the string. + CFX_WideString preserve(*this); + ReallocBeforeWrite(nNewLength); + } } void CFX_WideString::Reserve(FX_STRSIZE len) { GetBuffer(len); - ReleaseBuffer(GetLength()); } FX_WCHAR* CFX_WideString::GetBuffer(FX_STRSIZE nMinBufLength) { |