Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(109)

Unified Diff: core/fxcrt/fx_basic_bstring.cpp

Issue 1916303004: CFX_ByteString::Reserve(), ReleaseBuffer() fixes. (Closed) Base URL: https://pdfium.googlesource.com/pdfium.git@master
Patch Set: Remove stray TrimLeft() Created 4 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | core/fxcrt/fx_basic_bstring_unittest.cpp » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: core/fxcrt/fx_basic_bstring.cpp
diff --git a/core/fxcrt/fx_basic_bstring.cpp b/core/fxcrt/fx_basic_bstring.cpp
index 1622c4bc55c2363bbcc84aa066c67844ad52f761..e098d8fe506c112ef8c857af09fad0630e86edb8 100644
--- a/core/fxcrt/fx_basic_bstring.cpp
+++ b/core/fxcrt/fx_basic_bstring.cpp
@@ -276,20 +276,25 @@ void CFX_ByteString::ReleaseBuffer(FX_STRSIZE nNewLength) {
if (nNewLength == -1)
nNewLength = FXSYS_strlen(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_ByteString preserve(*this);
+ ReallocBeforeWrite(nNewLength);
+ }
}
void CFX_ByteString::Reserve(FX_STRSIZE len) {
GetBuffer(len);
- ReleaseBuffer(GetLength());
}
FX_CHAR* CFX_ByteString::GetBuffer(FX_STRSIZE nMinBufLength) {
« no previous file with comments | « no previous file | core/fxcrt/fx_basic_bstring_unittest.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698