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/check.h"
14#include "core/fxcrt/check_op.h"
15#include "core/fxcrt/fx_memory.h"
16#include "core/fxcrt/fx_safe_types.h"
17#include "core/fxcrt/stl_util.h"
18
19namespace fxcrt {
20
21// static
22template <typename CharType>
23RetainPtr<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;
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 pdfium::WrapRetain(new (pData) StringDataTemplate(nLen, usableLen));
46}
47
48// static
49template <typename CharType>
50RetainPtr<StringDataTemplate<CharType>> StringDataTemplate<CharType>::Create(
51 pdfium::span<const CharType> str) {
52 RetainPtr<StringDataTemplate> result = Create(str.size());
53 result->CopyContents(str);
54 return result;
55}
56
57template <typename CharType>
58void StringDataTemplate<CharType>::Release() {
59 if (--m_nRefs <= 0)
60 FX_StringFree(this);
61}
62
63template <typename CharType>
65 const StringDataTemplate& other) {
66 fxcrt::Copy(other.capacity_span().first(other.m_nDataLength + 1),
67 capacity_span());
68}
69
70template <typename CharType>
72 pdfium::span<const CharType> str) {
73 fxcrt::Copy(str, capacity_span());
74 capacity_span()[str.size()] = 0;
75}
76
77template <typename CharType>
79 size_t offset,
80 pdfium::span<const CharType> str) {
81 fxcrt::Copy(str, capacity_span().subspan(offset));
82 capacity_span()[offset + str.size()] = 0;
83}
84
85template <typename CharType>
86StringDataTemplate<CharType>::StringDataTemplate(size_t dataLen,
87 size_t allocLen)
88 : m_nDataLength(dataLen), m_nAllocLength(allocLen) {
89 capacity_span()[dataLen] = 0;
90}
91
92// Instantiate.
93template class StringDataTemplate<char>;
94template class StringDataTemplate<wchar_t>;
95
96} // namespace fxcrt
#define DCHECK
Definition check.h:33
#define DCHECK_GT(x, y)
Definition check_op.h:20
void CopyContents(const StringDataTemplate &other)
void CopyContentsAt(size_t offset, pdfium::span< const CharType > str)
void CopyContents(pdfium::span< const CharType > str)
void StringDealloc(void *ptr)