Skip to content

Commit 20869f8

Browse files
Abseil Teamrogeeff
authored andcommitted
Export of internal Abseil changes
-- cea62ebc5d31c62aabcb94c066d9be506f34baf6 by Abseil Team <[email protected]>: Fix typo in `Cord::EndsWith()` docs PiperOrigin-RevId: 355023067 -- f89225a55476478ec167be50dea543f5414836f9 by Abseil Team <[email protected]>: 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 <[email protected]>: Fix two comment typos. PiperOrigin-RevId: 354952568 -- 5bb93ca3d57ead3633e1efde4aa28718987ef64f by CJ Johnson <[email protected]>: 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 <[email protected]>: Remove `preserve_most` attribute from CordRep::Destroy() PiperOrigin-RevId: 354921927 GitOrigin-RevId: cea62ebc5d31c62aabcb94c066d9be506f34baf6 Change-Id: Ibe1d66197db7ce9554594e07b1c6e7c6dea3c9da
1 parent 184d2f8 commit 20869f8

File tree

6 files changed

+64
-48
lines changed

6 files changed

+64
-48
lines changed

absl/cleanup/cleanup.h

Lines changed: 31 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -16,35 +16,39 @@
1616
// File: cleanup.h
1717
// -----------------------------------------------------------------------------
1818
//
19-
// `absl::Cleanup` implements the scope guard idiom, invoking `operator()() &&`
20-
// on the callback it was constructed with, on scope exit.
19+
// `absl::Cleanup` implements the scope guard idiom, invoking the contained
20+
// callback's `operator()() &&` on scope exit.
2121
//
2222
// Example:
2323
//
2424
// ```
25-
// void CopyGoodData(const char* input_path, const char* output_path) {
26-
// FILE* in_file = fopen(input_path, "r");
27-
// if (in_file == nullptr) return;
25+
// absl::Status CopyGoodData(const char* source_path, const char* sink_path) {
26+
// FILE* source_file = fopen(source_path, "r");
27+
// if (source_file == nullptr) {
28+
// return absl::NotFoundError("No source file"); // No cleanups execute
29+
// }
2830
//
29-
// // C++17 style using class template argument deduction
30-
// absl::Cleanup in_closer = [in_file] { fclose(in_file); };
31+
// // C++17 style cleanup using class template argument deduction
32+
// absl::Cleanup source_closer = [source_file] { fclose(source_file); };
3133
//
32-
// FILE* out_file = fopen(output_path, "w");
33-
// if (out_file == nullptr) return; // `in_closer` will run
34+
// FILE* sink_file = fopen(sink_path, "w");
35+
// if (sink_file == nullptr) {
36+
// return absl::NotFoundError("No sink file"); // First cleanup executes
37+
// }
3438
//
35-
// // C++11 style using the factory function
36-
// auto out_closer = absl::MakeCleanup([out_file] { fclose(out_file); });
39+
// // C++11 style cleanup using the factory function
40+
// auto sink_closer = absl::MakeCleanup([sink_file] { fclose(sink_file); });
3741
//
3842
// Data data;
39-
// while (ReadData(in_file, &data)) {
43+
// while (ReadData(source_file, &data)) {
4044
// if (data.IsBad()) {
41-
// LOG(ERROR) << "Found bad data.";
42-
// return; // `in_closer` and `out_closer` will run
45+
// absl::Status result = absl::FailedPreconditionError("Read bad data");
46+
// return result; // Both cleanups execute
4347
// }
44-
// SaveData(out_file, &data);
48+
// SaveData(sink_file, &data);
4549
// }
4650
//
47-
// // `in_closer` and `out_closer` will run
51+
// return absl::OkStatus(); // Both cleanups execute
4852
// }
4953
// ```
5054
//
@@ -54,6 +58,12 @@
5458
//
5559
// `std::move(cleanup).Invoke()` will execute the callback early, before
5660
// destruction, and prevent the callback from executing in the destructor.
61+
//
62+
// Usage:
63+
//
64+
// `absl::Cleanup` is not an interface type. It is only intended to be used
65+
// within the body of a function. It is not a value type and instead models a
66+
// control flow construct. Check out `defer` in Golang for something similar.
5767

