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_memory.h" |
| 11 #include "core/fxcrt/include/fx_system.h" |
| 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 // Calculate space needed for the fixed portion of the struct plus the |
| 21 // NUL char that is not included in |m_nAllocLength|. |
| 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, pStr, nLen * sizeof(CharType)); |
| 80 m_String[offset + nLen] = 0; |
| 81 } |
| 82 |
| 83 // To ensure ref counts do not overflow, consider the worst possible case: |
| 84 // the entire address space contains nothing but pointers to this object. |
| 85 // Since the count increments with each new pointer, the largest value is |
| 86 // the number of pointers that can fit into the address space. The size of |
| 87 // the address space itself is a good upper bound on it. |
| 88 intptr_t m_nRefs; // Would prefer ssize_t, but no windows support. |
| 89 |
| 90 // |FX_STRSIZE| is currently typedef'd as |int|. |
| 91 // TODO(palmer): It should be a |size_t|, or at least unsigned. |
| 92 // These lengths are in terms of number of characters, not bytes, and do not |
| 93 // include the terminating NUL character, but the underlying buffer is sized |
| 94 // to be capable of holding it. |
| 95 FX_STRSIZE m_nDataLength; |
| 96 FX_STRSIZE m_nAllocLength; |
| 97 |
| 98 // Not really 1, variable size. |
| 99 CharType m_String[1]; |
| 100 |
| 101 private: |
| 102 CFX_StringDataTemplate(FX_STRSIZE dataLen, FX_STRSIZE allocLen) |
| 103 : m_nRefs(0), m_nDataLength(dataLen), m_nAllocLength(allocLen) { |
| 104 FXSYS_assert(dataLen >= 0); |
| 105 FXSYS_assert(dataLen <= allocLen); |
| 106 m_String[dataLen] = 0; |
| 107 } |
| 108 |
| 109 ~CFX_StringDataTemplate() = delete; |
| 110 }; |
| 111 |
| 112 extern template class CFX_StringDataTemplate<FX_CHAR>; |
| 113 extern template class CFX_StringDataTemplate<FX_WCHAR>; |
| 114 |
| 115 #endif // CORE_FXCRT_CFX_STRING_DATA_TEMPLATE_H_ |
OLD | NEW |