Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
string_data_template.cpp
Go to the documentation of this file.
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
19namespace fxcrt {
20
21// static
22template <typename CharType>
23StringDataTemplate<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
49template <typename CharType>
50StringDataTemplate<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
58template <typename CharType>
59void StringDataTemplate<CharType>::Release() {
60 if (--m_nRefs <= 0)
61 FX_StringFree(this);
62}
63
64template <typename CharType>
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
72template <typename CharType>
73void 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
81template <typename CharType>
82void 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
92template <typename CharType>
93StringDataTemplate<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
101template class StringDataTemplate<char>;
102template class StringDataTemplate<wchar_t>;
103
104} // namespace fxcrt
void CopyContents(const CharType *pStr, size_t nLen)
void CopyContents(const StringDataTemplate &other)
void CopyContentsAt(size_t offset, const CharType *pStr, size_t nLen)
void StringDealloc(void *ptr)