From 20a1755a887c652d2f7057ab614b7a06ec492b97 Mon Sep 17 00:00:00 2001 From: Dan Sinclair Date: Thu, 21 Sep 2017 16:35:56 -0400 Subject: Move CFX_StringDataTemplate to StringDataTemplate This CL renames CFX_StringDataTemplate to StringDataTemplate and moves into the fxcrt namespace. Bug: pdfium:898 Change-Id: I1c1e5ae674c3cca34fd595272e9eebc9346ed6ac Reviewed-on: https://pdfium-review.googlesource.com/14618 Reviewed-by: Tom Sepez Commit-Queue: dsinclair --- BUILD.gn | 2 +- core/fxcrt/bytestring.cpp | 2 +- core/fxcrt/bytestring.h | 4 +- core/fxcrt/cfx_string_data_template.h | 118 -------------------------------- core/fxcrt/string_data_template.h | 123 ++++++++++++++++++++++++++++++++++ core/fxcrt/widestring.cpp | 2 +- core/fxcrt/widestring.h | 4 +- 7 files changed, 130 insertions(+), 125 deletions(-) delete mode 100644 core/fxcrt/cfx_string_data_template.h create mode 100644 core/fxcrt/string_data_template.h diff --git a/BUILD.gn b/BUILD.gn index eb67898cd4..a823fe41f6 100644 --- a/BUILD.gn +++ b/BUILD.gn @@ -821,7 +821,6 @@ static_library("fxcrt") { "core/fxcrt/cfx_fixedbufgrow.h", "core/fxcrt/cfx_memorystream.cpp", "core/fxcrt/cfx_memorystream.h", - "core/fxcrt/cfx_string_data_template.h", "core/fxcrt/cfx_string_pool_template.h", "core/fxcrt/cfx_unowned_ptr.h", "core/fxcrt/cfx_utf8decoder.cpp", @@ -857,6 +856,7 @@ static_library("fxcrt") { "core/fxcrt/observable.h", "core/fxcrt/retain_ptr.h", "core/fxcrt/shared_copy_on_write.h", + "core/fxcrt/string_data_template.h", "core/fxcrt/string_view_template.h", "core/fxcrt/widestring.cpp", "core/fxcrt/widestring.h", diff --git a/core/fxcrt/bytestring.cpp b/core/fxcrt/bytestring.cpp index 2d343ffd11..a78020bae4 100644 --- a/core/fxcrt/bytestring.cpp +++ b/core/fxcrt/bytestring.cpp @@ -20,7 +20,7 @@ #include "third_party/base/numerics/safe_math.h" #include "third_party/base/stl_util.h" -template class CFX_StringDataTemplate; +template class fxcrt::StringDataTemplate; template class fxcrt::StringViewTemplate; template class CFX_StringPoolTemplate; template struct std::hash; diff --git a/core/fxcrt/bytestring.h b/core/fxcrt/bytestring.h index 7342fdbea7..c2c41e8c47 100644 --- a/core/fxcrt/bytestring.h +++ b/core/fxcrt/bytestring.h @@ -12,9 +12,9 @@ #include #include -#include "core/fxcrt/cfx_string_data_template.h" #include "core/fxcrt/fx_system.h" #include "core/fxcrt/retain_ptr.h" +#include "core/fxcrt/string_data_template.h" #include "core/fxcrt/string_view_template.h" #include "third_party/base/optional.h" @@ -185,7 +185,7 @@ class ByteString { static ByteString FormatFloat(float f, int precision = 0); protected: - using StringData = CFX_StringDataTemplate; + using StringData = StringDataTemplate; void ReallocBeforeWrite(FX_STRSIZE nNewLen); void AllocBeforeWrite(FX_STRSIZE nNewLen); diff --git a/core/fxcrt/cfx_string_data_template.h b/core/fxcrt/cfx_string_data_template.h deleted file mode 100644 index eabb608818..0000000000 --- a/core/fxcrt/cfx_string_data_template.h +++ /dev/null @@ -1,118 +0,0 @@ -// Copyright 2016 PDFium Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com - -#ifndef CORE_FXCRT_CFX_STRING_DATA_TEMPLATE_H_ -#define CORE_FXCRT_CFX_STRING_DATA_TEMPLATE_H_ - -#include "core/fxcrt/fx_memory.h" -#include "core/fxcrt/fx_system.h" -#include "third_party/base/numerics/safe_math.h" - -template -class CFX_StringDataTemplate { - public: - static CFX_StringDataTemplate* Create(FX_STRSIZE nLen) { - ASSERT(nLen > 0); - - // Calculate space needed for the fixed portion of the struct plus the - // NUL char that is not included in |m_nAllocLength|. - int overhead = - offsetof(CFX_StringDataTemplate, m_String) + sizeof(CharType); - pdfium::base::CheckedNumeric nSize = nLen; - nSize *= sizeof(CharType); - nSize += overhead; - - // Now round to an 8-byte boundary. We'd expect that this is the minimum - // granularity of any of the underlying allocators, so there may be cases - // where we can save a re-alloc when adding a few characters to a string - // by using this otherwise wasted space. - nSize += 7; - nSize &= ~7; - FX_STRSIZE totalSize = nSize.ValueOrDie(); - FX_STRSIZE usableLen = (totalSize - overhead) / sizeof(CharType); - ASSERT(usableLen >= nLen); - - void* pData = pdfium::base::PartitionAllocGeneric( - gStringPartitionAllocator.root(), totalSize, "CFX_StringDataTemplate"); - return new (pData) CFX_StringDataTemplate(nLen, usableLen); - } - - static CFX_StringDataTemplate* Create(const CFX_StringDataTemplate& other) { - CFX_StringDataTemplate* result = Create(other.m_nDataLength); - result->CopyContents(other); - return result; - } - - static CFX_StringDataTemplate* Create(const CharType* pStr, FX_STRSIZE nLen) { - CFX_StringDataTemplate* result = Create(nLen); - result->CopyContents(pStr, nLen); - return result; - } - - void Retain() { ++m_nRefs; } - void Release() { - if (--m_nRefs <= 0) - pdfium::base::PartitionFreeGeneric(gStringPartitionAllocator.root(), - this); - } - - bool CanOperateInPlace(FX_STRSIZE nTotalLen) const { - return m_nRefs <= 1 && nTotalLen <= m_nAllocLength; - } - - void CopyContents(const CFX_StringDataTemplate& other) { - ASSERT(other.m_nDataLength <= m_nAllocLength); - memcpy(m_String, other.m_String, - (other.m_nDataLength + 1) * sizeof(CharType)); - } - - void CopyContents(const CharType* pStr, FX_STRSIZE nLen) { - ASSERT(nLen >= 0 && nLen <= m_nAllocLength); - memcpy(m_String, pStr, nLen * sizeof(CharType)); - m_String[nLen] = 0; - } - - void CopyContentsAt(FX_STRSIZE offset, - const CharType* pStr, - FX_STRSIZE nLen) { - ASSERT(offset >= 0 && nLen >= 0 && offset + nLen <= m_nAllocLength); - memcpy(m_String + offset, pStr, nLen * sizeof(CharType)); - m_String[offset + nLen] = 0; - } - - // To ensure ref counts do not overflow, consider the worst possible case: - // the entire address space contains nothing but pointers to this object. - // Since the count increments with each new pointer, the largest value is - // the number of pointers that can fit into the address space. The size of - // the address space itself is a good upper bound on it. - intptr_t m_nRefs; - - // |FX_STRSIZE| is currently typedef'd as |int|. - // TODO(palmer): It should be a |size_t|, or at least unsigned. - // These lengths are in terms of number of characters, not bytes, and do not - // include the terminating NUL character, but the underlying buffer is sized - // to be capable of holding it. - FX_STRSIZE m_nDataLength; - FX_STRSIZE m_nAllocLength; - - // Not really 1, variable size. - CharType m_String[1]; - - private: - CFX_StringDataTemplate(FX_STRSIZE dataLen, FX_STRSIZE allocLen) - : m_nRefs(0), m_nDataLength(dataLen), m_nAllocLength(allocLen) { - ASSERT(dataLen >= 0); - ASSERT(dataLen <= allocLen); - m_String[dataLen] = 0; - } - - ~CFX_StringDataTemplate() = delete; -}; - -extern template class CFX_StringDataTemplate; -extern template class CFX_StringDataTemplate; - -#endif // CORE_FXCRT_CFX_STRING_DATA_TEMPLATE_H_ diff --git a/core/fxcrt/string_data_template.h b/core/fxcrt/string_data_template.h new file mode 100644 index 0000000000..afec50fe01 --- /dev/null +++ b/core/fxcrt/string_data_template.h @@ -0,0 +1,123 @@ +// Copyright 2016 PDFium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com + +#ifndef CORE_FXCRT_STRING_DATA_TEMPLATE_H_ +#define CORE_FXCRT_STRING_DATA_TEMPLATE_H_ + +#include "core/fxcrt/fx_memory.h" +#include "core/fxcrt/fx_system.h" +#include "third_party/base/numerics/safe_math.h" + +namespace fxcrt { + +template +class StringDataTemplate { + public: + static StringDataTemplate* Create(FX_STRSIZE nLen) { + ASSERT(nLen > 0); + + // Calculate space needed for the fixed portion of the struct plus the + // NUL char that is not included in |m_nAllocLength|. + int overhead = offsetof(StringDataTemplate, m_String) + sizeof(CharType); + pdfium::base::CheckedNumeric nSize = nLen; + nSize *= sizeof(CharType); + nSize += overhead; + + // Now round to an 8-byte boundary. We'd expect that this is the minimum + // granularity of any of the underlying allocators, so there may be cases + // where we can save a re-alloc when adding a few characters to a string + // by using this otherwise wasted space. + nSize += 7; + nSize &= ~7; + FX_STRSIZE totalSize = nSize.ValueOrDie(); + FX_STRSIZE usableLen = (totalSize - overhead) / sizeof(CharType); + ASSERT(usableLen >= nLen); + + void* pData = pdfium::base::PartitionAllocGeneric( + gStringPartitionAllocator.root(), totalSize, "StringDataTemplate"); + return new (pData) StringDataTemplate(nLen, usableLen); + } + + static StringDataTemplate* Create(const StringDataTemplate& other) { + StringDataTemplate* result = Create(other.m_nDataLength); + result->CopyContents(other); + return result; + } + + static StringDataTemplate* Create(const CharType* pStr, FX_STRSIZE nLen) { + StringDataTemplate* result = Create(nLen); + result->CopyContents(pStr, nLen); + return result; + } + + void Retain() { ++m_nRefs; } + void Release() { + if (--m_nRefs <= 0) + pdfium::base::PartitionFreeGeneric(gStringPartitionAllocator.root(), + this); + } + + bool CanOperateInPlace(FX_STRSIZE nTotalLen) const { + return m_nRefs <= 1 && nTotalLen <= m_nAllocLength; + } + + void CopyContents(const StringDataTemplate& other) { + ASSERT(other.m_nDataLength <= m_nAllocLength); + memcpy(m_String, other.m_String, + (other.m_nDataLength + 1) * sizeof(CharType)); + } + + void CopyContents(const CharType* pStr, FX_STRSIZE nLen) { + ASSERT(nLen >= 0 && nLen <= m_nAllocLength); + memcpy(m_String, pStr, nLen * sizeof(CharType)); + m_String[nLen] = 0; + } + + void CopyContentsAt(FX_STRSIZE offset, + const CharType* pStr, + FX_STRSIZE nLen) { + ASSERT(offset >= 0 && nLen >= 0 && offset + nLen <= m_nAllocLength); + memcpy(m_String + offset, pStr, nLen * sizeof(CharType)); + m_String[offset + nLen] = 0; + } + + // To ensure ref counts do not overflow, consider the worst possible case: + // the entire address space contains nothing but pointers to this object. + // Since the count increments with each new pointer, the largest value is + // the number of pointers that can fit into the address space. The size of + // the address space itself is a good upper bound on it. + intptr_t m_nRefs; + + // |FX_STRSIZE| is currently typedef'd as |int|. + // TODO(palmer): It should be a |size_t|, or at least unsigned. + // These lengths are in terms of number of characters, not bytes, and do not + // include the terminating NUL character, but the underlying buffer is sized + // to be capable of holding it. + FX_STRSIZE m_nDataLength; + FX_STRSIZE m_nAllocLength; + + // Not really 1, variable size. + CharType m_String[1]; + + private: + StringDataTemplate(FX_STRSIZE dataLen, FX_STRSIZE allocLen) + : m_nRefs(0), m_nDataLength(dataLen), m_nAllocLength(allocLen) { + ASSERT(dataLen >= 0); + ASSERT(dataLen <= allocLen); + m_String[dataLen] = 0; + } + + ~StringDataTemplate() = delete; +}; + +extern template class StringDataTemplate; +extern template class StringDataTemplate; + +} // namespace fxcrt + +using fxcrt::StringDataTemplate; + +#endif // CORE_FXCRT_STRING_DATA_TEMPLATE_H_ diff --git a/core/fxcrt/widestring.cpp b/core/fxcrt/widestring.cpp index e90d8ced85..e5925845be 100644 --- a/core/fxcrt/widestring.cpp +++ b/core/fxcrt/widestring.cpp @@ -20,7 +20,7 @@ #include "third_party/base/numerics/safe_math.h" #include "third_party/base/stl_util.h" -template class CFX_StringDataTemplate; +template class fxcrt::StringDataTemplate; template class fxcrt::StringViewTemplate; template class CFX_StringPoolTemplate; template struct std::hash; diff --git a/core/fxcrt/widestring.h b/core/fxcrt/widestring.h index 4dc0abbc99..df7d95fc6f 100644 --- a/core/fxcrt/widestring.h +++ b/core/fxcrt/widestring.h @@ -11,10 +11,10 @@ #include #include -#include "core/fxcrt/cfx_string_data_template.h" #include "core/fxcrt/fx_memory.h" #include "core/fxcrt/fx_system.h" #include "core/fxcrt/retain_ptr.h" +#include "core/fxcrt/string_data_template.h" #include "core/fxcrt/string_view_template.h" #include "third_party/base/optional.h" @@ -179,7 +179,7 @@ class WideString { ByteString UTF16LE_Encode() const; protected: - using StringData = CFX_StringDataTemplate; + using StringData = StringDataTemplate; void ReallocBeforeWrite(FX_STRSIZE nLen); void AllocBeforeWrite(FX_STRSIZE nLen); -- cgit v1.2.3