summaryrefslogtreecommitdiff
path: root/absl/strings/cord.cc
diff options
context:
space:
mode:
authorGravatar Abseil Team <absl-team@google.com>2020-09-18 15:55:15 -0700
committerGravatar Derek Mauro <dmauro@google.com>2020-09-24 13:47:15 -0400
commitb56cbdd23834a65682c0b46f367f8679e83bc894 (patch)
treedacab9a64dd1a9e9668737e511d1a5420ff96001 /absl/strings/cord.cc
parentb832dce8489ef7b6231384909fd9b68d5a5ff2b7 (diff)
Abseil LTS 2020092320200923
What's New: * `absl::StatusOr<T>` has been released. See our [blog post](https://abseil.io/blog/2020-091021-status) for more information. * Abseil Flags reflection interfaces have been released. * Abseil Flags memory usage has been significantly optimized. * Abseil now supports a "hardened" build mode. This build mode enables runtime checks that guard against programming errors that may lead to security vulnerabilities. Notable Fixes: * Sanitizer dynamic annotations like `AnnotateRWLockCreate` that are also defined by the compiler sanitizer implementation are no longer also defined by Abseil. * Sanitizer macros are now prefixed with `ABSL_` to avoid naming collisions. * Sanitizer usage is now automatically detected and no longer requires macros like `ADDRESS_SANITIZER` to be defined on the command line. Breaking Changes: * Abseil no longer contains a `dynamic_annotations` library. Users using a supported build system (Bazel or CMake) are unaffected by this, but users manually specifying link libraries may get an error about a missing linker input. Baseline: 7680a5f8efe32de4753baadbd63e74e59d95bac1 Cherry picks: None
Diffstat (limited to 'absl/strings/cord.cc')
-rw-r--r--absl/strings/cord.cc203
1 files changed, 91 insertions, 112 deletions
diff --git a/absl/strings/cord.cc b/absl/strings/cord.cc
index d9503ae3..763dcc45 100644
--- a/absl/strings/cord.cc
+++ b/absl/strings/cord.cc
@@ -15,10 +15,12 @@
#include "absl/strings/cord.h"
#include <algorithm>
+#include <atomic>
#include <cstddef>
#include <cstdio>
#include <cstdlib>
#include <iomanip>
+#include <iostream>
#include <limits>
#include <ostream>
#include <sstream>
@@ -28,6 +30,7 @@
#include "absl/base/casts.h"
#include "absl/base/internal/raw_logging.h"
+#include "absl/base/macros.h"
#include "absl/base/port.h"
#include "absl/container/fixed_array.h"
#include "absl/container/inlined_vector.h"
@@ -58,48 +61,6 @@ enum CordRepKind {
FLAT = 3,
};
-namespace {
-
-// Type used with std::allocator for allocating and deallocating
-// `CordRepExternal`. std::allocator is used because it opaquely handles the
-// different new / delete overloads available on a given platform.
-struct alignas(absl::cord_internal::ExternalRepAlignment()) ExternalAllocType {
- unsigned char value[absl::cord_internal::ExternalRepAlignment()];
-};
-
-// Returns the number of objects to pass in to std::allocator<ExternalAllocType>
-// allocate() and deallocate() to create enough room for `CordRepExternal` with
-// `releaser_size` bytes on the end.
-constexpr size_t GetExternalAllocNumObjects(size_t releaser_size) {
- // Be sure to round up since `releaser_size` could be smaller than
- // `sizeof(ExternalAllocType)`.
- return (sizeof(CordRepExternal) + releaser_size + sizeof(ExternalAllocType) -
- 1) /
- sizeof(ExternalAllocType);
-}
-
-// Allocates enough memory for `CordRepExternal` and a releaser with size
-// `releaser_size` bytes.
-void* AllocateExternal(size_t releaser_size) {
- return std::allocator<ExternalAllocType>().allocate(
- GetExternalAllocNumObjects(releaser_size));
-}
-
-// Deallocates the memory for a `CordRepExternal` assuming it was allocated with
-// a releaser of given size and alignment.
-void DeallocateExternal(CordRepExternal* p, size_t releaser_size) {
- std::allocator<ExternalAllocType>().deallocate(
- reinterpret_cast<ExternalAllocType*>(p),
- GetExternalAllocNumObjects(releaser_size));
-}
-
-// Returns a pointer to the type erased releaser for the given CordRepExternal.
-void* GetExternalReleaser(CordRepExternal* rep) {
- return rep + 1;
-}
-
-} // namespace
-
namespace cord_internal {
inline CordRepConcat* CordRep::concat() {
@@ -136,8 +97,6 @@ inline const CordRepExternal* CordRep::external() const {
static const size_t kFlatOverhead = offsetof(CordRep, data);
-static_assert(kFlatOverhead == 13, "Unittests assume kFlatOverhead == 13");
-
// Largest and smallest flat node lengths we are willing to allocate
// Flat allocation size is stored in tag, which currently can encode sizes up
// to 4K, encoded as multiple of either 8 or 32 bytes.
@@ -195,52 +154,18 @@ static_assert(Fibonacci(63) == 6557470319842,
// The root node depth is allowed to become twice as large to reduce rebalancing
// for larger strings (see IsRootBalanced).
static constexpr uint64_t min_length[] = {
- Fibonacci(2),
- Fibonacci(3),
- Fibonacci(4),
- Fibonacci(5),
- Fibonacci(6),
- Fibonacci(7),
- Fibonacci(8),
- Fibonacci(9),
- Fibonacci(10),
- Fibonacci(11),
- Fibonacci(12),
- Fibonacci(13),
- Fibonacci(14),
- Fibonacci(15),
- Fibonacci(16),
- Fibonacci(17),
- Fibonacci(18),
- Fibonacci(19),
- Fibonacci(20),
- Fibonacci(21),
- Fibonacci(22),
- Fibonacci(23),
- Fibonacci(24),
- Fibonacci(25),
- Fibonacci(26),
- Fibonacci(27),
- Fibonacci(28),
- Fibonacci(29),
- Fibonacci(30),
- Fibonacci(31),
- Fibonacci(32),
- Fibonacci(33),
- Fibonacci(34),
- Fibonacci(35),
- Fibonacci(36),
- Fibonacci(37),
- Fibonacci(38),
- Fibonacci(39),
- Fibonacci(40),
- Fibonacci(41),
- Fibonacci(42),
- Fibonacci(43),
- Fibonacci(44),
- Fibonacci(45),
- Fibonacci(46),
- Fibonacci(47),
+ Fibonacci(2), Fibonacci(3), Fibonacci(4), Fibonacci(5),
+ Fibonacci(6), Fibonacci(7), Fibonacci(8), Fibonacci(9),
+ Fibonacci(10), Fibonacci(11), Fibonacci(12), Fibonacci(13),
+ Fibonacci(14), Fibonacci(15), Fibonacci(16), Fibonacci(17),
+ Fibonacci(18), Fibonacci(19), Fibonacci(20), Fibonacci(21),
+ Fibonacci(22), Fibonacci(23), Fibonacci(24), Fibonacci(25),
+ Fibonacci(26), Fibonacci(27), Fibonacci(28), Fibonacci(29),
+ Fibonacci(30), Fibonacci(31), Fibonacci(32), Fibonacci(33),
+ Fibonacci(34), Fibonacci(35), Fibonacci(36), Fibonacci(37),
+ Fibonacci(38), Fibonacci(39), Fibonacci(40), Fibonacci(41),
+ Fibonacci(42), Fibonacci(43), Fibonacci(44), Fibonacci(45),
+ Fibonacci(46), Fibonacci(47),
0xffffffffffffffffull, // Avoid overflow
};
@@ -337,11 +262,7 @@ static void UnrefInternal(CordRep* rep) {
}
} else if (rep->tag == EXTERNAL) {
CordRepExternal* rep_external = rep->external();
- absl::string_view data(rep_external->base, rep->length);
- void* releaser = GetExternalReleaser(rep_external);
- size_t releaser_size = rep_external->releaser_invoker(releaser, data);
- rep_external->~CordRepExternal();
- DeallocateExternal(rep_external, releaser_size);
+ rep_external->releaser_invoker(rep_external);
rep = nullptr;
} else if (rep->tag == SUBSTRING) {
CordRepSubstring* rep_substring = rep->substring();
@@ -491,18 +412,12 @@ static CordRep* NewTree(const char* data,
namespace cord_internal {
-ExternalRepReleaserPair NewExternalWithUninitializedReleaser(
- absl::string_view data, ExternalReleaserInvoker invoker,
- size_t releaser_size) {
+void InitializeCordRepExternal(absl::string_view data, CordRepExternal* rep) {
assert(!data.empty());
-
- void* raw_rep = AllocateExternal(releaser_size);
- auto* rep = new (raw_rep) CordRepExternal();
rep->length = data.size();
rep->tag = EXTERNAL;
rep->base = data.data();
- rep->releaser_invoker = invoker;
- return {VerifyTree(rep), GetExternalReleaser(rep)};
+ VerifyTree(rep);
}
} // namespace cord_internal
@@ -526,10 +441,7 @@ static CordRep* NewSubstring(CordRep* child, size_t offset, size_t length) {
// --------------------------------------------------------------------
// Cord::InlineRep functions
-// This will trigger LNK2005 in MSVC.
-#ifndef COMPILER_MSVC
-const unsigned char Cord::InlineRep::kMaxInline;
-#endif // COMPILER_MSVC
+constexpr unsigned char Cord::InlineRep::kMaxInline;
inline void Cord::InlineRep::set_data(const char* data, size_t n,
bool nullify_tail) {
@@ -585,7 +497,7 @@ void Cord::InlineRep::AppendTree(CordRep* tree) {
}
void Cord::InlineRep::PrependTree(CordRep* tree) {
- if (tree == nullptr) return;
+ assert(tree != nullptr);
size_t len = data_[kMaxInline];
if (len == 0) {
set_tree(tree);
@@ -738,6 +650,37 @@ Cord::Cord(absl::string_view src) {
}
}
+template <typename T, Cord::EnableIfString<T>>
+Cord::Cord(T&& src) {
+ if (
+ // String is short: copy data to avoid external block overhead.
+ src.size() <= kMaxBytesToCopy ||
+ // String is wasteful: copy data to avoid pinning too much unused memory.
+ src.size() < src.capacity() / 2
+ ) {
+ if (src.size() <= InlineRep::kMaxInline) {
+ contents_.set_data(src.data(), src.size(), false);
+ } else {
+ contents_.set_tree(NewTree(src.data(), src.size(), 0));
+ }
+ } else {
+ struct StringReleaser {
+ void operator()(absl::string_view /* data */) {}
+ std::string data;
+ };
+ const absl::string_view original_data = src;
+ auto* rep = static_cast<
+ ::absl::cord_internal::CordRepExternalImpl<StringReleaser>*>(
+ absl::cord_internal::NewExternalRep(
+ original_data, StringReleaser{std::forward<T>(src)}));
+ // Moving src may have invalidated its data pointer, so adjust it.
+ rep->base = rep->template get<0>().data.data();
+ contents_.set_tree(rep);
+ }
+}
+
+template Cord::Cord(std::string&& src);
+
// The destruction code is separate so that the compiler can determine
// that it does not need to call the destructor on a moved-from Cord.
void Cord::DestroyCordSlow() {
@@ -775,6 +718,18 @@ Cord& Cord::operator=(absl::string_view src) {
return *this;
}
+template <typename T, Cord::EnableIfString<T>>
+Cord& Cord::operator=(T&& src) {
+ if (src.size() <= kMaxBytesToCopy) {
+ *this = absl::string_view(src);
+ } else {
+ *this = Cord(std::forward<T>(src));
+ }
+ return *this;
+}
+
+template Cord& Cord::operator=(std::string&& src);
+
// TODO(sanjay): Move to Cord::InlineRep section of file. For now,
// we keep it here to make diffs easier.
void Cord::InlineRep::AppendArray(const char* src_data, size_t src_size) {
@@ -886,6 +841,17 @@ void Cord::Append(const Cord& src) { AppendImpl(src); }
void Cord::Append(Cord&& src) { AppendImpl(std::move(src)); }
+template <typename T, Cord::EnableIfString<T>>
+void Cord::Append(T&& src) {
+ if (src.size() <= kMaxBytesToCopy) {
+ Append(absl::string_view(src));
+ } else {
+ Append(Cord(std::forward<T>(src)));
+ }
+}
+
+template void Cord::Append(std::string&& src);
+
void Cord::Prepend(const Cord& src) {
CordRep* src_tree = src.contents_.tree();
if (src_tree != nullptr) {
@@ -915,6 +881,17 @@ void Cord::Prepend(absl::string_view src) {
}
}
+template <typename T, Cord::EnableIfString<T>>
+inline void Cord::Prepend(T&& src) {
+ if (src.size() <= kMaxBytesToCopy) {
+ Prepend(absl::string_view(src));
+ } else {
+ Prepend(Cord(std::forward<T>(src)));
+ }
+}
+
+template void Cord::Prepend(std::string&& src);
+
static CordRep* RemovePrefixFrom(CordRep* node, size_t n) {
if (n >= node->length) return nullptr;
if (n == 0) return Ref(node);
@@ -1175,7 +1152,7 @@ class CordForest {
void AddNode(CordRep* node) {
CordRep* sum = nullptr;
- // Collect together everything with which we will merge node
+ // Collect together everything with which we will merge with node
int i = 0;
for (; node->length > min_length[i + 1]; ++i) {
auto& tree_at_i = trees_[i];
@@ -1506,7 +1483,8 @@ void Cord::CopyToArraySlowPath(char* dst) const {
}
Cord::ChunkIterator& Cord::ChunkIterator::operator++() {
- assert(bytes_remaining_ > 0 && "Attempted to iterate past `end()`");
+ ABSL_HARDENING_ASSERT(bytes_remaining_ > 0 &&
+ "Attempted to iterate past `end()`");
assert(bytes_remaining_ >= current_chunk_.size());
bytes_remaining_ -= current_chunk_.size();
@@ -1545,7 +1523,8 @@ Cord::ChunkIterator& Cord::ChunkIterator::operator++() {
}
Cord Cord::ChunkIterator::AdvanceAndReadBytes(size_t n) {
- assert(bytes_remaining_ >= n && "Attempted to iterate past `end()`");
+ ABSL_HARDENING_ASSERT(bytes_remaining_ >= n &&
+ "Attempted to iterate past `end()`");
Cord subcord;
if (n <= InlineRep::kMaxInline) {
@@ -1716,7 +1695,7 @@ void Cord::ChunkIterator::AdvanceBytesSlowPath(size_t n) {
}
char Cord::operator[](size_t i) const {
- assert(i < size());
+ ABSL_HARDENING_ASSERT(i < size());
size_t offset = i;
const CordRep* rep = contents_.tree();
if (rep == nullptr) {