From 20869f89edbc38139f46bdc49c8b6c3a1f7bee4c Mon Sep 17 00:00:00 2001 From: Abseil Team Date: Mon, 1 Feb 2021 14:09:21 -0800 Subject: Export of internal Abseil changes -- cea62ebc5d31c62aabcb94c066d9be506f34baf6 by Abseil Team : Fix typo in `Cord::EndsWith()` docs PiperOrigin-RevId: 355023067 -- f89225a55476478ec167be50dea543f5414836f9 by Abseil Team : Add set_cordz_info() and get_cordz_info() methods to InlineData This change has preparations for future (optional) integration of CordzInfo sampling data into Cord's InlineData for non inlined cords. PiperOrigin-RevId: 354965340 -- 324057574aeb697bd3327cb905eb5bca16ade768 by Abseil Team : Fix two comment typos. PiperOrigin-RevId: 354952568 -- 5bb93ca3d57ead3633e1efde4aa28718987ef64f by CJ Johnson : Clarify doc comment for absl::Cleanup by using absl::Status return type and clarify the engaged state by surfacing the initial value in the public header. PiperOrigin-RevId: 354935253 -- ec95424594b24a1aec9bf7972b2355f37285506a by Abseil Team : Remove `preserve_most` attribute from CordRep::Destroy() PiperOrigin-RevId: 354921927 GitOrigin-RevId: cea62ebc5d31c62aabcb94c066d9be506f34baf6 Change-Id: Ibe1d66197db7ce9554594e07b1c6e7c6dea3c9da --- absl/cleanup/cleanup.h | 51 +++++++++++++++++++++-------------- absl/cleanup/internal/cleanup.h | 10 +++---- absl/hash/internal/wyhash_test.cc | 4 +-- absl/strings/BUILD.bazel | 1 + absl/strings/cord.h | 2 +- absl/strings/internal/cord_internal.h | 44 ++++++++++++++++-------------- 6 files changed, 64 insertions(+), 48 deletions(-) diff --git a/absl/cleanup/cleanup.h b/absl/cleanup/cleanup.h index f606b3f4..5a4bc546 100644 --- a/absl/cleanup/cleanup.h +++ b/absl/cleanup/cleanup.h @@ -16,35 +16,39 @@ // File: cleanup.h // ----------------------------------------------------------------------------- // -// `absl::Cleanup` implements the scope guard idiom, invoking `operator()() &&` -// on the callback it was constructed with, on scope exit. +// `absl::Cleanup` implements the scope guard idiom, invoking the contained +// callback's `operator()() &&` on scope exit. // // Example: // // ``` -// void CopyGoodData(const char* input_path, const char* output_path) { -// FILE* in_file = fopen(input_path, "r"); -// if (in_file == nullptr) return; +// absl::Status CopyGoodData(const char* source_path, const char* sink_path) { +// FILE* source_file = fopen(source_path, "r"); +// if (source_file == nullptr) { +// return absl::NotFoundError("No source file"); // No cleanups execute +// } // -// // C++17 style using class template argument deduction -// absl::Cleanup in_closer = [in_file] { fclose(in_file); }; +// // C++17 style cleanup using class template argument deduction +// absl::Cleanup source_closer = [source_file] { fclose(source_file); }; // -// FILE* out_file = fopen(output_path, "w"); -// if (out_file == nullptr) return; // `in_closer` will run +// FILE* sink_file = fopen(sink_path, "w"); +// if (sink_file == nullptr) { +// return absl::NotFoundError("No sink file"); // First cleanup executes +// } // -// // C++11 style using the factory function -// auto out_closer = absl::MakeCleanup([out_file] { fclose(out_file); }); +// // C++11 style cleanup using the factory function +// auto sink_closer = absl::MakeCleanup([sink_file] { fclose(sink_file); }); // // Data data; -// while (ReadData(in_file, &data)) { +// while (ReadData(source_file, &data)) { // if (data.IsBad()) { -// LOG(ERROR) << "Found bad data."; -// return; // `in_closer` and `out_closer` will run +// absl::Status result = absl::FailedPreconditionError("Read bad data"); +// return result; // Both cleanups execute // } -// SaveData(out_file, &data); +// SaveData(sink_file, &data); // } // -// // `in_closer` and `out_closer` will run +// return absl::OkStatus(); // Both cleanups execute // } // ``` // @@ -54,6 +58,12 @@ // // `std::move(cleanup).Invoke()` will execute the callback early, before // destruction, and prevent the callback from executing in the destructor. +// +// Usage: +// +// `absl::Cleanup` is not an interface type. It is only intended to be used +// within the body of a function. It is not a value type and instead models a +// control flow construct. Check out `defer` in Golang for something similar. #ifndef ABSL_CLEANUP_CLEANUP_H_ #define ABSL_CLEANUP_CLEANUP_H_ @@ -76,9 +86,10 @@ class ABSL_MUST_USE_RESULT Cleanup { "Callbacks that return values are not supported."); public: - Cleanup(Callback callback) : storage_(std::move(callback)) {} // NOLINT + Cleanup(Callback callback) // NOLINT + : storage_(std::move(callback), /*engaged=*/true) {} - Cleanup(Cleanup&& other) : storage_(std::move(other.storage_)) {} + Cleanup(Cleanup&& other) = default; void Cancel() && { ABSL_HARDENING_ASSERT(storage_.IsCallbackEngaged()); @@ -103,7 +114,7 @@ class ABSL_MUST_USE_RESULT Cleanup { // `absl::Cleanup c = /* callback */;` // -// C++17 type deduction API for creating an instance of `absl::Cleanup`. +// C++17 type deduction API for creating an instance of `absl::Cleanup` #if defined(ABSL_HAVE_CLASS_TEMPLATE_ARGUMENT_DEDUCTION) template Cleanup(Callback callback) -> Cleanup; @@ -111,7 +122,7 @@ Cleanup(Callback callback) -> Cleanup; // `auto c = absl::MakeCleanup(/* callback */);` // -// C++11 type deduction API for creating an instance of `absl::Cleanup`. +// C++11 type deduction API for creating an instance of `absl::Cleanup` template absl::Cleanup MakeCleanup(Callback callback) { static_assert(cleanup_internal::WasDeduced(), diff --git a/absl/cleanup/internal/cleanup.h b/absl/cleanup/internal/cleanup.h index 8fbca5bd..b68e3dd3 100644 --- a/absl/cleanup/internal/cleanup.h +++ b/absl/cleanup/internal/cleanup.h @@ -45,12 +45,12 @@ class Storage { public: Storage() = delete; - explicit Storage(Callback callback) - : engaged_(true), callback_(std::move(callback)) {} + Storage(Callback callback, bool engaged) + : callback_(std::move(callback)), engaged_(engaged) {} Storage(Storage&& other) - : engaged_(absl::exchange(other.engaged_, false)), - callback_(std::move(other.callback_)) {} + : callback_(std::move(other.callback_)), + engaged_(absl::exchange(other.engaged_, false)) {} Storage(const Storage& other) = delete; @@ -67,8 +67,8 @@ class Storage { } private: - bool engaged_; Callback callback_; + bool engaged_; }; } // namespace cleanup_internal diff --git a/absl/hash/internal/wyhash_test.cc b/absl/hash/internal/wyhash_test.cc index 30dc9e34..9fb06d23 100644 --- a/absl/hash/internal/wyhash_test.cc +++ b/absl/hash/internal/wyhash_test.cc @@ -1,14 +1,14 @@ // Copyright 2020 The Abseil Authors // // Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in cokSaltliance with 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 ikSaltlied. +// 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. diff --git a/absl/strings/BUILD.bazel b/absl/strings/BUILD.bazel index 794cf43a..5efaf896 100644 --- a/absl/strings/BUILD.bazel +++ b/absl/strings/BUILD.bazel @@ -286,6 +286,7 @@ cc_library( "//absl/base:base_internal", "//absl/base:config", "//absl/base:core_headers", + "//absl/base:endian", "//absl/base:raw_logging_internal", "//absl/base:throw_delegate", "//absl/container:compressed_tuple", diff --git a/absl/strings/cord.h b/absl/strings/cord.h index 17341bda..aefb5e53 100644 --- a/absl/strings/cord.h +++ b/absl/strings/cord.h @@ -289,7 +289,7 @@ class Cord { bool StartsWith(const Cord& rhs) const; bool StartsWith(absl::string_view rhs) const; - // Cord::EndsWidth() + // Cord::EndsWith() // // Determines whether the Cord ends with the passed string data `rhs`. bool EndsWith(absl::string_view rhs) const; diff --git a/absl/strings/internal/cord_internal.h b/absl/strings/internal/cord_internal.h index 96502433..cda00a44 100644 --- a/absl/strings/internal/cord_internal.h +++ b/absl/strings/internal/cord_internal.h @@ -22,6 +22,7 @@ #include #include "absl/base/config.h" +#include "absl/base/internal/endian.h" #include "absl/base/internal/invoke.h" #include "absl/base/optimization.h" #include "absl/container/internal/compressed_tuple.h" @@ -32,6 +33,8 @@ namespace absl { ABSL_NAMESPACE_BEGIN namespace cord_internal { +class CordzInfo; + // Default feature enable states for cord ring buffers enum CordFeatureDefaults { kCordEnableRingBufferDefault = false, @@ -193,26 +196,7 @@ struct CordRep { // -------------------------------------------------------------------- // Memory management - // This internal routine is called from the cold path of Unref below. Keeping - // it in a separate routine allows good inlining of Unref into many profitable - // call sites. However, the call to this function can be highly disruptive to - // the register pressure in those callers. To minimize the cost to callers, we - // use a special LLVM calling convention that preserves most registers. This - // allows the call to this routine in cold paths to not disrupt the caller's - // register pressure. This calling convention is not available on all - // platforms; we intentionally allow LLVM to ignore the attribute rather than - // attempting to hardcode the list of supported platforms. -#if defined(__clang__) && !defined(__i386__) -#if !(defined(ABSL_HAVE_MEMORY_SANITIZER) || \ - defined(ABSL_HAVE_THREAD_SANITIZER) || \ - defined(ABSL_HAVE_ADDRESS_SANITIZER) || \ - defined(UNDEFINED_BEHAVIOR_SANITIZER)) -#pragma clang diagnostic push -#pragma clang diagnostic ignored "-Wattributes" - __attribute__((preserve_most)) -#pragma clang diagnostic pop -#endif // *_SANITIZER -#endif + // Destroys the provided `rep`. static void Destroy(CordRep* rep); // Increments the reference count of `rep`. @@ -383,6 +367,26 @@ class InlineData { return as_tree_.cordz_info != kNullCordzInfo; } + // Returns the cordz_info sampling instance for this instance, or nullptr + // if the current instance is not sampled and does not have CordzInfo data. + // Requires the current instance to hold a tree value. + CordzInfo* cordz_info() const { + assert(is_tree()); + intptr_t info = + static_cast(absl::big_endian::ToHost64(as_tree_.cordz_info)); + assert(info & 1); + return reinterpret_cast(info - 1); + } + + // Sets the current cordz_info sampling instance for this instance, or nullptr + // if the current instance is not sampled and does not have CordzInfo data. + // Requires the current instance to hold a tree value. + void set_cordz_info(CordzInfo* cordz_info) { + assert(is_tree()); + intptr_t info = reinterpret_cast(cordz_info) | 1; + as_tree_.cordz_info = absl::big_endian::FromHost64(info); + } + // Returns a read only pointer to the character data inside this instance. // Requires the current instance to hold inline data. const char* as_chars() const { -- cgit v1.2.3