OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2016 PDFium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com | |
6 | |
7 #ifndef CORE_FXCRT_CFX_STRING_DATA_TEMPLATE_H_ | |
8 #define CORE_FXCRT_CFX_STRING_DATA_TEMPLATE_H_ | |
9 | |
10 #include "core/fxcrt/include/fx_system.h" | |
11 #include "core/fxcrt/include/fx_memory.h" | |
Lei Zhang
2016/03/31 00:07:19
nit: alphabetical order
Tom Sepez
2016/03/31 17:16:50
Done.
| |
12 #include "third_party/base/numerics/safe_math.h" | |
13 | |
14 template <typename CharType> | |
15 class CFX_StringDataTemplate { | |
16 public: | |
17 static CFX_StringDataTemplate* Create(FX_STRSIZE nLen) { | |
18 FXSYS_assert(nLen > 0); | |
19 | |
20 // Fixed portion of header plus a NUL char not included in m_nAllocLength. | |
Lei Zhang
2016/03/31 00:07:18
... is not included
Tom Sepez
2016/03/31 17:16:50
Done.
| |
21 // sizeof(CharType) is always 1, used for consistency with CFX_Widestring. | |
22 int overhead = | |
23 offsetof(CFX_StringDataTemplate, m_String) + sizeof(CharType); | |
24 pdfium::base::CheckedNumeric<int> nSize = nLen; | |
25 nSize *= sizeof(CharType); | |
26 nSize += overhead; | |
27 | |
28 // Now round to an 8-byte boundary. We'd expect that this is the minimum | |
29 // granularity of any of the underlying allocators, so there may be cases | |
30 // where we can save a re-alloc when adding a few characters to a string | |
31 // by using this otherwise wasted space. | |
32 nSize += 7; | |
33 int totalSize = nSize.ValueOrDie() & ~7; | |
34 int usableLen = (totalSize - overhead) / sizeof(CharType); | |
35 FXSYS_assert(usableLen >= nLen); | |
36 | |
37 void* pData = FX_Alloc(uint8_t, totalSize); | |
38 return new (pData) CFX_StringDataTemplate(nLen, usableLen); | |
39 } | |
40 | |
41 static CFX_StringDataTemplate* Create(const CFX_StringDataTemplate& other) { | |
42 CFX_StringDataTemplate* result = Create(other.m_nDataLength); | |
43 result->CopyContents(other); | |
44 return result; | |
45 } | |
46 | |
47 static CFX_StringDataTemplate* Create(const CharType* pStr, FX_STRSIZE nLen) { | |
48 CFX_StringDataTemplate* result = Create(nLen); | |
49 result->CopyContents(pStr, nLen); | |
50 return result; | |
51 } | |
52 | |
53 void Retain() { ++m_nRefs; } | |
54 void Release() { | |
55 if (--m_nRefs <= 0) | |
56 FX_Free(this); | |
57 } | |
58 | |
59 bool CanOperateInPlace(FX_STRSIZE nTotalLen) const { | |
60 return m_nRefs <= 1 && nTotalLen <= m_nAllocLength; | |
61 } | |
62 | |
63 void CopyContents(const CFX_StringDataTemplate& other) { | |
64 FXSYS_assert(other.m_nDataLength <= m_nAllocLength); | |
65 FXSYS_memcpy(m_String, other.m_String, | |
66 (other.m_nDataLength + 1) * sizeof(CharType)); | |
67 } | |
68 | |
69 void CopyContents(const CharType* pStr, FX_STRSIZE nLen) { | |
70 FXSYS_assert(nLen >= 0 && nLen <= m_nAllocLength); | |
71 FXSYS_memcpy(m_String, pStr, nLen * sizeof(CharType)); | |
72 m_String[nLen] = 0; | |
73 } | |
74 | |
75 void CopyContentsAt(FX_STRSIZE offset, | |
76 const CharType* pStr, | |
77 FX_STRSIZE nLen) { | |
78 FXSYS_assert(offset >= 0 && nLen >= 0 && offset + nLen <= m_nAllocLength); | |
79 FXSYS_memcpy(m_String + offset * sizeof(CharType), pStr, | |
80 nLen * sizeof(CharType)); | |
81 m_String[offset + nLen] = 0; | |
82 } | |
83 | |
84 // To ensure ref counts do not overflow, consider the worst possible case: | |
85 // the entire address space contains nothing but pointers to this object. | |
86 // Since the count increments with each new pointer, the largest value is | |
87 // the number of pointers that can fit into the address space. The size of | |
88 // the address space itself is a good upper bound on it. | |
89 intptr_t m_nRefs; // Would prefer ssize_t, but no windows support. | |
90 | |
91 // |FX_STRSIZE| is currently typedef'd as |int|. | |
92 // TODO(palmer): It should be a |size_t|, or at least unsigned. | |
93 // These lengths are in terms of number of characters, not bytes, and do not | |
94 // include the terminating NUL character, but the underlying buffer is sized | |
95 // to be capable of holding it. | |
96 FX_STRSIZE m_nDataLength; | |
97 FX_STRSIZE m_nAllocLength; | |
98 | |
99 // Not really 1, variable size. | |
100 CharType m_String[1]; | |
101 | |
102 private: | |
103 CFX_StringDataTemplate(FX_STRSIZE dataLen, FX_STRSIZE allocLen) | |
104 : m_nRefs(0), m_nDataLength(dataLen), m_nAllocLength(allocLen) { | |
105 FXSYS_assert(dataLen >= 0); | |
106 FXSYS_assert(dataLen <= allocLen); | |
107 m_String[dataLen] = 0; | |
108 } | |
109 | |
110 ~CFX_StringDataTemplate() = delete; | |
111 }; | |
112 | |
113 #endif // CORE_FXCRT_CFX_STRING_DATA_TEMPLATE_H_ | |
OLD | NEW |