Commit 4bb73931 by Abseil Team Committed by rogeeff

Export of internal Abseil changes

--
69b5d0b2a5adb49a53e51f9da6848eaa484242fe by Derek Mauro <dmauro@google.com>:

Changes the absl::Duration factory functions to disallow types that
are convertible to int or double, instead requiring that the argument
itself is indeed an integer or a floating-point number. This will
prevent callers from passing arguments, such as std::atomic<T>.

This change is an API break. Information and a tool to fix issues
can be found at
https://abseil.io/docs/cpp/tools/upgrades/duration-conversions

PiperOrigin-RevId: 387153494

--
786063e438ab6a55ac4baa88ad4d20a8293be52a by Evan Brown <ezb@google.com>:

Make ctrl_t be an enum class.

This adds type safety, and also when strict aliasing is enabled, the compiler will know that control bytes can't alias non-control bytes.

Also make H2() return h2_t.

PiperOrigin-RevId: 387120717

--
7e537aabec1c255d6e7c9d21232c597c1c2077bf by Evan Brown <ezb@google.com>:

Add some missing `const` keywords to ctrl_t* function parameters.

PiperOrigin-RevId: 386976062

--
da53ac6d91cabd951e81dd0a145e1e52b918955f by Martijn Vels <mvels@google.com>:

Change Seek and InitOffset to return nullptr instead of assert / fail.

This makes it consistent with the rest of the API (Next, Previous, Skip) and hardens it against invariants that are harder (or less likey) to be upheld correctly by the caller.

PiperOrigin-RevId: 386963283

--
a4d1faac020d5025edf53ce81808e5db68da7d89 by Abseil Team <absl-team@google.com>:

PC / Backtrace / Symbolization for Emscripten.

PiperOrigin-RevId: 386957724

--
97f2c47d83ba9d3ac89e1f55bd06897686ffd063 by Martijn Vels <mvels@google.com>:

Fix static casts ([-Wimplicit-int-conversion])

PiperOrigin-RevId: 386951646

--
9530c795248543817cbc4013953baa09c35f5e1a by Abseil Team <absl-team@google.com>:

Fix incorrect header guard in cord_rep_btree_navigator.h

PiperOrigin-RevId: 386907904

--
90ce5872406df2b7f4c428683741dc13a572267e by Abseil Team <absl-team@google.com>:

Small grammar fixes for some StatusCode descriptions.

PiperOrigin-RevId: 386906217

--
b30a2fd777f12a04a4d512f37a34614b0d05ce99 by Derek Mauro <dmauro@google.com>:

Skip length checking when constructing absl::string_view from std::string.
The length check causes unnecessary code bloat.

PiperOrigin-RevId: 386857974

--
fa171536c359bfa2a1b80297e844519bb9ee7791 by Martijn Vels <mvels@google.com>:

Introduce CordRepBtreeNavigator

CordRepBtreeNavigator implements bi-directional navigation over all data edges stored inside a Cord Btree.

