/src/abseil-cpp/absl/cleanup/internal/cleanup.h
Line | Count | Source (jump to first uncovered line) |
1 | | // Copyright 2021 The Abseil Authors. |
2 | | // |
3 | | // Licensed under the Apache License, Version 2.0 (the "License"); |
4 | | // you may not use this file except in compliance with the License. |
5 | | // You may obtain a copy of the License at |
6 | | // |
7 | | // https://www.apache.org/licenses/LICENSE-2.0 |
8 | | // |
9 | | // Unless required by applicable law or agreed to in writing, software |
10 | | // distributed under the License is distributed on an "AS IS" BASIS, |
11 | | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
12 | | // See the License for the specific language governing permissions and |
13 | | // limitations under the License. |
14 | | |
15 | | #ifndef ABSL_CLEANUP_INTERNAL_CLEANUP_H_ |
16 | | #define ABSL_CLEANUP_INTERNAL_CLEANUP_H_ |
17 | | |
18 | | #include <new> |
19 | | #include <type_traits> |
20 | | #include <utility> |
21 | | |
22 | | #include "absl/base/internal/invoke.h" |
23 | | #include "absl/base/macros.h" |
24 | | #include "absl/base/thread_annotations.h" |
25 | | #include "absl/utility/utility.h" |
26 | | |
27 | | namespace absl { |
28 | | ABSL_NAMESPACE_BEGIN |
29 | | |
30 | | namespace cleanup_internal { |
31 | | |
32 | | struct Tag {}; |
33 | | |
34 | | template <typename Arg, typename... Args> |
35 | 0 | constexpr bool WasDeduced() { |
36 | 0 | return (std::is_same<cleanup_internal::Tag, Arg>::value) && |
37 | 0 | (sizeof...(Args) == 0); |
38 | 0 | } |
39 | | |
40 | | template <typename Callback> |
41 | 0 | constexpr bool ReturnsVoid() { |
42 | 0 | return (std::is_same<base_internal::invoke_result_t<Callback>, void>::value); |
43 | 0 | } Unexecuted instantiation: log_sink_set.cc:bool absl::cleanup_internal::ReturnsVoid<absl::log_internal::(anonymous namespace)::GlobalLogSinkSet::LogToSinks(absl::LogEntry const&, absl::Span<absl::LogSink*>, bool)::{lambda()#1}>() Unexecuted instantiation: log_sink_set.cc:bool absl::cleanup_internal::ReturnsVoid<absl::log_internal::(anonymous namespace)::GlobalLogSinkSet::FlushLogSinks()::{lambda()#1}>() |
44 | | |
45 | | template <typename Callback> |
46 | | class Storage { |
47 | | public: |
48 | | Storage() = delete; |
49 | | |
50 | 3.24M | explicit Storage(Callback callback) { |
51 | | // Placement-new into a character buffer is used for eager destruction when |
52 | | // the cleanup is invoked or cancelled. To ensure this optimizes well, the |
53 | | // behavior is implemented locally instead of using an absl::optional. |
54 | 3.24M | ::new (GetCallbackBuffer()) Callback(std::move(callback)); |
55 | 3.24M | is_callback_engaged_ = true; |
56 | 3.24M | } log_sink_set.cc:absl::cleanup_internal::Storage<absl::log_internal::(anonymous namespace)::GlobalLogSinkSet::LogToSinks(absl::LogEntry const&, absl::Span<absl::LogSink*>, bool)::{lambda()#1}>::Storage({lambda()#1}) Line | Count | Source | 50 | 3.24M | explicit Storage(Callback callback) { | 51 | | // Placement-new into a character buffer is used for eager destruction when | 52 | | // the cleanup is invoked or cancelled. To ensure this optimizes well, the | 53 | | // behavior is implemented locally instead of using an absl::optional. | 54 | 3.24M | ::new (GetCallbackBuffer()) Callback(std::move(callback)); | 55 | 3.24M | is_callback_engaged_ = true; | 56 | 3.24M | } |
Unexecuted instantiation: log_sink_set.cc:absl::cleanup_internal::Storage<absl::log_internal::(anonymous namespace)::GlobalLogSinkSet::FlushLogSinks()::{lambda()#1}>::Storage({lambda()#1}) |
57 | | |
58 | | Storage(Storage&& other) { |
59 | | ABSL_HARDENING_ASSERT(other.IsCallbackEngaged()); |
60 | | |
61 | | ::new (GetCallbackBuffer()) Callback(std::move(other.GetCallback())); |
62 | | is_callback_engaged_ = true; |
63 | | |
64 | | other.DestroyCallback(); |
65 | | } |
66 | | |
67 | | Storage(const Storage& other) = delete; |
68 | | |
69 | | Storage& operator=(Storage&& other) = delete; |
70 | | |
71 | | Storage& operator=(const Storage& other) = delete; |
72 | | |
73 | 9.72M | void* GetCallbackBuffer() { return static_cast<void*>(+callback_buffer_); } log_sink_set.cc:absl::cleanup_internal::Storage<absl::log_internal::(anonymous namespace)::GlobalLogSinkSet::LogToSinks(absl::LogEntry const&, absl::Span<absl::LogSink*>, bool)::{lambda()#1}>::GetCallbackBuffer() Line | Count | Source | 73 | 9.72M | void* GetCallbackBuffer() { return static_cast<void*>(+callback_buffer_); } |
Unexecuted instantiation: log_sink_set.cc:absl::cleanup_internal::Storage<absl::log_internal::(anonymous namespace)::GlobalLogSinkSet::FlushLogSinks()::{lambda()#1}>::GetCallbackBuffer() |
74 | | |
75 | 6.48M | Callback& GetCallback() { |
76 | 6.48M | return *reinterpret_cast<Callback*>(GetCallbackBuffer()); |
77 | 6.48M | } log_sink_set.cc:absl::cleanup_internal::Storage<absl::log_internal::(anonymous namespace)::GlobalLogSinkSet::LogToSinks(absl::LogEntry const&, absl::Span<absl::LogSink*>, bool)::{lambda()#1}>::GetCallback() Line | Count | Source | 75 | 6.48M | Callback& GetCallback() { | 76 | 6.48M | return *reinterpret_cast<Callback*>(GetCallbackBuffer()); | 77 | 6.48M | } |
Unexecuted instantiation: log_sink_set.cc:absl::cleanup_internal::Storage<absl::log_internal::(anonymous namespace)::GlobalLogSinkSet::FlushLogSinks()::{lambda()#1}>::GetCallback() |
78 | | |
79 | 3.24M | bool IsCallbackEngaged() const { return is_callback_engaged_; } log_sink_set.cc:absl::cleanup_internal::Storage<absl::log_internal::(anonymous namespace)::GlobalLogSinkSet::LogToSinks(absl::LogEntry const&, absl::Span<absl::LogSink*>, bool)::{lambda()#1}>::IsCallbackEngaged() const Line | Count | Source | 79 | 3.24M | bool IsCallbackEngaged() const { return is_callback_engaged_; } |
Unexecuted instantiation: log_sink_set.cc:absl::cleanup_internal::Storage<absl::log_internal::(anonymous namespace)::GlobalLogSinkSet::FlushLogSinks()::{lambda()#1}>::IsCallbackEngaged() const |
80 | | |
81 | 3.24M | void DestroyCallback() { |
82 | 3.24M | is_callback_engaged_ = false; |
83 | 3.24M | GetCallback().~Callback(); |
84 | 3.24M | } log_sink_set.cc:absl::cleanup_internal::Storage<absl::log_internal::(anonymous namespace)::GlobalLogSinkSet::LogToSinks(absl::LogEntry const&, absl::Span<absl::LogSink*>, bool)::{lambda()#1}>::DestroyCallback() Line | Count | Source | 81 | 3.24M | void DestroyCallback() { | 82 | 3.24M | is_callback_engaged_ = false; | 83 | 3.24M | GetCallback().~Callback(); | 84 | 3.24M | } |
Unexecuted instantiation: log_sink_set.cc:absl::cleanup_internal::Storage<absl::log_internal::(anonymous namespace)::GlobalLogSinkSet::FlushLogSinks()::{lambda()#1}>::DestroyCallback() |
85 | | |
86 | 3.24M | void InvokeCallback() ABSL_NO_THREAD_SAFETY_ANALYSIS { |
87 | 3.24M | std::move(GetCallback())(); |
88 | 3.24M | } log_sink_set.cc:absl::cleanup_internal::Storage<absl::log_internal::(anonymous namespace)::GlobalLogSinkSet::LogToSinks(absl::LogEntry const&, absl::Span<absl::LogSink*>, bool)::{lambda()#1}>::InvokeCallback() Line | Count | Source | 86 | 3.24M | void InvokeCallback() ABSL_NO_THREAD_SAFETY_ANALYSIS { | 87 | 3.24M | std::move(GetCallback())(); | 88 | 3.24M | } |
Unexecuted instantiation: log_sink_set.cc:absl::cleanup_internal::Storage<absl::log_internal::(anonymous namespace)::GlobalLogSinkSet::FlushLogSinks()::{lambda()#1}>::InvokeCallback() |
89 | | |
90 | | private: |
91 | | bool is_callback_engaged_; |
92 | | alignas(Callback) char callback_buffer_[sizeof(Callback)]; |
93 | | }; |
94 | | |
95 | | } // namespace cleanup_internal |
96 | | |
97 | | ABSL_NAMESPACE_END |
98 | | } // namespace absl |
99 | | |
100 | | #endif // ABSL_CLEANUP_INTERNAL_CLEANUP_H_ |