5868
#ifndef ABSL_CLEANUP_CLEANUP_H_
5969
#define ABSL_CLEANUP_CLEANUP_H_
@@ -76,9 +86,10 @@ class ABSL_MUST_USE_RESULT Cleanup {
7686
"Callbacks that return values are not supported.");
7787

7888
public:
79-
Cleanup(Callback callback) : storage_(std::move(callback)) {} // NOLINT
89+
Cleanup(Callback callback) // NOLINT
90+
: storage_(std::move(callback), /*engaged=*/true) {}
8091

81-
Cleanup(Cleanup&& other) : storage_(std::move(other.storage_)) {}
92+
Cleanup(Cleanup&& other) = default;
8293

8394
void Cancel() && {
8495
ABSL_HARDENING_ASSERT(storage_.IsCallbackEngaged());
@@ -103,15 +114,15 @@ class ABSL_MUST_USE_RESULT Cleanup {
103114

104115
// `absl::Cleanup c = /* callback */;`
105116
//
106-
// C++17 type deduction API for creating an instance of `absl::Cleanup`.
117+
// C++17 type deduction API for creating an instance of `absl::Cleanup`
107118
#if defined(ABSL_HAVE_CLASS_TEMPLATE_ARGUMENT_DEDUCTION)
108119
template <typename Callback>
109120
Cleanup(Callback callback) -> Cleanup<cleanup_internal::Tag, Callback>;
110121
#endif // defined(ABSL_HAVE_CLASS_TEMPLATE_ARGUMENT_DEDUCTION)
111122

112123
// `auto c = absl::MakeCleanup(/* callback */);`
113124
//
114-
// C++11 type deduction API for creating an instance of `absl::Cleanup`.
125+
// C++11 type deduction API for creating an instance of `absl::Cleanup`
115126
template <typename... Args, typename Callback>
116127
absl::Cleanup<cleanup_internal::Tag, Callback> MakeCleanup(Callback callback) {
117128
static_assert(cleanup_internal::WasDeduced<cleanup_internal::Tag, Args...>(),

absl/cleanup/internal/cleanup.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -45,12 +45,12 @@ class Storage {
4545
public:
4646
Storage() = delete;
4747

48-
explicit Storage(Callback callback)
49-
: engaged_(true), callback_(std::move(callback)) {}
48+
Storage(Callback callback, bool engaged)
49+
: callback_(std::move(callback)), engaged_(engaged) {}
5050

5151
Storage(Storage&& other)
52-
: engaged_(absl::exchange(other.engaged_, false)),
53-
callback_(std::move(other.callback_)) {}
52+
: callback_(std::move(other.callback_)),
53+
engaged_(absl::exchange(other.engaged_, false)) {}
5454

5555
Storage(const Storage& other) = delete;
5656

@@ -67,8 +67,8 @@ class Storage {
6767
}
6868

6969
private:
70-
bool engaged_;
7170
Callback callback_;
71+
bool engaged_;
7272
};
7373

7474
} // namespace cleanup_internal

absl/hash/internal/wyhash_test.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
// Copyright 2020 The Abseil Authors
22
//
33
// Licensed under the Apache License, Version 2.0 (the "License");
4-
// you may not use this file except in cokSaltliance with the License.
4+
// you may not use this file except in compliance with the License.
55
// You may obtain a copy of the License at
66
//
77
// https://www.apache.org/licenses/LICENSE-2.0
88
//
99
// Unless required by applicable law or agreed to in writing, software
1010
// distributed under the License is distributed on an "AS IS" BASIS,
11-
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or ikSaltlied.
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

absl/strings/BUILD.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,7 @@ cc_library(
286286
"//absl/base:base_internal",
287287
"//absl/base:config",
288288
"//absl/base:core_headers",
289+
"//absl/base:endian",
289290
"//absl/base:raw_logging_internal",
290291
"//absl/base:throw_delegate",
291292
"//absl/container:compressed_tuple",

absl/strings/cord.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -289,7 +289,7 @@ class Cord {
289289
bool StartsWith(const Cord& rhs) const;
290290
bool StartsWith(absl::string_view rhs) const;
291291

292-
// Cord::EndsWidth()
292+
// Cord::EndsWith()
293293
//
294294
// Determines whether the Cord ends with the passed string data `rhs`.
295295
bool EndsWith(absl::string_view rhs) const;

absl/strings/internal/cord_internal.h

Lines changed: 24 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#include <type_traits>
2323

2424
#include "absl/base/config.h"
25+
#include "absl/base/internal/endian.h"
2526
#include "absl/base/internal/invoke.h"
2627
#include "absl/base/optimization.h"
2728
#include "absl/container/internal/compressed_tuple.h"
@@ -32,6 +33,8 @@ namespace absl {
3233
ABSL_NAMESPACE_BEGIN
3334
namespace cord_internal {
3435

36+
class CordzInfo;
37+
3538
// Default feature enable states for cord ring buffers
3639
enum CordFeatureDefaults {
3740
kCordEnableRingBufferDefault = false,
@@ -193,26 +196,7 @@ struct CordRep {
193196
// --------------------------------------------------------------------
194197
// Memory management
195198

196-
// This internal routine is called from the cold path of Unref below. Keeping
197-
// it in a separate routine allows good inlining of Unref into many profitable
198-
// call sites. However, the call to this function can be highly disruptive to
199-
// the register pressure in those callers. To minimize the cost to callers, we
200-
// use a special LLVM calling convention that preserves most registers. This
201-
// allows the call to this routine in cold paths to not disrupt the caller's
202-
// register pressure. This calling convention is not available on all
203-
// platforms; we intentionally allow LLVM to ignore the attribute rather than
204-
// attempting to hardcode the list of supported platforms.
205-
#if defined(__clang__) && !defined(__i386__)
206-
#if !(defined(ABSL_HAVE_MEMORY_SANITIZER) || \
207-
defined(ABSL_HAVE_THREAD_SANITIZER) || \
208-
defined(ABSL_HAVE_ADDRESS_SANITIZER) || \
209-
defined(UNDEFINED_BEHAVIOR_SANITIZER))
210-
#pragma clang diagnostic push
211-
#pragma clang diagnostic ignored "-Wattributes"
212-
__attribute__((preserve_most))
213-
#pragma clang diagnostic pop
214-
#endif // *_SANITIZER
215-
#endif
199+
// Destroys the provided `rep`.
216200
static void Destroy(CordRep* rep);
217201

218202
// Increments the reference count of `rep`.
@@ -383,6 +367,26 @@ class InlineData {
383367
return as_tree_.cordz_info != kNullCordzInfo;
384368
}
385369

370+
// Returns the cordz_info sampling instance for this instance, or nullptr
371+
// if the current instance is not sampled and does not have CordzInfo data.
372+
// Requires the current instance to hold a tree value.
373+
CordzInfo* cordz_info() const {
374+
assert(is_tree());
375+
intptr_t info =
376+
static_cast<intptr_t>(absl::big_endian::ToHost64(as_tree_.cordz_info));
377+
assert(info & 1);
378+
return reinterpret_cast<CordzInfo*>(info - 1);
379+
}
380+
381+
// Sets the current cordz_info sampling instance for this instance, or nullptr
382+
// if the current instance is not sampled and does not have CordzInfo data.
383+
// Requires the current instance to hold a tree value.
384+
void set_cordz_info(CordzInfo* cordz_info) {
385+
assert(is_tree());
386+
intptr_t info = reinterpret_cast<intptr_t>(cordz_info) | 1;
387+
as_tree_.cordz_info = absl::big_endian::FromHost64(info);
388+
}
389+
386390
// Returns a read only pointer to the character data inside this instance.
387391
// Requires the current instance to hold inline data.
388392
const char* as_chars() const {

0 commit comments

Comments
 (0)