PiperOrigin-RevId: 386519102
GitOrigin-RevId: 69b5d0b2a5adb49a53e51f9da6848eaa484242fe
Change-Id: I1b35188d66133f8cb73d346bc5564aac4e0b3e80
parent 0ce3ca95
...@@ -207,6 +207,8 @@ set(ABSL_INTERNAL_DLL_FILES ...@@ -207,6 +207,8 @@ set(ABSL_INTERNAL_DLL_FILES
"strings/internal/cord_rep_consume.cc" "strings/internal/cord_rep_consume.cc"
"strings/internal/cord_rep_btree.cc" "strings/internal/cord_rep_btree.cc"
"strings/internal/cord_rep_btree.h" "strings/internal/cord_rep_btree.h"
"strings/internal/cord_rep_btree_navigator.cc"
"strings/internal/cord_rep_btree_navigator.h"
"strings/internal/cord_rep_flat.h" "strings/internal/cord_rep_flat.h"
"strings/internal/cord_rep_ring.cc" "strings/internal/cord_rep_ring.cc"
"strings/internal/cord_rep_ring.h" "strings/internal/cord_rep_ring.h"
......
...@@ -37,25 +37,23 @@ inline size_t RandomSeed() { ...@@ -37,25 +37,23 @@ inline size_t RandomSeed() {
return value ^ static_cast<size_t>(reinterpret_cast<uintptr_t>(&counter)); return value ^ static_cast<size_t>(reinterpret_cast<uintptr_t>(&counter));
} }
bool ShouldInsertBackwards(size_t hash, ctrl_t* ctrl) { bool ShouldInsertBackwards(size_t hash, const ctrl_t* ctrl) {
// To avoid problems with weak hashes and single bit tests, we use % 13. // To avoid problems with weak hashes and single bit tests, we use % 13.
// TODO(kfm,sbenza): revisit after we do unconditional mixing // TODO(kfm,sbenza): revisit after we do unconditional mixing
return (H1(hash, ctrl) ^ RandomSeed()) % 13 > 6; return (H1(hash, ctrl) ^ RandomSeed()) % 13 > 6;
} }
void ConvertDeletedToEmptyAndFullToDeleted( void ConvertDeletedToEmptyAndFullToDeleted(ctrl_t* ctrl, size_t capacity) {
ctrl_t* ctrl, size_t capacity) { assert(ctrl[capacity] == ctrl_t::kSentinel);
assert(ctrl[capacity] == kSentinel);
assert(IsValidCapacity(capacity)); assert(IsValidCapacity(capacity));
for (ctrl_t* pos = ctrl; pos < ctrl + capacity; pos += Group::kWidth) { for (ctrl_t* pos = ctrl; pos < ctrl + capacity; pos += Group::kWidth) {
Group{pos}.ConvertSpecialToEmptyAndFullToDeleted(pos); Group{pos}.ConvertSpecialToEmptyAndFullToDeleted(pos);
} }
// Copy the cloned ctrl bytes. // Copy the cloned ctrl bytes.
std::memcpy(ctrl + capacity + 1, ctrl, NumClonedBytes()); std::memcpy(ctrl + capacity + 1, ctrl, NumClonedBytes());
ctrl[capacity] = kSentinel; ctrl[capacity] = ctrl_t::kSentinel;
} }
} // namespace container_internal } // namespace container_internal
ABSL_NAMESPACE_END ABSL_NAMESPACE_END
} // namespace absl } // namespace absl
...@@ -315,9 +315,17 @@ void BM_ReserveStringTable(benchmark::State& state) { ...@@ -315,9 +315,17 @@ void BM_ReserveStringTable(benchmark::State& state) {
} }
BENCHMARK(BM_ReserveStringTable)->Range(128, 4096); BENCHMARK(BM_ReserveStringTable)->Range(128, 4096);
// Like std::iota, except that ctrl_t doesn't support operator++.
template <typename CtrlIter>
void Iota(CtrlIter begin, CtrlIter end, int value) {
for (; begin != end; ++begin, ++value) {
*begin = static_cast<ctrl_t>(value);
}
}
void BM_Group_Match(benchmark::State& state) { void BM_Group_Match(benchmark::State& state) {
std::array<ctrl_t, Group::kWidth> group; std::array<ctrl_t, Group::kWidth> group;
std::iota(group.begin(), group.end(), -4); Iota(group.begin(), group.end(), -4);
Group g{group.data()}; Group g{group.data()};
h2_t h = 1; h2_t h = 1;
for (auto _ : state) { for (auto _ : state) {
...@@ -329,7 +337,7 @@ BENCHMARK(BM_Group_Match); ...@@ -329,7 +337,7 @@ BENCHMARK(BM_Group_Match);
void BM_Group_MatchEmpty(benchmark::State& state) { void BM_Group_MatchEmpty(benchmark::State& state) {
std::array<ctrl_t, Group::kWidth> group; std::array<ctrl_t, Group::kWidth> group;
std::iota(group.begin(), group.end(), -4); Iota(group.begin(), group.end(), -4);
Group g{group.data()}; Group g{group.data()};
for (auto _ : state) ::benchmark::DoNotOptimize(g.MatchEmpty()); for (auto _ : state) ::benchmark::DoNotOptimize(g.MatchEmpty());
} }
...@@ -337,7 +345,7 @@ BENCHMARK(BM_Group_MatchEmpty); ...@@ -337,7 +345,7 @@ BENCHMARK(BM_Group_MatchEmpty);
void BM_Group_MatchEmptyOrDeleted(benchmark::State& state) { void BM_Group_MatchEmptyOrDeleted(benchmark::State& state) {
std::array<ctrl_t, Group::kWidth> group; std::array<ctrl_t, Group::kWidth> group;
std::iota(group.begin(), group.end(), -4); Iota(group.begin(), group.end(), -4);
Group g{group.data()}; Group g{group.data()};
for (auto _ : state) ::benchmark::DoNotOptimize(g.MatchEmptyOrDeleted()); for (auto _ : state) ::benchmark::DoNotOptimize(g.MatchEmptyOrDeleted());
} }
...@@ -345,7 +353,7 @@ BENCHMARK(BM_Group_MatchEmptyOrDeleted); ...@@ -345,7 +353,7 @@ BENCHMARK(BM_Group_MatchEmptyOrDeleted);
void BM_Group_CountLeadingEmptyOrDeleted(benchmark::State& state) { void BM_Group_CountLeadingEmptyOrDeleted(benchmark::State& state) {
std::array<ctrl_t, Group::kWidth> group; std::array<ctrl_t, Group::kWidth> group;
std::iota(group.begin(), group.end(), -2); Iota(group.begin(), group.end(), -2);
Group g{group.data()}; Group g{group.data()};
for (auto _ : state) for (auto _ : state)
::benchmark::DoNotOptimize(g.CountLeadingEmptyOrDeleted()); ::benchmark::DoNotOptimize(g.CountLeadingEmptyOrDeleted());
...@@ -354,7 +362,7 @@ BENCHMARK(BM_Group_CountLeadingEmptyOrDeleted); ...@@ -354,7 +362,7 @@ BENCHMARK(BM_Group_CountLeadingEmptyOrDeleted);
void BM_Group_MatchFirstEmptyOrDeleted(benchmark::State& state) { void BM_Group_MatchFirstEmptyOrDeleted(benchmark::State& state) {
std::array<ctrl_t, Group::kWidth> group; std::array<ctrl_t, Group::kWidth> group;
std::iota(group.begin(), group.end(), -2); Iota(group.begin(), group.end(), -2);
Group g{group.data()}; Group g{group.data()};
for (auto _ : state) ::benchmark::DoNotOptimize(*g.MatchEmptyOrDeleted()); for (auto _ : state) ::benchmark::DoNotOptimize(*g.MatchEmptyOrDeleted());
} }
...@@ -363,8 +371,11 @@ BENCHMARK(BM_Group_MatchFirstEmptyOrDeleted); ...@@ -363,8 +371,11 @@ BENCHMARK(BM_Group_MatchFirstEmptyOrDeleted);
void BM_DropDeletes(benchmark::State& state) { void BM_DropDeletes(benchmark::State& state) {
constexpr size_t capacity = (1 << 20) - 1; constexpr size_t capacity = (1 << 20) - 1;
std::vector<ctrl_t> ctrl(capacity + 1 + Group::kWidth); std::vector<ctrl_t> ctrl(capacity + 1 + Group::kWidth);
ctrl[capacity] = kSentinel; ctrl[capacity] = ctrl_t::kSentinel;
std::vector<ctrl_t> pattern = {kEmpty, 2, kDeleted, 2, kEmpty, 1, kDeleted}; std::vector<ctrl_t> pattern = {ctrl_t::kEmpty, static_cast<ctrl_t>(2),
ctrl_t::kDeleted, static_cast<ctrl_t>(2),
ctrl_t::kEmpty, static_cast<ctrl_t>(1),
ctrl_t::kDeleted};
for (size_t i = 0; i != capacity; ++i) { for (size_t i = 0; i != capacity; ++i) {
ctrl[i] = pattern[i % pattern.size()]; ctrl[i] = pattern[i % pattern.size()];
} }
......
...@@ -58,6 +58,9 @@ using ::testing::Lt; ...@@ -58,6 +58,9 @@ using ::testing::Lt;
using ::testing::Pair; using ::testing::Pair;
using ::testing::UnorderedElementsAre; using ::testing::UnorderedElementsAre;
// Convenience function to static cast to ctrl_t.
ctrl_t CtrlT(int i) { return static_cast<ctrl_t>(i); }
TEST(Util, NormalizeCapacity) { TEST(Util, NormalizeCapacity) {
EXPECT_EQ(1, NormalizeCapacity(0)); EXPECT_EQ(1, NormalizeCapacity(0));
EXPECT_EQ(1, NormalizeCapacity(1)); EXPECT_EQ(1, NormalizeCapacity(1));
...@@ -170,15 +173,19 @@ TEST(Group, EmptyGroup) { ...@@ -170,15 +173,19 @@ TEST(Group, EmptyGroup) {
TEST(Group, Match) { TEST(Group, Match) {
if (Group::kWidth == 16) { if (Group::kWidth == 16) {
ctrl_t group[] = {kEmpty, 1, kDeleted, 3, kEmpty, 5, kSentinel, 7, ctrl_t group[] = {ctrl_t::kEmpty, CtrlT(1), ctrl_t::kDeleted, CtrlT(3),
7, 5, 3, 1, 1, 1, 1, 1}; ctrl_t::kEmpty, CtrlT(5), ctrl_t::kSentinel, CtrlT(7),
CtrlT(7), CtrlT(5), CtrlT(3), CtrlT(1),
CtrlT(1), CtrlT(1), CtrlT(1), CtrlT(1)};
EXPECT_THAT(Group{group}.Match(0), ElementsAre()); EXPECT_THAT(Group{group}.Match(0), ElementsAre());
EXPECT_THAT(Group{group}.Match(1), ElementsAre(1, 11, 12, 13, 14, 15)); EXPECT_THAT(Group{group}.Match(1), ElementsAre(1, 11, 12, 13, 14, 15));
EXPECT_THAT(Group{group}.Match(3), ElementsAre(3, 10)); EXPECT_THAT(Group{group}.Match(3), ElementsAre(3, 10));
EXPECT_THAT(Group{group}.Match(5), ElementsAre(5, 9)); EXPECT_THAT(Group{group}.Match(5), ElementsAre(5, 9));
EXPECT_THAT(Group{group}.Match(7), ElementsAre(7, 8)); EXPECT_THAT(Group{group}.Match(7), ElementsAre(7, 8));
} else if (Group::kWidth == 8) { } else if (Group::kWidth == 8) {
ctrl_t group[] = {kEmpty, 1, 2, kDeleted, 2, 1, kSentinel, 1}; ctrl_t group[] = {ctrl_t::kEmpty, CtrlT(1), CtrlT(2),
ctrl_t::kDeleted, CtrlT(2), CtrlT(1),
ctrl_t::kSentinel, CtrlT(1)};
EXPECT_THAT(Group{group}.Match(0), ElementsAre()); EXPECT_THAT(Group{group}.Match(0), ElementsAre());
EXPECT_THAT(Group{group}.Match(1), ElementsAre(1, 5, 7)); EXPECT_THAT(Group{group}.Match(1), ElementsAre(1, 5, 7));
EXPECT_THAT(Group{group}.Match(2), ElementsAre(2, 4)); EXPECT_THAT(Group{group}.Match(2), ElementsAre(2, 4));
...@@ -189,11 +196,15 @@ TEST(Group, Match) { ...@@ -189,11 +196,15 @@ TEST(Group, Match) {
TEST(Group, MatchEmpty) { TEST(Group, MatchEmpty) {
if (Group::kWidth == 16) { if (Group::kWidth == 16) {
ctrl_t group[] = {kEmpty, 1, kDeleted, 3, kEmpty, 5, kSentinel, 7, ctrl_t group[] = {ctrl_t::kEmpty, CtrlT(1), ctrl_t::kDeleted, CtrlT(3),
7, 5, 3, 1, 1, 1, 1, 1}; ctrl_t::kEmpty, CtrlT(5), ctrl_t::kSentinel, CtrlT(7),
CtrlT(7), CtrlT(5), CtrlT(3), CtrlT(1),
CtrlT(1), CtrlT(1), CtrlT(1), CtrlT(1)};
EXPECT_THAT(Group{group}.MatchEmpty(), ElementsAre(0, 4)); EXPECT_THAT(Group{group}.MatchEmpty(), ElementsAre(0, 4));
} else if (Group::kWidth == 8) { } else if (Group::kWidth == 8) {
ctrl_t group[] = {kEmpty, 1, 2, kDeleted, 2, 1, kSentinel, 1}; ctrl_t group[] = {ctrl_t::kEmpty, CtrlT(1), CtrlT(2),
ctrl_t::kDeleted, CtrlT(2), CtrlT(1),
ctrl_t::kSentinel, CtrlT(1)};
EXPECT_THAT(Group{group}.MatchEmpty(), ElementsAre(0)); EXPECT_THAT(Group{group}.MatchEmpty(), ElementsAre(0));
} else { } else {
FAIL() << "No test coverage for Group::kWidth==" << Group::kWidth; FAIL() << "No test coverage for Group::kWidth==" << Group::kWidth;
...@@ -202,11 +213,15 @@ TEST(Group, MatchEmpty) { ...@@ -202,11 +213,15 @@ TEST(Group, MatchEmpty) {
TEST(Group, MatchEmptyOrDeleted) { TEST(Group, MatchEmptyOrDeleted) {
if (Group::kWidth == 16) { if (Group::kWidth == 16) {
ctrl_t group[] = {kEmpty, 1, kDeleted, 3, kEmpty, 5, kSentinel, 7, ctrl_t group[] = {ctrl_t::kEmpty, CtrlT(1), ctrl_t::kDeleted, CtrlT(3),
7, 5, 3, 1, 1, 1, 1, 1}; ctrl_t::kEmpty, CtrlT(5), ctrl_t::kSentinel, CtrlT(7),
CtrlT(7), CtrlT(5), CtrlT(3), CtrlT(1),
CtrlT(1), CtrlT(1), CtrlT(1), CtrlT(1)};
EXPECT_THAT(Group{group}.MatchEmptyOrDeleted(), ElementsAre(0, 2, 4)); EXPECT_THAT(Group{group}.MatchEmptyOrDeleted(), ElementsAre(0, 2, 4));
} else if (Group::kWidth == 8) { } else if (Group::kWidth == 8) {
ctrl_t group[] = {kEmpty, 1, 2, kDeleted, 2, 1, kSentinel, 1}; ctrl_t group[] = {ctrl_t::kEmpty, CtrlT(1), CtrlT(2),
ctrl_t::kDeleted, CtrlT(2), CtrlT(1),
ctrl_t::kSentinel, CtrlT(1)};
EXPECT_THAT(Group{group}.MatchEmptyOrDeleted(), ElementsAre(0, 3)); EXPECT_THAT(Group{group}.MatchEmptyOrDeleted(), ElementsAre(0, 3));
} else { } else {
FAIL() << "No test coverage for Group::kWidth==" << Group::kWidth; FAIL() << "No test coverage for Group::kWidth==" << Group::kWidth;
...@@ -217,28 +232,32 @@ TEST(Batch, DropDeletes) { ...@@ -217,28 +232,32 @@ TEST(Batch, DropDeletes) {
constexpr size_t kCapacity = 63; constexpr size_t kCapacity = 63;
constexpr size_t kGroupWidth = container_internal::Group::kWidth; constexpr size_t kGroupWidth = container_internal::Group::kWidth;
std::vector<ctrl_t> ctrl(kCapacity + 1 + kGroupWidth); std::vector<ctrl_t> ctrl(kCapacity + 1 + kGroupWidth);
ctrl[kCapacity] = kSentinel; ctrl[kCapacity] = ctrl_t::kSentinel;
std::vector<ctrl_t> pattern = {kEmpty, 2, kDeleted, 2, kEmpty, 1, kDeleted}; std::vector<ctrl_t> pattern = {
ctrl_t::kEmpty, CtrlT(2), ctrl_t::kDeleted, CtrlT(2),
ctrl_t::kEmpty, CtrlT(1), ctrl_t::kDeleted};
for (size_t i = 0; i != kCapacity; ++i) { for (size_t i = 0; i != kCapacity; ++i) {
ctrl[i] = pattern[i % pattern.size()]; ctrl[i] = pattern[i % pattern.size()];
if (i < kGroupWidth - 1) if (i < kGroupWidth - 1)
ctrl[i + kCapacity + 1] = pattern[i % pattern.size()]; ctrl[i + kCapacity + 1] = pattern[i % pattern.size()];
} }
ConvertDeletedToEmptyAndFullToDeleted(ctrl.data(), kCapacity); ConvertDeletedToEmptyAndFullToDeleted(ctrl.data(), kCapacity);
ASSERT_EQ(ctrl[kCapacity], kSentinel); ASSERT_EQ(ctrl[kCapacity], ctrl_t::kSentinel);
for (size_t i = 0; i < kCapacity + kGroupWidth; ++i) { for (size_t i = 0; i < kCapacity + kGroupWidth; ++i) {
ctrl_t expected = pattern[i % (kCapacity + 1) % pattern.size()]; ctrl_t expected = pattern[i % (kCapacity + 1) % pattern.size()];
if (i == kCapacity) expected = kSentinel; if (i == kCapacity) expected = ctrl_t::kSentinel;
if (expected == kDeleted) expected = kEmpty; if (expected == ctrl_t::kDeleted) expected = ctrl_t::kEmpty;
if (IsFull(expected)) expected = kDeleted; if (IsFull(expected)) expected = ctrl_t::kDeleted;
EXPECT_EQ(ctrl[i], expected) EXPECT_EQ(ctrl[i], expected)
<< i << " " << int{pattern[i % pattern.size()]}; << i << " " << static_cast<int>(pattern[i % pattern.size()]);
} }
} }
TEST(Group, CountLeadingEmptyOrDeleted) { TEST(Group, CountLeadingEmptyOrDeleted) {
const std::vector<ctrl_t> empty_examples = {kEmpty, kDeleted}; const std::vector<ctrl_t> empty_examples = {ctrl_t::kEmpty, ctrl_t::kDeleted};
const std::vector<ctrl_t> full_examples = {0, 1, 2, 3, 5, 9, 127, kSentinel}; const std::vector<ctrl_t> full_examples = {
CtrlT(0), CtrlT(1), CtrlT(2), CtrlT(3),
CtrlT(5), CtrlT(9), CtrlT(127), ctrl_t::kSentinel};
for (ctrl_t empty : empty_examples) { for (ctrl_t empty : empty_examples) {
std::vector<ctrl_t> e(Group::kWidth, empty); std::vector<ctrl_t> e(Group::kWidth, empty);
...@@ -871,7 +890,7 @@ TEST(Table, RehashWithNoResize) { ...@@ -871,7 +890,7 @@ TEST(Table, RehashWithNoResize) {
const size_t capacity = t.capacity(); const size_t capacity = t.capacity();
// Remove elements from all groups except the first and the last one. // Remove elements from all groups except the first and the last one.
// All elements removed from full groups will be marked as kDeleted. // All elements removed from full groups will be marked as ctrl_t::kDeleted.
const size_t erase_begin = Group::kWidth / 2; const size_t erase_begin = Group::kWidth / 2;
const size_t erase_end = (t.size() / Group::kWidth - 1) * Group::kWidth; const size_t erase_end = (t.size() / Group::kWidth - 1) * Group::kWidth;
for (size_t i = erase_begin; i < erase_end; ++i) { for (size_t i = erase_begin; i < erase_end; ++i) {
......
...@@ -34,6 +34,7 @@ cc_library( ...@@ -34,6 +34,7 @@ cc_library(
"internal/stacktrace_aarch64-inl.inc", "internal/stacktrace_aarch64-inl.inc",
"internal/stacktrace_arm-inl.inc", "internal/stacktrace_arm-inl.inc",
"internal/stacktrace_config.h", "internal/stacktrace_config.h",
"internal/stacktrace_emscripten-inl.inc",
"internal/stacktrace_generic-inl.inc", "internal/stacktrace_generic-inl.inc",
"internal/stacktrace_powerpc-inl.inc", "internal/stacktrace_powerpc-inl.inc",
"internal/stacktrace_unimplemented-inl.inc", "internal/stacktrace_unimplemented-inl.inc",
...@@ -57,6 +58,7 @@ cc_library( ...@@ -57,6 +58,7 @@ cc_library(
"symbolize.cc", "symbolize.cc",
"symbolize_darwin.inc", "symbolize_darwin.inc",
"symbolize_elf.inc", "symbolize_elf.inc",
"symbolize_emscripten.inc",
"symbolize_unimplemented.inc", "symbolize_unimplemented.inc",
"symbolize_win32.inc", "symbolize_win32.inc",
], ],
......
...@@ -22,6 +22,7 @@ absl_cc_library( ...@@ -22,6 +22,7 @@ absl_cc_library(
"internal/stacktrace_aarch64-inl.inc" "internal/stacktrace_aarch64-inl.inc"
"internal/stacktrace_arm-inl.inc" "internal/stacktrace_arm-inl.inc"
"internal/stacktrace_config.h" "internal/stacktrace_config.h"
"internal/stacktrace_emscripten-inl.inc"
"internal/stacktrace_generic-inl.inc" "internal/stacktrace_generic-inl.inc"
"internal/stacktrace_powerpc-inl.inc" "internal/stacktrace_powerpc-inl.inc"
"internal/stacktrace_unimplemented-inl.inc" "internal/stacktrace_unimplemented-inl.inc"
...@@ -48,6 +49,7 @@ absl_cc_library( ...@@ -48,6 +49,7 @@ absl_cc_library(
"symbolize.cc" "symbolize.cc"
"symbolize_darwin.inc" "symbolize_darwin.inc"
"symbolize_elf.inc" "symbolize_elf.inc"
"symbolize_emscripten.inc"
"symbolize_unimplemented.inc" "symbolize_unimplemented.inc"
"symbolize_win32.inc" "symbolize_win32.inc"
COPTS COPTS
......
...@@ -37,6 +37,10 @@ ...@@ -37,6 +37,10 @@
"absl/debugging/internal/stacktrace_generic-inl.inc" "absl/debugging/internal/stacktrace_generic-inl.inc"
#endif #endif
#elif defined(__EMSCRIPTEN__)
#define ABSL_STACKTRACE_INL_HEADER \
"absl/debugging/internal/stacktrace_emscripten-inl.inc"
#elif defined(__linux__) && !defined(__ANDROID__) #elif defined(__linux__) && !defined(__ANDROID__)
#if defined(NO_FRAME_POINTER) && \ #if defined(NO_FRAME_POINTER) && \
...@@ -68,7 +72,6 @@ ...@@ -68,7 +72,6 @@
"absl/debugging/internal/stacktrace_generic-inl.inc" "absl/debugging/internal/stacktrace_generic-inl.inc"
#endif #endif
#endif #endif
#endif #endif
// Fallback to the empty implementation. // Fallback to the empty implementation.
......
// Copyright 2017 The Abseil Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// Portable implementation - just use glibc
//
// Note: The glibc implementation may cause a call to malloc.
// This can cause a deadlock in HeapProfiler.
#ifndef ABSL_DEBUGGING_INTERNAL_STACKTRACE_EMSCRIPTEN_INL_H_
#define ABSL_DEBUGGING_INTERNAL_STACKTRACE_EMSCRIPTEN_INL_H_
#include <emscripten.h>
#include <atomic>
#include <cstring>
#include "absl/base/attributes.h"
#include "absl/debugging/stacktrace.h"
extern "C" {
uintptr_t emscripten_stack_snapshot();
uint32_t emscripten_stack_unwind_buffer(uintptr_t pc, void *buffer,
uint32_t depth);
}
// Sometimes, we can try to get a stack trace from within a stack
// trace, which can cause a self-deadlock.
// Protect against such reentrant call by failing to get a stack trace.
//
// We use __thread here because the code here is extremely low level -- it is
// called while collecting stack traces from within malloc and mmap, and thus
// can not call anything which might call malloc or mmap itself.
static __thread int recursive = 0;
// The stack trace function might be invoked very early in the program's
// execution (e.g. from the very first malloc).
// As such, we suppress usage of backtrace during this early stage of execution.
static std::atomic<bool> disable_stacktraces(true); // Disabled until healthy.
// Waiting until static initializers run seems to be late enough.
// This file is included into stacktrace.cc so this will only run once.
ABSL_ATTRIBUTE_UNUSED static int stacktraces_enabler = []() {
// Check if we can even create stacktraces. If not, bail early and leave
// disable_stacktraces set as-is.
// clang-format off
if (!EM_ASM_INT({ return (typeof wasmOffsetConverter !== 'undefined'); })) {
return 0;
}
// clang-format on
disable_stacktraces.store(false, std::memory_order_relaxed);
return 0;
}();
template <bool IS_STACK_FRAMES, bool IS_WITH_CONTEXT>
static int UnwindImpl(void **result, int *sizes, int max_depth, int skip_count,
const void *ucp, int *min_dropped_frames) {
if (recursive || disable_stacktraces.load(std::memory_order_relaxed)) {
return 0;
}
++recursive;
static_cast<void>(ucp); // Unused.
constexpr int kStackLength = 64;
void *stack[kStackLength];
int size;
uintptr_t pc = emscripten_stack_snapshot();
size = emscripten_stack_unwind_buffer(pc, stack, kStackLength);
int result_count = size - skip_count;
if (result_count < 0) result_count = 0;
if (result_count > max_depth) result_count = max_depth;
for (int i = 0; i < result_count; i++) result[i] = stack[i + skip_count];
if (IS_STACK_FRAMES) {
// No implementation for finding out the stack frame sizes yet.
memset(sizes, 0, sizeof(*sizes) * result_count);
}
if (min_dropped_frames != nullptr) {
if (size - skip_count - max_depth > 0) {
*min_dropped_frames = size - skip_count - max_depth;
} else {
*min_dropped_frames = 0;
}
}
--recursive;
return result_count;
}
namespace absl {
ABSL_NAMESPACE_BEGIN
namespace debugging_internal {
bool StackTraceWorksForTest() { return true; }
} // namespace debugging_internal
ABSL_NAMESPACE_END
} // namespace absl
#endif // ABSL_DEBUGGING_INTERNAL_STACKTRACE_EMSCRIPTEN_INL_H_
...@@ -68,6 +68,12 @@ ABSL_NAMESPACE_END ...@@ -68,6 +68,12 @@ ABSL_NAMESPACE_END
#define ABSL_INTERNAL_HAVE_DARWIN_SYMBOLIZE 1 #define ABSL_INTERNAL_HAVE_DARWIN_SYMBOLIZE 1
#endif #endif
#ifdef ABSL_INTERNAL_HAVE_EMSCRIPTEN_SYMBOLIZE
#error ABSL_INTERNAL_HAVE_EMSCRIPTEN_SYMBOLIZE cannot be directly set
#elif defined(__EMSCRIPTEN__)
#define ABSL_INTERNAL_HAVE_EMSCRIPTEN_SYMBOLIZE 1
#endif
namespace absl { namespace absl {
ABSL_NAMESPACE_BEGIN ABSL_NAMESPACE_BEGIN
namespace debugging_internal { namespace debugging_internal {
......
...@@ -49,6 +49,7 @@ ...@@ -49,6 +49,7 @@
# include "absl/debugging/internal/stacktrace_aarch64-inl.inc" # include "absl/debugging/internal/stacktrace_aarch64-inl.inc"
# include "absl/debugging/internal/stacktrace_arm-inl.inc" # include "absl/debugging/internal/stacktrace_arm-inl.inc"
# include "absl/debugging/internal/stacktrace_emscripten-inl.inc"
# include "absl/debugging/internal/stacktrace_generic-inl.inc" # include "absl/debugging/internal/stacktrace_generic-inl.inc"
# include "absl/debugging/internal/stacktrace_powerpc-inl.inc" # include "absl/debugging/internal/stacktrace_powerpc-inl.inc"
# include "absl/debugging/internal/stacktrace_unimplemented-inl.inc" # include "absl/debugging/internal/stacktrace_unimplemented-inl.inc"
......
...@@ -31,6 +31,8 @@ ...@@ -31,6 +31,8 @@
#include "absl/debugging/symbolize_win32.inc" #include "absl/debugging/symbolize_win32.inc"
#elif defined(__APPLE__) #elif defined(__APPLE__)
#include "absl/debugging/symbolize_darwin.inc" #include "absl/debugging/symbolize_darwin.inc"
#elif defined(__EMSCRIPTEN__)
#include "absl/debugging/symbolize_emscripten.inc"
#else #else
#include "absl/debugging/symbolize_unimplemented.inc" #include "absl/debugging/symbolize_unimplemented.inc"
#endif #endif
// Copyright 2020 The Abseil Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include <cxxabi.h>
#include <emscripten.h>
#include <algorithm>
#include <cstring>
#include "absl/base/internal/raw_logging.h"
#include "absl/debugging/internal/demangle.h"
#include "absl/strings/numbers.h"
#include "absl/strings/str_cat.h"
#include "absl/strings/string_view.h"
extern "C" {
const char* emscripten_pc_get_function(const void* pc);
}
// clang-format off
EM_JS(bool, HaveOffsetConverter, (),
{ return typeof wasmOffsetConverter !== 'undefined'; });
// clang-format on
namespace absl {
ABSL_NAMESPACE_BEGIN
void InitializeSymbolizer(const char*) {
if (!HaveOffsetConverter()) {
ABSL_RAW_LOG(INFO,
"Symbolization unavailable. Rebuild with -sWASM=1 "
"and -sUSE_OFFSET_CONVERTER=1.");
}
}
bool Symbolize(const void* pc, char* out, int out_size) {
// Check if we have the offset converter necessary for pc_get_function.
// Without it, the program will abort().
if (!HaveOffsetConverter()) {
return false;
}
const char* func_name = emscripten_pc_get_function(pc);
if (func_name == nullptr) {
return false;
}
strncpy(out, func_name, out_size);
if (out[out_size - 1] != '\0') {
// strncpy() does not '\0' terminate when it truncates.
static constexpr char kEllipsis[] = "...";
int ellipsis_size = std::min<int>(sizeof(kEllipsis) - 1, out_size - 1);
memcpy(out + out_size - ellipsis_size - 1, kEllipsis, ellipsis_size);
out[out_size - 1] = '\0';
}
return true;
}
ABSL_NAMESPACE_END
} // namespace absl
...@@ -146,8 +146,22 @@ static const char *TrySymbolize(void *pc) { ...@@ -146,8 +146,22 @@ static const char *TrySymbolize(void *pc) {
return TrySymbolizeWithLimit(pc, sizeof(try_symbolize_buffer)); return TrySymbolizeWithLimit(pc, sizeof(try_symbolize_buffer));
} }
#if defined(ABSL_INTERNAL_HAVE_ELF_SYMBOLIZE) || \ #if defined(ABSL_INTERNAL_HAVE_ELF_SYMBOLIZE) || \
defined(ABSL_INTERNAL_HAVE_DARWIN_SYMBOLIZE) defined(ABSL_INTERNAL_HAVE_DARWIN_SYMBOLIZE) || \
defined(ABSL_INTERNAL_HAVE_EMSCRIPTEN_SYMBOLIZE)
// Test with a return address.
void ABSL_ATTRIBUTE_NOINLINE TestWithReturnAddress() {
#if defined(ABSL_HAVE_ATTRIBUTE_NOINLINE)
void *return_address = __builtin_return_address(0);
const char *symbol = TrySymbolize(return_address);
ABSL_RAW_CHECK(symbol != nullptr, "TestWithReturnAddress failed");
ABSL_RAW_CHECK(strcmp(symbol, "main") == 0, "TestWithReturnAddress failed");
std::cout << "TestWithReturnAddress passed" << std::endl;
#endif
}
#ifndef ABSL_INTERNAL_HAVE_EMSCRIPTEN_SYMBOLIZE
TEST(Symbolize, Cached) { TEST(Symbolize, Cached) {
// Compilers should give us pointers to them. // Compilers should give us pointers to them.
...@@ -418,6 +432,7 @@ TEST(Symbolize, ForEachSection) { ...@@ -418,6 +432,7 @@ TEST(Symbolize, ForEachSection) {
close(fd); close(fd);
} }
#endif // !ABSL_INTERNAL_HAVE_DARWIN_SYMBOLIZE #endif // !ABSL_INTERNAL_HAVE_DARWIN_SYMBOLIZE
#endif // !ABSL_INTERNAL_HAVE_EMSCRIPTEN_SYMBOLIZE
// x86 specific tests. Uses some inline assembler. // x86 specific tests. Uses some inline assembler.
extern "C" { extern "C" {
...@@ -466,17 +481,6 @@ void ABSL_ATTRIBUTE_NOINLINE TestWithPCInsideInlineFunction() { ...@@ -466,17 +481,6 @@ void ABSL_ATTRIBUTE_NOINLINE TestWithPCInsideInlineFunction() {
} }
} }
// Test with a return address.
void ABSL_ATTRIBUTE_NOINLINE TestWithReturnAddress() {
#if defined(ABSL_HAVE_ATTRIBUTE_NOINLINE)
void *return_address = __builtin_return_address(0);
const char *symbol = TrySymbolize(return_address);
ABSL_RAW_CHECK(symbol != nullptr, "TestWithReturnAddress failed");
ABSL_RAW_CHECK(strcmp(symbol, "main") == 0, "TestWithReturnAddress failed");
std::cout << "TestWithReturnAddress passed" << std::endl;
#endif
}
#if defined(__arm__) && ABSL_HAVE_ATTRIBUTE(target) #if defined(__arm__) && ABSL_HAVE_ATTRIBUTE(target)
// Test that we correctly identify bounds of Thumb functions on ARM. // Test that we correctly identify bounds of Thumb functions on ARM.
// //
...@@ -559,7 +563,6 @@ TEST(Symbolize, SymbolizeWithDemangling) { ...@@ -559,7 +563,6 @@ TEST(Symbolize, SymbolizeWithDemangling) {
#endif // !defined(ABSL_CONSUME_DLL) #endif // !defined(ABSL_CONSUME_DLL)
#else // Symbolizer unimplemented #else // Symbolizer unimplemented
TEST(Symbolize, Unimplemented) { TEST(Symbolize, Unimplemented) {
char buf[64]; char buf[64];
EXPECT_FALSE(absl::Symbolize((void *)(&nonstatic_func), buf, sizeof(buf))); EXPECT_FALSE(absl::Symbolize((void *)(&nonstatic_func), buf, sizeof(buf)));
......
...@@ -80,7 +80,7 @@ ABSL_NAMESPACE_BEGIN ...@@ -80,7 +80,7 @@ ABSL_NAMESPACE_BEGIN
// `kFailedPrecondition` if both codes apply. Similarly prefer `kNotFound` or // `kFailedPrecondition` if both codes apply. Similarly prefer `kNotFound` or
// `kAlreadyExists` over `kFailedPrecondition`. // `kAlreadyExists` over `kFailedPrecondition`.
// //
// Because these errors may travel RPC boundaries, these codes are tied to the // Because these errors may cross RPC boundaries, these codes are tied to the
// `google.rpc.Code` definitions within // `google.rpc.Code` definitions within
// https://github.com/googleapis/googleapis/blob/master/google/rpc/code.proto // https://github.com/googleapis/googleapis/blob/master/google/rpc/code.proto
// The string value of these RPC codes is denoted within each enum below. // The string value of these RPC codes is denoted within each enum below.
...@@ -114,10 +114,10 @@ enum class StatusCode : int { ...@@ -114,10 +114,10 @@ enum class StatusCode : int {
// StatusCode::kInvalidArgument // StatusCode::kInvalidArgument
// //
// kInvalidArgument (gRPC code "INVALID_ARGUMENT") indicates the caller // kInvalidArgument (gRPC code "INVALID_ARGUMENT") indicates the caller
// specified an invalid argument, such a malformed filename. Note that such // specified an invalid argument, such as a malformed filename. Note that use
// errors should be narrowly limited to indicate to the invalid nature of the // of such errors should be narrowly limited to indicate the invalid nature of
// arguments themselves. Errors with validly formed arguments that may cause // the arguments themselves. Errors with validly formed arguments that may
// errors with the state of the receiving system should be denoted with // cause errors with the state of the receiving system should be denoted with
// `kFailedPrecondition` instead. // `kFailedPrecondition` instead.
kInvalidArgument = 3, kInvalidArgument = 3,
...@@ -137,14 +137,15 @@ enum class StatusCode : int { ...@@ -137,14 +137,15 @@ enum class StatusCode : int {
// //
// `kNotFound` is useful if a request should be denied for an entire class of // `kNotFound` is useful if a request should be denied for an entire class of
// users, such as during a gradual feature rollout or undocumented allow list. // users, such as during a gradual feature rollout or undocumented allow list.
// If, instead, a request should be denied for specific sets of users, such as // If a request should be denied for specific sets of users, such as through
// through user-based access control, use `kPermissionDenied` instead. // user-based access control, use `kPermissionDenied` instead.
kNotFound = 5, kNotFound = 5,
// StatusCode::kAlreadyExists // StatusCode::kAlreadyExists
// //
// kAlreadyExists (gRPC code "ALREADY_EXISTS") indicates the entity that a // kAlreadyExists (gRPC code "ALREADY_EXISTS") indicates that the entity a
// caller attempted to create (such as file or directory) is already present. // caller attempted to create (such as a file or directory) is already
// present.
kAlreadyExists = 6, kAlreadyExists = 6,
// StatusCode::kPermissionDenied // StatusCode::kPermissionDenied
...@@ -183,7 +184,7 @@ enum class StatusCode : int { ...@@ -183,7 +184,7 @@ enum class StatusCode : int {
// level (such as when a client-specified test-and-set fails, indicating // level (such as when a client-specified test-and-set fails, indicating
// the client should restart a read-modify-write sequence). // the client should restart a read-modify-write sequence).
// (c) Use `kFailedPrecondition` if the client should not retry until // (c) Use `kFailedPrecondition` if the client should not retry until
// the system state has been explicitly fixed. For example, if an "rmdir" // the system state has been explicitly fixed. For example, if a "rmdir"
// fails because the directory is non-empty, `kFailedPrecondition` // fails because the directory is non-empty, `kFailedPrecondition`
// should be returned since the client should not retry unless // should be returned since the client should not retry unless
// the files are deleted from the directory. // the files are deleted from the directory.
...@@ -283,7 +284,7 @@ std::ostream& operator<<(std::ostream& os, StatusCode code); ...@@ -283,7 +284,7 @@ std::ostream& operator<<(std::ostream& os, StatusCode code);
// absl::StatusToStringMode // absl::StatusToStringMode
// //
// An `absl::StatusToStringMode` is an enumerated type indicating how // An `absl::StatusToStringMode` is an enumerated type indicating how
// `absl::Status::ToString()` should construct the output string for an non-ok // `absl::Status::ToString()` should construct the output string for a non-ok
// status. // status.
enum class StatusToStringMode : int { enum class StatusToStringMode : int {
// ToString will not contain any extra data (such as payloads). It will only // ToString will not contain any extra data (such as payloads). It will only
......
...@@ -270,12 +270,14 @@ cc_library( ...@@ -270,12 +270,14 @@ cc_library(
srcs = [ srcs = [
"internal/cord_internal.cc", "internal/cord_internal.cc",
"internal/cord_rep_btree.cc", "internal/cord_rep_btree.cc",
"internal/cord_rep_btree_navigator.cc",
"internal/cord_rep_consume.cc", "internal/cord_rep_consume.cc",
"internal/cord_rep_ring.cc", "internal/cord_rep_ring.cc",
], ],
hdrs = [ hdrs = [
"internal/cord_internal.h", "internal/cord_internal.h",
"internal/cord_rep_btree.h", "internal/cord_rep_btree.h",
"internal/cord_rep_btree_navigator.h",
"internal/cord_rep_consume.h", "internal/cord_rep_consume.h",
"internal/cord_rep_flat.h", "internal/cord_rep_flat.h",
"internal/cord_rep_ring.h", "internal/cord_rep_ring.h",
...@@ -318,6 +320,22 @@ cc_test( ...@@ -318,6 +320,22 @@ cc_test(
], ],
) )
cc_test(
name = "cord_rep_btree_navigator_test",
size = "medium",
srcs = ["internal/cord_rep_btree_navigator_test.cc"],
copts = ABSL_TEST_COPTS,
visibility = ["//visibility:private"],
deps = [
":cord_internal",
":cord_rep_test_util",
":strings",
"//absl/base:config",
"//absl/base:raw_logging_internal",
"@com_google_googletest//:gtest_main",
],
)
cc_library( cc_library(
name = "cordz_update_tracker", name = "cordz_update_tracker",
hdrs = ["internal/cordz_update_tracker.h"], hdrs = ["internal/cordz_update_tracker.h"],
......
...@@ -556,6 +556,7 @@ absl_cc_library( ...@@ -556,6 +556,7 @@ absl_cc_library(
HDRS HDRS
"internal/cord_internal.h" "internal/cord_internal.h"
"internal/cord_rep_btree.h" "internal/cord_rep_btree.h"
"internal/cord_rep_btree_navigator.h"
"internal/cord_rep_consume.h" "internal/cord_rep_consume.h"
"internal/cord_rep_flat.h" "internal/cord_rep_flat.h"
"internal/cord_rep_ring.h" "internal/cord_rep_ring.h"
...@@ -563,6 +564,7 @@ absl_cc_library( ...@@ -563,6 +564,7 @@ absl_cc_library(
SRCS SRCS
"internal/cord_internal.cc" "internal/cord_internal.cc"
"internal/cord_rep_btree.cc" "internal/cord_rep_btree.cc"
"internal/cord_rep_btree_navigator.cc"
"internal/cord_rep_consume.cc" "internal/cord_rep_consume.cc"
"internal/cord_rep_ring.cc" "internal/cord_rep_ring.cc"
COPTS COPTS
...@@ -959,6 +961,24 @@ absl_cc_test( ...@@ -959,6 +961,24 @@ absl_cc_test(
absl_cc_test( absl_cc_test(
NAME NAME
cord_rep_btree_navigator_test
SRCS
"internal/cord_rep_btree_navigator_test.cc"
COPTS
${ABSL_TEST_COPTS}
DEPS
absl::base
absl::config
absl::cord_internal
absl::cord_rep_test_util
absl::core_headers
absl::raw_logging_internal
absl::strings
GTest::gmock_main
)
absl_cc_test(
NAME
cord_ring_test cord_ring_test
SRCS SRCS
"cord_ring_test.cc" "cord_ring_test.cc"
......
...@@ -507,9 +507,9 @@ inline const CordRepBtree* CordRep::btree() const { ...@@ -507,9 +507,9 @@ inline const CordRepBtree* CordRep::btree() const {
inline void CordRepBtree::InitInstance(int height, size_t begin, size_t end) { inline void CordRepBtree::InitInstance(int height, size_t begin, size_t end) {
tag = BTREE; tag = BTREE;
storage[0] = height; storage[0] = static_cast<uint8_t>(height);
storage[1] = begin; storage[1] = static_cast<uint8_t>(begin);
storage[2] = end; storage[2] = static_cast<uint8_t>(end);
} }
inline CordRep* CordRepBtree::Edge(size_t index) const { inline CordRep* CordRepBtree::Edge(size_t index) const {
......
// Copyright 2021 The Abseil Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "absl/strings/internal/cord_rep_btree_navigator.h"
#include <cassert>
#include "absl/strings/internal/cord_internal.h"
#include "absl/strings/internal/cord_rep_btree.h"
namespace absl {
ABSL_NAMESPACE_BEGIN
namespace cord_internal {
using ReadResult = CordRepBtreeNavigator::ReadResult;
namespace {
// Returns a `CordRepSubstring` from `rep` starting at `offset` of size `n`.
// If `rep` is already a `CordRepSubstring` instance, an adjusted instance is
// created based on the old offset and new offset.
// Adopts a reference on `rep`. Rep must be a valid data edge. Returns
// nullptr if `n == 0`, `rep` if `n == rep->length`.
// Requires `offset < rep->length` and `offset + n <= rep->length`.
// TODO(192061034): move to utility library in internal and optimize for small
// substrings of larger reps.
inline CordRep* Substring(CordRep* rep, size_t offset, size_t n) {
assert(n <= rep->length);
assert(offset < rep->length);
assert(offset <= rep->length - n);
assert(CordRepBtree::IsDataEdge(rep));
if (n == 0) return nullptr;
if (n == rep->length) return CordRep::Ref(rep);
if (rep->tag == SUBSTRING) {
offset += rep->substring()->start;
rep = rep->substring()->child;
}
CordRepSubstring* substring = new CordRepSubstring();
substring->length = n;
substring->tag = SUBSTRING;
substring->start = offset;
substring->child = CordRep::Ref(rep);
return substring;
}
inline CordRep* Substring(CordRep* rep, size_t offset) {
return Substring(rep, offset, rep->length - offset);
}
} // namespace
CordRepBtreeNavigator::Position CordRepBtreeNavigator::Skip(size_t n) {
int height = 0;
size_t index = index_[0];
CordRepBtree* node = node_[0];
CordRep* edge = node->Edge(index);
// Overall logic: Find an edge of at least the length we need to skip.
// We consume all edges which are smaller (i.e., must be 100% skipped).
// If we exhausted all edges on the current level, we move one level
// up the tree, and repeat until we either find the edge, or until we hit
// the top of the tree meaning the skip exceeds tree->length.
while (n >= edge->length) {
n -= edge->length;
while (++index == node->end()) {
if (++height > height_) return {nullptr, n};
node = node_[height];
index = index_[height];
}
edge = node->Edge(index);
}
// If we moved up the tree, descend down to the leaf level, consuming all
// edges that must be skipped.
while (height > 0) {
node = edge->btree();
index_[height] = index;
node_[--height] = node;
index = node->begin();
edge = node->Edge(index);
while (n >= edge->length) {
n -= edge->length;
++index;
assert(index != node->end());
edge = node->Edge(index);
}
}
index_[0] = index;
return {edge, n};
}
ReadResult CordRepBtreeNavigator::Read(size_t edge_offset, size_t n) {
int height = 0;
size_t length = edge_offset + n;
size_t index = index_[0];
CordRepBtree* node = node_[0];
CordRep* edge = node->Edge(index);
assert(edge_offset < edge->length);
if (length < edge->length) {
return {Substring(edge, edge_offset, n), length};
}
// Similar to 'Skip', we consume all edges that are inside the 'length' of
// data that needs to be read. If we exhaust the current level, we move one
// level up the tree and repeat until we hit the final edge that must be
// (partially) read. We consume all edges into `subtree`.
CordRepBtree* subtree = CordRepBtree::New(Substring(edge, edge_offset));
size_t subtree_end = 1;
do {
length -= edge->length;
while (++index == node->end()) {
index_[height] = index;
if (++height > height_) {
subtree->set_end(subtree_end);
if (length == 0) return {subtree, 0};
CordRep::Unref(subtree);
return {nullptr, length};
}
if (length != 0) {
subtree->set_end(subtree_end);
subtree = CordRepBtree::New(subtree);
subtree_end = 1;
}
node = node_[height];
index = index_[height];
}
edge = node->Edge(index);
if (length >= edge->length) {
subtree->length += edge->length;
subtree->edges_[subtree_end++] = CordRep::Ref(edge);
}
} while (length >= edge->length);
CordRepBtree* tree = subtree;
subtree->length += length;
// If we moved up the tree, descend down to the leaf level, consuming all
// edges that must be read, adding 'down' nodes to `subtree`.
while (height > 0) {
node = edge->btree();
index_[height] = index;
node_[--height] = node;
index = node->begin();
edge = node->Edge(index);
if (length != 0) {
CordRepBtree* right = CordRepBtree::New(height);
right->length = length;
subtree->edges_[subtree_end++] = right;
subtree->set_end(subtree_end);
subtree = right;
subtree_end = 0;
while (length >= edge->length) {
subtree->edges_[subtree_end++] = CordRep::Ref(edge);
length -= edge->length;
edge = node->Edge(++index);
}
}
}
// Add any (partial) edge still remaining at the leaf level.
if (length != 0) {
subtree->edges_[subtree_end++] = Substring(edge, 0, length);
}
subtree->set_end(subtree_end);
index_[0] = index;
return {tree, length};
}
} // namespace cord_internal
ABSL_NAMESPACE_END
} // namespace absl
...@@ -191,7 +191,9 @@ class string_view { ...@@ -191,7 +191,9 @@ class string_view {
ABSL_ATTRIBUTE_LIFETIME_BOUND) noexcept ABSL_ATTRIBUTE_LIFETIME_BOUND) noexcept
// This is implemented in terms of `string_view(p, n)` so `str.size()` // This is implemented in terms of `string_view(p, n)` so `str.size()`
// doesn't need to be reevaluated after `ptr_` is set. // doesn't need to be reevaluated after `ptr_` is set.
: string_view(str.data(), str.size()) {} // The length check is also skipped since it is unnecessary and causes
// code bloat.
: string_view(str.data(), str.size(), SkipCheckLengthTag{}) {}
// Implicit constructor of a `string_view` from NUL-terminated `str`. When // Implicit constructor of a `string_view` from NUL-terminated `str`. When
// accepting possibly null strings, use `absl::NullSafeStringView(str)` // accepting possibly null strings, use `absl::NullSafeStringView(str)`
...@@ -594,6 +596,12 @@ class string_view { ...@@ -594,6 +596,12 @@ class string_view {
} }
private: private:
// The constructor from std::string delegates to this constuctor.
// See the comment on that constructor for the rationale.
struct SkipCheckLengthTag {};
string_view(const char* data, size_type len, SkipCheckLengthTag) noexcept
: ptr_(data), length_(len) {}
static constexpr size_type kMaxSize = static constexpr size_type kMaxSize =
(std::numeric_limits<difference_type>::max)(); (std::numeric_limits<difference_type>::max)();
......
...@@ -182,18 +182,29 @@ class Duration { ...@@ -182,18 +182,29 @@ class Duration {
// Overloads that forward to either the int64_t or double overloads above. // Overloads that forward to either the int64_t or double overloads above.
// Integer operands must be representable as int64_t. // Integer operands must be representable as int64_t.
template <typename T> template <typename T, time_internal::EnableIfIntegral<T> = 0>
Duration& operator*=(T r) { Duration& operator*=(T r) {
int64_t x = r; int64_t x = r;
return *this *= x; return *this *= x;
} }
template <typename T>
template <typename T, time_internal::EnableIfIntegral<T> = 0>
Duration& operator/=(T r) { Duration& operator/=(T r) {
int64_t x = r; int64_t x = r;
return *this /= x; return *this /= x;
} }
Duration& operator*=(float r) { return *this *= static_cast<double>(r); }
Duration& operator/=(float r) { return *this /= static_cast<double>(r); } template <typename T, time_internal::EnableIfFloat<T> = 0>
Duration& operator*=(T r) {
double x = r;
return *this *= x;
}
template <typename T, time_internal::EnableIfFloat<T> = 0>
Duration& operator/=(T r) {
double x = r;
return *this /= x;
}
template <typename H> template <typename H>
friend H AbslHashValue(H h, Duration d) { friend H AbslHashValue(H h, Duration d) {
...@@ -392,12 +403,30 @@ constexpr Duration InfiniteDuration(); ...@@ -392,12 +403,30 @@ constexpr Duration InfiniteDuration();
// //
// absl::Duration a = absl::Seconds(60); // absl::Duration a = absl::Seconds(60);
// absl::Duration b = absl::Minutes(1); // b == a // absl::Duration b = absl::Minutes(1); // b == a
constexpr Duration Nanoseconds(int64_t n); template <typename T, time_internal::EnableIfIntegral<T> = 0>
constexpr Duration Microseconds(int64_t n); constexpr Duration Nanoseconds(T n) {
constexpr Duration Milliseconds(int64_t n); return time_internal::FromInt64(n, std::nano{});
constexpr Duration Seconds(int64_t n); }
constexpr Duration Minutes(int64_t n); template <typename T, time_internal::EnableIfIntegral<T> = 0>
constexpr Duration Hours(int64_t n); constexpr Duration Microseconds(T n) {
return time_internal::FromInt64(n, std::micro{});
}
template <typename T, time_internal::EnableIfIntegral<T> = 0>
constexpr Duration Milliseconds(T n) {
return time_internal::FromInt64(n, std::milli{});
}
template <typename T, time_internal::EnableIfIntegral<T> = 0>
constexpr Duration Seconds(T n) {
return time_internal::FromInt64(n, std::ratio<1>{});
}
template <typename T, time_internal::EnableIfIntegral<T> = 0>
constexpr Duration Minutes(T n) {
return time_internal::FromInt64(n, std::ratio<60>{});
}
template <typename T, time_internal::EnableIfIntegral<T> = 0>
constexpr Duration Hours(T n) {
return time_internal::FromInt64(n, std::ratio<3600>{});
}
// Factory overloads for constructing `Duration` values from a floating-point // Factory overloads for constructing `Duration` values from a floating-point
// number of the unit indicated by the factory function's name. These functions // number of the unit indicated by the factory function's name. These functions
...@@ -1496,25 +1525,6 @@ T ToChronoDuration(Duration d) { ...@@ -1496,25 +1525,6 @@ T ToChronoDuration(Duration d) {
} // namespace time_internal } // namespace time_internal
constexpr Duration Nanoseconds(int64_t n) {
return time_internal::FromInt64(n, std::nano{});
}
constexpr Duration Microseconds(int64_t n) {
return time_internal::FromInt64(n, std::micro{});
}
constexpr Duration Milliseconds(int64_t n) {
return time_internal::FromInt64(n, std::milli{});
}
constexpr Duration Seconds(int64_t n) {
return time_internal::FromInt64(n, std::ratio<1>{});
}
constexpr Duration Minutes(int64_t n) {
return time_internal::FromInt64(n, std::ratio<60>{});
}
constexpr Duration Hours(int64_t n) {
return time_internal::FromInt64(n, std::ratio<3600>{});
}
constexpr bool operator<(Duration lhs, Duration rhs) { constexpr bool operator<(Duration lhs, Duration rhs) {
return time_internal::GetRepHi(lhs) != time_internal::GetRepHi(rhs) return time_internal::GetRepHi(lhs) != time_internal::GetRepHi(rhs)
? time_internal::GetRepHi(lhs) < time_internal::GetRepHi(rhs) ? time_internal::GetRepHi(lhs) < time_internal::GetRepHi(rhs)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment