1 // Copyright 2020 The PDFium Authors
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 #include "core/fxcrt/string_data_template.h"
8
9 #include <string.h>
10
11 #include <new>
12
13 #include "core/fxcrt/fx_memcpy_wrappers.h"
14 #include "core/fxcrt/fx_memory.h"
15 #include "core/fxcrt/fx_safe_types.h"
16 #include "third_party/base/check.h"
17 #include "third_party/base/check_op.h"
18
19 namespace fxcrt {
20
21 // static
22 template <typename CharType>
Create(size_t nLen)23 StringDataTemplate<CharType>* StringDataTemplate<CharType>::Create(
24 size_t nLen) {
25 DCHECK_GT(nLen, 0u);
26
27 // Calculate space needed for the fixed portion of the struct plus the
28 // NUL char that is not included in |m_nAllocLength|.
29 int overhead = offsetof(StringDataTemplate, m_String) + sizeof(CharType);
30 FX_SAFE_SIZE_T nSize = nLen;
31 nSize *= sizeof(CharType);
32 nSize += overhead;
33
34 // Now round to an 16-byte boundary, assuming the underlying allocator is most
35 // likely PartitionAlloc, which has 16 byte chunks. This will help with cases
36 // where we can save a re-alloc when adding a few characters to a string by
37 // using this otherwise wasted space.
38 nSize += 15;
39 nSize &= ~15;
40 size_t totalSize = nSize.ValueOrDie();
41 size_t usableLen = (totalSize - overhead) / sizeof(CharType);
42 DCHECK(usableLen >= nLen);
43
44 void* pData = FX_StringAlloc(char, totalSize);
45 return new (pData) StringDataTemplate(nLen, usableLen);
46 }
47
48 // static
49 template <typename CharType>
Create(const CharType * pStr,size_t nLen)50 StringDataTemplate<CharType>* StringDataTemplate<CharType>::Create(
51 const CharType* pStr,
52 size_t nLen) {
53 StringDataTemplate* result = Create(nLen);
54 result->CopyContents(pStr, nLen);
55 return result;
56 }
57
58 template <typename CharType>
Release()59 void StringDataTemplate<CharType>::Release() {
60 if (--m_nRefs <= 0)
61 FX_StringFree(this);
62 }
63
64 template <typename CharType>
CopyContents(const StringDataTemplate & other)65 void StringDataTemplate<CharType>::CopyContents(
66 const StringDataTemplate& other) {
67 DCHECK(other.m_nDataLength <= m_nAllocLength);
68 memcpy(m_String, other.m_String,
69 (other.m_nDataLength + 1) * sizeof(CharType));
70 }
71
72 template <typename CharType>
CopyContents(const CharType * pStr,size_t nLen)73 void StringDataTemplate<CharType>::CopyContents(const CharType* pStr,
74 size_t nLen) {
75 DCHECK_GE(nLen, 0u);
76 DCHECK_LE(nLen, m_nAllocLength);
77 FXSYS_memcpy(m_String, pStr, nLen * sizeof(CharType));
78 m_String[nLen] = 0;
79 }
80
81 template <typename CharType>
CopyContentsAt(size_t offset,const CharType * pStr,size_t nLen)82 void StringDataTemplate<CharType>::CopyContentsAt(size_t offset,
83 const CharType* pStr,
84 size_t nLen) {
85 DCHECK_GE(offset, 0u);
86 DCHECK_GE(nLen, 0u);
87 DCHECK_LE(offset + nLen, m_nAllocLength);
88 FXSYS_memcpy(m_String + offset, pStr, nLen * sizeof(CharType));
89 m_String[offset + nLen] = 0;
90 }
91
92 template <typename CharType>
StringDataTemplate(size_t dataLen,size_t allocLen)93 StringDataTemplate<CharType>::StringDataTemplate(size_t dataLen,
94 size_t allocLen)
95 : m_nDataLength(dataLen), m_nAllocLength(allocLen) {
96 DCHECK_GE(dataLen, 0u);
97 DCHECK_LE(dataLen, allocLen);
98 m_String[dataLen] = 0;
99 }
100
101 template class StringDataTemplate<char>;
102 template class StringDataTemplate<wchar_t>;
103
104 } // namespace fxcrt
105