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

Unified Diff: core/src/fxcrt/fx_basic_bstring.cpp

Issue 1120813003: Merge to XFA: Take adavange of unused bytes at end of CFX strings (Closed) Base URL: https://pdfium.googlesource.com/pdfium.git@xfa
Patch Set: Created 5 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/src/fxcrt/fx_basic_wstring.cpp » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: core/src/fxcrt/fx_basic_bstring.cpp
diff --git a/core/src/fxcrt/fx_basic_bstring.cpp b/core/src/fxcrt/fx_basic_bstring.cpp
index 961aebe69c001ebc0f9cd62fcf6c51e262c46b9e..2c8f7a766bfa873cb452b10ac1d620e1689b3e15 100644
--- a/core/src/fxcrt/fx_basic_bstring.cpp
+++ b/core/src/fxcrt/fx_basic_bstring.cpp
@@ -52,13 +52,25 @@ static CFX_StringData* FX_AllocString(int nLen)
if (nLen == 0 || nLen < 0) {
return NULL;
}
+
+ int overhead = sizeof(long) * 3 + 1; // 3 longs in header plus 1 for NUL.
pdfium::base::CheckedNumeric<int> nSize = nLen;
- nSize += sizeof(long) * 3 + 1;
- CFX_StringData* pData = (CFX_StringData*)FX_Alloc(FX_BYTE, nSize.ValueOrDie());
+ nSize += overhead;
+
+ // Now round to an 8-byte boundary. We'd expect that this is the minimum
+ // granularity of any of the underlying allocators, so there may be cases
+ // where we can save a re-alloc when adding a few characters to a string
+ // by using this otherwise wasted space.
+ nSize += 7;
+ int totalSize = nSize.ValueOrDie() & ~7;
+ int usableSize = totalSize - overhead;
+ FXSYS_assert(usableSize >= nLen);
+
+ CFX_StringData* pData = (CFX_StringData*)FX_Alloc(FX_BYTE, totalSize);
if (!pData) {
return NULL;
}
- pData->m_nAllocLength = nLen;
+ pData->m_nAllocLength = usableSize;
pData->m_nDataLength = nLen;
pData->m_nRefs = 1;
pData->m_String[nLen] = 0;
« no previous file with comments | « no previous file | core/src/fxcrt/fx_basic_wstring.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698