OLD | NEW |
1 // Copyright 2014 PDFium Authors. All rights reserved. | 1 // Copyright 2014 PDFium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com | 5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com |
6 | 6 |
7 #include "../../include/fxcrt/fx_basic.h" | 7 #include "../../include/fxcrt/fx_basic.h" |
8 #include "../../../third_party/base/numerics/safe_math.h" | 8 #include "../../../third_party/base/numerics/safe_math.h" |
9 | 9 |
10 static int _Buffer_itoa(char* buf, int i, FX_DWORD flags) | 10 static int _Buffer_itoa(char* buf, int i, FX_DWORD flags) |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
45 char buf[32]; | 45 char buf[32]; |
46 return CFX_ByteStringC(buf, _Buffer_itoa(buf, i, flags)); | 46 return CFX_ByteStringC(buf, _Buffer_itoa(buf, i, flags)); |
47 } | 47 } |
48 static CFX_StringData* FX_AllocString(int nLen) | 48 static CFX_StringData* FX_AllocString(int nLen) |
49 { | 49 { |
50 // |nLen| is currently declared as in |int|. TODO(palmer): It should be | 50 // |nLen| is currently declared as in |int|. TODO(palmer): It should be |
51 // a |size_t|, or at least unsigned. | 51 // a |size_t|, or at least unsigned. |
52 if (nLen == 0 || nLen < 0) { | 52 if (nLen == 0 || nLen < 0) { |
53 return NULL; | 53 return NULL; |
54 } | 54 } |
| 55 |
| 56 int overhead = sizeof(long) * 3 + 1; // 3 longs in header plus 1 for NUL. |
55 pdfium::base::CheckedNumeric<int> nSize = nLen; | 57 pdfium::base::CheckedNumeric<int> nSize = nLen; |
56 nSize += sizeof(long) * 3 + 1; | 58 nSize += overhead; |
57 CFX_StringData* pData = (CFX_StringData*)FX_Alloc(FX_BYTE, nSize.ValueOrDie(
)); | 59 |
| 60 // Now round to an 8-byte boundary. We'd expect that this is the minimum |
| 61 // granularity of any of the underlying allocators, so there may be cases |
| 62 // where we can save a re-alloc when adding a few characters to a string |
| 63 // by using this otherwise wasted space. |
| 64 nSize += 7; |
| 65 int totalSize = nSize.ValueOrDie() & ~7; |
| 66 int usableSize = totalSize - overhead; |
| 67 FXSYS_assert(usableSize >= nLen); |
| 68 |
| 69 CFX_StringData* pData = (CFX_StringData*)FX_Alloc(FX_BYTE, totalSize); |
58 if (!pData) { | 70 if (!pData) { |
59 return NULL; | 71 return NULL; |
60 } | 72 } |
61 pData->m_nAllocLength = nLen; | 73 pData->m_nAllocLength = usableSize; |
62 pData->m_nDataLength = nLen; | 74 pData->m_nDataLength = nLen; |
63 pData->m_nRefs = 1; | 75 pData->m_nRefs = 1; |
64 pData->m_String[nLen] = 0; | 76 pData->m_String[nLen] = 0; |
65 return pData; | 77 return pData; |
66 } | 78 } |
67 static void FX_ReleaseString(CFX_StringData* pData) | 79 static void FX_ReleaseString(CFX_StringData* pData) |
68 { | 80 { |
69 if (pData == NULL) { | 81 if (pData == NULL) { |
70 return; | 82 return; |
71 } | 83 } |
(...skipping 1049 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1121 scale /= 10; | 1133 scale /= 10; |
1122 } | 1134 } |
1123 return buf_size; | 1135 return buf_size; |
1124 } | 1136 } |
1125 CFX_ByteString CFX_ByteString::FormatFloat(FX_FLOAT d, int precision) | 1137 CFX_ByteString CFX_ByteString::FormatFloat(FX_FLOAT d, int precision) |
1126 { | 1138 { |
1127 FX_CHAR buf[32]; | 1139 FX_CHAR buf[32]; |
1128 FX_STRSIZE len = FX_ftoa(d, buf); | 1140 FX_STRSIZE len = FX_ftoa(d, buf); |
1129 return CFX_ByteString(buf, len); | 1141 return CFX_ByteString(buf, len); |
1130 } | 1142 } |
OLD | NEW |