Add files using upload-large-folder tool
Browse filesThis view is limited to 50 files because it contains too many changes. See raw diff
- include/absl/algorithm/algorithm.h +64 -0
- include/absl/algorithm/container.h +1830 -0
- include/absl/base/attributes.h +1008 -0
- include/absl/base/call_once.h +228 -0
- include/absl/base/casts.h +180 -0
- include/absl/base/config.h +984 -0
- include/absl/base/const_init.h +76 -0
- include/absl/base/dynamic_annotations.h +480 -0
- include/absl/base/internal/atomic_hook.h +200 -0
- include/absl/base/internal/atomic_hook_test_helper.h +34 -0
- include/absl/base/internal/cycleclock.h +144 -0
- include/absl/base/internal/cycleclock_config.h +55 -0
- include/absl/base/internal/direct_mmap.h +170 -0
- include/absl/base/internal/dynamic_annotations.h +398 -0
- include/absl/base/internal/endian.h +283 -0
- include/absl/base/internal/errno_saver.h +43 -0
- include/absl/base/internal/exception_safety_testing.h +1109 -0
- include/absl/base/internal/exception_testing.h +42 -0
- include/absl/base/internal/fast_type_id.h +50 -0
- include/absl/base/internal/hide_ptr.h +51 -0
- include/absl/base/internal/identity.h +39 -0
- include/absl/base/internal/inline_variable.h +108 -0
- include/absl/base/internal/inline_variable_testing.h +46 -0
- include/absl/base/internal/invoke.h +241 -0
- include/absl/base/internal/low_level_alloc.h +127 -0
- include/absl/base/internal/low_level_scheduling.h +134 -0
- include/absl/base/internal/nullability_impl.h +69 -0
- include/absl/base/internal/per_thread_tls.h +52 -0
- include/absl/base/internal/poison.h +59 -0
- include/absl/base/internal/pretty_function.h +33 -0
- include/absl/base/internal/raw_logging.h +217 -0
- include/absl/base/internal/scheduling_mode.h +58 -0
- include/absl/base/internal/scoped_set_env.h +45 -0
- include/absl/base/internal/spinlock.h +275 -0
- include/absl/base/internal/spinlock_akaros.inc +35 -0
- include/absl/base/internal/spinlock_linux.inc +71 -0
- include/absl/base/internal/spinlock_posix.inc +46 -0
- include/absl/base/internal/spinlock_wait.h +95 -0
- include/absl/base/internal/spinlock_win32.inc +40 -0
- include/absl/base/internal/strerror.h +39 -0
- include/absl/base/internal/sysinfo.h +74 -0
- include/absl/base/internal/thread_identity.h +273 -0
- include/absl/base/internal/throw_delegate.h +75 -0
- include/absl/base/internal/tracing.h +81 -0
- include/absl/base/internal/tsan_mutex_interface.h +68 -0
- include/absl/base/internal/unaligned_access.h +89 -0
- include/absl/base/internal/unscaledcycleclock.h +108 -0
- include/absl/base/internal/unscaledcycleclock_config.h +62 -0
- include/absl/base/log_severity.h +185 -0
- include/absl/base/macros.h +220 -0
include/absl/algorithm/algorithm.h
ADDED
|
@@ -0,0 +1,64 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
// Copyright 2017 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 |
+
// -----------------------------------------------------------------------------
|
| 16 |
+
// File: algorithm.h
|
| 17 |
+
// -----------------------------------------------------------------------------
|
| 18 |
+
//
|
| 19 |
+
// This header file contains Google extensions to the standard <algorithm> C++
|
| 20 |
+
// header.
|
| 21 |
+
|
| 22 |
+
#ifndef ABSL_ALGORITHM_ALGORITHM_H_
|
| 23 |
+
#define ABSL_ALGORITHM_ALGORITHM_H_
|
| 24 |
+
|
| 25 |
+
#include <algorithm>
|
| 26 |
+
#include <iterator>
|
| 27 |
+
#include <type_traits>
|
| 28 |
+
|
| 29 |
+
#include "absl/base/config.h"
|
| 30 |
+
|
| 31 |
+
namespace absl {
|
| 32 |
+
ABSL_NAMESPACE_BEGIN
|
| 33 |
+
|
| 34 |
+
// equal()
|
| 35 |
+
// rotate()
|
| 36 |
+
//
|
| 37 |
+
// Historical note: Abseil once provided implementations of these algorithms
|
| 38 |
+
// prior to their adoption in C++14. New code should prefer to use the std
|
| 39 |
+
// variants.
|
| 40 |
+
//
|
| 41 |
+
// See the documentation for the STL <algorithm> header for more information:
|
| 42 |
+
// https://en.cppreference.com/w/cpp/header/algorithm
|
| 43 |
+
using std::equal;
|
| 44 |
+
using std::rotate;
|
| 45 |
+
|
| 46 |
+
// linear_search()
|
| 47 |
+
//
|
| 48 |
+
// Performs a linear search for `value` using the iterator `first` up to
|
| 49 |
+
// but not including `last`, returning true if [`first`, `last`) contains an
|
| 50 |
+
// element equal to `value`.
|
| 51 |
+
//
|
| 52 |
+
// A linear search is of O(n) complexity which is guaranteed to make at most
|
| 53 |
+
// n = (`last` - `first`) comparisons. A linear search over short containers
|
| 54 |
+
// may be faster than a binary search, even when the container is sorted.
|
| 55 |
+
template <typename InputIterator, typename EqualityComparable>
|
| 56 |
+
ABSL_INTERNAL_CONSTEXPR_SINCE_CXX20 bool linear_search(
|
| 57 |
+
InputIterator first, InputIterator last, const EqualityComparable& value) {
|
| 58 |
+
return std::find(first, last, value) != last;
|
| 59 |
+
}
|
| 60 |
+
|
| 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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
// Copyright 2017 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 |
+
// -----------------------------------------------------------------------------
|
| 16 |
+
// File: container.h
|
| 17 |
+
// -----------------------------------------------------------------------------
|
| 18 |
+
//
|
| 19 |
+
// This header file provides Container-based versions of algorithmic functions
|
| 20 |
+
// within the C++ standard library. The following standard library sets of
|
| 21 |
+
// functions are covered within this file:
|
| 22 |
+
//
|
| 23 |
+
// * Algorithmic <iterator> functions
|
| 24 |
+
// * Algorithmic <numeric> functions
|
| 25 |
+
// * <algorithm> functions
|
| 26 |
+
//
|
| 27 |
+
// The standard library functions operate on iterator ranges; the functions
|
| 28 |
+
// within this API operate on containers, though many return iterator ranges.
|
| 29 |
+
//
|
| 30 |
+
// All functions within this API are named with a `c_` prefix. Calls such as
|
| 31 |
+
// `absl::c_xx(container, ...) are equivalent to std:: functions such as
|
| 32 |
+
// `std::xx(std::begin(cont), std::end(cont), ...)`. Functions that act on
|
| 33 |
+
// iterators but not conceptually on iterator ranges (e.g. `std::iter_swap`)
|
| 34 |
+
// have no equivalent here.
|
| 35 |
+
//
|
| 36 |
+
// For template parameter and variable naming, `C` indicates the container type
|
| 37 |
+
// to which the function is applied, `Pred` indicates the predicate object type
|
| 38 |
+
// to be used by the function and `T` indicates the applicable element type.
|
| 39 |
+
|
| 40 |
+
#ifndef ABSL_ALGORITHM_CONTAINER_H_
|
| 41 |
+
#define ABSL_ALGORITHM_CONTAINER_H_
|
| 42 |
+
|
| 43 |
+
#include <algorithm>
|
| 44 |
+
#include <cassert>
|
| 45 |
+
#include <iterator>
|
| 46 |
+
#include <numeric>
|
| 47 |
+
#include <random>
|
| 48 |
+
#include <type_traits>
|
| 49 |
+
#include <unordered_map>
|
| 50 |
+
#include <unordered_set>
|
| 51 |
+
#include <utility>
|
| 52 |
+
#include <vector>
|
| 53 |
+
|
| 54 |
+
#include "absl/algorithm/algorithm.h"
|
| 55 |
+
#include "absl/base/config.h"
|
| 56 |
+
#include "absl/base/macros.h"
|
| 57 |
+
#include "absl/base/nullability.h"
|
| 58 |
+
#include "absl/meta/type_traits.h"
|
| 59 |
+
|
| 60 |
+
namespace absl {
|
| 61 |
+
ABSL_NAMESPACE_BEGIN
|
| 62 |
+
namespace container_algorithm_internal {
|
| 63 |
+
|
| 64 |
+
// NOTE: it is important to defer to ADL lookup for building with C++ modules,
|
| 65 |
+
// especially for headers like <valarray> which are not visible from this file
|
| 66 |
+
// but specialize std::begin and std::end.
|
| 67 |
+
using std::begin;
|
| 68 |
+
using std::end;
|
| 69 |
+
|
| 70 |
+
// The type of the iterator given by begin(c) (possibly std::begin(c)).
|
| 71 |
+
// ContainerIter<const vector<T>> gives vector<T>::const_iterator,
|
| 72 |
+
// while ContainerIter<vector<T>> gives vector<T>::iterator.
|
| 73 |
+
template <typename C>
|
| 74 |
+
using ContainerIter = decltype(begin(std::declval<C&>()));
|
| 75 |
+
|
| 76 |
+
// An MSVC bug involving template parameter substitution requires us to use
|
| 77 |
+
// decltype() here instead of just std::pair.
|
| 78 |
+
template <typename C1, typename C2>
|
| 79 |
+
using ContainerIterPairType =
|
| 80 |
+
decltype(std::make_pair(ContainerIter<C1>(), ContainerIter<C2>()));
|
| 81 |
+
|
| 82 |
+
template <typename C>
|
| 83 |
+
using ContainerDifferenceType = decltype(std::distance(
|
| 84 |
+
std::declval<ContainerIter<C>>(), std::declval<ContainerIter<C>>()));
|
| 85 |
+
|
| 86 |
+
template <typename C>
|
| 87 |
+
using ContainerPointerType =
|
| 88 |
+
typename std::iterator_traits<ContainerIter<C>>::pointer;
|
| 89 |
+
|
| 90 |
+
// container_algorithm_internal::c_begin and
|
| 91 |
+
// container_algorithm_internal::c_end are abbreviations for proper ADL
|
| 92 |
+
// lookup of std::begin and std::end, i.e.
|
| 93 |
+
// using std::begin;
|
| 94 |
+
// using std::end;
|
| 95 |
+
// std::foo(begin(c), end(c));
|
| 96 |
+
// becomes
|
| 97 |
+
// std::foo(container_algorithm_internal::c_begin(c),
|
| 98 |
+
// container_algorithm_internal::c_end(c));
|
| 99 |
+
// These are meant for internal use only.
|
| 100 |
+
|
| 101 |
+
template <typename C>
|
| 102 |
+
ABSL_INTERNAL_CONSTEXPR_SINCE_CXX17 ContainerIter<C> c_begin(C& c) {
|
| 103 |
+
return begin(c);
|
| 104 |
+
}
|
| 105 |
+
|
| 106 |
+
template <typename C>
|
| 107 |
+
ABSL_INTERNAL_CONSTEXPR_SINCE_CXX17 ContainerIter<C> c_end(C& c) {
|
| 108 |
+
return end(c);
|
| 109 |
+
}
|
| 110 |
+
|
| 111 |
+
template <typename T>
|
| 112 |
+
struct IsUnorderedContainer : std::false_type {};
|
| 113 |
+
|
| 114 |
+
template <class Key, class T, class Hash, class KeyEqual, class Allocator>
|
| 115 |
+
struct IsUnorderedContainer<
|
| 116 |
+
std::unordered_map<Key, T, Hash, KeyEqual, Allocator>> : std::true_type {};
|
| 117 |
+
|
| 118 |
+
template <class Key, class Hash, class KeyEqual, class Allocator>
|
| 119 |
+
struct IsUnorderedContainer<std::unordered_set<Key, Hash, KeyEqual, Allocator>>
|
| 120 |
+
: std::true_type {};
|
| 121 |
+
|
| 122 |
+
} // namespace container_algorithm_internal
|
| 123 |
+
|
| 124 |
+
// PUBLIC API
|
| 125 |
+
|
| 126 |
+
//------------------------------------------------------------------------------
|
| 127 |
+
// Abseil algorithm.h functions
|
| 128 |
+
//------------------------------------------------------------------------------
|
| 129 |
+
|
| 130 |
+
// c_linear_search()
|
| 131 |
+
//
|
| 132 |
+
// Container-based version of absl::linear_search() for performing a linear
|
| 133 |
+
// search within a container.
|
| 134 |
+
//
|
| 135 |
+
// For a generalization that uses a predicate, see absl::c_any_of().
|
| 136 |
+
template <typename C, typename EqualityComparable>
|
| 137 |
+
ABSL_INTERNAL_CONSTEXPR_SINCE_CXX20 bool c_linear_search(
|
| 138 |
+
const C& c, EqualityComparable&& value) {
|
| 139 |
+
return absl::linear_search(container_algorithm_internal::c_begin(c),
|
| 140 |
+
container_algorithm_internal::c_end(c),
|
| 141 |
+
std::forward<EqualityComparable>(value));
|
| 142 |
+
}
|
| 143 |
+
|
| 144 |
+
//------------------------------------------------------------------------------
|
| 145 |
+
// <iterator> algorithms
|
| 146 |
+
//------------------------------------------------------------------------------
|
| 147 |
+
|
| 148 |
+
// c_distance()
|
| 149 |
+
//
|
| 150 |
+
// Container-based version of the <iterator> `std::distance()` function to
|
| 151 |
+
// return the number of elements within a container.
|
| 152 |
+
template <typename C>
|
| 153 |
+
ABSL_INTERNAL_CONSTEXPR_SINCE_CXX17
|
| 154 |
+
container_algorithm_internal::ContainerDifferenceType<const C>
|
| 155 |
+
c_distance(const C& c) {
|
| 156 |
+
return std::distance(container_algorithm_internal::c_begin(c),
|
| 157 |
+
container_algorithm_internal::c_end(c));
|
| 158 |
+
}
|
| 159 |
+
|
| 160 |
+
//------------------------------------------------------------------------------
|
| 161 |
+
// <algorithm> Non-modifying sequence operations
|
| 162 |
+
//------------------------------------------------------------------------------
|
| 163 |
+
|
| 164 |
+
// c_all_of()
|
| 165 |
+
//
|
| 166 |
+
// Container-based version of the <algorithm> `std::all_of()` function to
|
| 167 |
+
// test if all elements within a container satisfy a condition.
|
| 168 |
+
template <typename C, typename Pred>
|
| 169 |
+
ABSL_INTERNAL_CONSTEXPR_SINCE_CXX20 bool c_all_of(const C& c, Pred&& pred) {
|
| 170 |
+
return std::all_of(container_algorithm_internal::c_begin(c),
|
| 171 |
+
container_algorithm_internal::c_end(c),
|
| 172 |
+
std::forward<Pred>(pred));
|
| 173 |
+
}
|
| 174 |
+
|
| 175 |
+
// c_any_of()
|
| 176 |
+
//
|
| 177 |
+
// Container-based version of the <algorithm> `std::any_of()` function to
|
| 178 |
+
// test if any element in a container fulfills a condition.
|
| 179 |
+
template <typename C, typename Pred>
|
| 180 |
+
ABSL_INTERNAL_CONSTEXPR_SINCE_CXX20 bool c_any_of(const C& c, Pred&& pred) {
|
| 181 |
+
return std::any_of(container_algorithm_internal::c_begin(c),
|
| 182 |
+
container_algorithm_internal::c_end(c),
|
| 183 |
+
std::forward<Pred>(pred));
|
| 184 |
+
}
|
| 185 |
+
|
| 186 |
+
// c_none_of()
|
| 187 |
+
//
|
| 188 |
+
// Container-based version of the <algorithm> `std::none_of()` function to
|
| 189 |
+
// test if no elements in a container fulfill a condition.
|
| 190 |
+
template <typename C, typename Pred>
|
| 191 |
+
ABSL_INTERNAL_CONSTEXPR_SINCE_CXX20 bool c_none_of(const C& c, Pred&& pred) {
|
| 192 |
+
return std::none_of(container_algorithm_internal::c_begin(c),
|
| 193 |
+
container_algorithm_internal::c_end(c),
|
| 194 |
+
std::forward<Pred>(pred));
|
| 195 |
+
}
|
| 196 |
+
|
| 197 |
+
// c_for_each()
|
| 198 |
+
//
|
| 199 |
+
// Container-based version of the <algorithm> `std::for_each()` function to
|
| 200 |
+
// apply a function to a container's elements.
|
| 201 |
+
template <typename C, typename Function>
|
| 202 |
+
ABSL_INTERNAL_CONSTEXPR_SINCE_CXX20 decay_t<Function> c_for_each(C&& c,
|
| 203 |
+
Function&& f) {
|
| 204 |
+
return std::for_each(container_algorithm_internal::c_begin(c),
|
| 205 |
+
container_algorithm_internal::c_end(c),
|
| 206 |
+
std::forward<Function>(f));
|
| 207 |
+
}
|
| 208 |
+
|
| 209 |
+
// c_find()
|
| 210 |
+
//
|
| 211 |
+
// Container-based version of the <algorithm> `std::find()` function to find
|
| 212 |
+
// the first element containing the passed value within a container value.
|
| 213 |
+
template <typename C, typename T>
|
| 214 |
+
ABSL_INTERNAL_CONSTEXPR_SINCE_CXX20
|
| 215 |
+
container_algorithm_internal::ContainerIter<C>
|
| 216 |
+
c_find(C& c, T&& value) {
|
| 217 |
+
return std::find(container_algorithm_internal::c_begin(c),
|
| 218 |
+
container_algorithm_internal::c_end(c),
|
| 219 |
+
std::forward<T>(value));
|
| 220 |
+
}
|
| 221 |
+
|
| 222 |
+
// c_contains()
|
| 223 |
+
//
|
| 224 |
+
// Container-based version of the <algorithm> `std::ranges::contains()` C++23
|
| 225 |
+
// function to search a container for a value.
|
| 226 |
+
template <typename Sequence, typename T>
|
| 227 |
+
ABSL_INTERNAL_CONSTEXPR_SINCE_CXX20 bool c_contains(const Sequence& sequence,
|
| 228 |
+
T&& value) {
|
| 229 |
+
return absl::c_find(sequence, std::forward<T>(value)) !=
|
| 230 |
+
container_algorithm_internal::c_end(sequence);
|
| 231 |
+
}
|
| 232 |
+
|
| 233 |
+
// c_find_if()
|
| 234 |
+
//
|
| 235 |
+
// Container-based version of the <algorithm> `std::find_if()` function to find
|
| 236 |
+
// the first element in a container matching the given condition.
|
| 237 |
+
template <typename C, typename Pred>
|
| 238 |
+
ABSL_INTERNAL_CONSTEXPR_SINCE_CXX20
|
| 239 |
+
container_algorithm_internal::ContainerIter<C>
|
| 240 |
+
c_find_if(C& c, Pred&& pred) {
|
| 241 |
+
return std::find_if(container_algorithm_internal::c_begin(c),
|
| 242 |
+
container_algorithm_internal::c_end(c),
|
| 243 |
+
std::forward<Pred>(pred));
|
| 244 |
+
}
|
| 245 |
+
|
| 246 |
+
// c_find_if_not()
|
| 247 |
+
//
|
| 248 |
+
// Container-based version of the <algorithm> `std::find_if_not()` function to
|
| 249 |
+
// find the first element in a container not matching the given condition.
|
| 250 |
+
template <typename C, typename Pred>
|
| 251 |
+
ABSL_INTERNAL_CONSTEXPR_SINCE_CXX20
|
| 252 |
+
container_algorithm_internal::ContainerIter<C>
|
| 253 |
+
c_find_if_not(C& c, Pred&& pred) {
|
| 254 |
+
return std::find_if_not(container_algorithm_internal::c_begin(c),
|
| 255 |
+
container_algorithm_internal::c_end(c),
|
| 256 |
+
std::forward<Pred>(pred));
|
| 257 |
+
}
|
| 258 |
+
|
| 259 |
+
// c_find_end()
|
| 260 |
+
//
|
| 261 |
+
// Container-based version of the <algorithm> `std::find_end()` function to
|
| 262 |
+
// find the last subsequence within a container.
|
| 263 |
+
template <typename Sequence1, typename Sequence2>
|
| 264 |
+
ABSL_INTERNAL_CONSTEXPR_SINCE_CXX20
|
| 265 |
+
container_algorithm_internal::ContainerIter<Sequence1>
|
| 266 |
+
c_find_end(Sequence1& sequence, Sequence2& subsequence) {
|
| 267 |
+
return std::find_end(container_algorithm_internal::c_begin(sequence),
|
| 268 |
+
container_algorithm_internal::c_end(sequence),
|
| 269 |
+
container_algorithm_internal::c_begin(subsequence),
|
| 270 |
+
container_algorithm_internal::c_end(subsequence));
|
| 271 |
+
}
|
| 272 |
+
|
| 273 |
+
// Overload of c_find_end() for using a predicate evaluation other than `==` as
|
| 274 |
+
// the function's test condition.
|
| 275 |
+
template <typename Sequence1, typename Sequence2, typename BinaryPredicate>
|
| 276 |
+
ABSL_INTERNAL_CONSTEXPR_SINCE_CXX20
|
| 277 |
+
container_algorithm_internal::ContainerIter<Sequence1>
|
| 278 |
+
c_find_end(Sequence1& sequence, Sequence2& subsequence,
|
| 279 |
+
BinaryPredicate&& pred) {
|
| 280 |
+
return std::find_end(container_algorithm_internal::c_begin(sequence),
|
| 281 |
+
container_algorithm_internal::c_end(sequence),
|
| 282 |
+
container_algorithm_internal::c_begin(subsequence),
|
| 283 |
+
container_algorithm_internal::c_end(subsequence),
|
| 284 |
+
std::forward<BinaryPredicate>(pred));
|
| 285 |
+
}
|
| 286 |
+
|
| 287 |
+
// c_find_first_of()
|
| 288 |
+
//
|
| 289 |
+
// Container-based version of the <algorithm> `std::find_first_of()` function to
|
| 290 |
+
// find the first element within the container that is also within the options
|
| 291 |
+
// container.
|
| 292 |
+
template <typename C1, typename C2>
|
| 293 |
+
ABSL_INTERNAL_CONSTEXPR_SINCE_CXX20
|
| 294 |
+
container_algorithm_internal::ContainerIter<C1>
|
| 295 |
+
c_find_first_of(C1& container, const C2& options) {
|
| 296 |
+
return std::find_first_of(container_algorithm_internal::c_begin(container),
|
| 297 |
+
container_algorithm_internal::c_end(container),
|
| 298 |
+
container_algorithm_internal::c_begin(options),
|
| 299 |
+
container_algorithm_internal::c_end(options));
|
| 300 |
+
}
|
| 301 |
+
|
| 302 |
+
// Overload of c_find_first_of() for using a predicate evaluation other than
|
| 303 |
+
// `==` as the function's test condition.
|
| 304 |
+
template <typename C1, typename C2, typename BinaryPredicate>
|
| 305 |
+
ABSL_INTERNAL_CONSTEXPR_SINCE_CXX20
|
| 306 |
+
container_algorithm_internal::ContainerIter<C1>
|
| 307 |
+
c_find_first_of(C1& container, const C2& options, BinaryPredicate&& pred) {
|
| 308 |
+
return std::find_first_of(container_algorithm_internal::c_begin(container),
|
| 309 |
+
container_algorithm_internal::c_end(container),
|
| 310 |
+
container_algorithm_internal::c_begin(options),
|
| 311 |
+
container_algorithm_internal::c_end(options),
|
| 312 |
+
std::forward<BinaryPredicate>(pred));
|
| 313 |
+
}
|
| 314 |
+
|
| 315 |
+
// c_adjacent_find()
|
| 316 |
+
//
|
| 317 |
+
// Container-based version of the <algorithm> `std::adjacent_find()` function to
|
| 318 |
+
// find equal adjacent elements within a container.
|
| 319 |
+
template <typename Sequence>
|
| 320 |
+
ABSL_INTERNAL_CONSTEXPR_SINCE_CXX20
|
| 321 |
+
container_algorithm_internal::ContainerIter<Sequence>
|
| 322 |
+
c_adjacent_find(Sequence& sequence) {
|
| 323 |
+
return std::adjacent_find(container_algorithm_internal::c_begin(sequence),
|
| 324 |
+
container_algorithm_internal::c_end(sequence));
|
| 325 |
+
}
|
| 326 |
+
|
| 327 |
+
// Overload of c_adjacent_find() for using a predicate evaluation other than
|
| 328 |
+
// `==` as the function's test condition.
|
| 329 |
+
template <typename Sequence, typename BinaryPredicate>
|
| 330 |
+
ABSL_INTERNAL_CONSTEXPR_SINCE_CXX20
|
| 331 |
+
container_algorithm_internal::ContainerIter<Sequence>
|
| 332 |
+
c_adjacent_find(Sequence& sequence, BinaryPredicate&& pred) {
|
| 333 |
+
return std::adjacent_find(container_algorithm_internal::c_begin(sequence),
|
| 334 |
+
container_algorithm_internal::c_end(sequence),
|
| 335 |
+
std::forward<BinaryPredicate>(pred));
|
| 336 |
+
}
|
| 337 |
+
|
| 338 |
+
// c_count()
|
| 339 |
+
//
|
| 340 |
+
// Container-based version of the <algorithm> `std::count()` function to count
|
| 341 |
+
// values that match within a container.
|
| 342 |
+
template <typename C, typename T>
|
| 343 |
+
ABSL_INTERNAL_CONSTEXPR_SINCE_CXX20
|
| 344 |
+
container_algorithm_internal::ContainerDifferenceType<const C>
|
| 345 |
+
c_count(const C& c, T&& value) {
|
| 346 |
+
return std::count(container_algorithm_internal::c_begin(c),
|
| 347 |
+
container_algorithm_internal::c_end(c),
|
| 348 |
+
std::forward<T>(value));
|
| 349 |
+
}
|
| 350 |
+
|
| 351 |
+
// c_count_if()
|
| 352 |
+
//
|
| 353 |
+
// Container-based version of the <algorithm> `std::count_if()` function to
|
| 354 |
+
// count values matching a condition within a container.
|
| 355 |
+
template <typename C, typename Pred>
|
| 356 |
+
ABSL_INTERNAL_CONSTEXPR_SINCE_CXX20
|
| 357 |
+
container_algorithm_internal::ContainerDifferenceType<const C>
|
| 358 |
+
c_count_if(const C& c, Pred&& pred) {
|
| 359 |
+
return std::count_if(container_algorithm_internal::c_begin(c),
|
| 360 |
+
container_algorithm_internal::c_end(c),
|
| 361 |
+
std::forward<Pred>(pred));
|
| 362 |
+
}
|
| 363 |
+
|
| 364 |
+
// c_mismatch()
|
| 365 |
+
//
|
| 366 |
+
// Container-based version of the <algorithm> `std::mismatch()` function to
|
| 367 |
+
// return the first element where two ordered containers differ. Applies `==` to
|
| 368 |
+
// the first N elements of `c1` and `c2`, where N = min(size(c1), size(c2)).
|
| 369 |
+
template <typename C1, typename C2>
|
| 370 |
+
ABSL_INTERNAL_CONSTEXPR_SINCE_CXX20
|
| 371 |
+
container_algorithm_internal::ContainerIterPairType<C1, C2>
|
| 372 |
+
c_mismatch(C1& c1, C2& c2) {
|
| 373 |
+
return std::mismatch(container_algorithm_internal::c_begin(c1),
|
| 374 |
+
container_algorithm_internal::c_end(c1),
|
| 375 |
+
container_algorithm_internal::c_begin(c2),
|
| 376 |
+
container_algorithm_internal::c_end(c2));
|
| 377 |
+
}
|
| 378 |
+
|
| 379 |
+
// Overload of c_mismatch() for using a predicate evaluation other than `==` as
|
| 380 |
+
// the function's test condition. Applies `pred`to the first N elements of `c1`
|
| 381 |
+
// and `c2`, where N = min(size(c1), size(c2)).
|
| 382 |
+
template <typename C1, typename C2, typename BinaryPredicate>
|
| 383 |
+
ABSL_INTERNAL_CONSTEXPR_SINCE_CXX20
|
| 384 |
+
container_algorithm_internal::ContainerIterPairType<C1, C2>
|
| 385 |
+
c_mismatch(C1& c1, C2& c2, BinaryPredicate pred) {
|
| 386 |
+
return std::mismatch(container_algorithm_internal::c_begin(c1),
|
| 387 |
+
container_algorithm_internal::c_end(c1),
|
| 388 |
+
container_algorithm_internal::c_begin(c2),
|
| 389 |
+
container_algorithm_internal::c_end(c2), pred);
|
| 390 |
+
}
|
| 391 |
+
|
| 392 |
+
// c_equal()
|
| 393 |
+
//
|
| 394 |
+
// Container-based version of the <algorithm> `std::equal()` function to
|
| 395 |
+
// test whether two containers are equal.
|
| 396 |
+
template <typename C1, typename C2>
|
| 397 |
+
ABSL_INTERNAL_CONSTEXPR_SINCE_CXX20 bool c_equal(const C1& c1, const C2& c2) {
|
| 398 |
+
return std::equal(container_algorithm_internal::c_begin(c1),
|
| 399 |
+
container_algorithm_internal::c_end(c1),
|
| 400 |
+
container_algorithm_internal::c_begin(c2),
|
| 401 |
+
container_algorithm_internal::c_end(c2));
|
| 402 |
+
}
|
| 403 |
+
|
| 404 |
+
// Overload of c_equal() for using a predicate evaluation other than `==` as
|
| 405 |
+
// the function's test condition.
|
| 406 |
+
template <typename C1, typename C2, typename BinaryPredicate>
|
| 407 |
+
ABSL_INTERNAL_CONSTEXPR_SINCE_CXX20 bool c_equal(const C1& c1, const C2& c2,
|
| 408 |
+
BinaryPredicate&& pred) {
|
| 409 |
+
return std::equal(container_algorithm_internal::c_begin(c1),
|
| 410 |
+
container_algorithm_internal::c_end(c1),
|
| 411 |
+
container_algorithm_internal::c_begin(c2),
|
| 412 |
+
container_algorithm_internal::c_end(c2),
|
| 413 |
+
std::forward<BinaryPredicate>(pred));
|
| 414 |
+
}
|
| 415 |
+
|
| 416 |
+
// c_is_permutation()
|
| 417 |
+
//
|
| 418 |
+
// Container-based version of the <algorithm> `std::is_permutation()` function
|
| 419 |
+
// to test whether a container is a permutation of another.
|
| 420 |
+
template <typename C1, typename C2>
|
| 421 |
+
ABSL_INTERNAL_CONSTEXPR_SINCE_CXX20 bool c_is_permutation(const C1& c1,
|
| 422 |
+
const C2& c2) {
|
| 423 |
+
return std::is_permutation(container_algorithm_internal::c_begin(c1),
|
| 424 |
+
container_algorithm_internal::c_end(c1),
|
| 425 |
+
container_algorithm_internal::c_begin(c2),
|
| 426 |
+
container_algorithm_internal::c_end(c2));
|
| 427 |
+
}
|
| 428 |
+
|
| 429 |
+
// Overload of c_is_permutation() for using a predicate evaluation other than
|
| 430 |
+
// `==` as the function's test condition.
|
| 431 |
+
template <typename C1, typename C2, typename BinaryPredicate>
|
| 432 |
+
ABSL_INTERNAL_CONSTEXPR_SINCE_CXX20 bool c_is_permutation(
|
| 433 |
+
const C1& c1, const C2& c2, BinaryPredicate&& pred) {
|
| 434 |
+
return std::is_permutation(container_algorithm_internal::c_begin(c1),
|
| 435 |
+
container_algorithm_internal::c_end(c1),
|
| 436 |
+
container_algorithm_internal::c_begin(c2),
|
| 437 |
+
container_algorithm_internal::c_end(c2),
|
| 438 |
+
std::forward<BinaryPredicate>(pred));
|
| 439 |
+
}
|
| 440 |
+
|
| 441 |
+
// c_search()
|
| 442 |
+
//
|
| 443 |
+
// Container-based version of the <algorithm> `std::search()` function to search
|
| 444 |
+
// a container for a subsequence.
|
| 445 |
+
template <typename Sequence1, typename Sequence2>
|
| 446 |
+
ABSL_INTERNAL_CONSTEXPR_SINCE_CXX20
|
| 447 |
+
container_algorithm_internal::ContainerIter<Sequence1>
|
| 448 |
+
c_search(Sequence1& sequence, Sequence2& subsequence) {
|
| 449 |
+
return std::search(container_algorithm_internal::c_begin(sequence),
|
| 450 |
+
container_algorithm_internal::c_end(sequence),
|
| 451 |
+
container_algorithm_internal::c_begin(subsequence),
|
| 452 |
+
container_algorithm_internal::c_end(subsequence));
|
| 453 |
+
}
|
| 454 |
+
|
| 455 |
+
// Overload of c_search() for using a predicate evaluation other than
|
| 456 |
+
// `==` as the function's test condition.
|
| 457 |
+
template <typename Sequence1, typename Sequence2, typename BinaryPredicate>
|
| 458 |
+
ABSL_INTERNAL_CONSTEXPR_SINCE_CXX20
|
| 459 |
+
container_algorithm_internal::ContainerIter<Sequence1>
|
| 460 |
+
c_search(Sequence1& sequence, Sequence2& subsequence,
|
| 461 |
+
BinaryPredicate&& pred) {
|
| 462 |
+
return std::search(container_algorithm_internal::c_begin(sequence),
|
| 463 |
+
container_algorithm_internal::c_end(sequence),
|
| 464 |
+
container_algorithm_internal::c_begin(subsequence),
|
| 465 |
+
container_algorithm_internal::c_end(subsequence),
|
| 466 |
+
std::forward<BinaryPredicate>(pred));
|
| 467 |
+
}
|
| 468 |
+
|
| 469 |
+
// c_contains_subrange()
|
| 470 |
+
//
|
| 471 |
+
// Container-based version of the <algorithm> `std::ranges::contains_subrange()`
|
| 472 |
+
// C++23 function to search a container for a subsequence.
|
| 473 |
+
template <typename Sequence1, typename Sequence2>
|
| 474 |
+
ABSL_INTERNAL_CONSTEXPR_SINCE_CXX20 bool c_contains_subrange(
|
| 475 |
+
Sequence1& sequence, Sequence2& subsequence) {
|
| 476 |
+
return absl::c_search(sequence, subsequence) !=
|
| 477 |
+
container_algorithm_internal::c_end(sequence);
|
| 478 |
+
}
|
| 479 |
+
|
| 480 |
+
// Overload of c_contains_subrange() for using a predicate evaluation other than
|
| 481 |
+
// `==` as the function's test condition.
|
| 482 |
+
template <typename Sequence1, typename Sequence2, typename BinaryPredicate>
|
| 483 |
+
ABSL_INTERNAL_CONSTEXPR_SINCE_CXX20 bool c_contains_subrange(
|
| 484 |
+
Sequence1& sequence, Sequence2& subsequence, BinaryPredicate&& pred) {
|
| 485 |
+
return absl::c_search(sequence, subsequence,
|
| 486 |
+
std::forward<BinaryPredicate>(pred)) !=
|
| 487 |
+
container_algorithm_internal::c_end(sequence);
|
| 488 |
+
}
|
| 489 |
+
|
| 490 |
+
// c_search_n()
|
| 491 |
+
//
|
| 492 |
+
// Container-based version of the <algorithm> `std::search_n()` function to
|
| 493 |
+
// search a container for the first sequence of N elements.
|
| 494 |
+
template <typename Sequence, typename Size, typename T>
|
| 495 |
+
ABSL_INTERNAL_CONSTEXPR_SINCE_CXX20
|
| 496 |
+
container_algorithm_internal::ContainerIter<Sequence>
|
| 497 |
+
c_search_n(Sequence& sequence, Size count, T&& value) {
|
| 498 |
+
return std::search_n(container_algorithm_internal::c_begin(sequence),
|
| 499 |
+
container_algorithm_internal::c_end(sequence), count,
|
| 500 |
+
std::forward<T>(value));
|
| 501 |
+
}
|
| 502 |
+
|
| 503 |
+
// Overload of c_search_n() for using a predicate evaluation other than
|
| 504 |
+
// `==` as the function's test condition.
|
| 505 |
+
template <typename Sequence, typename Size, typename T,
|
| 506 |
+
typename BinaryPredicate>
|
| 507 |
+
ABSL_INTERNAL_CONSTEXPR_SINCE_CXX20
|
| 508 |
+
container_algorithm_internal::ContainerIter<Sequence>
|
| 509 |
+
c_search_n(Sequence& sequence, Size count, T&& value,
|
| 510 |
+
BinaryPredicate&& pred) {
|
| 511 |
+
return std::search_n(container_algorithm_internal::c_begin(sequence),
|
| 512 |
+
container_algorithm_internal::c_end(sequence), count,
|
| 513 |
+
std::forward<T>(value),
|
| 514 |
+
std::forward<BinaryPredicate>(pred));
|
| 515 |
+
}
|
| 516 |
+
|
| 517 |
+
//------------------------------------------------------------------------------
|
| 518 |
+
// <algorithm> Modifying sequence operations
|
| 519 |
+
//------------------------------------------------------------------------------
|
| 520 |
+
|
| 521 |
+
// c_copy()
|
| 522 |
+
//
|
| 523 |
+
// Container-based version of the <algorithm> `std::copy()` function to copy a
|
| 524 |
+
// container's elements into an iterator.
|
| 525 |
+
template <typename InputSequence, typename OutputIterator>
|
| 526 |
+
OutputIterator c_copy(const InputSequence& input, OutputIterator output) {
|
| 527 |
+
return std::copy(container_algorithm_internal::c_begin(input),
|
| 528 |
+
container_algorithm_internal::c_end(input), output);
|
| 529 |
+
}
|
| 530 |
+
|
| 531 |
+
// c_copy_n()
|
| 532 |
+
//
|
| 533 |
+
// Container-based version of the <algorithm> `std::copy_n()` function to copy a
|
| 534 |
+
// container's first N elements into an iterator.
|
| 535 |
+
template <typename C, typename Size, typename OutputIterator>
|
| 536 |
+
OutputIterator c_copy_n(const C& input, Size n, OutputIterator output) {
|
| 537 |
+
return std::copy_n(container_algorithm_internal::c_begin(input), n, output);
|
| 538 |
+
}
|
| 539 |
+
|
| 540 |
+
// c_copy_if()
|
| 541 |
+
//
|
| 542 |
+
// Container-based version of the <algorithm> `std::copy_if()` function to copy
|
| 543 |
+
// a container's elements satisfying some condition into an iterator.
|
| 544 |
+
template <typename InputSequence, typename OutputIterator, typename Pred>
|
| 545 |
+
OutputIterator c_copy_if(const InputSequence& input, OutputIterator output,
|
| 546 |
+
Pred&& pred) {
|
| 547 |
+
return std::copy_if(container_algorithm_internal::c_begin(input),
|
| 548 |
+
container_algorithm_internal::c_end(input), output,
|
| 549 |
+
std::forward<Pred>(pred));
|
| 550 |
+
}
|
| 551 |
+
|
| 552 |
+
// c_copy_backward()
|
| 553 |
+
//
|
| 554 |
+
// Container-based version of the <algorithm> `std::copy_backward()` function to
|
| 555 |
+
// copy a container's elements in reverse order into an iterator.
|
| 556 |
+
template <typename C, typename BidirectionalIterator>
|
| 557 |
+
BidirectionalIterator c_copy_backward(const C& src,
|
| 558 |
+
BidirectionalIterator dest) {
|
| 559 |
+
return std::copy_backward(container_algorithm_internal::c_begin(src),
|
| 560 |
+
container_algorithm_internal::c_end(src), dest);
|
| 561 |
+
}
|
| 562 |
+
|
| 563 |
+
// c_move()
|
| 564 |
+
//
|
| 565 |
+
// Container-based version of the <algorithm> `std::move()` function to move
|
| 566 |
+
// a container's elements into an iterator.
|
| 567 |
+
template <typename C, typename OutputIterator>
|
| 568 |
+
OutputIterator c_move(C&& src, OutputIterator dest) {
|
| 569 |
+
return std::move(container_algorithm_internal::c_begin(src),
|
| 570 |
+
container_algorithm_internal::c_end(src), dest);
|
| 571 |
+
}
|
| 572 |
+
|
| 573 |
+
// c_move_backward()
|
| 574 |
+
//
|
| 575 |
+
// Container-based version of the <algorithm> `std::move_backward()` function to
|
| 576 |
+
// move a container's elements into an iterator in reverse order.
|
| 577 |
+
template <typename C, typename BidirectionalIterator>
|
| 578 |
+
BidirectionalIterator c_move_backward(C&& src, BidirectionalIterator dest) {
|
| 579 |
+
return std::move_backward(container_algorithm_internal::c_begin(src),
|
| 580 |
+
container_algorithm_internal::c_end(src), dest);
|
| 581 |
+
}
|
| 582 |
+
|
| 583 |
+
// c_swap_ranges()
|
| 584 |
+
//
|
| 585 |
+
// Container-based version of the <algorithm> `std::swap_ranges()` function to
|
| 586 |
+
// swap a container's elements with another container's elements. Swaps the
|
| 587 |
+
// first N elements of `c1` and `c2`, where N = min(size(c1), size(c2)).
|
| 588 |
+
template <typename C1, typename C2>
|
| 589 |
+
container_algorithm_internal::ContainerIter<C2> c_swap_ranges(C1& c1, C2& c2) {
|
| 590 |
+
auto first1 = container_algorithm_internal::c_begin(c1);
|
| 591 |
+
auto last1 = container_algorithm_internal::c_end(c1);
|
| 592 |
+
auto first2 = container_algorithm_internal::c_begin(c2);
|
| 593 |
+
auto last2 = container_algorithm_internal::c_end(c2);
|
| 594 |
+
|
| 595 |
+
using std::swap;
|
| 596 |
+
for (; first1 != last1 && first2 != last2; ++first1, (void)++first2) {
|
| 597 |
+
swap(*first1, *first2);
|
| 598 |
+
}
|
| 599 |
+
return first2;
|
| 600 |
+
}
|
| 601 |
+
|
| 602 |
+
// c_transform()
|
| 603 |
+
//
|
| 604 |
+
// Container-based version of the <algorithm> `std::transform()` function to
|
| 605 |
+
// transform a container's elements using the unary operation, storing the
|
| 606 |
+
// result in an iterator pointing to the last transformed element in the output
|
| 607 |
+
// range.
|
| 608 |
+
template <typename InputSequence, typename OutputIterator, typename UnaryOp>
|
| 609 |
+
OutputIterator c_transform(const InputSequence& input, OutputIterator output,
|
| 610 |
+
UnaryOp&& unary_op) {
|
| 611 |
+
return std::transform(container_algorithm_internal::c_begin(input),
|
| 612 |
+
container_algorithm_internal::c_end(input), output,
|
| 613 |
+
std::forward<UnaryOp>(unary_op));
|
| 614 |
+
}
|
| 615 |
+
|
| 616 |
+
// Overload of c_transform() for performing a transformation using a binary
|
| 617 |
+
// predicate. Applies `binary_op` to the first N elements of `c1` and `c2`,
|
| 618 |
+
// where N = min(size(c1), size(c2)).
|
| 619 |
+
template <typename InputSequence1, typename InputSequence2,
|
| 620 |
+
typename OutputIterator, typename BinaryOp>
|
| 621 |
+
OutputIterator c_transform(const InputSequence1& input1,
|
| 622 |
+
const InputSequence2& input2, OutputIterator output,
|
| 623 |
+
BinaryOp&& binary_op) {
|
| 624 |
+
auto first1 = container_algorithm_internal::c_begin(input1);
|
| 625 |
+
auto last1 = container_algorithm_internal::c_end(input1);
|
| 626 |
+
auto first2 = container_algorithm_internal::c_begin(input2);
|
| 627 |
+
auto last2 = container_algorithm_internal::c_end(input2);
|
| 628 |
+
for (; first1 != last1 && first2 != last2;
|
| 629 |
+
++first1, (void)++first2, ++output) {
|
| 630 |
+
*output = binary_op(*first1, *first2);
|
| 631 |
+
}
|
| 632 |
+
|
| 633 |
+
return output;
|
| 634 |
+
}
|
| 635 |
+
|
| 636 |
+
// c_replace()
|
| 637 |
+
//
|
| 638 |
+
// Container-based version of the <algorithm> `std::replace()` function to
|
| 639 |
+
// replace a container's elements of some value with a new value. The container
|
| 640 |
+
// is modified in place.
|
| 641 |
+
template <typename Sequence, typename T>
|
| 642 |
+
void c_replace(Sequence& sequence, const T& old_value, const T& new_value) {
|
| 643 |
+
std::replace(container_algorithm_internal::c_begin(sequence),
|
| 644 |
+
container_algorithm_internal::c_end(sequence), old_value,
|
| 645 |
+
new_value);
|
| 646 |
+
}
|
| 647 |
+
|
| 648 |
+
// c_replace_if()
|
| 649 |
+
//
|
| 650 |
+
// Container-based version of the <algorithm> `std::replace_if()` function to
|
| 651 |
+
// replace a container's elements of some value with a new value based on some
|
| 652 |
+
// condition. The container is modified in place.
|
| 653 |
+
template <typename C, typename Pred, typename T>
|
| 654 |
+
void c_replace_if(C& c, Pred&& pred, T&& new_value) {
|
| 655 |
+
std::replace_if(container_algorithm_internal::c_begin(c),
|
| 656 |
+
container_algorithm_internal::c_end(c),
|
| 657 |
+
std::forward<Pred>(pred), std::forward<T>(new_value));
|
| 658 |
+
}
|
| 659 |
+
|
| 660 |
+
// c_replace_copy()
|
| 661 |
+
//
|
| 662 |
+
// Container-based version of the <algorithm> `std::replace_copy()` function to
|
| 663 |
+
// replace a container's elements of some value with a new value and return the
|
| 664 |
+
// results within an iterator.
|
| 665 |
+
template <typename C, typename OutputIterator, typename T>
|
| 666 |
+
OutputIterator c_replace_copy(const C& c, OutputIterator result, T&& old_value,
|
| 667 |
+
T&& new_value) {
|
| 668 |
+
return std::replace_copy(container_algorithm_internal::c_begin(c),
|
| 669 |
+
container_algorithm_internal::c_end(c), result,
|
| 670 |
+
std::forward<T>(old_value),
|
| 671 |
+
std::forward<T>(new_value));
|
| 672 |
+
}
|
| 673 |
+
|
| 674 |
+
// c_replace_copy_if()
|
| 675 |
+
//
|
| 676 |
+
// Container-based version of the <algorithm> `std::replace_copy_if()` function
|
| 677 |
+
// to replace a container's elements of some value with a new value based on
|
| 678 |
+
// some condition, and return the results within an iterator.
|
| 679 |
+
template <typename C, typename OutputIterator, typename Pred, typename T>
|
| 680 |
+
OutputIterator c_replace_copy_if(const C& c, OutputIterator result, Pred&& pred,
|
| 681 |
+
const T& new_value) {
|
| 682 |
+
return std::replace_copy_if(container_algorithm_internal::c_begin(c),
|
| 683 |
+
container_algorithm_internal::c_end(c), result,
|
| 684 |
+
std::forward<Pred>(pred), new_value);
|
| 685 |
+
}
|
| 686 |
+
|
| 687 |
+
// c_fill()
|
| 688 |
+
//
|
| 689 |
+
// Container-based version of the <algorithm> `std::fill()` function to fill a
|
| 690 |
+
// container with some value.
|
| 691 |
+
template <typename C, typename T>
|
| 692 |
+
void c_fill(C& c, const T& value) {
|
| 693 |
+
std::fill(container_algorithm_internal::c_begin(c),
|
| 694 |
+
container_algorithm_internal::c_end(c), value);
|
| 695 |
+
}
|
| 696 |
+
|
| 697 |
+
// c_fill_n()
|
| 698 |
+
//
|
| 699 |
+
// Container-based version of the <algorithm> `std::fill_n()` function to fill
|
| 700 |
+
// the first N elements in a container with some value.
|
| 701 |
+
template <typename C, typename Size, typename T>
|
| 702 |
+
void c_fill_n(C& c, Size n, const T& value) {
|
| 703 |
+
std::fill_n(container_algorithm_internal::c_begin(c), n, value);
|
| 704 |
+
}
|
| 705 |
+
|
| 706 |
+
// c_generate()
|
| 707 |
+
//
|
| 708 |
+
// Container-based version of the <algorithm> `std::generate()` function to
|
| 709 |
+
// assign a container's elements to the values provided by the given generator.
|
| 710 |
+
template <typename C, typename Generator>
|
| 711 |
+
void c_generate(C& c, Generator&& gen) {
|
| 712 |
+
std::generate(container_algorithm_internal::c_begin(c),
|
| 713 |
+
container_algorithm_internal::c_end(c),
|
| 714 |
+
std::forward<Generator>(gen));
|
| 715 |
+
}
|
| 716 |
+
|
| 717 |
+
// c_generate_n()
|
| 718 |
+
//
|
| 719 |
+
// Container-based version of the <algorithm> `std::generate_n()` function to
|
| 720 |
+
// assign a container's first N elements to the values provided by the given
|
| 721 |
+
// generator.
|
| 722 |
+
template <typename C, typename Size, typename Generator>
|
| 723 |
+
container_algorithm_internal::ContainerIter<C> c_generate_n(C& c, Size n,
|
| 724 |
+
Generator&& gen) {
|
| 725 |
+
return std::generate_n(container_algorithm_internal::c_begin(c), n,
|
| 726 |
+
std::forward<Generator>(gen));
|
| 727 |
+
}
|
| 728 |
+
|
| 729 |
+
// Note: `c_xx()` <algorithm> container versions for `remove()`, `remove_if()`,
|
| 730 |
+
// and `unique()` are omitted, because it's not clear whether or not such
|
| 731 |
+
// functions should call erase on their supplied sequences afterwards. Either
|
| 732 |
+
// behavior would be surprising for a different set of users.
|
| 733 |
+
|
| 734 |
+
// c_remove_copy()
|
| 735 |
+
//
|
| 736 |
+
// Container-based version of the <algorithm> `std::remove_copy()` function to
|
| 737 |
+
// copy a container's elements while removing any elements matching the given
|
| 738 |
+
// `value`.
|
| 739 |
+
template <typename C, typename OutputIterator, typename T>
|
| 740 |
+
OutputIterator c_remove_copy(const C& c, OutputIterator result,
|
| 741 |
+
const T& value) {
|
| 742 |
+
return std::remove_copy(container_algorithm_internal::c_begin(c),
|
| 743 |
+
container_algorithm_internal::c_end(c), result,
|
| 744 |
+
value);
|
| 745 |
+
}
|
| 746 |
+
|
| 747 |
+
// c_remove_copy_if()
|
| 748 |
+
//
|
| 749 |
+
// Container-based version of the <algorithm> `std::remove_copy_if()` function
|
| 750 |
+
// to copy a container's elements while removing any elements matching the given
|
| 751 |
+
// condition.
|
| 752 |
+
template <typename C, typename OutputIterator, typename Pred>
|
| 753 |
+
OutputIterator c_remove_copy_if(const C& c, OutputIterator result,
|
| 754 |
+
Pred&& pred) {
|
| 755 |
+
return std::remove_copy_if(container_algorithm_internal::c_begin(c),
|
| 756 |
+
container_algorithm_internal::c_end(c), result,
|
| 757 |
+
std::forward<Pred>(pred));
|
| 758 |
+
}
|
| 759 |
+
|
| 760 |
+
// c_unique_copy()
|
| 761 |
+
//
|
| 762 |
+
// Container-based version of the <algorithm> `std::unique_copy()` function to
|
| 763 |
+
// copy a container's elements while removing any elements containing duplicate
|
| 764 |
+
// values.
|
| 765 |
+
template <typename C, typename OutputIterator>
|
| 766 |
+
OutputIterator c_unique_copy(const C& c, OutputIterator result) {
|
| 767 |
+
return std::unique_copy(container_algorithm_internal::c_begin(c),
|
| 768 |
+
container_algorithm_internal::c_end(c), result);
|
| 769 |
+
}
|
| 770 |
+
|
| 771 |
+
// Overload of c_unique_copy() for using a predicate evaluation other than
|
| 772 |
+
// `==` for comparing uniqueness of the element values.
|
| 773 |
+
template <typename C, typename OutputIterator, typename BinaryPredicate>
|
| 774 |
+
OutputIterator c_unique_copy(const C& c, OutputIterator result,
|
| 775 |
+
BinaryPredicate&& pred) {
|
| 776 |
+
return std::unique_copy(container_algorithm_internal::c_begin(c),
|
| 777 |
+
container_algorithm_internal::c_end(c), result,
|
| 778 |
+
std::forward<BinaryPredicate>(pred));
|
| 779 |
+
}
|
| 780 |
+
|
| 781 |
+
// c_reverse()
|
| 782 |
+
//
|
| 783 |
+
// Container-based version of the <algorithm> `std::reverse()` function to
|
| 784 |
+
// reverse a container's elements.
|
| 785 |
+
template <typename Sequence>
|
| 786 |
+
void c_reverse(Sequence& sequence) {
|
| 787 |
+
std::reverse(container_algorithm_internal::c_begin(sequence),
|
| 788 |
+
container_algorithm_internal::c_end(sequence));
|
| 789 |
+
}
|
| 790 |
+
|
| 791 |
+
// c_reverse_copy()
|
| 792 |
+
//
|
| 793 |
+
// Container-based version of the <algorithm> `std::reverse()` function to
|
| 794 |
+
// reverse a container's elements and write them to an iterator range.
|
| 795 |
+
template <typename C, typename OutputIterator>
|
| 796 |
+
OutputIterator c_reverse_copy(const C& sequence, OutputIterator result) {
|
| 797 |
+
return std::reverse_copy(container_algorithm_internal::c_begin(sequence),
|
| 798 |
+
container_algorithm_internal::c_end(sequence),
|
| 799 |
+
result);
|
| 800 |
+
}
|
| 801 |
+
|
| 802 |
+
// c_rotate()
|
| 803 |
+
//
|
| 804 |
+
// Container-based version of the <algorithm> `std::rotate()` function to
|
| 805 |
+
// shift a container's elements leftward such that the `middle` element becomes
|
| 806 |
+
// the first element in the container.
|
| 807 |
+
template <typename C,
|
| 808 |
+
typename Iterator = container_algorithm_internal::ContainerIter<C>>
|
| 809 |
+
Iterator c_rotate(C& sequence, Iterator middle) {
|
| 810 |
+
return absl::rotate(container_algorithm_internal::c_begin(sequence), middle,
|
| 811 |
+
container_algorithm_internal::c_end(sequence));
|
| 812 |
+
}
|
| 813 |
+
|
| 814 |
+
// c_rotate_copy()
|
| 815 |
+
//
|
| 816 |
+
// Container-based version of the <algorithm> `std::rotate_copy()` function to
|
| 817 |
+
// shift a container's elements leftward such that the `middle` element becomes
|
| 818 |
+
// the first element in a new iterator range.
|
| 819 |
+
template <typename C, typename OutputIterator>
|
| 820 |
+
OutputIterator c_rotate_copy(
|
| 821 |
+
const C& sequence,
|
| 822 |
+
container_algorithm_internal::ContainerIter<const C> middle,
|
| 823 |
+
OutputIterator result) {
|
| 824 |
+
return std::rotate_copy(container_algorithm_internal::c_begin(sequence),
|
| 825 |
+
middle, container_algorithm_internal::c_end(sequence),
|
| 826 |
+
result);
|
| 827 |
+
}
|
| 828 |
+
|
| 829 |
+
// c_shuffle()
|
| 830 |
+
//
|
| 831 |
+
// Container-based version of the <algorithm> `std::shuffle()` function to
|
| 832 |
+
// randomly shuffle elements within the container using a `gen()` uniform random
|
| 833 |
+
// number generator.
|
| 834 |
+
template <typename RandomAccessContainer, typename UniformRandomBitGenerator>
|
| 835 |
+
void c_shuffle(RandomAccessContainer& c, UniformRandomBitGenerator&& gen) {
|
| 836 |
+
std::shuffle(container_algorithm_internal::c_begin(c),
|
| 837 |
+
container_algorithm_internal::c_end(c),
|
| 838 |
+
std::forward<UniformRandomBitGenerator>(gen));
|
| 839 |
+
}
|
| 840 |
+
|
| 841 |
+
// c_sample()
|
| 842 |
+
//
|
| 843 |
+
// Container-based version of the <algorithm> `std::sample()` function to
|
| 844 |
+
// randomly sample elements from the container without replacement using a
|
| 845 |
+
// `gen()` uniform random number generator and write them to an iterator range.
|
| 846 |
+
template <typename C, typename OutputIterator, typename Distance,
|
| 847 |
+
typename UniformRandomBitGenerator>
|
| 848 |
+
OutputIterator c_sample(const C& c, OutputIterator result, Distance n,
|
| 849 |
+
UniformRandomBitGenerator&& gen) {
|
| 850 |
+
#if defined(__cpp_lib_sample) && __cpp_lib_sample >= 201603L
|
| 851 |
+
return std::sample(container_algorithm_internal::c_begin(c),
|
| 852 |
+
container_algorithm_internal::c_end(c), result, n,
|
| 853 |
+
std::forward<UniformRandomBitGenerator>(gen));
|
| 854 |
+
#else
|
| 855 |
+
// Fall back to a stable selection-sampling implementation.
|
| 856 |
+
auto first = container_algorithm_internal::c_begin(c);
|
| 857 |
+
Distance unsampled_elements = c_distance(c);
|
| 858 |
+
n = (std::min)(n, unsampled_elements);
|
| 859 |
+
for (; n != 0; ++first) {
|
| 860 |
+
Distance r =
|
| 861 |
+
std::uniform_int_distribution<Distance>(0, --unsampled_elements)(gen);
|
| 862 |
+
if (r < n) {
|
| 863 |
+
*result++ = *first;
|
| 864 |
+
--n;
|
| 865 |
+
}
|
| 866 |
+
}
|
| 867 |
+
return result;
|
| 868 |
+
#endif
|
| 869 |
+
}
|
| 870 |
+
|
| 871 |
+
//------------------------------------------------------------------------------
|
| 872 |
+
// <algorithm> Partition functions
|
| 873 |
+
//------------------------------------------------------------------------------
|
| 874 |
+
|
| 875 |
+
// c_is_partitioned()
|
| 876 |
+
//
|
| 877 |
+
// Container-based version of the <algorithm> `std::is_partitioned()` function
|
| 878 |
+
// to test whether all elements in the container for which `pred` returns `true`
|
| 879 |
+
// precede those for which `pred` is `false`.
|
| 880 |
+
template <typename C, typename Pred>
|
| 881 |
+
bool c_is_partitioned(const C& c, Pred&& pred) {
|
| 882 |
+
return std::is_partitioned(container_algorithm_internal::c_begin(c),
|
| 883 |
+
container_algorithm_internal::c_end(c),
|
| 884 |
+
std::forward<Pred>(pred));
|
| 885 |
+
}
|
| 886 |
+
|
| 887 |
+
// c_partition()
|
| 888 |
+
//
|
| 889 |
+
// Container-based version of the <algorithm> `std::partition()` function
|
| 890 |
+
// to rearrange all elements in a container in such a way that all elements for
|
| 891 |
+
// which `pred` returns `true` precede all those for which it returns `false`,
|
| 892 |
+
// returning an iterator to the first element of the second group.
|
| 893 |
+
template <typename C, typename Pred>
|
| 894 |
+
container_algorithm_internal::ContainerIter<C> c_partition(C& c, Pred&& pred) {
|
| 895 |
+
return std::partition(container_algorithm_internal::c_begin(c),
|
| 896 |
+
container_algorithm_internal::c_end(c),
|
| 897 |
+
std::forward<Pred>(pred));
|
| 898 |
+
}
|
| 899 |
+
|
| 900 |
+
// c_stable_partition()
|
| 901 |
+
//
|
| 902 |
+
// Container-based version of the <algorithm> `std::stable_partition()` function
|
| 903 |
+
// to rearrange all elements in a container in such a way that all elements for
|
| 904 |
+
// which `pred` returns `true` precede all those for which it returns `false`,
|
| 905 |
+
// preserving the relative ordering between the two groups. The function returns
|
| 906 |
+
// an iterator to the first element of the second group.
|
| 907 |
+
template <typename C, typename Pred>
|
| 908 |
+
container_algorithm_internal::ContainerIter<C> c_stable_partition(C& c,
|
| 909 |
+
Pred&& pred) {
|
| 910 |
+
return std::stable_partition(container_algorithm_internal::c_begin(c),
|
| 911 |
+
container_algorithm_internal::c_end(c),
|
| 912 |
+
std::forward<Pred>(pred));
|
| 913 |
+
}
|
| 914 |
+
|
| 915 |
+
// c_partition_copy()
|
| 916 |
+
//
|
| 917 |
+
// Container-based version of the <algorithm> `std::partition_copy()` function
|
| 918 |
+
// to partition a container's elements and return them into two iterators: one
|
| 919 |
+
// for which `pred` returns `true`, and one for which `pred` returns `false.`
|
| 920 |
+
|
| 921 |
+
template <typename C, typename OutputIterator1, typename OutputIterator2,
|
| 922 |
+
typename Pred>
|
| 923 |
+
std::pair<OutputIterator1, OutputIterator2> c_partition_copy(
|
| 924 |
+
const C& c, OutputIterator1 out_true, OutputIterator2 out_false,
|
| 925 |
+
Pred&& pred) {
|
| 926 |
+
return std::partition_copy(container_algorithm_internal::c_begin(c),
|
| 927 |
+
container_algorithm_internal::c_end(c), out_true,
|
| 928 |
+
out_false, std::forward<Pred>(pred));
|
| 929 |
+
}
|
| 930 |
+
|
| 931 |
+
// c_partition_point()
|
| 932 |
+
//
|
| 933 |
+
// Container-based version of the <algorithm> `std::partition_point()` function
|
| 934 |
+
// to return the first element of an already partitioned container for which
|
| 935 |
+
// the given `pred` is not `true`.
|
| 936 |
+
template <typename C, typename Pred>
|
| 937 |
+
container_algorithm_internal::ContainerIter<C> c_partition_point(C& c,
|
| 938 |
+
Pred&& pred) {
|
| 939 |
+
return std::partition_point(container_algorithm_internal::c_begin(c),
|
| 940 |
+
container_algorithm_internal::c_end(c),
|
| 941 |
+
std::forward<Pred>(pred));
|
| 942 |
+
}
|
| 943 |
+
|
| 944 |
+
//------------------------------------------------------------------------------
|
| 945 |
+
// <algorithm> Sorting functions
|
| 946 |
+
//------------------------------------------------------------------------------
|
| 947 |
+
|
| 948 |
+
// c_sort()
|
| 949 |
+
//
|
| 950 |
+
// Container-based version of the <algorithm> `std::sort()` function
|
| 951 |
+
// to sort elements in ascending order of their values.
|
| 952 |
+
template <typename C>
|
| 953 |
+
void c_sort(C& c) {
|
| 954 |
+
std::sort(container_algorithm_internal::c_begin(c),
|
| 955 |
+
container_algorithm_internal::c_end(c));
|
| 956 |
+
}
|
| 957 |
+
|
| 958 |
+
// Overload of c_sort() for performing a `comp` comparison other than the
|
| 959 |
+
// default `operator<`.
|
| 960 |
+
template <typename C, typename LessThan>
|
| 961 |
+
void c_sort(C& c, LessThan&& comp) {
|
| 962 |
+
std::sort(container_algorithm_internal::c_begin(c),
|
| 963 |
+
container_algorithm_internal::c_end(c),
|
| 964 |
+
std::forward<LessThan>(comp));
|
| 965 |
+
}
|
| 966 |
+
|
| 967 |
+
// c_stable_sort()
|
| 968 |
+
//
|
| 969 |
+
// Container-based version of the <algorithm> `std::stable_sort()` function
|
| 970 |
+
// to sort elements in ascending order of their values, preserving the order
|
| 971 |
+
// of equivalents.
|
| 972 |
+
template <typename C>
|
| 973 |
+
void c_stable_sort(C& c) {
|
| 974 |
+
std::stable_sort(container_algorithm_internal::c_begin(c),
|
| 975 |
+
container_algorithm_internal::c_end(c));
|
| 976 |
+
}
|
| 977 |
+
|
| 978 |
+
// Overload of c_stable_sort() for performing a `comp` comparison other than the
|
| 979 |
+
// default `operator<`.
|
| 980 |
+
template <typename C, typename LessThan>
|
| 981 |
+
void c_stable_sort(C& c, LessThan&& comp) {
|
| 982 |
+
std::stable_sort(container_algorithm_internal::c_begin(c),
|
| 983 |
+
container_algorithm_internal::c_end(c),
|
| 984 |
+
std::forward<LessThan>(comp));
|
| 985 |
+
}
|
| 986 |
+
|
| 987 |
+
// c_is_sorted()
|
| 988 |
+
//
|
| 989 |
+
// Container-based version of the <algorithm> `std::is_sorted()` function
|
| 990 |
+
// to evaluate whether the given container is sorted in ascending order.
|
| 991 |
+
template <typename C>
|
| 992 |
+
bool c_is_sorted(const C& c) {
|
| 993 |
+
return std::is_sorted(container_algorithm_internal::c_begin(c),
|
| 994 |
+
container_algorithm_internal::c_end(c));
|
| 995 |
+
}
|
| 996 |
+
|
| 997 |
+
// c_is_sorted() overload for performing a `comp` comparison other than the
|
| 998 |
+
// default `operator<`.
|
| 999 |
+
template <typename C, typename LessThan>
|
| 1000 |
+
bool c_is_sorted(const C& c, LessThan&& comp) {
|
| 1001 |
+
return std::is_sorted(container_algorithm_internal::c_begin(c),
|
| 1002 |
+
container_algorithm_internal::c_end(c),
|
| 1003 |
+
std::forward<LessThan>(comp));
|
| 1004 |
+
}
|
| 1005 |
+
|
| 1006 |
+
// c_partial_sort()
|
| 1007 |
+
//
|
| 1008 |
+
// Container-based version of the <algorithm> `std::partial_sort()` function
|
| 1009 |
+
// to rearrange elements within a container such that elements before `middle`
|
| 1010 |
+
// are sorted in ascending order.
|
| 1011 |
+
template <typename RandomAccessContainer>
|
| 1012 |
+
void c_partial_sort(
|
| 1013 |
+
RandomAccessContainer& sequence,
|
| 1014 |
+
container_algorithm_internal::ContainerIter<RandomAccessContainer> middle) {
|
| 1015 |
+
std::partial_sort(container_algorithm_internal::c_begin(sequence), middle,
|
| 1016 |
+
container_algorithm_internal::c_end(sequence));
|
| 1017 |
+
}
|
| 1018 |
+
|
| 1019 |
+
// Overload of c_partial_sort() for performing a `comp` comparison other than
|
| 1020 |
+
// the default `operator<`.
|
| 1021 |
+
template <typename RandomAccessContainer, typename LessThan>
|
| 1022 |
+
void c_partial_sort(
|
| 1023 |
+
RandomAccessContainer& sequence,
|
| 1024 |
+
container_algorithm_internal::ContainerIter<RandomAccessContainer> middle,
|
| 1025 |
+
LessThan&& comp) {
|
| 1026 |
+
std::partial_sort(container_algorithm_internal::c_begin(sequence), middle,
|
| 1027 |
+
container_algorithm_internal::c_end(sequence),
|
| 1028 |
+
std::forward<LessThan>(comp));
|
| 1029 |
+
}
|
| 1030 |
+
|
| 1031 |
+
// c_partial_sort_copy()
|
| 1032 |
+
//
|
| 1033 |
+
// Container-based version of the <algorithm> `std::partial_sort_copy()`
|
| 1034 |
+
// function to sort the elements in the given range `result` within the larger
|
| 1035 |
+
// `sequence` in ascending order (and using `result` as the output parameter).
|
| 1036 |
+
// At most min(result.last - result.first, sequence.last - sequence.first)
|
| 1037 |
+
// elements from the sequence will be stored in the result.
|
| 1038 |
+
template <typename C, typename RandomAccessContainer>
|
| 1039 |
+
container_algorithm_internal::ContainerIter<RandomAccessContainer>
|
| 1040 |
+
c_partial_sort_copy(const C& sequence, RandomAccessContainer& result) {
|
| 1041 |
+
return std::partial_sort_copy(container_algorithm_internal::c_begin(sequence),
|
| 1042 |
+
container_algorithm_internal::c_end(sequence),
|
| 1043 |
+
container_algorithm_internal::c_begin(result),
|
| 1044 |
+
container_algorithm_internal::c_end(result));
|
| 1045 |
+
}
|
| 1046 |
+
|
| 1047 |
+
// Overload of c_partial_sort_copy() for performing a `comp` comparison other
|
| 1048 |
+
// than the default `operator<`.
|
| 1049 |
+
template <typename C, typename RandomAccessContainer, typename LessThan>
|
| 1050 |
+
container_algorithm_internal::ContainerIter<RandomAccessContainer>
|
| 1051 |
+
c_partial_sort_copy(const C& sequence, RandomAccessContainer& result,
|
| 1052 |
+
LessThan&& comp) {
|
| 1053 |
+
return std::partial_sort_copy(container_algorithm_internal::c_begin(sequence),
|
| 1054 |
+
container_algorithm_internal::c_end(sequence),
|
| 1055 |
+
container_algorithm_internal::c_begin(result),
|
| 1056 |
+
container_algorithm_internal::c_end(result),
|
| 1057 |
+
std::forward<LessThan>(comp));
|
| 1058 |
+
}
|
| 1059 |
+
|
| 1060 |
+
// c_is_sorted_until()
|
| 1061 |
+
//
|
| 1062 |
+
// Container-based version of the <algorithm> `std::is_sorted_until()` function
|
| 1063 |
+
// to return the first element within a container that is not sorted in
|
| 1064 |
+
// ascending order as an iterator.
|
| 1065 |
+
template <typename C>
|
| 1066 |
+
container_algorithm_internal::ContainerIter<C> c_is_sorted_until(C& c) {
|
| 1067 |
+
return std::is_sorted_until(container_algorithm_internal::c_begin(c),
|
| 1068 |
+
container_algorithm_internal::c_end(c));
|
| 1069 |
+
}
|
| 1070 |
+
|
| 1071 |
+
// Overload of c_is_sorted_until() for performing a `comp` comparison other than
|
| 1072 |
+
// the default `operator<`.
|
| 1073 |
+
template <typename C, typename LessThan>
|
| 1074 |
+
container_algorithm_internal::ContainerIter<C> c_is_sorted_until(
|
| 1075 |
+
C& c, LessThan&& comp) {
|
| 1076 |
+
return std::is_sorted_until(container_algorithm_internal::c_begin(c),
|
| 1077 |
+
container_algorithm_internal::c_end(c),
|
| 1078 |
+
std::forward<LessThan>(comp));
|
| 1079 |
+
}
|
| 1080 |
+
|
| 1081 |
+
// c_nth_element()
|
| 1082 |
+
//
|
| 1083 |
+
// Container-based version of the <algorithm> `std::nth_element()` function
|
| 1084 |
+
// to rearrange the elements within a container such that the `nth` element
|
| 1085 |
+
// would be in that position in an ordered sequence; other elements may be in
|
| 1086 |
+
// any order, except that all preceding `nth` will be less than that element,
|
| 1087 |
+
// and all following `nth` will be greater than that element.
|
| 1088 |
+
template <typename RandomAccessContainer>
|
| 1089 |
+
void c_nth_element(
|
| 1090 |
+
RandomAccessContainer& sequence,
|
| 1091 |
+
container_algorithm_internal::ContainerIter<RandomAccessContainer> nth) {
|
| 1092 |
+
std::nth_element(container_algorithm_internal::c_begin(sequence), nth,
|
| 1093 |
+
container_algorithm_internal::c_end(sequence));
|
| 1094 |
+
}
|
| 1095 |
+
|
| 1096 |
+
// Overload of c_nth_element() for performing a `comp` comparison other than
|
| 1097 |
+
// the default `operator<`.
|
| 1098 |
+
template <typename RandomAccessContainer, typename LessThan>
|
| 1099 |
+
void c_nth_element(
|
| 1100 |
+
RandomAccessContainer& sequence,
|
| 1101 |
+
container_algorithm_internal::ContainerIter<RandomAccessContainer> nth,
|
| 1102 |
+
LessThan&& comp) {
|
| 1103 |
+
std::nth_element(container_algorithm_internal::c_begin(sequence), nth,
|
| 1104 |
+
container_algorithm_internal::c_end(sequence),
|
| 1105 |
+
std::forward<LessThan>(comp));
|
| 1106 |
+
}
|
| 1107 |
+
|
| 1108 |
+
//------------------------------------------------------------------------------
|
| 1109 |
+
// <algorithm> Binary Search
|
| 1110 |
+
//------------------------------------------------------------------------------
|
| 1111 |
+
|
| 1112 |
+
// c_lower_bound()
|
| 1113 |
+
//
|
| 1114 |
+
// Container-based version of the <algorithm> `std::lower_bound()` function
|
| 1115 |
+
// to return an iterator pointing to the first element in a sorted container
|
| 1116 |
+
// which does not compare less than `value`.
|
| 1117 |
+
template <typename Sequence, typename T>
|
| 1118 |
+
container_algorithm_internal::ContainerIter<Sequence> c_lower_bound(
|
| 1119 |
+
Sequence& sequence, const T& value) {
|
| 1120 |
+
return std::lower_bound(container_algorithm_internal::c_begin(sequence),
|
| 1121 |
+
container_algorithm_internal::c_end(sequence), value);
|
| 1122 |
+
}
|
| 1123 |
+
|
| 1124 |
+
// Overload of c_lower_bound() for performing a `comp` comparison other than
|
| 1125 |
+
// the default `operator<`.
|
| 1126 |
+
template <typename Sequence, typename T, typename LessThan>
|
| 1127 |
+
container_algorithm_internal::ContainerIter<Sequence> c_lower_bound(
|
| 1128 |
+
Sequence& sequence, const T& value, LessThan&& comp) {
|
| 1129 |
+
return std::lower_bound(container_algorithm_internal::c_begin(sequence),
|
| 1130 |
+
container_algorithm_internal::c_end(sequence), value,
|
| 1131 |
+
std::forward<LessThan>(comp));
|
| 1132 |
+
}
|
| 1133 |
+
|
| 1134 |
+
// c_upper_bound()
|
| 1135 |
+
//
|
| 1136 |
+
// Container-based version of the <algorithm> `std::upper_bound()` function
|
| 1137 |
+
// to return an iterator pointing to the first element in a sorted container
|
| 1138 |
+
// which is greater than `value`.
|
| 1139 |
+
template <typename Sequence, typename T>
|
| 1140 |
+
container_algorithm_internal::ContainerIter<Sequence> c_upper_bound(
|
| 1141 |
+
Sequence& sequence, const T& value) {
|
| 1142 |
+
return std::upper_bound(container_algorithm_internal::c_begin(sequence),
|
| 1143 |
+
container_algorithm_internal::c_end(sequence), value);
|
| 1144 |
+
}
|
| 1145 |
+
|
| 1146 |
+
// Overload of c_upper_bound() for performing a `comp` comparison other than
|
| 1147 |
+
// the default `operator<`.
|
| 1148 |
+
template <typename Sequence, typename T, typename LessThan>
|
| 1149 |
+
container_algorithm_internal::ContainerIter<Sequence> c_upper_bound(
|
| 1150 |
+
Sequence& sequence, const T& value, LessThan&& comp) {
|
| 1151 |
+
return std::upper_bound(container_algorithm_internal::c_begin(sequence),
|
| 1152 |
+
container_algorithm_internal::c_end(sequence), value,
|
| 1153 |
+
std::forward<LessThan>(comp));
|
| 1154 |
+
}
|
| 1155 |
+
|
| 1156 |
+
// c_equal_range()
|
| 1157 |
+
//
|
| 1158 |
+
// Container-based version of the <algorithm> `std::equal_range()` function
|
| 1159 |
+
// to return an iterator pair pointing to the first and last elements in a
|
| 1160 |
+
// sorted container which compare equal to `value`.
|
| 1161 |
+
template <typename Sequence, typename T>
|
| 1162 |
+
container_algorithm_internal::ContainerIterPairType<Sequence, Sequence>
|
| 1163 |
+
c_equal_range(Sequence& sequence, const T& value) {
|
| 1164 |
+
return std::equal_range(container_algorithm_internal::c_begin(sequence),
|
| 1165 |
+
container_algorithm_internal::c_end(sequence), value);
|
| 1166 |
+
}
|
| 1167 |
+
|
| 1168 |
+
// Overload of c_equal_range() for performing a `comp` comparison other than
|
| 1169 |
+
// the default `operator<`.
|
| 1170 |
+
template <typename Sequence, typename T, typename LessThan>
|
| 1171 |
+
container_algorithm_internal::ContainerIterPairType<Sequence, Sequence>
|
| 1172 |
+
c_equal_range(Sequence& sequence, const T& value, LessThan&& comp) {
|
| 1173 |
+
return std::equal_range(container_algorithm_internal::c_begin(sequence),
|
| 1174 |
+
container_algorithm_internal::c_end(sequence), value,
|
| 1175 |
+
std::forward<LessThan>(comp));
|
| 1176 |
+
}
|
| 1177 |
+
|
| 1178 |
+
// c_binary_search()
|
| 1179 |
+
//
|
| 1180 |
+
// Container-based version of the <algorithm> `std::binary_search()` function
|
| 1181 |
+
// to test if any element in the sorted container contains a value equivalent to
|
| 1182 |
+
// 'value'.
|
| 1183 |
+
template <typename Sequence, typename T>
|
| 1184 |
+
bool c_binary_search(const Sequence& sequence, const T& value) {
|
| 1185 |
+
return std::binary_search(container_algorithm_internal::c_begin(sequence),
|
| 1186 |
+
container_algorithm_internal::c_end(sequence),
|
| 1187 |
+
value);
|
| 1188 |
+
}
|
| 1189 |
+
|
| 1190 |
+
// Overload of c_binary_search() for performing a `comp` comparison other than
|
| 1191 |
+
// the default `operator<`.
|
| 1192 |
+
template <typename Sequence, typename T, typename LessThan>
|
| 1193 |
+
bool c_binary_search(const Sequence& sequence, const T& value,
|
| 1194 |
+
LessThan&& comp) {
|
| 1195 |
+
return std::binary_search(container_algorithm_internal::c_begin(sequence),
|
| 1196 |
+
container_algorithm_internal::c_end(sequence),
|
| 1197 |
+
value, std::forward<LessThan>(comp));
|
| 1198 |
+
}
|
| 1199 |
+
|
| 1200 |
+
//------------------------------------------------------------------------------
|
| 1201 |
+
// <algorithm> Merge functions
|
| 1202 |
+
//------------------------------------------------------------------------------
|
| 1203 |
+
|
| 1204 |
+
// c_merge()
|
| 1205 |
+
//
|
| 1206 |
+
// Container-based version of the <algorithm> `std::merge()` function
|
| 1207 |
+
// to merge two sorted containers into a single sorted iterator.
|
| 1208 |
+
template <typename C1, typename C2, typename OutputIterator>
|
| 1209 |
+
OutputIterator c_merge(const C1& c1, const C2& c2, OutputIterator result) {
|
| 1210 |
+
return std::merge(container_algorithm_internal::c_begin(c1),
|
| 1211 |
+
container_algorithm_internal::c_end(c1),
|
| 1212 |
+
container_algorithm_internal::c_begin(c2),
|
| 1213 |
+
container_algorithm_internal::c_end(c2), result);
|
| 1214 |
+
}
|
| 1215 |
+
|
| 1216 |
+
// Overload of c_merge() for performing a `comp` comparison other than
|
| 1217 |
+
// the default `operator<`.
|
| 1218 |
+
template <typename C1, typename C2, typename OutputIterator, typename LessThan>
|
| 1219 |
+
OutputIterator c_merge(const C1& c1, const C2& c2, OutputIterator result,
|
| 1220 |
+
LessThan&& comp) {
|
| 1221 |
+
return std::merge(container_algorithm_internal::c_begin(c1),
|
| 1222 |
+
container_algorithm_internal::c_end(c1),
|
| 1223 |
+
container_algorithm_internal::c_begin(c2),
|
| 1224 |
+
container_algorithm_internal::c_end(c2), result,
|
| 1225 |
+
std::forward<LessThan>(comp));
|
| 1226 |
+
}
|
| 1227 |
+
|
| 1228 |
+
// c_inplace_merge()
|
| 1229 |
+
//
|
| 1230 |
+
// Container-based version of the <algorithm> `std::inplace_merge()` function
|
| 1231 |
+
// to merge a supplied iterator `middle` into a container.
|
| 1232 |
+
template <typename C>
|
| 1233 |
+
void c_inplace_merge(C& c,
|
| 1234 |
+
container_algorithm_internal::ContainerIter<C> middle) {
|
| 1235 |
+
std::inplace_merge(container_algorithm_internal::c_begin(c), middle,
|
| 1236 |
+
container_algorithm_internal::c_end(c));
|
| 1237 |
+
}
|
| 1238 |
+
|
| 1239 |
+
// Overload of c_inplace_merge() for performing a merge using a `comp` other
|
| 1240 |
+
// than `operator<`.
|
| 1241 |
+
template <typename C, typename LessThan>
|
| 1242 |
+
void c_inplace_merge(C& c,
|
| 1243 |
+
container_algorithm_internal::ContainerIter<C> middle,
|
| 1244 |
+
LessThan&& comp) {
|
| 1245 |
+
std::inplace_merge(container_algorithm_internal::c_begin(c), middle,
|
| 1246 |
+
container_algorithm_internal::c_end(c),
|
| 1247 |
+
std::forward<LessThan>(comp));
|
| 1248 |
+
}
|
| 1249 |
+
|
| 1250 |
+
// c_includes()
|
| 1251 |
+
//
|
| 1252 |
+
// Container-based version of the <algorithm> `std::includes()` function
|
| 1253 |
+
// to test whether a sorted container `c1` entirely contains another sorted
|
| 1254 |
+
// container `c2`.
|
| 1255 |
+
template <typename C1, typename C2>
|
| 1256 |
+
bool c_includes(const C1& c1, const C2& c2) {
|
| 1257 |
+
return std::includes(container_algorithm_internal::c_begin(c1),
|
| 1258 |
+
container_algorithm_internal::c_end(c1),
|
| 1259 |
+
container_algorithm_internal::c_begin(c2),
|
| 1260 |
+
container_algorithm_internal::c_end(c2));
|
| 1261 |
+
}
|
| 1262 |
+
|
| 1263 |
+
// Overload of c_includes() for performing a merge using a `comp` other than
|
| 1264 |
+
// `operator<`.
|
| 1265 |
+
template <typename C1, typename C2, typename LessThan>
|
| 1266 |
+
bool c_includes(const C1& c1, const C2& c2, LessThan&& comp) {
|
| 1267 |
+
return std::includes(container_algorithm_internal::c_begin(c1),
|
| 1268 |
+
container_algorithm_internal::c_end(c1),
|
| 1269 |
+
container_algorithm_internal::c_begin(c2),
|
| 1270 |
+
container_algorithm_internal::c_end(c2),
|
| 1271 |
+
std::forward<LessThan>(comp));
|
| 1272 |
+
}
|
| 1273 |
+
|
| 1274 |
+
// c_set_union()
|
| 1275 |
+
//
|
| 1276 |
+
// Container-based version of the <algorithm> `std::set_union()` function
|
| 1277 |
+
// to return an iterator containing the union of two containers; duplicate
|
| 1278 |
+
// values are not copied into the output.
|
| 1279 |
+
template <typename C1, typename C2, typename OutputIterator,
|
| 1280 |
+
typename = typename std::enable_if<
|
| 1281 |
+
!container_algorithm_internal::IsUnorderedContainer<C1>::value,
|
| 1282 |
+
void>::type,
|
| 1283 |
+
typename = typename std::enable_if<
|
| 1284 |
+
!container_algorithm_internal::IsUnorderedContainer<C2>::value,
|
| 1285 |
+
void>::type>
|
| 1286 |
+
OutputIterator c_set_union(const C1& c1, const C2& c2, OutputIterator output) {
|
| 1287 |
+
return std::set_union(container_algorithm_internal::c_begin(c1),
|
| 1288 |
+
container_algorithm_internal::c_end(c1),
|
| 1289 |
+
container_algorithm_internal::c_begin(c2),
|
| 1290 |
+
container_algorithm_internal::c_end(c2), output);
|
| 1291 |
+
}
|
| 1292 |
+
|
| 1293 |
+
// Overload of c_set_union() for performing a merge using a `comp` other than
|
| 1294 |
+
// `operator<`.
|
| 1295 |
+
template <typename C1, typename C2, typename OutputIterator, typename LessThan,
|
| 1296 |
+
typename = typename std::enable_if<
|
| 1297 |
+
!container_algorithm_internal::IsUnorderedContainer<C1>::value,
|
| 1298 |
+
void>::type,
|
| 1299 |
+
typename = typename std::enable_if<
|
| 1300 |
+
!container_algorithm_internal::IsUnorderedContainer<C2>::value,
|
| 1301 |
+
void>::type>
|
| 1302 |
+
OutputIterator c_set_union(const C1& c1, const C2& c2, OutputIterator output,
|
| 1303 |
+
LessThan&& comp) {
|
| 1304 |
+
return std::set_union(container_algorithm_internal::c_begin(c1),
|
| 1305 |
+
container_algorithm_internal::c_end(c1),
|
| 1306 |
+
container_algorithm_internal::c_begin(c2),
|
| 1307 |
+
container_algorithm_internal::c_end(c2), output,
|
| 1308 |
+
std::forward<LessThan>(comp));
|
| 1309 |
+
}
|
| 1310 |
+
|
| 1311 |
+
// c_set_intersection()
|
| 1312 |
+
//
|
| 1313 |
+
// Container-based version of the <algorithm> `std::set_intersection()` function
|
| 1314 |
+
// to return an iterator containing the intersection of two sorted containers.
|
| 1315 |
+
template <typename C1, typename C2, typename OutputIterator,
|
| 1316 |
+
typename = typename std::enable_if<
|
| 1317 |
+
!container_algorithm_internal::IsUnorderedContainer<C1>::value,
|
| 1318 |
+
void>::type,
|
| 1319 |
+
typename = typename std::enable_if<
|
| 1320 |
+
!container_algorithm_internal::IsUnorderedContainer<C2>::value,
|
| 1321 |
+
void>::type>
|
| 1322 |
+
OutputIterator c_set_intersection(const C1& c1, const C2& c2,
|
| 1323 |
+
OutputIterator output) {
|
| 1324 |
+
// In debug builds, ensure that both containers are sorted with respect to the
|
| 1325 |
+
// default comparator. std::set_intersection requires the containers be sorted
|
| 1326 |
+
// using operator<.
|
| 1327 |
+
assert(absl::c_is_sorted(c1));
|
| 1328 |
+
assert(absl::c_is_sorted(c2));
|
| 1329 |
+
return std::set_intersection(container_algorithm_internal::c_begin(c1),
|
| 1330 |
+
container_algorithm_internal::c_end(c1),
|
| 1331 |
+
container_algorithm_internal::c_begin(c2),
|
| 1332 |
+
container_algorithm_internal::c_end(c2), output);
|
| 1333 |
+
}
|
| 1334 |
+
|
| 1335 |
+
// Overload of c_set_intersection() for performing a merge using a `comp` other
|
| 1336 |
+
// than `operator<`.
|
| 1337 |
+
template <typename C1, typename C2, typename OutputIterator, typename LessThan,
|
| 1338 |
+
typename = typename std::enable_if<
|
| 1339 |
+
!container_algorithm_internal::IsUnorderedContainer<C1>::value,
|
| 1340 |
+
void>::type,
|
| 1341 |
+
typename = typename std::enable_if<
|
| 1342 |
+
!container_algorithm_internal::IsUnorderedContainer<C2>::value,
|
| 1343 |
+
void>::type>
|
| 1344 |
+
OutputIterator c_set_intersection(const C1& c1, const C2& c2,
|
| 1345 |
+
OutputIterator output, LessThan&& comp) {
|
| 1346 |
+
// In debug builds, ensure that both containers are sorted with respect to the
|
| 1347 |
+
// default comparator. std::set_intersection requires the containers be sorted
|
| 1348 |
+
// using the same comparator.
|
| 1349 |
+
assert(absl::c_is_sorted(c1, comp));
|
| 1350 |
+
assert(absl::c_is_sorted(c2, comp));
|
| 1351 |
+
return std::set_intersection(container_algorithm_internal::c_begin(c1),
|
| 1352 |
+
container_algorithm_internal::c_end(c1),
|
| 1353 |
+
container_algorithm_internal::c_begin(c2),
|
| 1354 |
+
container_algorithm_internal::c_end(c2), output,
|
| 1355 |
+
std::forward<LessThan>(comp));
|
| 1356 |
+
}
|
| 1357 |
+
|
| 1358 |
+
// c_set_difference()
|
| 1359 |
+
//
|
| 1360 |
+
// Container-based version of the <algorithm> `std::set_difference()` function
|
| 1361 |
+
// to return an iterator containing elements present in the first container but
|
| 1362 |
+
// not in the second.
|
| 1363 |
+
template <typename C1, typename C2, typename OutputIterator,
|
| 1364 |
+
typename = typename std::enable_if<
|
| 1365 |
+
!container_algorithm_internal::IsUnorderedContainer<C1>::value,
|
| 1366 |
+
void>::type,
|
| 1367 |
+
typename = typename std::enable_if<
|
| 1368 |
+
!container_algorithm_internal::IsUnorderedContainer<C2>::value,
|
| 1369 |
+
void>::type>
|
| 1370 |
+
OutputIterator c_set_difference(const C1& c1, const C2& c2,
|
| 1371 |
+
OutputIterator output) {
|
| 1372 |
+
return std::set_difference(container_algorithm_internal::c_begin(c1),
|
| 1373 |
+
container_algorithm_internal::c_end(c1),
|
| 1374 |
+
container_algorithm_internal::c_begin(c2),
|
| 1375 |
+
container_algorithm_internal::c_end(c2), output);
|
| 1376 |
+
}
|
| 1377 |
+
|
| 1378 |
+
// Overload of c_set_difference() for performing a merge using a `comp` other
|
| 1379 |
+
// than `operator<`.
|
| 1380 |
+
template <typename C1, typename C2, typename OutputIterator, typename LessThan,
|
| 1381 |
+
typename = typename std::enable_if<
|
| 1382 |
+
!container_algorithm_internal::IsUnorderedContainer<C1>::value,
|
| 1383 |
+
void>::type,
|
| 1384 |
+
typename = typename std::enable_if<
|
| 1385 |
+
!container_algorithm_internal::IsUnorderedContainer<C2>::value,
|
| 1386 |
+
void>::type>
|
| 1387 |
+
OutputIterator c_set_difference(const C1& c1, const C2& c2,
|
| 1388 |
+
OutputIterator output, LessThan&& comp) {
|
| 1389 |
+
return std::set_difference(container_algorithm_internal::c_begin(c1),
|
| 1390 |
+
container_algorithm_internal::c_end(c1),
|
| 1391 |
+
container_algorithm_internal::c_begin(c2),
|
| 1392 |
+
container_algorithm_internal::c_end(c2), output,
|
| 1393 |
+
std::forward<LessThan>(comp));
|
| 1394 |
+
}
|
| 1395 |
+
|
| 1396 |
+
// c_set_symmetric_difference()
|
| 1397 |
+
//
|
| 1398 |
+
// Container-based version of the <algorithm> `std::set_symmetric_difference()`
|
| 1399 |
+
// function to return an iterator containing elements present in either one
|
| 1400 |
+
// container or the other, but not both.
|
| 1401 |
+
template <typename C1, typename C2, typename OutputIterator,
|
| 1402 |
+
typename = typename std::enable_if<
|
| 1403 |
+
!container_algorithm_internal::IsUnorderedContainer<C1>::value,
|
| 1404 |
+
void>::type,
|
| 1405 |
+
typename = typename std::enable_if<
|
| 1406 |
+
!container_algorithm_internal::IsUnorderedContainer<C2>::value,
|
| 1407 |
+
void>::type>
|
| 1408 |
+
OutputIterator c_set_symmetric_difference(const C1& c1, const C2& c2,
|
| 1409 |
+
OutputIterator output) {
|
| 1410 |
+
return std::set_symmetric_difference(
|
| 1411 |
+
container_algorithm_internal::c_begin(c1),
|
| 1412 |
+
container_algorithm_internal::c_end(c1),
|
| 1413 |
+
container_algorithm_internal::c_begin(c2),
|
| 1414 |
+
container_algorithm_internal::c_end(c2), output);
|
| 1415 |
+
}
|
| 1416 |
+
|
| 1417 |
+
// Overload of c_set_symmetric_difference() for performing a merge using a
|
| 1418 |
+
// `comp` other than `operator<`.
|
| 1419 |
+
template <typename C1, typename C2, typename OutputIterator, typename LessThan,
|
| 1420 |
+
typename = typename std::enable_if<
|
| 1421 |
+
!container_algorithm_internal::IsUnorderedContainer<C1>::value,
|
| 1422 |
+
void>::type,
|
| 1423 |
+
typename = typename std::enable_if<
|
| 1424 |
+
!container_algorithm_internal::IsUnorderedContainer<C2>::value,
|
| 1425 |
+
void>::type>
|
| 1426 |
+
OutputIterator c_set_symmetric_difference(const C1& c1, const C2& c2,
|
| 1427 |
+
OutputIterator output,
|
| 1428 |
+
LessThan&& comp) {
|
| 1429 |
+
return std::set_symmetric_difference(
|
| 1430 |
+
container_algorithm_internal::c_begin(c1),
|
| 1431 |
+
container_algorithm_internal::c_end(c1),
|
| 1432 |
+
container_algorithm_internal::c_begin(c2),
|
| 1433 |
+
container_algorithm_internal::c_end(c2), output,
|
| 1434 |
+
std::forward<LessThan>(comp));
|
| 1435 |
+
}
|
| 1436 |
+
|
| 1437 |
+
//------------------------------------------------------------------------------
|
| 1438 |
+
// <algorithm> Heap functions
|
| 1439 |
+
//------------------------------------------------------------------------------
|
| 1440 |
+
|
| 1441 |
+
// c_push_heap()
|
| 1442 |
+
//
|
| 1443 |
+
// Container-based version of the <algorithm> `std::push_heap()` function
|
| 1444 |
+
// to push a value onto a container heap.
|
| 1445 |
+
template <typename RandomAccessContainer>
|
| 1446 |
+
void c_push_heap(RandomAccessContainer& sequence) {
|
| 1447 |
+
std::push_heap(container_algorithm_internal::c_begin(sequence),
|
| 1448 |
+
container_algorithm_internal::c_end(sequence));
|
| 1449 |
+
}
|
| 1450 |
+
|
| 1451 |
+
// Overload of c_push_heap() for performing a push operation on a heap using a
|
| 1452 |
+
// `comp` other than `operator<`.
|
| 1453 |
+
template <typename RandomAccessContainer, typename LessThan>
|
| 1454 |
+
void c_push_heap(RandomAccessContainer& sequence, LessThan&& comp) {
|
| 1455 |
+
std::push_heap(container_algorithm_internal::c_begin(sequence),
|
| 1456 |
+
container_algorithm_internal::c_end(sequence),
|
| 1457 |
+
std::forward<LessThan>(comp));
|
| 1458 |
+
}
|
| 1459 |
+
|
| 1460 |
+
// c_pop_heap()
|
| 1461 |
+
//
|
| 1462 |
+
// Container-based version of the <algorithm> `std::pop_heap()` function
|
| 1463 |
+
// to pop a value from a heap container.
|
| 1464 |
+
template <typename RandomAccessContainer>
|
| 1465 |
+
void c_pop_heap(RandomAccessContainer& sequence) {
|
| 1466 |
+
std::pop_heap(container_algorithm_internal::c_begin(sequence),
|
| 1467 |
+
container_algorithm_internal::c_end(sequence));
|
| 1468 |
+
}
|
| 1469 |
+
|
| 1470 |
+
// Overload of c_pop_heap() for performing a pop operation on a heap using a
|
| 1471 |
+
// `comp` other than `operator<`.
|
| 1472 |
+
template <typename RandomAccessContainer, typename LessThan>
|
| 1473 |
+
void c_pop_heap(RandomAccessContainer& sequence, LessThan&& comp) {
|
| 1474 |
+
std::pop_heap(container_algorithm_internal::c_begin(sequence),
|
| 1475 |
+
container_algorithm_internal::c_end(sequence),
|
| 1476 |
+
std::forward<LessThan>(comp));
|
| 1477 |
+
}
|
| 1478 |
+
|
| 1479 |
+
// c_make_heap()
|
| 1480 |
+
//
|
| 1481 |
+
// Container-based version of the <algorithm> `std::make_heap()` function
|
| 1482 |
+
// to make a container a heap.
|
| 1483 |
+
template <typename RandomAccessContainer>
|
| 1484 |
+
void c_make_heap(RandomAccessContainer& sequence) {
|
| 1485 |
+
std::make_heap(container_algorithm_internal::c_begin(sequence),
|
| 1486 |
+
container_algorithm_internal::c_end(sequence));
|
| 1487 |
+
}
|
| 1488 |
+
|
| 1489 |
+
// Overload of c_make_heap() for performing heap comparisons using a
|
| 1490 |
+
// `comp` other than `operator<`
|
| 1491 |
+
template <typename RandomAccessContainer, typename LessThan>
|
| 1492 |
+
void c_make_heap(RandomAccessContainer& sequence, LessThan&& comp) {
|
| 1493 |
+
std::make_heap(container_algorithm_internal::c_begin(sequence),
|
| 1494 |
+
container_algorithm_internal::c_end(sequence),
|
| 1495 |
+
std::forward<LessThan>(comp));
|
| 1496 |
+
}
|
| 1497 |
+
|
| 1498 |
+
// c_sort_heap()
|
| 1499 |
+
//
|
| 1500 |
+
// Container-based version of the <algorithm> `std::sort_heap()` function
|
| 1501 |
+
// to sort a heap into ascending order (after which it is no longer a heap).
|
| 1502 |
+
template <typename RandomAccessContainer>
|
| 1503 |
+
void c_sort_heap(RandomAccessContainer& sequence) {
|
| 1504 |
+
std::sort_heap(container_algorithm_internal::c_begin(sequence),
|
| 1505 |
+
container_algorithm_internal::c_end(sequence));
|
| 1506 |
+
}
|
| 1507 |
+
|
| 1508 |
+
// Overload of c_sort_heap() for performing heap comparisons using a
|
| 1509 |
+
// `comp` other than `operator<`
|
| 1510 |
+
template <typename RandomAccessContainer, typename LessThan>
|
| 1511 |
+
void c_sort_heap(RandomAccessContainer& sequence, LessThan&& comp) {
|
| 1512 |
+
std::sort_heap(container_algorithm_internal::c_begin(sequence),
|
| 1513 |
+
container_algorithm_internal::c_end(sequence),
|
| 1514 |
+
std::forward<LessThan>(comp));
|
| 1515 |
+
}
|
| 1516 |
+
|
| 1517 |
+
// c_is_heap()
|
| 1518 |
+
//
|
| 1519 |
+
// Container-based version of the <algorithm> `std::is_heap()` function
|
| 1520 |
+
// to check whether the given container is a heap.
|
| 1521 |
+
template <typename RandomAccessContainer>
|
| 1522 |
+
bool c_is_heap(const RandomAccessContainer& sequence) {
|
| 1523 |
+
return std::is_heap(container_algorithm_internal::c_begin(sequence),
|
| 1524 |
+
container_algorithm_internal::c_end(sequence));
|
| 1525 |
+
}
|
| 1526 |
+
|
| 1527 |
+
// Overload of c_is_heap() for performing heap comparisons using a
|
| 1528 |
+
// `comp` other than `operator<`
|
| 1529 |
+
template <typename RandomAccessContainer, typename LessThan>
|
| 1530 |
+
bool c_is_heap(const RandomAccessContainer& sequence, LessThan&& comp) {
|
| 1531 |
+
return std::is_heap(container_algorithm_internal::c_begin(sequence),
|
| 1532 |
+
container_algorithm_internal::c_end(sequence),
|
| 1533 |
+
std::forward<LessThan>(comp));
|
| 1534 |
+
}
|
| 1535 |
+
|
| 1536 |
+
// c_is_heap_until()
|
| 1537 |
+
//
|
| 1538 |
+
// Container-based version of the <algorithm> `std::is_heap_until()` function
|
| 1539 |
+
// to find the first element in a given container which is not in heap order.
|
| 1540 |
+
template <typename RandomAccessContainer>
|
| 1541 |
+
container_algorithm_internal::ContainerIter<RandomAccessContainer>
|
| 1542 |
+
c_is_heap_until(RandomAccessContainer& sequence) {
|
| 1543 |
+
return std::is_heap_until(container_algorithm_internal::c_begin(sequence),
|
| 1544 |
+
container_algorithm_internal::c_end(sequence));
|
| 1545 |
+
}
|
| 1546 |
+
|
| 1547 |
+
// Overload of c_is_heap_until() for performing heap comparisons using a
|
| 1548 |
+
// `comp` other than `operator<`
|
| 1549 |
+
template <typename RandomAccessContainer, typename LessThan>
|
| 1550 |
+
container_algorithm_internal::ContainerIter<RandomAccessContainer>
|
| 1551 |
+
c_is_heap_until(RandomAccessContainer& sequence, LessThan&& comp) {
|
| 1552 |
+
return std::is_heap_until(container_algorithm_internal::c_begin(sequence),
|
| 1553 |
+
container_algorithm_internal::c_end(sequence),
|
| 1554 |
+
std::forward<LessThan>(comp));
|
| 1555 |
+
}
|
| 1556 |
+
|
| 1557 |
+
//------------------------------------------------------------------------------
|
| 1558 |
+
// <algorithm> Min/max
|
| 1559 |
+
//------------------------------------------------------------------------------
|
| 1560 |
+
|
| 1561 |
+
// c_min_element()
|
| 1562 |
+
//
|
| 1563 |
+
// Container-based version of the <algorithm> `std::min_element()` function
|
| 1564 |
+
// to return an iterator pointing to the element with the smallest value, using
|
| 1565 |
+
// `operator<` to make the comparisons.
|
| 1566 |
+
template <typename Sequence>
|
| 1567 |
+
ABSL_INTERNAL_CONSTEXPR_SINCE_CXX17
|
| 1568 |
+
container_algorithm_internal::ContainerIter<Sequence>
|
| 1569 |
+
c_min_element(Sequence& sequence) {
|
| 1570 |
+
return std::min_element(container_algorithm_internal::c_begin(sequence),
|
| 1571 |
+
container_algorithm_internal::c_end(sequence));
|
| 1572 |
+
}
|
| 1573 |
+
|
| 1574 |
+
// Overload of c_min_element() for performing a `comp` comparison other than
|
| 1575 |
+
// `operator<`.
|
| 1576 |
+
template <typename Sequence, typename LessThan>
|
| 1577 |
+
ABSL_INTERNAL_CONSTEXPR_SINCE_CXX17
|
| 1578 |
+
container_algorithm_internal::ContainerIter<Sequence>
|
| 1579 |
+
c_min_element(Sequence& sequence, LessThan&& comp) {
|
| 1580 |
+
return std::min_element(container_algorithm_internal::c_begin(sequence),
|
| 1581 |
+
container_algorithm_internal::c_end(sequence),
|
| 1582 |
+
std::forward<LessThan>(comp));
|
| 1583 |
+
}
|
| 1584 |
+
|
| 1585 |
+
// c_max_element()
|
| 1586 |
+
//
|
| 1587 |
+
// Container-based version of the <algorithm> `std::max_element()` function
|
| 1588 |
+
// to return an iterator pointing to the element with the largest value, using
|
| 1589 |
+
// `operator<` to make the comparisons.
|
| 1590 |
+
template <typename Sequence>
|
| 1591 |
+
ABSL_INTERNAL_CONSTEXPR_SINCE_CXX17
|
| 1592 |
+
container_algorithm_internal::ContainerIter<Sequence>
|
| 1593 |
+
c_max_element(Sequence& sequence) {
|
| 1594 |
+
return std::max_element(container_algorithm_internal::c_begin(sequence),
|
| 1595 |
+
container_algorithm_internal::c_end(sequence));
|
| 1596 |
+
}
|
| 1597 |
+
|
| 1598 |
+
// Overload of c_max_element() for performing a `comp` comparison other than
|
| 1599 |
+
// `operator<`.
|
| 1600 |
+
template <typename Sequence, typename LessThan>
|
| 1601 |
+
ABSL_INTERNAL_CONSTEXPR_SINCE_CXX17
|
| 1602 |
+
container_algorithm_internal::ContainerIter<Sequence>
|
| 1603 |
+
c_max_element(Sequence& sequence, LessThan&& comp) {
|
| 1604 |
+
return std::max_element(container_algorithm_internal::c_begin(sequence),
|
| 1605 |
+
container_algorithm_internal::c_end(sequence),
|
| 1606 |
+
std::forward<LessThan>(comp));
|
| 1607 |
+
}
|
| 1608 |
+
|
| 1609 |
+
// c_minmax_element()
|
| 1610 |
+
//
|
| 1611 |
+
// Container-based version of the <algorithm> `std::minmax_element()` function
|
| 1612 |
+
// to return a pair of iterators pointing to the elements containing the
|
| 1613 |
+
// smallest and largest values, respectively, using `operator<` to make the
|
| 1614 |
+
// comparisons.
|
| 1615 |
+
template <typename C>
|
| 1616 |
+
ABSL_INTERNAL_CONSTEXPR_SINCE_CXX17
|
| 1617 |
+
container_algorithm_internal::ContainerIterPairType<C, C>
|
| 1618 |
+
c_minmax_element(C& c) {
|
| 1619 |
+
return std::minmax_element(container_algorithm_internal::c_begin(c),
|
| 1620 |
+
container_algorithm_internal::c_end(c));
|
| 1621 |
+
}
|
| 1622 |
+
|
| 1623 |
+
// Overload of c_minmax_element() for performing `comp` comparisons other than
|
| 1624 |
+
// `operator<`.
|
| 1625 |
+
template <typename C, typename LessThan>
|
| 1626 |
+
ABSL_INTERNAL_CONSTEXPR_SINCE_CXX17
|
| 1627 |
+
container_algorithm_internal::ContainerIterPairType<C, C>
|
| 1628 |
+
c_minmax_element(C& c, LessThan&& comp) {
|
| 1629 |
+
return std::minmax_element(container_algorithm_internal::c_begin(c),
|
| 1630 |
+
container_algorithm_internal::c_end(c),
|
| 1631 |
+
std::forward<LessThan>(comp));
|
| 1632 |
+
}
|
| 1633 |
+
|
| 1634 |
+
//------------------------------------------------------------------------------
|
| 1635 |
+
// <algorithm> Lexicographical Comparisons
|
| 1636 |
+
//------------------------------------------------------------------------------
|
| 1637 |
+
|
| 1638 |
+
// c_lexicographical_compare()
|
| 1639 |
+
//
|
| 1640 |
+
// Container-based version of the <algorithm> `std::lexicographical_compare()`
|
| 1641 |
+
// function to lexicographically compare (e.g. sort words alphabetically) two
|
| 1642 |
+
// container sequences. The comparison is performed using `operator<`. Note
|
| 1643 |
+
// that capital letters ("A-Z") have ASCII values less than lowercase letters
|
| 1644 |
+
// ("a-z").
|
| 1645 |
+
template <typename Sequence1, typename Sequence2>
|
| 1646 |
+
bool c_lexicographical_compare(const Sequence1& sequence1,
|
| 1647 |
+
const Sequence2& sequence2) {
|
| 1648 |
+
return std::lexicographical_compare(
|
| 1649 |
+
container_algorithm_internal::c_begin(sequence1),
|
| 1650 |
+
container_algorithm_internal::c_end(sequence1),
|
| 1651 |
+
container_algorithm_internal::c_begin(sequence2),
|
| 1652 |
+
container_algorithm_internal::c_end(sequence2));
|
| 1653 |
+
}
|
| 1654 |
+
|
| 1655 |
+
// Overload of c_lexicographical_compare() for performing a lexicographical
|
| 1656 |
+
// comparison using a `comp` operator instead of `operator<`.
|
| 1657 |
+
template <typename Sequence1, typename Sequence2, typename LessThan>
|
| 1658 |
+
bool c_lexicographical_compare(const Sequence1& sequence1,
|
| 1659 |
+
const Sequence2& sequence2, LessThan&& comp) {
|
| 1660 |
+
return std::lexicographical_compare(
|
| 1661 |
+
container_algorithm_internal::c_begin(sequence1),
|
| 1662 |
+
container_algorithm_internal::c_end(sequence1),
|
| 1663 |
+
container_algorithm_internal::c_begin(sequence2),
|
| 1664 |
+
container_algorithm_internal::c_end(sequence2),
|
| 1665 |
+
std::forward<LessThan>(comp));
|
| 1666 |
+
}
|
| 1667 |
+
|
| 1668 |
+
// c_next_permutation()
|
| 1669 |
+
//
|
| 1670 |
+
// Container-based version of the <algorithm> `std::next_permutation()` function
|
| 1671 |
+
// to rearrange a container's elements into the next lexicographically greater
|
| 1672 |
+
// permutation.
|
| 1673 |
+
template <typename C>
|
| 1674 |
+
bool c_next_permutation(C& c) {
|
| 1675 |
+
return std::next_permutation(container_algorithm_internal::c_begin(c),
|
| 1676 |
+
container_algorithm_internal::c_end(c));
|
| 1677 |
+
}
|
| 1678 |
+
|
| 1679 |
+
// Overload of c_next_permutation() for performing a lexicographical
|
| 1680 |
+
// comparison using a `comp` operator instead of `operator<`.
|
| 1681 |
+
template <typename C, typename LessThan>
|
| 1682 |
+
bool c_next_permutation(C& c, LessThan&& comp) {
|
| 1683 |
+
return std::next_permutation(container_algorithm_internal::c_begin(c),
|
| 1684 |
+
container_algorithm_internal::c_end(c),
|
| 1685 |
+
std::forward<LessThan>(comp));
|
| 1686 |
+
}
|
| 1687 |
+
|
| 1688 |
+
// c_prev_permutation()
|
| 1689 |
+
//
|
| 1690 |
+
// Container-based version of the <algorithm> `std::prev_permutation()` function
|
| 1691 |
+
// to rearrange a container's elements into the next lexicographically lesser
|
| 1692 |
+
// permutation.
|
| 1693 |
+
template <typename C>
|
| 1694 |
+
bool c_prev_permutation(C& c) {
|
| 1695 |
+
return std::prev_permutation(container_algorithm_internal::c_begin(c),
|
| 1696 |
+
container_algorithm_internal::c_end(c));
|
| 1697 |
+
}
|
| 1698 |
+
|
| 1699 |
+
// Overload of c_prev_permutation() for performing a lexicographical
|
| 1700 |
+
// comparison using a `comp` operator instead of `operator<`.
|
| 1701 |
+
template <typename C, typename LessThan>
|
| 1702 |
+
bool c_prev_permutation(C& c, LessThan&& comp) {
|
| 1703 |
+
return std::prev_permutation(container_algorithm_internal::c_begin(c),
|
| 1704 |
+
container_algorithm_internal::c_end(c),
|
| 1705 |
+
std::forward<LessThan>(comp));
|
| 1706 |
+
}
|
| 1707 |
+
|
| 1708 |
+
//------------------------------------------------------------------------------
|
| 1709 |
+
// <numeric> algorithms
|
| 1710 |
+
//------------------------------------------------------------------------------
|
| 1711 |
+
|
| 1712 |
+
// c_iota()
|
| 1713 |
+
//
|
| 1714 |
+
// Container-based version of the <numeric> `std::iota()` function
|
| 1715 |
+
// to compute successive values of `value`, as if incremented with `++value`
|
| 1716 |
+
// after each element is written, and write them to the container.
|
| 1717 |
+
template <typename Sequence, typename T>
|
| 1718 |
+
void c_iota(Sequence& sequence, const T& value) {
|
| 1719 |
+
std::iota(container_algorithm_internal::c_begin(sequence),
|
| 1720 |
+
container_algorithm_internal::c_end(sequence), value);
|
| 1721 |
+
}
|
| 1722 |
+
|
| 1723 |
+
// c_accumulate()
|
| 1724 |
+
//
|
| 1725 |
+
// Container-based version of the <numeric> `std::accumulate()` function
|
| 1726 |
+
// to accumulate the element values of a container to `init` and return that
|
| 1727 |
+
// accumulation by value.
|
| 1728 |
+
//
|
| 1729 |
+
// Note: Due to a language technicality this function has return type
|
| 1730 |
+
// absl::decay_t<T>. As a user of this function you can casually read
|
| 1731 |
+
// this as "returns T by value" and assume it does the right thing.
|
| 1732 |
+
template <typename Sequence, typename T>
|
| 1733 |
+
decay_t<T> c_accumulate(const Sequence& sequence, T&& init) {
|
| 1734 |
+
return std::accumulate(container_algorithm_internal::c_begin(sequence),
|
| 1735 |
+
container_algorithm_internal::c_end(sequence),
|
| 1736 |
+
std::forward<T>(init));
|
| 1737 |
+
}
|
| 1738 |
+
|
| 1739 |
+
// Overload of c_accumulate() for using a binary operations other than
|
| 1740 |
+
// addition for computing the accumulation.
|
| 1741 |
+
template <typename Sequence, typename T, typename BinaryOp>
|
| 1742 |
+
decay_t<T> c_accumulate(const Sequence& sequence, T&& init,
|
| 1743 |
+
BinaryOp&& binary_op) {
|
| 1744 |
+
return std::accumulate(container_algorithm_internal::c_begin(sequence),
|
| 1745 |
+
container_algorithm_internal::c_end(sequence),
|
| 1746 |
+
std::forward<T>(init),
|
| 1747 |
+
std::forward<BinaryOp>(binary_op));
|
| 1748 |
+
}
|
| 1749 |
+
|
| 1750 |
+
// c_inner_product()
|
| 1751 |
+
//
|
| 1752 |
+
// Container-based version of the <numeric> `std::inner_product()` function
|
| 1753 |
+
// to compute the cumulative inner product of container element pairs.
|
| 1754 |
+
//
|
| 1755 |
+
// Note: Due to a language technicality this function has return type
|
| 1756 |
+
// absl::decay_t<T>. As a user of this function you can casually read
|
| 1757 |
+
// this as "returns T by value" and assume it does the right thing.
|
| 1758 |
+
template <typename Sequence1, typename Sequence2, typename T>
|
| 1759 |
+
decay_t<T> c_inner_product(const Sequence1& factors1, const Sequence2& factors2,
|
| 1760 |
+
T&& sum) {
|
| 1761 |
+
return std::inner_product(container_algorithm_internal::c_begin(factors1),
|
| 1762 |
+
container_algorithm_internal::c_end(factors1),
|
| 1763 |
+
container_algorithm_internal::c_begin(factors2),
|
| 1764 |
+
std::forward<T>(sum));
|
| 1765 |
+
}
|
| 1766 |
+
|
| 1767 |
+
// Overload of c_inner_product() for using binary operations other than
|
| 1768 |
+
// `operator+` (for computing the accumulation) and `operator*` (for computing
|
| 1769 |
+
// the product between the two container's element pair).
|
| 1770 |
+
template <typename Sequence1, typename Sequence2, typename T,
|
| 1771 |
+
typename BinaryOp1, typename BinaryOp2>
|
| 1772 |
+
decay_t<T> c_inner_product(const Sequence1& factors1, const Sequence2& factors2,
|
| 1773 |
+
T&& sum, BinaryOp1&& op1, BinaryOp2&& op2) {
|
| 1774 |
+
return std::inner_product(container_algorithm_internal::c_begin(factors1),
|
| 1775 |
+
container_algorithm_internal::c_end(factors1),
|
| 1776 |
+
container_algorithm_internal::c_begin(factors2),
|
| 1777 |
+
std::forward<T>(sum), std::forward<BinaryOp1>(op1),
|
| 1778 |
+
std::forward<BinaryOp2>(op2));
|
| 1779 |
+
}
|
| 1780 |
+
|
| 1781 |
+
// c_adjacent_difference()
|
| 1782 |
+
//
|
| 1783 |
+
// Container-based version of the <numeric> `std::adjacent_difference()`
|
| 1784 |
+
// function to compute the difference between each element and the one preceding
|
| 1785 |
+
// it and write it to an iterator.
|
| 1786 |
+
template <typename InputSequence, typename OutputIt>
|
| 1787 |
+
OutputIt c_adjacent_difference(const InputSequence& input,
|
| 1788 |
+
OutputIt output_first) {
|
| 1789 |
+
return std::adjacent_difference(container_algorithm_internal::c_begin(input),
|
| 1790 |
+
container_algorithm_internal::c_end(input),
|
| 1791 |
+
output_first);
|
| 1792 |
+
}
|
| 1793 |
+
|
| 1794 |
+
// Overload of c_adjacent_difference() for using a binary operation other than
|
| 1795 |
+
// subtraction to compute the adjacent difference.
|
| 1796 |
+
template <typename InputSequence, typename OutputIt, typename BinaryOp>
|
| 1797 |
+
OutputIt c_adjacent_difference(const InputSequence& input,
|
| 1798 |
+
OutputIt output_first, BinaryOp&& op) {
|
| 1799 |
+
return std::adjacent_difference(container_algorithm_internal::c_begin(input),
|
| 1800 |
+
container_algorithm_internal::c_end(input),
|
| 1801 |
+
output_first, std::forward<BinaryOp>(op));
|
| 1802 |
+
}
|
| 1803 |
+
|
| 1804 |
+
// c_partial_sum()
|
| 1805 |
+
//
|
| 1806 |
+
// Container-based version of the <numeric> `std::partial_sum()` function
|
| 1807 |
+
// to compute the partial sum of the elements in a sequence and write them
|
| 1808 |
+
// to an iterator. The partial sum is the sum of all element values so far in
|
| 1809 |
+
// the sequence.
|
| 1810 |
+
template <typename InputSequence, typename OutputIt>
|
| 1811 |
+
OutputIt c_partial_sum(const InputSequence& input, OutputIt output_first) {
|
| 1812 |
+
return std::partial_sum(container_algorithm_internal::c_begin(input),
|
| 1813 |
+
container_algorithm_internal::c_end(input),
|
| 1814 |
+
output_first);
|
| 1815 |
+
}
|
| 1816 |
+
|
| 1817 |
+
// Overload of c_partial_sum() for using a binary operation other than addition
|
| 1818 |
+
// to compute the "partial sum".
|
| 1819 |
+
template <typename InputSequence, typename OutputIt, typename BinaryOp>
|
| 1820 |
+
OutputIt c_partial_sum(const InputSequence& input, OutputIt output_first,
|
| 1821 |
+
BinaryOp&& op) {
|
| 1822 |
+
return std::partial_sum(container_algorithm_internal::c_begin(input),
|
| 1823 |
+
container_algorithm_internal::c_end(input),
|
| 1824 |
+
output_first, std::forward<BinaryOp>(op));
|
| 1825 |
+
}
|
| 1826 |
+
|
| 1827 |
+
ABSL_NAMESPACE_END
|
| 1828 |
+
} // namespace absl
|
| 1829 |
+
|
| 1830 |
+
#endif // ABSL_ALGORITHM_CONTAINER_H_
|
include/absl/base/attributes.h
ADDED
|
@@ -0,0 +1,1008 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
// Copyright 2017 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 |
+
// This header file defines macros for declaring attributes for functions,
|
| 16 |
+
// types, and variables.
|
| 17 |
+
//
|
| 18 |
+
// These macros are used within Abseil and allow the compiler to optimize, where
|
| 19 |
+
// applicable, certain function calls.
|
| 20 |
+
//
|
| 21 |
+
// Most macros here are exposing GCC or Clang features, and are stubbed out for
|
| 22 |
+
// other compilers.
|
| 23 |
+
//
|
| 24 |
+
// GCC attributes documentation:
|
| 25 |
+
// https://gcc.gnu.org/onlinedocs/gcc-4.7.0/gcc/Function-Attributes.html
|
| 26 |
+
// https://gcc.gnu.org/onlinedocs/gcc-4.7.0/gcc/Variable-Attributes.html
|
| 27 |
+
// https://gcc.gnu.org/onlinedocs/gcc-4.7.0/gcc/Type-Attributes.html
|
| 28 |
+
//
|
| 29 |
+
// Most attributes in this file are already supported by GCC 4.7. However, some
|
| 30 |
+
// of them are not supported in older version of Clang. Thus, we check
|
| 31 |
+
// `__has_attribute()` first. If the check fails, we check if we are on GCC and
|
| 32 |
+
// assume the attribute exists on GCC (which is verified on GCC 4.7).
|
| 33 |
+
|
| 34 |
+
// SKIP_ABSL_INLINE_NAMESPACE_CHECK
|
| 35 |
+
|
| 36 |
+
#ifndef ABSL_BASE_ATTRIBUTES_H_
|
| 37 |
+
#define ABSL_BASE_ATTRIBUTES_H_
|
| 38 |
+
|
| 39 |
+
#include "absl/base/config.h"
|
| 40 |
+
|
| 41 |
+
// ABSL_HAVE_ATTRIBUTE
|
| 42 |
+
//
|
| 43 |
+
// A function-like feature checking macro that is a wrapper around
|
| 44 |
+
// `__has_attribute`, which is defined by GCC 5+ and Clang and evaluates to a
|
| 45 |
+
// nonzero constant integer if the attribute is supported or 0 if not.
|
| 46 |
+
//
|
| 47 |
+
// It evaluates to zero if `__has_attribute` is not defined by the compiler.
|
| 48 |
+
//
|
| 49 |
+
// GCC: https://gcc.gnu.org/gcc-5/changes.html
|
| 50 |
+
// Clang: https://clang.llvm.org/docs/LanguageExtensions.html
|
| 51 |
+
#ifdef __has_attribute
|
| 52 |
+
#define ABSL_HAVE_ATTRIBUTE(x) __has_attribute(x)
|
| 53 |
+
#else
|
| 54 |
+
#define ABSL_HAVE_ATTRIBUTE(x) 0
|
| 55 |
+
#endif
|
| 56 |
+
|
| 57 |
+
// ABSL_HAVE_CPP_ATTRIBUTE
|
| 58 |
+
//
|
| 59 |
+
// A function-like feature checking macro that accepts C++11 style attributes.
|
| 60 |
+
// It's a wrapper around `__has_cpp_attribute`, defined by ISO C++ SD-6
|
| 61 |
+
// (https://en.cppreference.com/w/cpp/experimental/feature_test). If we don't
|
| 62 |
+
// find `__has_cpp_attribute`, will evaluate to 0.
|
| 63 |
+
#if defined(__cplusplus) && defined(__has_cpp_attribute)
|
| 64 |
+
// NOTE: requiring __cplusplus above should not be necessary, but
|
| 65 |
+
// works around https://bugs.llvm.org/show_bug.cgi?id=23435.
|
| 66 |
+
#define ABSL_HAVE_CPP_ATTRIBUTE(x) __has_cpp_attribute(x)
|
| 67 |
+
#else
|
| 68 |
+
#define ABSL_HAVE_CPP_ATTRIBUTE(x) 0
|
| 69 |
+
#endif
|
| 70 |
+
|
| 71 |
+
// -----------------------------------------------------------------------------
|
| 72 |
+
// Function Attributes
|
| 73 |
+
// -----------------------------------------------------------------------------
|
| 74 |
+
//
|
| 75 |
+
// GCC: https://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
|
| 76 |
+
// Clang: https://clang.llvm.org/docs/AttributeReference.html
|
| 77 |
+
|
| 78 |
+
// ABSL_PRINTF_ATTRIBUTE
|
| 79 |
+
// ABSL_SCANF_ATTRIBUTE
|
| 80 |
+
//
|
| 81 |
+
// Tells the compiler to perform `printf` format string checking if the
|
| 82 |
+
// compiler supports it; see the 'format' attribute in
|
| 83 |
+
// <https://gcc.gnu.org/onlinedocs/gcc-4.7.0/gcc/Function-Attributes.html>.
|
| 84 |
+
//
|
| 85 |
+
// Note: As the GCC manual states, "[s]ince non-static C++ methods
|
| 86 |
+
// have an implicit 'this' argument, the arguments of such methods
|
| 87 |
+
// should be counted from two, not one."
|
| 88 |
+
#if ABSL_HAVE_ATTRIBUTE(format) || (defined(__GNUC__) && !defined(__clang__))
|
| 89 |
+
#define ABSL_PRINTF_ATTRIBUTE(string_index, first_to_check) \
|
| 90 |
+
__attribute__((__format__(__printf__, string_index, first_to_check)))
|
| 91 |
+
#define ABSL_SCANF_ATTRIBUTE(string_index, first_to_check) \
|
| 92 |
+
__attribute__((__format__(__scanf__, string_index, first_to_check)))
|
| 93 |
+
#else
|
| 94 |
+
#define ABSL_PRINTF_ATTRIBUTE(string_index, first_to_check)
|
| 95 |
+
#define ABSL_SCANF_ATTRIBUTE(string_index, first_to_check)
|
| 96 |
+
#endif
|
| 97 |
+
|
| 98 |
+
// ABSL_ATTRIBUTE_ALWAYS_INLINE
|
| 99 |
+
// ABSL_ATTRIBUTE_NOINLINE
|
| 100 |
+
//
|
| 101 |
+
// Forces functions to either inline or not inline. Introduced in gcc 3.1.
|
| 102 |
+
#if ABSL_HAVE_ATTRIBUTE(always_inline) || \
|
| 103 |
+
(defined(__GNUC__) && !defined(__clang__))
|
| 104 |
+
#define ABSL_ATTRIBUTE_ALWAYS_INLINE __attribute__((always_inline))
|
| 105 |
+
#define ABSL_HAVE_ATTRIBUTE_ALWAYS_INLINE 1
|
| 106 |
+
#else
|
| 107 |
+
#define ABSL_ATTRIBUTE_ALWAYS_INLINE
|
| 108 |
+
#endif
|
| 109 |
+
|
| 110 |
+
#if ABSL_HAVE_ATTRIBUTE(noinline) || (defined(__GNUC__) && !defined(__clang__))
|
| 111 |
+
#define ABSL_ATTRIBUTE_NOINLINE __attribute__((noinline))
|
| 112 |
+
#define ABSL_HAVE_ATTRIBUTE_NOINLINE 1
|
| 113 |
+
#else
|
| 114 |
+
#define ABSL_ATTRIBUTE_NOINLINE
|
| 115 |
+
#endif
|
| 116 |
+
|
| 117 |
+
// ABSL_ATTRIBUTE_NO_TAIL_CALL
|
| 118 |
+
//
|
| 119 |
+
// Prevents the compiler from optimizing away stack frames for functions which
|
| 120 |
+
// end in a call to another function.
|
| 121 |
+
#if ABSL_HAVE_ATTRIBUTE(disable_tail_calls)
|
| 122 |
+
#define ABSL_HAVE_ATTRIBUTE_NO_TAIL_CALL 1
|
| 123 |
+
#define ABSL_ATTRIBUTE_NO_TAIL_CALL __attribute__((disable_tail_calls))
|
| 124 |
+
#elif defined(__GNUC__) && !defined(__clang__) && !defined(__e2k__)
|
| 125 |
+
#define ABSL_HAVE_ATTRIBUTE_NO_TAIL_CALL 1
|
| 126 |
+
#define ABSL_ATTRIBUTE_NO_TAIL_CALL \
|
| 127 |
+
__attribute__((optimize("no-optimize-sibling-calls")))
|
| 128 |
+
#else
|
| 129 |
+
#define ABSL_ATTRIBUTE_NO_TAIL_CALL
|
| 130 |
+
#define ABSL_HAVE_ATTRIBUTE_NO_TAIL_CALL 0
|
| 131 |
+
#endif
|
| 132 |
+
|
| 133 |
+
// ABSL_ATTRIBUTE_WEAK
|
| 134 |
+
//
|
| 135 |
+
// Tags a function as weak for the purposes of compilation and linking.
|
| 136 |
+
// Weak attributes did not work properly in LLVM's Windows backend before
|
| 137 |
+
// 9.0.0, so disable them there. See https://bugs.llvm.org/show_bug.cgi?id=37598
|
| 138 |
+
// for further information. Weak attributes do not work across DLL boundary.
|
| 139 |
+
// The MinGW compiler doesn't complain about the weak attribute until the link
|
| 140 |
+
// step, presumably because Windows doesn't use ELF binaries.
|
| 141 |
+
#if (ABSL_HAVE_ATTRIBUTE(weak) || \
|
| 142 |
+
(defined(__GNUC__) && !defined(__clang__))) && \
|
| 143 |
+
(!defined(_WIN32) || \
|
| 144 |
+
(defined(__clang__) && __clang_major__ >= 9 && \
|
| 145 |
+
!defined(ABSL_BUILD_DLL) && !defined(ABSL_CONSUME_DLL))) && \
|
| 146 |
+
!defined(__MINGW32__)
|
| 147 |
+
#undef ABSL_ATTRIBUTE_WEAK
|
| 148 |
+
#define ABSL_ATTRIBUTE_WEAK __attribute__((weak))
|
| 149 |
+
#define ABSL_HAVE_ATTRIBUTE_WEAK 1
|
| 150 |
+
#else
|
| 151 |
+
#define ABSL_ATTRIBUTE_WEAK
|
| 152 |
+
#define ABSL_HAVE_ATTRIBUTE_WEAK 0
|
| 153 |
+
#endif
|
| 154 |
+
|
| 155 |
+
// ABSL_ATTRIBUTE_NONNULL
|
| 156 |
+
//
|
| 157 |
+
// Tells the compiler either (a) that a particular function parameter
|
| 158 |
+
// should be a non-null pointer, or (b) that all pointer arguments should
|
| 159 |
+
// be non-null.
|
| 160 |
+
//
|
| 161 |
+
// Note: As the GCC manual states, "[s]ince non-static C++ methods
|
| 162 |
+
// have an implicit 'this' argument, the arguments of such methods
|
| 163 |
+
// should be counted from two, not one."
|
| 164 |
+
//
|
| 165 |
+
// Args are indexed starting at 1.
|
| 166 |
+
//
|
| 167 |
+
// For non-static class member functions, the implicit `this` argument
|
| 168 |
+
// is arg 1, and the first explicit argument is arg 2. For static class member
|
| 169 |
+
// functions, there is no implicit `this`, and the first explicit argument is
|
| 170 |
+
// arg 1.
|
| 171 |
+
//
|
| 172 |
+
// Example:
|
| 173 |
+
//
|
| 174 |
+
// /* arg_a cannot be null, but arg_b can */
|
| 175 |
+
// void Function(void* arg_a, void* arg_b) ABSL_ATTRIBUTE_NONNULL(1);
|
| 176 |
+
//
|
| 177 |
+
// class C {
|
| 178 |
+
// /* arg_a cannot be null, but arg_b can */
|
| 179 |
+
// void Method(void* arg_a, void* arg_b) ABSL_ATTRIBUTE_NONNULL(2);
|
| 180 |
+
//
|
| 181 |
+
// /* arg_a cannot be null, but arg_b can */
|
| 182 |
+
// static void StaticMethod(void* arg_a, void* arg_b)
|
| 183 |
+
// ABSL_ATTRIBUTE_NONNULL(1);
|
| 184 |
+
// };
|
| 185 |
+
//
|
| 186 |
+
// If no arguments are provided, then all pointer arguments should be non-null.
|
| 187 |
+
//
|
| 188 |
+
// /* No pointer arguments may be null. */
|
| 189 |
+
// void Function(void* arg_a, void* arg_b, int arg_c) ABSL_ATTRIBUTE_NONNULL();
|
| 190 |
+
//
|
| 191 |
+
// NOTE: The GCC nonnull attribute actually accepts a list of arguments, but
|
| 192 |
+
// ABSL_ATTRIBUTE_NONNULL does not.
|
| 193 |
+
#if ABSL_HAVE_ATTRIBUTE(nonnull) || (defined(__GNUC__) && !defined(__clang__))
|
| 194 |
+
#define ABSL_ATTRIBUTE_NONNULL(arg_index) __attribute__((nonnull(arg_index)))
|
| 195 |
+
#else
|
| 196 |
+
#define ABSL_ATTRIBUTE_NONNULL(...)
|
| 197 |
+
#endif
|
| 198 |
+
|
| 199 |
+
// ABSL_ATTRIBUTE_NORETURN
|
| 200 |
+
//
|
| 201 |
+
// Tells the compiler that a given function never returns.
|
| 202 |
+
//
|
| 203 |
+
// Deprecated: Prefer the `[[noreturn]]` attribute standardized by C++11 over
|
| 204 |
+
// this macro.
|
| 205 |
+
#if ABSL_HAVE_ATTRIBUTE(noreturn) || (defined(__GNUC__) && !defined(__clang__))
|
| 206 |
+
#define ABSL_ATTRIBUTE_NORETURN __attribute__((noreturn))
|
| 207 |
+
#elif defined(_MSC_VER)
|
| 208 |
+
#define ABSL_ATTRIBUTE_NORETURN __declspec(noreturn)
|
| 209 |
+
#else
|
| 210 |
+
#define ABSL_ATTRIBUTE_NORETURN
|
| 211 |
+
#endif
|
| 212 |
+
|
| 213 |
+
// ABSL_ATTRIBUTE_NO_SANITIZE_ADDRESS
|
| 214 |
+
//
|
| 215 |
+
// Tells the AddressSanitizer (or other memory testing tools) to ignore a given
|
| 216 |
+
// function. Useful for cases when a function reads random locations on stack,
|
| 217 |
+
// calls _exit from a cloned subprocess, deliberately accesses buffer
|
| 218 |
+
// out of bounds or does other scary things with memory.
|
| 219 |
+
// NOTE: GCC supports AddressSanitizer(asan) since 4.8.
|
| 220 |
+
// https://gcc.gnu.org/gcc-4.8/changes.html
|
| 221 |
+
#if defined(ABSL_HAVE_ADDRESS_SANITIZER) && \
|
| 222 |
+
ABSL_HAVE_ATTRIBUTE(no_sanitize_address)
|
| 223 |
+
#define ABSL_ATTRIBUTE_NO_SANITIZE_ADDRESS __attribute__((no_sanitize_address))
|
| 224 |
+
#elif defined(ABSL_HAVE_ADDRESS_SANITIZER) && defined(_MSC_VER) && \
|
| 225 |
+
_MSC_VER >= 1928
|
| 226 |
+
// https://docs.microsoft.com/en-us/cpp/cpp/no-sanitize-address
|
| 227 |
+
#define ABSL_ATTRIBUTE_NO_SANITIZE_ADDRESS __declspec(no_sanitize_address)
|
| 228 |
+
#elif defined(ABSL_HAVE_HWADDRESS_SANITIZER) && ABSL_HAVE_ATTRIBUTE(no_sanitize)
|
| 229 |
+
// HWAddressSanitizer is a sanitizer similar to AddressSanitizer, which uses CPU
|
| 230 |
+
// features to detect similar bugs with less CPU and memory overhead.
|
| 231 |
+
// NOTE: GCC supports HWAddressSanitizer(hwasan) since 11.
|
| 232 |
+
// https://gcc.gnu.org/gcc-11/changes.html
|
| 233 |
+
#define ABSL_ATTRIBUTE_NO_SANITIZE_ADDRESS \
|
| 234 |
+
__attribute__((no_sanitize("hwaddress")))
|
| 235 |
+
#else
|
| 236 |
+
#define ABSL_ATTRIBUTE_NO_SANITIZE_ADDRESS
|
| 237 |
+
#endif
|
| 238 |
+
|
| 239 |
+
// ABSL_ATTRIBUTE_NO_SANITIZE_MEMORY
|
| 240 |
+
//
|
| 241 |
+
// Tells the MemorySanitizer to relax the handling of a given function. All "Use
|
| 242 |
+
// of uninitialized value" warnings from such functions will be suppressed, and
|
| 243 |
+
// all values loaded from memory will be considered fully initialized. This
|
| 244 |
+
// attribute is similar to the ABSL_ATTRIBUTE_NO_SANITIZE_ADDRESS attribute
|
| 245 |
+
// above, but deals with initialized-ness rather than addressability issues.
|
| 246 |
+
// NOTE: MemorySanitizer(msan) is supported by Clang but not GCC.
|
| 247 |
+
#if ABSL_HAVE_ATTRIBUTE(no_sanitize_memory)
|
| 248 |
+
#define ABSL_ATTRIBUTE_NO_SANITIZE_MEMORY __attribute__((no_sanitize_memory))
|
| 249 |
+
#else
|
| 250 |
+
#define ABSL_ATTRIBUTE_NO_SANITIZE_MEMORY
|
| 251 |
+
#endif
|
| 252 |
+
|
| 253 |
+
// ABSL_ATTRIBUTE_NO_SANITIZE_THREAD
|
| 254 |
+
//
|
| 255 |
+
// Tells the ThreadSanitizer to not instrument a given function.
|
| 256 |
+
// NOTE: GCC supports ThreadSanitizer(tsan) since 4.8.
|
| 257 |
+
// https://gcc.gnu.org/gcc-4.8/changes.html
|
| 258 |
+
#if ABSL_HAVE_ATTRIBUTE(no_sanitize_thread)
|
| 259 |
+
#define ABSL_ATTRIBUTE_NO_SANITIZE_THREAD __attribute__((no_sanitize_thread))
|
| 260 |
+
#else
|
| 261 |
+
#define ABSL_ATTRIBUTE_NO_SANITIZE_THREAD
|
| 262 |
+
#endif
|
| 263 |
+
|
| 264 |
+
// ABSL_ATTRIBUTE_NO_SANITIZE_UNDEFINED
|
| 265 |
+
//
|
| 266 |
+
// Tells the UndefinedSanitizer to ignore a given function. Useful for cases
|
| 267 |
+
// where certain behavior (eg. division by zero) is being used intentionally.
|
| 268 |
+
// NOTE: GCC supports UndefinedBehaviorSanitizer(ubsan) since 4.9.
|
| 269 |
+
// https://gcc.gnu.org/gcc-4.9/changes.html
|
| 270 |
+
#if ABSL_HAVE_ATTRIBUTE(no_sanitize_undefined)
|
| 271 |
+
#define ABSL_ATTRIBUTE_NO_SANITIZE_UNDEFINED \
|
| 272 |
+
__attribute__((no_sanitize_undefined))
|
| 273 |
+
#elif ABSL_HAVE_ATTRIBUTE(no_sanitize)
|
| 274 |
+
#define ABSL_ATTRIBUTE_NO_SANITIZE_UNDEFINED \
|
| 275 |
+
__attribute__((no_sanitize("undefined")))
|
| 276 |
+
#else
|
| 277 |
+
#define ABSL_ATTRIBUTE_NO_SANITIZE_UNDEFINED
|
| 278 |
+
#endif
|
| 279 |
+
|
| 280 |
+
// ABSL_ATTRIBUTE_NO_SANITIZE_CFI
|
| 281 |
+
//
|
| 282 |
+
// Tells the ControlFlowIntegrity sanitizer to not instrument a given function.
|
| 283 |
+
// See https://clang.llvm.org/docs/ControlFlowIntegrity.html for details.
|
| 284 |
+
#if ABSL_HAVE_ATTRIBUTE(no_sanitize) && defined(__llvm__)
|
| 285 |
+
#define ABSL_ATTRIBUTE_NO_SANITIZE_CFI __attribute__((no_sanitize("cfi")))
|
| 286 |
+
#else
|
| 287 |
+
#define ABSL_ATTRIBUTE_NO_SANITIZE_CFI
|
| 288 |
+
#endif
|
| 289 |
+
|
| 290 |
+
// ABSL_ATTRIBUTE_NO_SANITIZE_SAFESTACK
|
| 291 |
+
//
|
| 292 |
+
// Tells the SafeStack to not instrument a given function.
|
| 293 |
+
// See https://clang.llvm.org/docs/SafeStack.html for details.
|
| 294 |
+
#if ABSL_HAVE_ATTRIBUTE(no_sanitize)
|
| 295 |
+
#define ABSL_ATTRIBUTE_NO_SANITIZE_SAFESTACK \
|
| 296 |
+
__attribute__((no_sanitize("safe-stack")))
|
| 297 |
+
#else
|
| 298 |
+
#define ABSL_ATTRIBUTE_NO_SANITIZE_SAFESTACK
|
| 299 |
+
#endif
|
| 300 |
+
|
| 301 |
+
// ABSL_ATTRIBUTE_RETURNS_NONNULL
|
| 302 |
+
//
|
| 303 |
+
// Tells the compiler that a particular function never returns a null pointer.
|
| 304 |
+
#if ABSL_HAVE_ATTRIBUTE(returns_nonnull)
|
| 305 |
+
#define ABSL_ATTRIBUTE_RETURNS_NONNULL __attribute__((returns_nonnull))
|
| 306 |
+
#else
|
| 307 |
+
#define ABSL_ATTRIBUTE_RETURNS_NONNULL
|
| 308 |
+
#endif
|
| 309 |
+
|
| 310 |
+
// ABSL_HAVE_ATTRIBUTE_SECTION
|
| 311 |
+
//
|
| 312 |
+
// Indicates whether labeled sections are supported. Weak symbol support is
|
| 313 |
+
// a prerequisite. Labeled sections are not supported on Darwin/iOS.
|
| 314 |
+
#ifdef ABSL_HAVE_ATTRIBUTE_SECTION
|
| 315 |
+
#error ABSL_HAVE_ATTRIBUTE_SECTION cannot be directly set
|
| 316 |
+
#elif (ABSL_HAVE_ATTRIBUTE(section) || \
|
| 317 |
+
(defined(__GNUC__) && !defined(__clang__))) && \
|
| 318 |
+
!defined(__APPLE__) && ABSL_HAVE_ATTRIBUTE_WEAK
|
| 319 |
+
#define ABSL_HAVE_ATTRIBUTE_SECTION 1
|
| 320 |
+
|
| 321 |
+
// ABSL_ATTRIBUTE_SECTION
|
| 322 |
+
//
|
| 323 |
+
// Tells the compiler/linker to put a given function into a section and define
|
| 324 |
+
// `__start_ ## name` and `__stop_ ## name` symbols to bracket the section.
|
| 325 |
+
// This functionality is supported by GNU linker. Any function annotated with
|
| 326 |
+
// `ABSL_ATTRIBUTE_SECTION` must not be inlined, or it will be placed into
|
| 327 |
+
// whatever section its caller is placed into.
|
| 328 |
+
//
|
| 329 |
+
#ifndef ABSL_ATTRIBUTE_SECTION
|
| 330 |
+
#define ABSL_ATTRIBUTE_SECTION(name) \
|
| 331 |
+
__attribute__((section(#name))) __attribute__((noinline))
|
| 332 |
+
#endif
|
| 333 |
+
|
| 334 |
+
// ABSL_ATTRIBUTE_SECTION_VARIABLE
|
| 335 |
+
//
|
| 336 |
+
// Tells the compiler/linker to put a given variable into a section and define
|
| 337 |
+
// `__start_ ## name` and `__stop_ ## name` symbols to bracket the section.
|
| 338 |
+
// This functionality is supported by GNU linker.
|
| 339 |
+
#ifndef ABSL_ATTRIBUTE_SECTION_VARIABLE
|
| 340 |
+
#ifdef _AIX
|
| 341 |
+
// __attribute__((section(#name))) on AIX is achieved by using the `.csect`
|
| 342 |
+
// psudo op which includes an additional integer as part of its syntax indcating
|
| 343 |
+
// alignment. If data fall under different alignments then you might get a
|
| 344 |
+
// compilation error indicating a `Section type conflict`.
|
| 345 |
+
#define ABSL_ATTRIBUTE_SECTION_VARIABLE(name)
|
| 346 |
+
#else
|
| 347 |
+
#define ABSL_ATTRIBUTE_SECTION_VARIABLE(name) __attribute__((section(#name)))
|
| 348 |
+
#endif
|
| 349 |
+
#endif
|
| 350 |
+
|
| 351 |
+
// ABSL_DECLARE_ATTRIBUTE_SECTION_VARS
|
| 352 |
+
//
|
| 353 |
+
// A weak section declaration to be used as a global declaration
|
| 354 |
+
// for ABSL_ATTRIBUTE_SECTION_START|STOP(name) to compile and link
|
| 355 |
+
// even without functions with ABSL_ATTRIBUTE_SECTION(name).
|
| 356 |
+
// ABSL_DEFINE_ATTRIBUTE_SECTION should be in the exactly one file; it's
|
| 357 |
+
// a no-op on ELF but not on Mach-O.
|
| 358 |
+
//
|
| 359 |
+
#ifndef ABSL_DECLARE_ATTRIBUTE_SECTION_VARS
|
| 360 |
+
#define ABSL_DECLARE_ATTRIBUTE_SECTION_VARS(name) \
|
| 361 |
+
extern char __start_##name[] ABSL_ATTRIBUTE_WEAK; \
|
| 362 |
+
extern char __stop_##name[] ABSL_ATTRIBUTE_WEAK
|
| 363 |
+
#endif
|
| 364 |
+
#ifndef ABSL_DEFINE_ATTRIBUTE_SECTION_VARS
|
| 365 |
+
#define ABSL_INIT_ATTRIBUTE_SECTION_VARS(name)
|
| 366 |
+
#define ABSL_DEFINE_ATTRIBUTE_SECTION_VARS(name)
|
| 367 |
+
#endif
|
| 368 |
+
|
| 369 |
+
// ABSL_ATTRIBUTE_SECTION_START
|
| 370 |
+
//
|
| 371 |
+
// Returns `void*` pointers to start/end of a section of code with
|
| 372 |
+
// functions having ABSL_ATTRIBUTE_SECTION(name).
|
| 373 |
+
// Returns 0 if no such functions exist.
|
| 374 |
+
// One must ABSL_DECLARE_ATTRIBUTE_SECTION_VARS(name) for this to compile and
|
| 375 |
+
// link.
|
| 376 |
+
//
|
| 377 |
+
#define ABSL_ATTRIBUTE_SECTION_START(name) \
|
| 378 |
+
(reinterpret_cast<void *>(__start_##name))
|
| 379 |
+
#define ABSL_ATTRIBUTE_SECTION_STOP(name) \
|
| 380 |
+
(reinterpret_cast<void *>(__stop_##name))
|
| 381 |
+
|
| 382 |
+
#else // !ABSL_HAVE_ATTRIBUTE_SECTION
|
| 383 |
+
|
| 384 |
+
#define ABSL_HAVE_ATTRIBUTE_SECTION 0
|
| 385 |
+
|
| 386 |
+
// provide dummy definitions
|
| 387 |
+
#define ABSL_ATTRIBUTE_SECTION(name)
|
| 388 |
+
#define ABSL_ATTRIBUTE_SECTION_VARIABLE(name)
|
| 389 |
+
#define ABSL_INIT_ATTRIBUTE_SECTION_VARS(name)
|
| 390 |
+
#define ABSL_DEFINE_ATTRIBUTE_SECTION_VARS(name)
|
| 391 |
+
#define ABSL_DECLARE_ATTRIBUTE_SECTION_VARS(name)
|
| 392 |
+
#define ABSL_ATTRIBUTE_SECTION_START(name) (reinterpret_cast<void *>(0))
|
| 393 |
+
#define ABSL_ATTRIBUTE_SECTION_STOP(name) (reinterpret_cast<void *>(0))
|
| 394 |
+
|
| 395 |
+
#endif // ABSL_ATTRIBUTE_SECTION
|
| 396 |
+
|
| 397 |
+
// ABSL_ATTRIBUTE_STACK_ALIGN_FOR_OLD_LIBC
|
| 398 |
+
//
|
| 399 |
+
// Support for aligning the stack on 32-bit x86.
|
| 400 |
+
#if ABSL_HAVE_ATTRIBUTE(force_align_arg_pointer) || \
|
| 401 |
+
(defined(__GNUC__) && !defined(__clang__))
|
| 402 |
+
#if defined(__i386__)
|
| 403 |
+
#define ABSL_ATTRIBUTE_STACK_ALIGN_FOR_OLD_LIBC \
|
| 404 |
+
__attribute__((force_align_arg_pointer))
|
| 405 |
+
#define ABSL_REQUIRE_STACK_ALIGN_TRAMPOLINE (0)
|
| 406 |
+
#elif defined(__x86_64__)
|
| 407 |
+
#define ABSL_REQUIRE_STACK_ALIGN_TRAMPOLINE (1)
|
| 408 |
+
#define ABSL_ATTRIBUTE_STACK_ALIGN_FOR_OLD_LIBC
|
| 409 |
+
#else // !__i386__ && !__x86_64
|
| 410 |
+
#define ABSL_REQUIRE_STACK_ALIGN_TRAMPOLINE (0)
|
| 411 |
+
#define ABSL_ATTRIBUTE_STACK_ALIGN_FOR_OLD_LIBC
|
| 412 |
+
#endif // __i386__
|
| 413 |
+
#else
|
| 414 |
+
#define ABSL_ATTRIBUTE_STACK_ALIGN_FOR_OLD_LIBC
|
| 415 |
+
#define ABSL_REQUIRE_STACK_ALIGN_TRAMPOLINE (0)
|
| 416 |
+
#endif
|
| 417 |
+
|
| 418 |
+
// ABSL_MUST_USE_RESULT
|
| 419 |
+
//
|
| 420 |
+
// Tells the compiler to warn about unused results.
|
| 421 |
+
//
|
| 422 |
+
// For code or headers that are assured to only build with C++17 and up, prefer
|
| 423 |
+
// just using the standard `[[nodiscard]]` directly over this macro.
|
| 424 |
+
//
|
| 425 |
+
// When annotating a function, it must appear as the first part of the
|
| 426 |
+
// declaration or definition. The compiler will warn if the return value from
|
| 427 |
+
// such a function is unused:
|
| 428 |
+
//
|
| 429 |
+
// ABSL_MUST_USE_RESULT Sprocket* AllocateSprocket();
|
| 430 |
+
// AllocateSprocket(); // Triggers a warning.
|
| 431 |
+
//
|
| 432 |
+
// When annotating a class, it is equivalent to annotating every function which
|
| 433 |
+
// returns an instance.
|
| 434 |
+
//
|
| 435 |
+
// class ABSL_MUST_USE_RESULT Sprocket {};
|
| 436 |
+
// Sprocket(); // Triggers a warning.
|
| 437 |
+
//
|
| 438 |
+
// Sprocket MakeSprocket();
|
| 439 |
+
// MakeSprocket(); // Triggers a warning.
|
| 440 |
+
//
|
| 441 |
+
// Note that references and pointers are not instances:
|
| 442 |
+
//
|
| 443 |
+
// Sprocket* SprocketPointer();
|
| 444 |
+
// SprocketPointer(); // Does *not* trigger a warning.
|
| 445 |
+
//
|
| 446 |
+
// ABSL_MUST_USE_RESULT allows using cast-to-void to suppress the unused result
|
| 447 |
+
// warning. For that, warn_unused_result is used only for clang but not for gcc.
|
| 448 |
+
// https://gcc.gnu.org/bugzilla/show_bug.cgi?id=66425
|
| 449 |
+
//
|
| 450 |
+
// Note: past advice was to place the macro after the argument list.
|
| 451 |
+
//
|
| 452 |
+
// TODO(b/176172494): Use ABSL_HAVE_CPP_ATTRIBUTE(nodiscard) when all code is
|
| 453 |
+
// compliant with the stricter [[nodiscard]].
|
| 454 |
+
#if defined(__clang__) && ABSL_HAVE_ATTRIBUTE(warn_unused_result)
|
| 455 |
+
#define ABSL_MUST_USE_RESULT __attribute__((warn_unused_result))
|
| 456 |
+
#else
|
| 457 |
+
#define ABSL_MUST_USE_RESULT
|
| 458 |
+
#endif
|
| 459 |
+
|
| 460 |
+
// ABSL_ATTRIBUTE_HOT, ABSL_ATTRIBUTE_COLD
|
| 461 |
+
//
|
| 462 |
+
// Tells GCC that a function is hot or cold. GCC can use this information to
|
| 463 |
+
// improve static analysis, i.e. a conditional branch to a cold function
|
| 464 |
+
// is likely to be not-taken.
|
| 465 |
+
// This annotation is used for function declarations.
|
| 466 |
+
//
|
| 467 |
+
// Example:
|
| 468 |
+
//
|
| 469 |
+
// int foo() ABSL_ATTRIBUTE_HOT;
|
| 470 |
+
#if ABSL_HAVE_ATTRIBUTE(hot) || (defined(__GNUC__) && !defined(__clang__))
|
| 471 |
+
#define ABSL_ATTRIBUTE_HOT __attribute__((hot))
|
| 472 |
+
#else
|
| 473 |
+
#define ABSL_ATTRIBUTE_HOT
|
| 474 |
+
#endif
|
| 475 |
+
|
| 476 |
+
#if ABSL_HAVE_ATTRIBUTE(cold) || (defined(__GNUC__) && !defined(__clang__))
|
| 477 |
+
#define ABSL_ATTRIBUTE_COLD __attribute__((cold))
|
| 478 |
+
#else
|
| 479 |
+
#define ABSL_ATTRIBUTE_COLD
|
| 480 |
+
#endif
|
| 481 |
+
|
| 482 |
+
// ABSL_XRAY_ALWAYS_INSTRUMENT, ABSL_XRAY_NEVER_INSTRUMENT, ABSL_XRAY_LOG_ARGS
|
| 483 |
+
//
|
| 484 |
+
// We define the ABSL_XRAY_ALWAYS_INSTRUMENT and ABSL_XRAY_NEVER_INSTRUMENT
|
| 485 |
+
// macro used as an attribute to mark functions that must always or never be
|
| 486 |
+
// instrumented by XRay. Currently, this is only supported in Clang/LLVM.
|
| 487 |
+
//
|
| 488 |
+
// For reference on the LLVM XRay instrumentation, see
|
| 489 |
+
// http://llvm.org/docs/XRay.html.
|
| 490 |
+
//
|
| 491 |
+
// A function with the XRAY_ALWAYS_INSTRUMENT macro attribute in its declaration
|
| 492 |
+
// will always get the XRay instrumentation sleds. These sleds may introduce
|
| 493 |
+
// some binary size and runtime overhead and must be used sparingly.
|
| 494 |
+
//
|
| 495 |
+
// These attributes only take effect when the following conditions are met:
|
| 496 |
+
//
|
| 497 |
+
// * The file/target is built in at least C++11 mode, with a Clang compiler
|
| 498 |
+
// that supports XRay attributes.
|
| 499 |
+
// * The file/target is built with the -fxray-instrument flag set for the
|
| 500 |
+
// Clang/LLVM compiler.
|
| 501 |
+
// * The function is defined in the translation unit (the compiler honors the
|
| 502 |
+
// attribute in either the definition or the declaration, and must match).
|
| 503 |
+
//
|
| 504 |
+
// There are cases when, even when building with XRay instrumentation, users
|
| 505 |
+
// might want to control specifically which functions are instrumented for a
|
| 506 |
+
// particular build using special-case lists provided to the compiler. These
|
| 507 |
+
// special case lists are provided to Clang via the
|
| 508 |
+
// -fxray-always-instrument=... and -fxray-never-instrument=... flags. The
|
| 509 |
+
// attributes in source take precedence over these special-case lists.
|
| 510 |
+
//
|
| 511 |
+
// To disable the XRay attributes at build-time, users may define
|
| 512 |
+
// ABSL_NO_XRAY_ATTRIBUTES. Do NOT define ABSL_NO_XRAY_ATTRIBUTES on specific
|
| 513 |
+
// packages/targets, as this may lead to conflicting definitions of functions at
|
| 514 |
+
// link-time.
|
| 515 |
+
//
|
| 516 |
+
// XRay isn't currently supported on Android:
|
| 517 |
+
// https://github.com/android/ndk/issues/368
|
| 518 |
+
#if ABSL_HAVE_CPP_ATTRIBUTE(clang::xray_always_instrument) && \
|
| 519 |
+
!defined(ABSL_NO_XRAY_ATTRIBUTES) && !defined(__ANDROID__)
|
| 520 |
+
#define ABSL_XRAY_ALWAYS_INSTRUMENT [[clang::xray_always_instrument]]
|
| 521 |
+
#define ABSL_XRAY_NEVER_INSTRUMENT [[clang::xray_never_instrument]]
|
| 522 |
+
#if ABSL_HAVE_CPP_ATTRIBUTE(clang::xray_log_args)
|
| 523 |
+
#define ABSL_XRAY_LOG_ARGS(N) \
|
| 524 |
+
[[clang::xray_always_instrument, clang::xray_log_args(N)]]
|
| 525 |
+
#else
|
| 526 |
+
#define ABSL_XRAY_LOG_ARGS(N) [[clang::xray_always_instrument]]
|
| 527 |
+
#endif
|
| 528 |
+
#else
|
| 529 |
+
#define ABSL_XRAY_ALWAYS_INSTRUMENT
|
| 530 |
+
#define ABSL_XRAY_NEVER_INSTRUMENT
|
| 531 |
+
#define ABSL_XRAY_LOG_ARGS(N)
|
| 532 |
+
#endif
|
| 533 |
+
|
| 534 |
+
// ABSL_ATTRIBUTE_REINITIALIZES
|
| 535 |
+
//
|
| 536 |
+
// Indicates that a member function reinitializes the entire object to a known
|
| 537 |
+
// state, independent of the previous state of the object.
|
| 538 |
+
//
|
| 539 |
+
// The clang-tidy check bugprone-use-after-move allows member functions marked
|
| 540 |
+
// with this attribute to be called on objects that have been moved from;
|
| 541 |
+
// without the attribute, this would result in a use-after-move warning.
|
| 542 |
+
#if ABSL_HAVE_CPP_ATTRIBUTE(clang::reinitializes)
|
| 543 |
+
#define ABSL_ATTRIBUTE_REINITIALIZES [[clang::reinitializes]]
|
| 544 |
+
#else
|
| 545 |
+
#define ABSL_ATTRIBUTE_REINITIALIZES
|
| 546 |
+
#endif
|
| 547 |
+
|
| 548 |
+
// -----------------------------------------------------------------------------
|
| 549 |
+
// Variable Attributes
|
| 550 |
+
// -----------------------------------------------------------------------------
|
| 551 |
+
|
| 552 |
+
// ABSL_ATTRIBUTE_UNUSED
|
| 553 |
+
//
|
| 554 |
+
// Prevents the compiler from complaining about variables that appear unused.
|
| 555 |
+
//
|
| 556 |
+
// For code or headers that are assured to only build with C++17 and up, prefer
|
| 557 |
+
// just using the standard '[[maybe_unused]]' directly over this macro.
|
| 558 |
+
//
|
| 559 |
+
// Due to differences in positioning requirements between the old, compiler
|
| 560 |
+
// specific __attribute__ syntax and the now standard [[maybe_unused]], this
|
| 561 |
+
// macro does not attempt to take advantage of '[[maybe_unused]]'.
|
| 562 |
+
#if ABSL_HAVE_ATTRIBUTE(unused) || (defined(__GNUC__) && !defined(__clang__))
|
| 563 |
+
#undef ABSL_ATTRIBUTE_UNUSED
|
| 564 |
+
#define ABSL_ATTRIBUTE_UNUSED __attribute__((__unused__))
|
| 565 |
+
#else
|
| 566 |
+
#define ABSL_ATTRIBUTE_UNUSED
|
| 567 |
+
#endif
|
| 568 |
+
|
| 569 |
+
// ABSL_ATTRIBUTE_INITIAL_EXEC
|
| 570 |
+
//
|
| 571 |
+
// Tells the compiler to use "initial-exec" mode for a thread-local variable.
|
| 572 |
+
// See http://people.redhat.com/drepper/tls.pdf for the gory details.
|
| 573 |
+
#if ABSL_HAVE_ATTRIBUTE(tls_model) || (defined(__GNUC__) && !defined(__clang__))
|
| 574 |
+
#define ABSL_ATTRIBUTE_INITIAL_EXEC __attribute__((tls_model("initial-exec")))
|
| 575 |
+
#else
|
| 576 |
+
#define ABSL_ATTRIBUTE_INITIAL_EXEC
|
| 577 |
+
#endif
|
| 578 |
+
|
| 579 |
+
// ABSL_ATTRIBUTE_PACKED
|
| 580 |
+
//
|
| 581 |
+
// Instructs the compiler not to use natural alignment for a tagged data
|
| 582 |
+
// structure, but instead to reduce its alignment to 1.
|
| 583 |
+
//
|
| 584 |
+
// Therefore, DO NOT APPLY THIS ATTRIBUTE TO STRUCTS CONTAINING ATOMICS. Doing
|
| 585 |
+
// so can cause atomic variables to be mis-aligned and silently violate
|
| 586 |
+
// atomicity on x86.
|
| 587 |
+
//
|
| 588 |
+
// This attribute can either be applied to members of a structure or to a
|
| 589 |
+
// structure in its entirety. Applying this attribute (judiciously) to a
|
| 590 |
+
// structure in its entirety to optimize the memory footprint of very
|
| 591 |
+
// commonly-used structs is fine. Do not apply this attribute to a structure in
|
| 592 |
+
// its entirety if the purpose is to control the offsets of the members in the
|
| 593 |
+
// structure. Instead, apply this attribute only to structure members that need
|
| 594 |
+
// it.
|
| 595 |
+
//
|
| 596 |
+
// When applying ABSL_ATTRIBUTE_PACKED only to specific structure members the
|
| 597 |
+
// natural alignment of structure members not annotated is preserved. Aligned
|
| 598 |
+
// member accesses are faster than non-aligned member accesses even if the
|
| 599 |
+
// targeted microprocessor supports non-aligned accesses.
|
| 600 |
+
#if ABSL_HAVE_ATTRIBUTE(packed) || (defined(__GNUC__) && !defined(__clang__))
|
| 601 |
+
#define ABSL_ATTRIBUTE_PACKED __attribute__((__packed__))
|
| 602 |
+
#else
|
| 603 |
+
#define ABSL_ATTRIBUTE_PACKED
|
| 604 |
+
#endif
|
| 605 |
+
|
| 606 |
+
// ABSL_ATTRIBUTE_FUNC_ALIGN
|
| 607 |
+
//
|
| 608 |
+
// Tells the compiler to align the function start at least to certain
|
| 609 |
+
// alignment boundary
|
| 610 |
+
#if ABSL_HAVE_ATTRIBUTE(aligned) || (defined(__GNUC__) && !defined(__clang__))
|
| 611 |
+
#define ABSL_ATTRIBUTE_FUNC_ALIGN(bytes) __attribute__((aligned(bytes)))
|
| 612 |
+
#else
|
| 613 |
+
#define ABSL_ATTRIBUTE_FUNC_ALIGN(bytes)
|
| 614 |
+
#endif
|
| 615 |
+
|
| 616 |
+
// ABSL_FALLTHROUGH_INTENDED
|
| 617 |
+
//
|
| 618 |
+
// Annotates implicit fall-through between switch labels, allowing a case to
|
| 619 |
+
// indicate intentional fallthrough and turn off warnings about any lack of a
|
| 620 |
+
// `break` statement. The ABSL_FALLTHROUGH_INTENDED macro should be followed by
|
| 621 |
+
// a semicolon and can be used in most places where `break` can, provided that
|
| 622 |
+
// no statements exist between it and the next switch label.
|
| 623 |
+
//
|
| 624 |
+
// Example:
|
| 625 |
+
//
|
| 626 |
+
// switch (x) {
|
| 627 |
+
// case 40:
|
| 628 |
+
// case 41:
|
| 629 |
+
// if (truth_is_out_there) {
|
| 630 |
+
// ++x;
|
| 631 |
+
// ABSL_FALLTHROUGH_INTENDED; // Use instead of/along with annotations
|
| 632 |
+
// // in comments
|
| 633 |
+
// } else {
|
| 634 |
+
// return x;
|
| 635 |
+
// }
|
| 636 |
+
// case 42:
|
| 637 |
+
// ...
|
| 638 |
+
//
|
| 639 |
+
// Notes: When supported, GCC and Clang can issue a warning on switch labels
|
| 640 |
+
// with unannotated fallthrough using the warning `-Wimplicit-fallthrough`. See
|
| 641 |
+
// clang documentation on language extensions for details:
|
| 642 |
+
// https://clang.llvm.org/docs/AttributeReference.html#fallthrough-clang-fallthrough
|
| 643 |
+
//
|
| 644 |
+
// When used with unsupported compilers, the ABSL_FALLTHROUGH_INTENDED macro has
|
| 645 |
+
// no effect on diagnostics. In any case this macro has no effect on runtime
|
| 646 |
+
// behavior and performance of code.
|
| 647 |
+
|
| 648 |
+
#ifdef ABSL_FALLTHROUGH_INTENDED
|
| 649 |
+
#error "ABSL_FALLTHROUGH_INTENDED should not be defined."
|
| 650 |
+
#elif ABSL_HAVE_CPP_ATTRIBUTE(fallthrough)
|
| 651 |
+
#define ABSL_FALLTHROUGH_INTENDED [[fallthrough]]
|
| 652 |
+
#elif ABSL_HAVE_CPP_ATTRIBUTE(clang::fallthrough)
|
| 653 |
+
#define ABSL_FALLTHROUGH_INTENDED [[clang::fallthrough]]
|
| 654 |
+
#elif ABSL_HAVE_CPP_ATTRIBUTE(gnu::fallthrough)
|
| 655 |
+
#define ABSL_FALLTHROUGH_INTENDED [[gnu::fallthrough]]
|
| 656 |
+
#else
|
| 657 |
+
#define ABSL_FALLTHROUGH_INTENDED \
|
| 658 |
+
do { \
|
| 659 |
+
} while (0)
|
| 660 |
+
#endif
|
| 661 |
+
|
| 662 |
+
// ABSL_DEPRECATED()
|
| 663 |
+
//
|
| 664 |
+
// Marks a deprecated class, struct, enum, function, method and variable
|
| 665 |
+
// declarations. The macro argument is used as a custom diagnostic message (e.g.
|
| 666 |
+
// suggestion of a better alternative).
|
| 667 |
+
//
|
| 668 |
+
// For code or headers that are assured to only build with C++14 and up, prefer
|
| 669 |
+
// just using the standard `[[deprecated("message")]]` directly over this macro.
|
| 670 |
+
//
|
| 671 |
+
// Examples:
|
| 672 |
+
//
|
| 673 |
+
// class ABSL_DEPRECATED("Use Bar instead") Foo {...};
|
| 674 |
+
//
|
| 675 |
+
// ABSL_DEPRECATED("Use Baz() instead") void Bar() {...}
|
| 676 |
+
//
|
| 677 |
+
// template <typename T>
|
| 678 |
+
// ABSL_DEPRECATED("Use DoThat() instead")
|
| 679 |
+
// void DoThis();
|
| 680 |
+
//
|
| 681 |
+
// enum FooEnum {
|
| 682 |
+
// kBar ABSL_DEPRECATED("Use kBaz instead"),
|
| 683 |
+
// };
|
| 684 |
+
//
|
| 685 |
+
// Every usage of a deprecated entity will trigger a warning when compiled with
|
| 686 |
+
// GCC/Clang's `-Wdeprecated-declarations` option. Google's production toolchain
|
| 687 |
+
// turns this warning off by default, instead relying on clang-tidy to report
|
| 688 |
+
// new uses of deprecated code.
|
| 689 |
+
#if ABSL_HAVE_ATTRIBUTE(deprecated)
|
| 690 |
+
#define ABSL_DEPRECATED(message) __attribute__((deprecated(message)))
|
| 691 |
+
#else
|
| 692 |
+
#define ABSL_DEPRECATED(message)
|
| 693 |
+
#endif
|
| 694 |
+
|
| 695 |
+
// When deprecating Abseil code, it is sometimes necessary to turn off the
|
| 696 |
+
// warning within Abseil, until the deprecated code is actually removed. The
|
| 697 |
+
// deprecated code can be surrounded with these directives to achieve that
|
| 698 |
+
// result.
|
| 699 |
+
//
|
| 700 |
+
// class ABSL_DEPRECATED("Use Bar instead") Foo;
|
| 701 |
+
//
|
| 702 |
+
// ABSL_INTERNAL_DISABLE_DEPRECATED_DECLARATION_WARNING
|
| 703 |
+
// Baz ComputeBazFromFoo(Foo f);
|
| 704 |
+
// ABSL_INTERNAL_RESTORE_DEPRECATED_DECLARATION_WARNING
|
| 705 |
+
#if defined(__GNUC__) || defined(__clang__)
|
| 706 |
+
// Clang also supports these GCC pragmas.
|
| 707 |
+
#define ABSL_INTERNAL_DISABLE_DEPRECATED_DECLARATION_WARNING \
|
| 708 |
+
_Pragma("GCC diagnostic push") \
|
| 709 |
+
_Pragma("GCC diagnostic ignored \"-Wdeprecated-declarations\"")
|
| 710 |
+
#define ABSL_INTERNAL_RESTORE_DEPRECATED_DECLARATION_WARNING \
|
| 711 |
+
_Pragma("GCC diagnostic pop")
|
| 712 |
+
#elif defined(_MSC_VER)
|
| 713 |
+
#define ABSL_INTERNAL_DISABLE_DEPRECATED_DECLARATION_WARNING \
|
| 714 |
+
_Pragma("warning(push)") _Pragma("warning(disable: 4996)")
|
| 715 |
+
#define ABSL_INTERNAL_RESTORE_DEPRECATED_DECLARATION_WARNING \
|
| 716 |
+
_Pragma("warning(pop)")
|
| 717 |
+
#else
|
| 718 |
+
#define ABSL_INTERNAL_DISABLE_DEPRECATED_DECLARATION_WARNING
|
| 719 |
+
#define ABSL_INTERNAL_RESTORE_DEPRECATED_DECLARATION_WARNING
|
| 720 |
+
#endif // defined(__GNUC__) || defined(__clang__)
|
| 721 |
+
|
| 722 |
+
// ABSL_CONST_INIT
|
| 723 |
+
//
|
| 724 |
+
// A variable declaration annotated with the `ABSL_CONST_INIT` attribute will
|
| 725 |
+
// not compile (on supported platforms) unless the variable has a constant
|
| 726 |
+
// initializer. This is useful for variables with static and thread storage
|
| 727 |
+
// duration, because it guarantees that they will not suffer from the so-called
|
| 728 |
+
// "static init order fiasco".
|
| 729 |
+
//
|
| 730 |
+
// This attribute must be placed on the initializing declaration of the
|
| 731 |
+
// variable. Some compilers will give a -Wmissing-constinit warning when this
|
| 732 |
+
// attribute is placed on some other declaration but missing from the
|
| 733 |
+
// initializing declaration.
|
| 734 |
+
//
|
| 735 |
+
// In some cases (notably with thread_local variables), `ABSL_CONST_INIT` can
|
| 736 |
+
// also be used in a non-initializing declaration to tell the compiler that a
|
| 737 |
+
// variable is already initialized, reducing overhead that would otherwise be
|
| 738 |
+
// incurred by a hidden guard variable. Thus annotating all declarations with
|
| 739 |
+
// this attribute is recommended to potentially enhance optimization.
|
| 740 |
+
//
|
| 741 |
+
// Example:
|
| 742 |
+
//
|
| 743 |
+
// class MyClass {
|
| 744 |
+
// public:
|
| 745 |
+
// ABSL_CONST_INIT static MyType my_var;
|
| 746 |
+
// };
|
| 747 |
+
//
|
| 748 |
+
// ABSL_CONST_INIT MyType MyClass::my_var = MakeMyType(...);
|
| 749 |
+
//
|
| 750 |
+
// For code or headers that are assured to only build with C++20 and up, prefer
|
| 751 |
+
// just using the standard `constinit` keyword directly over this macro.
|
| 752 |
+
//
|
| 753 |
+
// Note that this attribute is redundant if the variable is declared constexpr.
|
| 754 |
+
#if defined(__cpp_constinit) && __cpp_constinit >= 201907L
|
| 755 |
+
#define ABSL_CONST_INIT constinit
|
| 756 |
+
#elif ABSL_HAVE_CPP_ATTRIBUTE(clang::require_constant_initialization)
|
| 757 |
+
#define ABSL_CONST_INIT [[clang::require_constant_initialization]]
|
| 758 |
+
#else
|
| 759 |
+
#define ABSL_CONST_INIT
|
| 760 |
+
#endif
|
| 761 |
+
|
| 762 |
+
// ABSL_ATTRIBUTE_PURE_FUNCTION
|
| 763 |
+
//
|
| 764 |
+
// ABSL_ATTRIBUTE_PURE_FUNCTION is used to annotate declarations of "pure"
|
| 765 |
+
// functions. A function is pure if its return value is only a function of its
|
| 766 |
+
// arguments. The pure attribute prohibits a function from modifying the state
|
| 767 |
+
// of the program that is observable by means other than inspecting the
|
| 768 |
+
// function's return value. Declaring such functions with the pure attribute
|
| 769 |
+
// allows the compiler to avoid emitting some calls in repeated invocations of
|
| 770 |
+
// the function with the same argument values.
|
| 771 |
+
//
|
| 772 |
+
// Example:
|
| 773 |
+
//
|
| 774 |
+
// ABSL_ATTRIBUTE_PURE_FUNCTION std::string FormatTime(Time t);
|
| 775 |
+
#if ABSL_HAVE_CPP_ATTRIBUTE(gnu::pure)
|
| 776 |
+
#define ABSL_ATTRIBUTE_PURE_FUNCTION [[gnu::pure]]
|
| 777 |
+
#elif ABSL_HAVE_ATTRIBUTE(pure)
|
| 778 |
+
#define ABSL_ATTRIBUTE_PURE_FUNCTION __attribute__((pure))
|
| 779 |
+
#else
|
| 780 |
+
// If the attribute isn't defined, we'll fallback to ABSL_MUST_USE_RESULT since
|
| 781 |
+
// pure functions are useless if its return is ignored.
|
| 782 |
+
#define ABSL_ATTRIBUTE_PURE_FUNCTION ABSL_MUST_USE_RESULT
|
| 783 |
+
#endif
|
| 784 |
+
|
| 785 |
+
// ABSL_ATTRIBUTE_CONST_FUNCTION
|
| 786 |
+
//
|
| 787 |
+
// ABSL_ATTRIBUTE_CONST_FUNCTION is used to annotate declarations of "const"
|
| 788 |
+
// functions. A const function is similar to a pure function, with one
|
| 789 |
+
// exception: Pure functions may return value that depend on a non-volatile
|
| 790 |
+
// object that isn't provided as a function argument, while the const function
|
| 791 |
+
// is guaranteed to return the same result given the same arguments.
|
| 792 |
+
//
|
| 793 |
+
// Example:
|
| 794 |
+
//
|
| 795 |
+
// ABSL_ATTRIBUTE_CONST_FUNCTION int64_t ToInt64Milliseconds(Duration d);
|
| 796 |
+
#if defined(_MSC_VER) && !defined(__clang__)
|
| 797 |
+
// Put the MSVC case first since MSVC seems to parse const as a C++ keyword.
|
| 798 |
+
#define ABSL_ATTRIBUTE_CONST_FUNCTION ABSL_ATTRIBUTE_PURE_FUNCTION
|
| 799 |
+
#elif ABSL_HAVE_CPP_ATTRIBUTE(gnu::const)
|
| 800 |
+
#define ABSL_ATTRIBUTE_CONST_FUNCTION [[gnu::const]]
|
| 801 |
+
#elif ABSL_HAVE_ATTRIBUTE(const)
|
| 802 |
+
#define ABSL_ATTRIBUTE_CONST_FUNCTION __attribute__((const))
|
| 803 |
+
#else
|
| 804 |
+
// Since const functions are more restrictive pure function, we'll fallback to a
|
| 805 |
+
// pure function if the const attribute is not handled.
|
| 806 |
+
#define ABSL_ATTRIBUTE_CONST_FUNCTION ABSL_ATTRIBUTE_PURE_FUNCTION
|
| 807 |
+
#endif
|
| 808 |
+
|
| 809 |
+
// ABSL_ATTRIBUTE_LIFETIME_BOUND indicates that a resource owned by a function
|
| 810 |
+
// parameter or implicit object parameter is retained by the return value of the
|
| 811 |
+
// annotated function (or, for a parameter of a constructor, in the value of the
|
| 812 |
+
// constructed object). This attribute causes warnings to be produced if a
|
| 813 |
+
// temporary object does not live long enough.
|
| 814 |
+
//
|
| 815 |
+
// When applied to a reference parameter, the referenced object is assumed to be
|
| 816 |
+
// retained by the return value of the function. When applied to a non-reference
|
| 817 |
+
// parameter (for example, a pointer or a class type), all temporaries
|
| 818 |
+
// referenced by the parameter are assumed to be retained by the return value of
|
| 819 |
+
// the function.
|
| 820 |
+
//
|
| 821 |
+
// See also the upstream documentation:
|
| 822 |
+
// https://clang.llvm.org/docs/AttributeReference.html#lifetimebound
|
| 823 |
+
// https://learn.microsoft.com/en-us/cpp/code-quality/c26816?view=msvc-170
|
| 824 |
+
#if ABSL_HAVE_CPP_ATTRIBUTE(clang::lifetimebound)
|
| 825 |
+
#define ABSL_ATTRIBUTE_LIFETIME_BOUND [[clang::lifetimebound]]
|
| 826 |
+
#elif ABSL_HAVE_CPP_ATTRIBUTE(msvc::lifetimebound)
|
| 827 |
+
#define ABSL_ATTRIBUTE_LIFETIME_BOUND [[msvc::lifetimebound]]
|
| 828 |
+
#elif ABSL_HAVE_ATTRIBUTE(lifetimebound)
|
| 829 |
+
#define ABSL_ATTRIBUTE_LIFETIME_BOUND __attribute__((lifetimebound))
|
| 830 |
+
#else
|
| 831 |
+
#define ABSL_ATTRIBUTE_LIFETIME_BOUND
|
| 832 |
+
#endif
|
| 833 |
+
|
| 834 |
+
// Internal attribute; name and documentation TBD.
|
| 835 |
+
//
|
| 836 |
+
// See the upstream documentation:
|
| 837 |
+
// https://clang.llvm.org/docs/AttributeReference.html#lifetime_capture_by
|
| 838 |
+
#if ABSL_HAVE_CPP_ATTRIBUTE(clang::lifetime_capture_by)
|
| 839 |
+
#define ABSL_INTERNAL_ATTRIBUTE_CAPTURED_BY(Owner) \
|
| 840 |
+
[[clang::lifetime_capture_by(Owner)]]
|
| 841 |
+
#else
|
| 842 |
+
#define ABSL_INTERNAL_ATTRIBUTE_CAPTURED_BY(Owner)
|
| 843 |
+
#endif
|
| 844 |
+
|
| 845 |
+
// ABSL_ATTRIBUTE_VIEW indicates that a type is solely a "view" of data that it
|
| 846 |
+
// points to, similarly to a span, string_view, or other non-owning reference
|
| 847 |
+
// type.
|
| 848 |
+
// This enables diagnosing certain lifetime issues similar to those enabled by
|
| 849 |
+
// ABSL_ATTRIBUTE_LIFETIME_BOUND, such as:
|
| 850 |
+
//
|
| 851 |
+
// struct ABSL_ATTRIBUTE_VIEW StringView {
|
| 852 |
+
// template<class R>
|
| 853 |
+
// StringView(const R&);
|
| 854 |
+
// };
|
| 855 |
+
//
|
| 856 |
+
// StringView f(std::string s) {
|
| 857 |
+
// return s; // warning: address of stack memory returned
|
| 858 |
+
// }
|
| 859 |
+
//
|
| 860 |
+
// We disable this on Clang versions < 13 because of the following
|
| 861 |
+
// false-positive:
|
| 862 |
+
//
|
| 863 |
+
// absl::string_view f(absl::optional<absl::string_view> sv) { return *sv; }
|
| 864 |
+
//
|
| 865 |
+
// See the following links for details:
|
| 866 |
+
// https://reviews.llvm.org/D64448
|
| 867 |
+
// https://lists.llvm.org/pipermail/cfe-dev/2018-November/060355.html
|
| 868 |
+
#if ABSL_HAVE_CPP_ATTRIBUTE(gsl::Pointer) && \
|
| 869 |
+
(!defined(__clang_major__) || __clang_major__ >= 13)
|
| 870 |
+
#define ABSL_ATTRIBUTE_VIEW [[gsl::Pointer]]
|
| 871 |
+
#else
|
| 872 |
+
#define ABSL_ATTRIBUTE_VIEW
|
| 873 |
+
#endif
|
| 874 |
+
|
| 875 |
+
// ABSL_ATTRIBUTE_OWNER indicates that a type is a container, smart pointer, or
|
| 876 |
+
// similar class that owns all the data that it points to.
|
| 877 |
+
// This enables diagnosing certain lifetime issues similar to those enabled by
|
| 878 |
+
// ABSL_ATTRIBUTE_LIFETIME_BOUND, such as:
|
| 879 |
+
//
|
| 880 |
+
// struct ABSL_ATTRIBUTE_VIEW StringView {
|
| 881 |
+
// template<class R>
|
| 882 |
+
// StringView(const R&);
|
| 883 |
+
// };
|
| 884 |
+
//
|
| 885 |
+
// struct ABSL_ATTRIBUTE_OWNER String {};
|
| 886 |
+
//
|
| 887 |
+
// StringView f(String s) {
|
| 888 |
+
// return s; // warning: address of stack memory returned
|
| 889 |
+
// }
|
| 890 |
+
//
|
| 891 |
+
// We disable this on Clang versions < 13 because of the following
|
| 892 |
+
// false-positive:
|
| 893 |
+
//
|
| 894 |
+
// absl::string_view f(absl::optional<absl::string_view> sv) { return *sv; }
|
| 895 |
+
//
|
| 896 |
+
// See the following links for details:
|
| 897 |
+
// https://reviews.llvm.org/D64448
|
| 898 |
+
// https://lists.llvm.org/pipermail/cfe-dev/2018-November/060355.html
|
| 899 |
+
#if ABSL_HAVE_CPP_ATTRIBUTE(gsl::Owner) && \
|
| 900 |
+
(!defined(__clang_major__) || __clang_major__ >= 13)
|
| 901 |
+
#define ABSL_ATTRIBUTE_OWNER [[gsl::Owner]]
|
| 902 |
+
#else
|
| 903 |
+
#define ABSL_ATTRIBUTE_OWNER
|
| 904 |
+
#endif
|
| 905 |
+
|
| 906 |
+
// ABSL_ATTRIBUTE_TRIVIAL_ABI
|
| 907 |
+
// Indicates that a type is "trivially relocatable" -- meaning it can be
|
| 908 |
+
// relocated without invoking the constructor/destructor, using a form of move
|
| 909 |
+
// elision.
|
| 910 |
+
//
|
| 911 |
+
// From a memory safety point of view, putting aside destructor ordering, it's
|
| 912 |
+
// safe to apply ABSL_ATTRIBUTE_TRIVIAL_ABI if an object's location
|
| 913 |
+
// can change over the course of its lifetime: if a constructor can be run one
|
| 914 |
+
// place, and then the object magically teleports to another place where some
|
| 915 |
+
// methods are run, and then the object teleports to yet another place where it
|
| 916 |
+
// is destroyed. This is notably not true for self-referential types, where the
|
| 917 |
+
// move-constructor must keep the self-reference up to date. If the type changed
|
| 918 |
+
// location without invoking the move constructor, it would have a dangling
|
| 919 |
+
// self-reference.
|
| 920 |
+
//
|
| 921 |
+
// The use of this teleporting machinery means that the number of paired
|
| 922 |
+
// move/destroy operations can change, and so it is a bad idea to apply this to
|
| 923 |
+
// a type meant to count the number of moves.
|
| 924 |
+
//
|
| 925 |
+
// Warning: applying this can, rarely, break callers. Objects passed by value
|
| 926 |
+
// will be destroyed at the end of the call, instead of the end of the
|
| 927 |
+
// full-expression containing the call. In addition, it changes the ABI
|
| 928 |
+
// of functions accepting this type by value (e.g. to pass in registers).
|
| 929 |
+
//
|
| 930 |
+
// See also the upstream documentation:
|
| 931 |
+
// https://clang.llvm.org/docs/AttributeReference.html#trivial-abi
|
| 932 |
+
//
|
| 933 |
+
// b/321691395 - This is currently disabled in open-source builds since
|
| 934 |
+
// compiler support differs. If system libraries compiled with GCC are mixed
|
| 935 |
+
// with libraries compiled with Clang, types will have different ideas about
|
| 936 |
+
// their ABI, leading to hard to debug crashes.
|
| 937 |
+
#define ABSL_ATTRIBUTE_TRIVIAL_ABI
|
| 938 |
+
|
| 939 |
+
// ABSL_ATTRIBUTE_NO_UNIQUE_ADDRESS
|
| 940 |
+
//
|
| 941 |
+
// Indicates a data member can be optimized to occupy no space (if it is empty)
|
| 942 |
+
// and/or its tail padding can be used for other members.
|
| 943 |
+
//
|
| 944 |
+
// For code that is assured to only build with C++20 or later, prefer using
|
| 945 |
+
// the standard attribute `[[no_unique_address]]` directly instead of this
|
| 946 |
+
// macro.
|
| 947 |
+
//
|
| 948 |
+
// https://devblogs.microsoft.com/cppblog/msvc-cpp20-and-the-std-cpp20-switch/#c20-no_unique_address
|
| 949 |
+
// Current versions of MSVC have disabled `[[no_unique_address]]` since it
|
| 950 |
+
// breaks ABI compatibility, but offers `[[msvc::no_unique_address]]` for
|
| 951 |
+
// situations when it can be assured that it is desired. Since Abseil does not
|
| 952 |
+
// claim ABI compatibility in mixed builds, we can offer it unconditionally.
|
| 953 |
+
#if defined(_MSC_VER) && _MSC_VER >= 1929
|
| 954 |
+
#define ABSL_ATTRIBUTE_NO_UNIQUE_ADDRESS [[msvc::no_unique_address]]
|
| 955 |
+
#elif ABSL_HAVE_CPP_ATTRIBUTE(no_unique_address)
|
| 956 |
+
#define ABSL_ATTRIBUTE_NO_UNIQUE_ADDRESS [[no_unique_address]]
|
| 957 |
+
#else
|
| 958 |
+
#define ABSL_ATTRIBUTE_NO_UNIQUE_ADDRESS
|
| 959 |
+
#endif
|
| 960 |
+
|
| 961 |
+
// ABSL_ATTRIBUTE_UNINITIALIZED
|
| 962 |
+
//
|
| 963 |
+
// GCC and Clang support a flag `-ftrivial-auto-var-init=<option>` (<option>
|
| 964 |
+
// can be "zero" or "pattern") that can be used to initialize automatic stack
|
| 965 |
+
// variables. Variables with this attribute will be left uninitialized,
|
| 966 |
+
// overriding the compiler flag.
|
| 967 |
+
//
|
| 968 |
+
// See https://clang.llvm.org/docs/AttributeReference.html#uninitialized
|
| 969 |
+
// and https://gcc.gnu.org/onlinedocs/gcc/Common-Variable-Attributes.html#index-uninitialized-variable-attribute
|
| 970 |
+
#if ABSL_HAVE_CPP_ATTRIBUTE(clang::uninitialized)
|
| 971 |
+
#define ABSL_ATTRIBUTE_UNINITIALIZED [[clang::uninitialized]]
|
| 972 |
+
#elif ABSL_HAVE_CPP_ATTRIBUTE(gnu::uninitialized)
|
| 973 |
+
#define ABSL_ATTRIBUTE_UNINITIALIZED [[gnu::uninitialized]]
|
| 974 |
+
#elif ABSL_HAVE_ATTRIBUTE(uninitialized)
|
| 975 |
+
#define ABSL_ATTRIBUTE_UNINITIALIZED __attribute__((uninitialized))
|
| 976 |
+
#else
|
| 977 |
+
#define ABSL_ATTRIBUTE_UNINITIALIZED
|
| 978 |
+
#endif
|
| 979 |
+
|
| 980 |
+
// ABSL_ATTRIBUTE_WARN_UNUSED
|
| 981 |
+
//
|
| 982 |
+
// Compilers routinely warn about trivial variables that are unused. For
|
| 983 |
+
// non-trivial types, this warning is suppressed since the
|
| 984 |
+
// constructor/destructor may be intentional and load-bearing, for example, with
|
| 985 |
+
// a RAII scoped lock.
|
| 986 |
+
//
|
| 987 |
+
// For example:
|
| 988 |
+
//
|
| 989 |
+
// class ABSL_ATTRIBUTE_WARN_UNUSED MyType {
|
| 990 |
+
// public:
|
| 991 |
+
// MyType();
|
| 992 |
+
// ~MyType();
|
| 993 |
+
// };
|
| 994 |
+
//
|
| 995 |
+
// void foo() {
|
| 996 |
+
// // Warns with ABSL_ATTRIBUTE_WARN_UNUSED attribute present.
|
| 997 |
+
// MyType unused;
|
| 998 |
+
// }
|
| 999 |
+
//
|
| 1000 |
+
// See https://clang.llvm.org/docs/AttributeReference.html#warn-unused and
|
| 1001 |
+
// https://gcc.gnu.org/onlinedocs/gcc/C_002b_002b-Attributes.html#index-warn_005funused-type-attribute
|
| 1002 |
+
#if ABSL_HAVE_CPP_ATTRIBUTE(gnu::warn_unused)
|
| 1003 |
+
#define ABSL_ATTRIBUTE_WARN_UNUSED [[gnu::warn_unused]]
|
| 1004 |
+
#else
|
| 1005 |
+
#define ABSL_ATTRIBUTE_WARN_UNUSED
|
| 1006 |
+
#endif
|
| 1007 |
+
|
| 1008 |
+
#endif // ABSL_BASE_ATTRIBUTES_H_
|
include/absl/base/call_once.h
ADDED
|
@@ -0,0 +1,228 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
// Copyright 2017 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 |
+
// -----------------------------------------------------------------------------
|
| 16 |
+
// File: call_once.h
|
| 17 |
+
// -----------------------------------------------------------------------------
|
| 18 |
+
//
|
| 19 |
+
// This header file provides an Abseil version of `std::call_once` for invoking
|
| 20 |
+
// a given function at most once, across all threads. This Abseil version is
|
| 21 |
+
// faster than the C++11 version and incorporates the C++17 argument-passing
|
| 22 |
+
// fix, so that (for example) non-const references may be passed to the invoked
|
| 23 |
+
// function.
|
| 24 |
+
|
| 25 |
+
#ifndef ABSL_BASE_CALL_ONCE_H_
|
| 26 |
+
#define ABSL_BASE_CALL_ONCE_H_
|
| 27 |
+
|
| 28 |
+
#include <algorithm>
|
| 29 |
+
#include <atomic>
|
| 30 |
+
#include <cstdint>
|
| 31 |
+
#include <type_traits>
|
| 32 |
+
#include <utility>
|
| 33 |
+
|
| 34 |
+
#include "absl/base/attributes.h"
|
| 35 |
+
#include "absl/base/config.h"
|
| 36 |
+
#include "absl/base/internal/invoke.h"
|
| 37 |
+
#include "absl/base/internal/low_level_scheduling.h"
|
| 38 |
+
#include "absl/base/internal/raw_logging.h"
|
| 39 |
+
#include "absl/base/internal/scheduling_mode.h"
|
| 40 |
+
#include "absl/base/internal/spinlock_wait.h"
|
| 41 |
+
#include "absl/base/macros.h"
|
| 42 |
+
#include "absl/base/nullability.h"
|
| 43 |
+
#include "absl/base/optimization.h"
|
| 44 |
+
#include "absl/base/port.h"
|
| 45 |
+
|
| 46 |
+
namespace absl {
|
| 47 |
+
ABSL_NAMESPACE_BEGIN
|
| 48 |
+
|
| 49 |
+
class once_flag;
|
| 50 |
+
|
| 51 |
+
namespace base_internal {
|
| 52 |
+
absl::Nonnull<std::atomic<uint32_t>*> ControlWord(
|
| 53 |
+
absl::Nonnull<absl::once_flag*> flag);
|
| 54 |
+
} // namespace base_internal
|
| 55 |
+
|
| 56 |
+
// call_once()
|
| 57 |
+
//
|
| 58 |
+
// For all invocations using a given `once_flag`, invokes a given `fn` exactly
|
| 59 |
+
// once across all threads. The first call to `call_once()` with a particular
|
| 60 |
+
// `once_flag` argument (that does not throw an exception) will run the
|
| 61 |
+
// specified function with the provided `args`; other calls with the same
|
| 62 |
+
// `once_flag` argument will not run the function, but will wait
|
| 63 |
+
// for the provided function to finish running (if it is still running).
|
| 64 |
+
//
|
| 65 |
+
// This mechanism provides a safe, simple, and fast mechanism for one-time
|
| 66 |
+
// initialization in a multi-threaded process.
|
| 67 |
+
//
|
| 68 |
+
// Example:
|
| 69 |
+
//
|
| 70 |
+
// class MyInitClass {
|
| 71 |
+
// public:
|
| 72 |
+
// ...
|
| 73 |
+
// mutable absl::once_flag once_;
|
| 74 |
+
//
|
| 75 |
+
// MyInitClass* init() const {
|
| 76 |
+
// absl::call_once(once_, &MyInitClass::Init, this);
|
| 77 |
+
// return ptr_;
|
| 78 |
+
// }
|
| 79 |
+
//
|
| 80 |
+
template <typename Callable, typename... Args>
|
| 81 |
+
void call_once(absl::once_flag& flag, Callable&& fn, Args&&... args);
|
| 82 |
+
|
| 83 |
+
// once_flag
|
| 84 |
+
//
|
| 85 |
+
// Objects of this type are used to distinguish calls to `call_once()` and
|
| 86 |
+
// ensure the provided function is only invoked once across all threads. This
|
| 87 |
+
// type is not copyable or movable. However, it has a `constexpr`
|
| 88 |
+
// constructor, and is safe to use as a namespace-scoped global variable.
|
| 89 |
+
class once_flag {
|
| 90 |
+
public:
|
| 91 |
+
constexpr once_flag() : control_(0) {}
|
| 92 |
+
once_flag(const once_flag&) = delete;
|
| 93 |
+
once_flag& operator=(const once_flag&) = delete;
|
| 94 |
+
|
| 95 |
+
private:
|
| 96 |
+
friend absl::Nonnull<std::atomic<uint32_t>*> base_internal::ControlWord(
|
| 97 |
+
absl::Nonnull<once_flag*> flag);
|
| 98 |
+
std::atomic<uint32_t> control_;
|
| 99 |
+
};
|
| 100 |
+
|
| 101 |
+
//------------------------------------------------------------------------------
|
| 102 |
+
// End of public interfaces.
|
| 103 |
+
// Implementation details follow.
|
| 104 |
+
//------------------------------------------------------------------------------
|
| 105 |
+
|
| 106 |
+
namespace base_internal {
|
| 107 |
+
|
| 108 |
+
// Like call_once, but uses KERNEL_ONLY scheduling. Intended to be used to
|
| 109 |
+
// initialize entities used by the scheduler implementation.
|
| 110 |
+
template <typename Callable, typename... Args>
|
| 111 |
+
void LowLevelCallOnce(absl::Nonnull<absl::once_flag*> flag, Callable&& fn,
|
| 112 |
+
Args&&... args);
|
| 113 |
+
|
| 114 |
+
// Disables scheduling while on stack when scheduling mode is non-cooperative.
|
| 115 |
+
// No effect for cooperative scheduling modes.
|
| 116 |
+
class SchedulingHelper {
|
| 117 |
+
public:
|
| 118 |
+
explicit SchedulingHelper(base_internal::SchedulingMode mode) : mode_(mode) {
|
| 119 |
+
if (mode_ == base_internal::SCHEDULE_KERNEL_ONLY) {
|
| 120 |
+
guard_result_ = base_internal::SchedulingGuard::DisableRescheduling();
|
| 121 |
+
}
|
| 122 |
+
}
|
| 123 |
+
|
| 124 |
+
~SchedulingHelper() {
|
| 125 |
+
if (mode_ == base_internal::SCHEDULE_KERNEL_ONLY) {
|
| 126 |
+
base_internal::SchedulingGuard::EnableRescheduling(guard_result_);
|
| 127 |
+
}
|
| 128 |
+
}
|
| 129 |
+
|
| 130 |
+
private:
|
| 131 |
+
base_internal::SchedulingMode mode_;
|
| 132 |
+
bool guard_result_ = false;
|
| 133 |
+
};
|
| 134 |
+
|
| 135 |
+
// Bit patterns for call_once state machine values. Internal implementation
|
| 136 |
+
// detail, not for use by clients.
|
| 137 |
+
//
|
| 138 |
+
// The bit patterns are arbitrarily chosen from unlikely values, to aid in
|
| 139 |
+
// debugging. However, kOnceInit must be 0, so that a zero-initialized
|
| 140 |
+
// once_flag will be valid for immediate use.
|
| 141 |
+
enum {
|
| 142 |
+
kOnceInit = 0,
|
| 143 |
+
kOnceRunning = 0x65C2937B,
|
| 144 |
+
kOnceWaiter = 0x05A308D2,
|
| 145 |
+
// A very small constant is chosen for kOnceDone so that it fit in a single
|
| 146 |
+
// compare with immediate instruction for most common ISAs. This is verified
|
| 147 |
+
// for x86, POWER and ARM.
|
| 148 |
+
kOnceDone = 221, // Random Number
|
| 149 |
+
};
|
| 150 |
+
|
| 151 |
+
template <typename Callable, typename... Args>
|
| 152 |
+
void
|
| 153 |
+
CallOnceImpl(absl::Nonnull<std::atomic<uint32_t>*> control,
|
| 154 |
+
base_internal::SchedulingMode scheduling_mode, Callable&& fn,
|
| 155 |
+
Args&&... args) {
|
| 156 |
+
#ifndef NDEBUG
|
| 157 |
+
{
|
| 158 |
+
uint32_t old_control = control->load(std::memory_order_relaxed);
|
| 159 |
+
if (old_control != kOnceInit &&
|
| 160 |
+
old_control != kOnceRunning &&
|
| 161 |
+
old_control != kOnceWaiter &&
|
| 162 |
+
old_control != kOnceDone) {
|
| 163 |
+
ABSL_RAW_LOG(FATAL, "Unexpected value for control word: 0x%lx",
|
| 164 |
+
static_cast<unsigned long>(old_control)); // NOLINT
|
| 165 |
+
}
|
| 166 |
+
}
|
| 167 |
+
#endif // NDEBUG
|
| 168 |
+
static const base_internal::SpinLockWaitTransition trans[] = {
|
| 169 |
+
{kOnceInit, kOnceRunning, true},
|
| 170 |
+
{kOnceRunning, kOnceWaiter, false},
|
| 171 |
+
{kOnceDone, kOnceDone, true}};
|
| 172 |
+
|
| 173 |
+
// Must do this before potentially modifying control word's state.
|
| 174 |
+
base_internal::SchedulingHelper maybe_disable_scheduling(scheduling_mode);
|
| 175 |
+
// Short circuit the simplest case to avoid procedure call overhead.
|
| 176 |
+
// The base_internal::SpinLockWait() call returns either kOnceInit or
|
| 177 |
+
// kOnceDone. If it returns kOnceDone, it must have loaded the control word
|
| 178 |
+
// with std::memory_order_acquire and seen a value of kOnceDone.
|
| 179 |
+
uint32_t old_control = kOnceInit;
|
| 180 |
+
if (control->compare_exchange_strong(old_control, kOnceRunning,
|
| 181 |
+
std::memory_order_relaxed) ||
|
| 182 |
+
base_internal::SpinLockWait(control, ABSL_ARRAYSIZE(trans), trans,
|
| 183 |
+
scheduling_mode) == kOnceInit) {
|
| 184 |
+
base_internal::invoke(std::forward<Callable>(fn),
|
| 185 |
+
std::forward<Args>(args)...);
|
| 186 |
+
old_control =
|
| 187 |
+
control->exchange(base_internal::kOnceDone, std::memory_order_release);
|
| 188 |
+
if (old_control == base_internal::kOnceWaiter) {
|
| 189 |
+
base_internal::SpinLockWake(control, true);
|
| 190 |
+
}
|
| 191 |
+
} // else *control is already kOnceDone
|
| 192 |
+
}
|
| 193 |
+
|
| 194 |
+
inline absl::Nonnull<std::atomic<uint32_t>*> ControlWord(
|
| 195 |
+
absl::Nonnull<once_flag*> flag) {
|
| 196 |
+
return &flag->control_;
|
| 197 |
+
}
|
| 198 |
+
|
| 199 |
+
template <typename Callable, typename... Args>
|
| 200 |
+
void LowLevelCallOnce(absl::Nonnull<absl::once_flag*> flag, Callable&& fn,
|
| 201 |
+
Args&&... args) {
|
| 202 |
+
std::atomic<uint32_t>* once = base_internal::ControlWord(flag);
|
| 203 |
+
uint32_t s = once->load(std::memory_order_acquire);
|
| 204 |
+
if (ABSL_PREDICT_FALSE(s != base_internal::kOnceDone)) {
|
| 205 |
+
base_internal::CallOnceImpl(once, base_internal::SCHEDULE_KERNEL_ONLY,
|
| 206 |
+
std::forward<Callable>(fn),
|
| 207 |
+
std::forward<Args>(args)...);
|
| 208 |
+
}
|
| 209 |
+
}
|
| 210 |
+
|
| 211 |
+
} // namespace base_internal
|
| 212 |
+
|
| 213 |
+
template <typename Callable, typename... Args>
|
| 214 |
+
void
|
| 215 |
+
call_once(absl::once_flag& flag, Callable&& fn, Args&&... args) {
|
| 216 |
+
std::atomic<uint32_t>* once = base_internal::ControlWord(&flag);
|
| 217 |
+
uint32_t s = once->load(std::memory_order_acquire);
|
| 218 |
+
if (ABSL_PREDICT_FALSE(s != base_internal::kOnceDone)) {
|
| 219 |
+
base_internal::CallOnceImpl(
|
| 220 |
+
once, base_internal::SCHEDULE_COOPERATIVE_AND_KERN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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
//
|
| 2 |
+
// Copyright 2017 The Abseil Authors.
|
| 3 |
+
//
|
| 4 |
+
// Licensed under the Apache License, Version 2.0 (the "License");
|
| 5 |
+
// you may not use this file except in compliance with the License.
|
| 6 |
+
// You may obtain a copy of the License at
|
| 7 |
+
//
|
| 8 |
+
// https://www.apache.org/licenses/LICENSE-2.0
|
| 9 |
+
//
|
| 10 |
+
// Unless required by applicable law or agreed to in writing, software
|
| 11 |
+
// distributed under the License is distributed on an "AS IS" BASIS,
|
| 12 |
+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
| 13 |
+
// See the License for the specific language governing permissions and
|
| 14 |
+
// limitations under the License.
|
| 15 |
+
//
|
| 16 |
+
// -----------------------------------------------------------------------------
|
| 17 |
+
// File: casts.h
|
| 18 |
+
// -----------------------------------------------------------------------------
|
| 19 |
+
//
|
| 20 |
+
// This header file defines casting templates to fit use cases not covered by
|
| 21 |
+
// the standard casts provided in the C++ standard. As with all cast operations,
|
| 22 |
+
// use these with caution and only if alternatives do not exist.
|
| 23 |
+
|
| 24 |
+
#ifndef ABSL_BASE_CASTS_H_
|
| 25 |
+
#define ABSL_BASE_CASTS_H_
|
| 26 |
+
|
| 27 |
+
#include <cstring>
|
| 28 |
+
#include <memory>
|
| 29 |
+
#include <type_traits>
|
| 30 |
+
#include <utility>
|
| 31 |
+
|
| 32 |
+
#if defined(__cpp_lib_bit_cast) && __cpp_lib_bit_cast >= 201806L
|
| 33 |
+
#include <bit> // For std::bit_cast.
|
| 34 |
+
#endif // defined(__cpp_lib_bit_cast) && __cpp_lib_bit_cast >= 201806L
|
| 35 |
+
|
| 36 |
+
#include "absl/base/internal/identity.h"
|
| 37 |
+
#include "absl/base/macros.h"
|
| 38 |
+
#include "absl/meta/type_traits.h"
|
| 39 |
+
|
| 40 |
+
namespace absl {
|
| 41 |
+
ABSL_NAMESPACE_BEGIN
|
| 42 |
+
|
| 43 |
+
// implicit_cast()
|
| 44 |
+
//
|
| 45 |
+
// Performs an implicit conversion between types following the language
|
| 46 |
+
// rules for implicit conversion; if an implicit conversion is otherwise
|
| 47 |
+
// allowed by the language in the given context, this function performs such an
|
| 48 |
+
// implicit conversion.
|
| 49 |
+
//
|
| 50 |
+
// Example:
|
| 51 |
+
//
|
| 52 |
+
// // If the context allows implicit conversion:
|
| 53 |
+
// From from;
|
| 54 |
+
// To to = from;
|
| 55 |
+
//
|
| 56 |
+
// // Such code can be replaced by:
|
| 57 |
+
// implicit_cast<To>(from);
|
| 58 |
+
//
|
| 59 |
+
// An `implicit_cast()` may also be used to annotate numeric type conversions
|
| 60 |
+
// that, although safe, may produce compiler warnings (such as `long` to `int`).
|
| 61 |
+
// Additionally, an `implicit_cast()` is also useful within return statements to
|
| 62 |
+
// indicate a specific implicit conversion is being undertaken.
|
| 63 |
+
//
|
| 64 |
+
// Example:
|
| 65 |
+
//
|
| 66 |
+
// return implicit_cast<double>(size_in_bytes) / capacity_;
|
| 67 |
+
//
|
| 68 |
+
// Annotating code with `implicit_cast()` allows you to explicitly select
|
| 69 |
+
// particular overloads and template instantiations, while providing a safer
|
| 70 |
+
// cast than `reinterpret_cast()` or `static_cast()`.
|
| 71 |
+
//
|
| 72 |
+
// Additionally, an `implicit_cast()` can be used to allow upcasting within a
|
| 73 |
+
// type hierarchy where incorrect use of `static_cast()` could accidentally
|
| 74 |
+
// allow downcasting.
|
| 75 |
+
//
|
| 76 |
+
// Finally, an `implicit_cast()` can be used to perform implicit conversions
|
| 77 |
+
// from unrelated types that otherwise couldn't be implicitly cast directly;
|
| 78 |
+
// C++ will normally only implicitly cast "one step" in such conversions.
|
| 79 |
+
//
|
| 80 |
+
// That is, if C is a type which can be implicitly converted to B, with B being
|
| 81 |
+
// a type that can be implicitly converted to A, an `implicit_cast()` can be
|
| 82 |
+
// used to convert C to B (which the compiler can then implicitly convert to A
|
| 83 |
+
// using language rules).
|
| 84 |
+
//
|
| 85 |
+
// Example:
|
| 86 |
+
//
|
| 87 |
+
// // Assume an object C is convertible to B, which is implicitly convertible
|
| 88 |
+
// // to A
|
| 89 |
+
// A a = implicit_cast<B>(C);
|
| 90 |
+
//
|
| 91 |
+
// Such implicit cast chaining may be useful within template logic.
|
| 92 |
+
template <typename To>
|
| 93 |
+
constexpr To implicit_cast(typename absl::internal::type_identity_t<To> to) {
|
| 94 |
+
return to;
|
| 95 |
+
}
|
| 96 |
+
|
| 97 |
+
// bit_cast()
|
| 98 |
+
//
|
| 99 |
+
// Creates a value of the new type `Dest` whose representation is the same as
|
| 100 |
+
// that of the argument, which is of (deduced) type `Source` (a "bitwise cast";
|
| 101 |
+
// every bit in the value representation of the result is equal to the
|
| 102 |
+
// corresponding bit in the object representation of the source). Source and
|
| 103 |
+
// destination types must be of the same size, and both types must be trivially
|
| 104 |
+
// copyable.
|
| 105 |
+
//
|
| 106 |
+
// As with most casts, use with caution. A `bit_cast()` might be needed when you
|
| 107 |
+
// need to treat a value as the value of some other type, for example, to access
|
| 108 |
+
// the individual bits of an object which are not normally accessible through
|
| 109 |
+
// the object's type, such as for working with the binary representation of a
|
| 110 |
+
// floating point value:
|
| 111 |
+
//
|
| 112 |
+
// float f = 3.14159265358979;
|
| 113 |
+
// int i = bit_cast<int>(f);
|
| 114 |
+
// // i = 0x40490fdb
|
| 115 |
+
//
|
| 116 |
+
// Reinterpreting and accessing a value directly as a different type (as shown
|
| 117 |
+
// below) usually results in undefined behavior.
|
| 118 |
+
//
|
| 119 |
+
// Example:
|
| 120 |
+
//
|
| 121 |
+
// // WRONG
|
| 122 |
+
// float f = 3.14159265358979;
|
| 123 |
+
// int i = reinterpret_cast<int&>(f); // Wrong
|
| 124 |
+
// int j = *reinterpret_cast<int*>(&f); // Equally wrong
|
| 125 |
+
// int k = *bit_cast<int*>(&f); // Equally wrong
|
| 126 |
+
//
|
| 127 |
+
// Reinterpret-casting results in undefined behavior according to the ISO C++
|
| 128 |
+
// specification, section [basic.lval]. Roughly, this section says: if an object
|
| 129 |
+
// in memory has one type, and a program accesses it with a different type, the
|
| 130 |
+
// result is undefined behavior for most "different type".
|
| 131 |
+
//
|
| 132 |
+
// Using bit_cast on a pointer and then dereferencing it is no better than using
|
| 133 |
+
// reinterpret_cast. You should only use bit_cast on the value itself.
|
| 134 |
+
//
|
| 135 |
+
// Such casting results in type punning: holding an object in memory of one type
|
| 136 |
+
// and reading its bits back using a different type. A `bit_cast()` avoids this
|
| 137 |
+
// issue by copying the object representation to a new value, which avoids
|
| 138 |
+
// introducing this undefined behavior (since the original value is never
|
| 139 |
+
// accessed in the wrong way).
|
| 140 |
+
//
|
| 141 |
+
// The requirements of `absl::bit_cast` are more strict than that of
|
| 142 |
+
// `std::bit_cast` unless compiler support is available. Specifically, without
|
| 143 |
+
// compiler support, this implementation also requires `Dest` to be
|
| 144 |
+
// default-constructible. In C++20, `absl::bit_cast` is replaced by
|
| 145 |
+
// `std::bit_cast`.
|
| 146 |
+
#if defined(__cpp_lib_bit_cast) && __cpp_lib_bit_cast >= 201806L
|
| 147 |
+
|
| 148 |
+
using std::bit_cast;
|
| 149 |
+
|
| 150 |
+
#else // defined(__cpp_lib_bit_cast) && __cpp_lib_bit_cast >= 201806L
|
| 151 |
+
|
| 152 |
+
template <
|
| 153 |
+
typename Dest, typename Source,
|
| 154 |
+
typename std::enable_if<sizeof(Dest) == sizeof(Source) &&
|
| 155 |
+
std::is_trivially_copyable<Source>::value &&
|
| 156 |
+
std::is_trivially_copyable<Dest>::value
|
| 157 |
+
#if !ABSL_HAVE_BUILTIN(__builtin_bit_cast)
|
| 158 |
+
&& std::is_default_constructible<Dest>::value
|
| 159 |
+
#endif // !ABSL_HAVE_BUILTIN(__builtin_bit_cast)
|
| 160 |
+
,
|
| 161 |
+
int>::type = 0>
|
| 162 |
+
#if ABSL_HAVE_BUILTIN(__builtin_bit_cast)
|
| 163 |
+
inline constexpr Dest bit_cast(const Source& source) {
|
| 164 |
+
return __builtin_bit_cast(Dest, source);
|
| 165 |
+
}
|
| 166 |
+
#else // ABSL_HAVE_BUILTIN(__builtin_bit_cast)
|
| 167 |
+
inline Dest bit_cast(const Source& source) {
|
| 168 |
+
Dest dest;
|
| 169 |
+
memcpy(static_cast<void*>(std::addressof(dest)),
|
| 170 |
+
static_cast<const void*>(std::addressof(source)), sizeof(dest));
|
| 171 |
+
return dest;
|
| 172 |
+
}
|
| 173 |
+
#endif // ABSL_HAVE_BUILTIN(__builtin_bit_cast)
|
| 174 |
+
|
| 175 |
+
#endif // defined(__cpp_lib_bit_cast) && __cpp_lib_bit_cast >= 201806L
|
| 176 |
+
|
| 177 |
+
ABSL_NAMESPACE_END
|
| 178 |
+
} // namespace absl
|
| 179 |
+
|
| 180 |
+
#endif // ABSL_BASE_CASTS_H_
|
include/absl/base/config.h
ADDED
|
@@ -0,0 +1,984 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
//
|
| 2 |
+
// Copyright 2017 The Abseil Authors.
|
| 3 |
+
//
|
| 4 |
+
// Licensed under the Apache License, Version 2.0 (the "License");
|
| 5 |
+
// you may not use this file except in compliance with the License.
|
| 6 |
+
// You may obtain a copy of the License at
|
| 7 |
+
//
|
| 8 |
+
// https://www.apache.org/licenses/LICENSE-2.0
|
| 9 |
+
//
|
| 10 |
+
// Unless required by applicable law or agreed to in writing, software
|
| 11 |
+
// distributed under the License is distributed on an "AS IS" BASIS,
|
| 12 |
+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
| 13 |
+
// See the License for the specific language governing permissions and
|
| 14 |
+
// limitations under the License.
|
| 15 |
+
//
|
| 16 |
+
// -----------------------------------------------------------------------------
|
| 17 |
+
// File: config.h
|
| 18 |
+
// -----------------------------------------------------------------------------
|
| 19 |
+
//
|
| 20 |
+
// This header file defines a set of macros for checking the presence of
|
| 21 |
+
// important compiler and platform features. Such macros can be used to
|
| 22 |
+
// produce portable code by parameterizing compilation based on the presence or
|
| 23 |
+
// lack of a given feature.
|
| 24 |
+
//
|
| 25 |
+
// We define a "feature" as some interface we wish to program to: for example,
|
| 26 |
+
// a library function or system call. A value of `1` indicates support for
|
| 27 |
+
// that feature; any other value indicates the feature support is undefined.
|
| 28 |
+
//
|
| 29 |
+
// Example:
|
| 30 |
+
//
|
| 31 |
+
// Suppose a programmer wants to write a program that uses the 'mmap()' system
|
| 32 |
+
// call. The Abseil macro for that feature (`ABSL_HAVE_MMAP`) allows you to
|
| 33 |
+
// selectively include the `mmap.h` header and bracket code using that feature
|
| 34 |
+
// in the macro:
|
| 35 |
+
//
|
| 36 |
+
// #include "absl/base/config.h"
|
| 37 |
+
//
|
| 38 |
+
// #ifdef ABSL_HAVE_MMAP
|
| 39 |
+
// #include "sys/mman.h"
|
| 40 |
+
// #endif //ABSL_HAVE_MMAP
|
| 41 |
+
//
|
| 42 |
+
// ...
|
| 43 |
+
// #ifdef ABSL_HAVE_MMAP
|
| 44 |
+
// void *ptr = mmap(...);
|
| 45 |
+
// ...
|
| 46 |
+
// #endif // ABSL_HAVE_MMAP
|
| 47 |
+
|
| 48 |
+
#ifndef ABSL_BASE_CONFIG_H_
|
| 49 |
+
#define ABSL_BASE_CONFIG_H_
|
| 50 |
+
|
| 51 |
+
// Included for the __GLIBC__ macro (or similar macros on other systems).
|
| 52 |
+
#include <limits.h>
|
| 53 |
+
|
| 54 |
+
#ifdef __cplusplus
|
| 55 |
+
// Included for __GLIBCXX__, _LIBCPP_VERSION
|
| 56 |
+
#include <cstddef>
|
| 57 |
+
#endif // __cplusplus
|
| 58 |
+
|
| 59 |
+
// ABSL_INTERNAL_CPLUSPLUS_LANG
|
| 60 |
+
//
|
| 61 |
+
// MSVC does not set the value of __cplusplus correctly, but instead uses
|
| 62 |
+
// _MSVC_LANG as a stand-in.
|
| 63 |
+
// https://docs.microsoft.com/en-us/cpp/preprocessor/predefined-macros
|
| 64 |
+
//
|
| 65 |
+
// However, there are reports that MSVC even sets _MSVC_LANG incorrectly at
|
| 66 |
+
// times, for example:
|
| 67 |
+
// https://github.com/microsoft/vscode-cpptools/issues/1770
|
| 68 |
+
// https://reviews.llvm.org/D70996
|
| 69 |
+
//
|
| 70 |
+
// For this reason, this symbol is considered INTERNAL and code outside of
|
| 71 |
+
// Abseil must not use it.
|
| 72 |
+
#if defined(_MSVC_LANG)
|
| 73 |
+
#define ABSL_INTERNAL_CPLUSPLUS_LANG _MSVC_LANG
|
| 74 |
+
#elif defined(__cplusplus)
|
| 75 |
+
#define ABSL_INTERNAL_CPLUSPLUS_LANG __cplusplus
|
| 76 |
+
#endif
|
| 77 |
+
|
| 78 |
+
#if defined(ABSL_INTERNAL_CPLUSPLUS_LANG) && \
|
| 79 |
+
ABSL_INTERNAL_CPLUSPLUS_LANG >= 202002L
|
| 80 |
+
// Include library feature test macros.
|
| 81 |
+
#include <version>
|
| 82 |
+
#endif
|
| 83 |
+
|
| 84 |
+
#if defined(__APPLE__)
|
| 85 |
+
// Included for TARGET_OS_IPHONE, __IPHONE_OS_VERSION_MIN_REQUIRED,
|
| 86 |
+
// __IPHONE_8_0.
|
| 87 |
+
#include <Availability.h>
|
| 88 |
+
#include <TargetConditionals.h>
|
| 89 |
+
#endif
|
| 90 |
+
|
| 91 |
+
#include "absl/base/options.h"
|
| 92 |
+
#include "absl/base/policy_checks.h"
|
| 93 |
+
|
| 94 |
+
// Abseil long-term support (LTS) releases will define
|
| 95 |
+
// `ABSL_LTS_RELEASE_VERSION` to the integer representing the date string of the
|
| 96 |
+
// LTS release version, and will define `ABSL_LTS_RELEASE_PATCH_LEVEL` to the
|
| 97 |
+
// integer representing the patch-level for that release.
|
| 98 |
+
//
|
| 99 |
+
// For example, for LTS release version "20300401.2", this would give us
|
| 100 |
+
// ABSL_LTS_RELEASE_VERSION == 20300401 && ABSL_LTS_RELEASE_PATCH_LEVEL == 2
|
| 101 |
+
//
|
| 102 |
+
// These symbols will not be defined in non-LTS code.
|
| 103 |
+
//
|
| 104 |
+
// Abseil recommends that clients live-at-head. Therefore, if you are using
|
| 105 |
+
// these symbols to assert a minimum version requirement, we recommend you do it
|
| 106 |
+
// as
|
| 107 |
+
//
|
| 108 |
+
// #if defined(ABSL_LTS_RELEASE_VERSION) && ABSL_LTS_RELEASE_VERSION < 20300401
|
| 109 |
+
// #error Project foo requires Abseil LTS version >= 20300401
|
| 110 |
+
// #endif
|
| 111 |
+
//
|
| 112 |
+
// The `defined(ABSL_LTS_RELEASE_VERSION)` part of the check excludes
|
| 113 |
+
// live-at-head clients from the minimum version assertion.
|
| 114 |
+
//
|
| 115 |
+
// See https://abseil.io/about/releases for more information on Abseil release
|
| 116 |
+
// management.
|
| 117 |
+
//
|
| 118 |
+
// LTS releases can be obtained from
|
| 119 |
+
// https://github.com/abseil/abseil-cpp/releases.
|
| 120 |
+
#define ABSL_LTS_RELEASE_VERSION 20250127
|
| 121 |
+
#define ABSL_LTS_RELEASE_PATCH_LEVEL 1
|
| 122 |
+
|
| 123 |
+
// Helper macro to convert a CPP variable to a string literal.
|
| 124 |
+
#define ABSL_INTERNAL_DO_TOKEN_STR(x) #x
|
| 125 |
+
#define ABSL_INTERNAL_TOKEN_STR(x) ABSL_INTERNAL_DO_TOKEN_STR(x)
|
| 126 |
+
|
| 127 |
+
// -----------------------------------------------------------------------------
|
| 128 |
+
// Abseil namespace annotations
|
| 129 |
+
// -----------------------------------------------------------------------------
|
| 130 |
+
|
| 131 |
+
// ABSL_NAMESPACE_BEGIN/ABSL_NAMESPACE_END
|
| 132 |
+
//
|
| 133 |
+
// An annotation placed at the beginning/end of each `namespace absl` scope.
|
| 134 |
+
// This is used to inject an inline namespace.
|
| 135 |
+
//
|
| 136 |
+
// The proper way to write Abseil code in the `absl` namespace is:
|
| 137 |
+
//
|
| 138 |
+
// namespace absl {
|
| 139 |
+
// ABSL_NAMESPACE_BEGIN
|
| 140 |
+
//
|
| 141 |
+
// void Foo(); // absl::Foo().
|
| 142 |
+
//
|
| 143 |
+
// ABSL_NAMESPACE_END
|
| 144 |
+
// } // namespace absl
|
| 145 |
+
//
|
| 146 |
+
// Users of Abseil should not use these macros, because users of Abseil should
|
| 147 |
+
// not write `namespace absl {` in their own code for any reason. (Abseil does
|
| 148 |
+
// not support forward declarations of its own types, nor does it support
|
| 149 |
+
// user-provided specialization of Abseil templates. Code that violates these
|
| 150 |
+
// rules may be broken without warning.)
|
| 151 |
+
#if !defined(ABSL_OPTION_USE_INLINE_NAMESPACE) || \
|
| 152 |
+
!defined(ABSL_OPTION_INLINE_NAMESPACE_NAME)
|
| 153 |
+
#error options.h is misconfigured.
|
| 154 |
+
#endif
|
| 155 |
+
|
| 156 |
+
// Check that ABSL_OPTION_INLINE_NAMESPACE_NAME is neither "head" nor ""
|
| 157 |
+
#if defined(__cplusplus) && ABSL_OPTION_USE_INLINE_NAMESPACE == 1
|
| 158 |
+
|
| 159 |
+
#define ABSL_INTERNAL_INLINE_NAMESPACE_STR \
|
| 160 |
+
ABSL_INTERNAL_TOKEN_STR(ABSL_OPTION_INLINE_NAMESPACE_NAME)
|
| 161 |
+
|
| 162 |
+
static_assert(ABSL_INTERNAL_INLINE_NAMESPACE_STR[0] != '\0',
|
| 163 |
+
"options.h misconfigured: ABSL_OPTION_INLINE_NAMESPACE_NAME must "
|
| 164 |
+
"not be empty.");
|
| 165 |
+
static_assert(ABSL_INTERNAL_INLINE_NAMESPACE_STR[0] != 'h' ||
|
| 166 |
+
ABSL_INTERNAL_INLINE_NAMESPACE_STR[1] != 'e' ||
|
| 167 |
+
ABSL_INTERNAL_INLINE_NAMESPACE_STR[2] != 'a' ||
|
| 168 |
+
ABSL_INTERNAL_INLINE_NAMESPACE_STR[3] != 'd' ||
|
| 169 |
+
ABSL_INTERNAL_INLINE_NAMESPACE_STR[4] != '\0',
|
| 170 |
+
"options.h misconfigured: ABSL_OPTION_INLINE_NAMESPACE_NAME must "
|
| 171 |
+
"be changed to a new, unique identifier name.");
|
| 172 |
+
|
| 173 |
+
#endif
|
| 174 |
+
|
| 175 |
+
#if ABSL_OPTION_USE_INLINE_NAMESPACE == 0
|
| 176 |
+
#define ABSL_NAMESPACE_BEGIN
|
| 177 |
+
#define ABSL_NAMESPACE_END
|
| 178 |
+
#define ABSL_INTERNAL_C_SYMBOL(x) x
|
| 179 |
+
#elif ABSL_OPTION_USE_INLINE_NAMESPACE == 1
|
| 180 |
+
#define ABSL_NAMESPACE_BEGIN \
|
| 181 |
+
inline namespace ABSL_OPTION_INLINE_NAMESPACE_NAME {
|
| 182 |
+
#define ABSL_NAMESPACE_END }
|
| 183 |
+
#define ABSL_INTERNAL_C_SYMBOL_HELPER_2(x, v) x##_##v
|
| 184 |
+
#define ABSL_INTERNAL_C_SYMBOL_HELPER_1(x, v) \
|
| 185 |
+
ABSL_INTERNAL_C_SYMBOL_HELPER_2(x, v)
|
| 186 |
+
#define ABSL_INTERNAL_C_SYMBOL(x) \
|
| 187 |
+
ABSL_INTERNAL_C_SYMBOL_HELPER_1(x, ABSL_OPTION_INLINE_NAMESPACE_NAME)
|
| 188 |
+
#else
|
| 189 |
+
#error options.h is misconfigured.
|
| 190 |
+
#endif
|
| 191 |
+
|
| 192 |
+
// -----------------------------------------------------------------------------
|
| 193 |
+
// Compiler Feature Checks
|
| 194 |
+
// -----------------------------------------------------------------------------
|
| 195 |
+
|
| 196 |
+
// ABSL_HAVE_BUILTIN()
|
| 197 |
+
//
|
| 198 |
+
// Checks whether the compiler supports a Clang Feature Checking Macro, and if
|
| 199 |
+
// so, checks whether it supports the provided builtin function "x" where x
|
| 200 |
+
// is one of the functions noted in
|
| 201 |
+
// https://clang.llvm.org/docs/LanguageExtensions.html
|
| 202 |
+
//
|
| 203 |
+
// Note: Use this macro to avoid an extra level of #ifdef __has_builtin check.
|
| 204 |
+
// http://releases.llvm.org/3.3/tools/clang/docs/LanguageExtensions.html
|
| 205 |
+
#ifdef __has_builtin
|
| 206 |
+
#define ABSL_HAVE_BUILTIN(x) __has_builtin(x)
|
| 207 |
+
#else
|
| 208 |
+
#define ABSL_HAVE_BUILTIN(x) 0
|
| 209 |
+
#endif
|
| 210 |
+
|
| 211 |
+
#ifdef __has_feature
|
| 212 |
+
#define ABSL_HAVE_FEATURE(f) __has_feature(f)
|
| 213 |
+
#else
|
| 214 |
+
#define ABSL_HAVE_FEATURE(f) 0
|
| 215 |
+
#endif
|
| 216 |
+
|
| 217 |
+
// Portable check for GCC minimum version:
|
| 218 |
+
// https://gcc.gnu.org/onlinedocs/cpp/Common-Predefined-Macros.html
|
| 219 |
+
#if defined(__GNUC__) && defined(__GNUC_MINOR__)
|
| 220 |
+
#define ABSL_INTERNAL_HAVE_MIN_GNUC_VERSION(x, y) \
|
| 221 |
+
(__GNUC__ > (x) || __GNUC__ == (x) && __GNUC_MINOR__ >= (y))
|
| 222 |
+
#else
|
| 223 |
+
#define ABSL_INTERNAL_HAVE_MIN_GNUC_VERSION(x, y) 0
|
| 224 |
+
#endif
|
| 225 |
+
|
| 226 |
+
#if defined(__clang__) && defined(__clang_major__) && defined(__clang_minor__)
|
| 227 |
+
#define ABSL_INTERNAL_HAVE_MIN_CLANG_VERSION(x, y) \
|
| 228 |
+
(__clang_major__ > (x) || __clang_major__ == (x) && __clang_minor__ >= (y))
|
| 229 |
+
#else
|
| 230 |
+
#define ABSL_INTERNAL_HAVE_MIN_CLANG_VERSION(x, y) 0
|
| 231 |
+
#endif
|
| 232 |
+
|
| 233 |
+
// ABSL_HAVE_TLS is defined to 1 when __thread should be supported.
|
| 234 |
+
// We assume __thread is supported on Linux when compiled with Clang or
|
| 235 |
+
// compiled against libstdc++ with _GLIBCXX_HAVE_TLS defined.
|
| 236 |
+
#ifdef ABSL_HAVE_TLS
|
| 237 |
+
#error ABSL_HAVE_TLS cannot be directly set
|
| 238 |
+
#elif (defined(__linux__)) && (defined(__clang__) || defined(_GLIBCXX_HAVE_TLS))
|
| 239 |
+
#define ABSL_HAVE_TLS 1
|
| 240 |
+
#endif
|
| 241 |
+
|
| 242 |
+
// ABSL_HAVE_STD_IS_TRIVIALLY_DESTRUCTIBLE
|
| 243 |
+
//
|
| 244 |
+
// Checks whether `std::is_trivially_destructible<T>` is supported.
|
| 245 |
+
#ifdef ABSL_HAVE_STD_IS_TRIVIALLY_DESTRUCTIBLE
|
| 246 |
+
#error ABSL_HAVE_STD_IS_TRIVIALLY_DESTRUCTIBLE cannot be directly set
|
| 247 |
+
#define ABSL_HAVE_STD_IS_TRIVIALLY_DESTRUCTIBLE 1
|
| 248 |
+
#endif
|
| 249 |
+
|
| 250 |
+
// ABSL_HAVE_STD_IS_TRIVIALLY_CONSTRUCTIBLE
|
| 251 |
+
//
|
| 252 |
+
// Checks whether `std::is_trivially_default_constructible<T>` and
|
| 253 |
+
// `std::is_trivially_copy_constructible<T>` are supported.
|
| 254 |
+
#ifdef ABSL_HAVE_STD_IS_TRIVIALLY_CONSTRUCTIBLE
|
| 255 |
+
#error ABSL_HAVE_STD_IS_TRIVIALLY_CONSTRUCTIBLE cannot be directly set
|
| 256 |
+
#else
|
| 257 |
+
#define ABSL_HAVE_STD_IS_TRIVIALLY_CONSTRUCTIBLE 1
|
| 258 |
+
#endif
|
| 259 |
+
|
| 260 |
+
// ABSL_HAVE_STD_IS_TRIVIALLY_ASSIGNABLE
|
| 261 |
+
//
|
| 262 |
+
// Checks whether `std::is_trivially_copy_assignable<T>` is supported.
|
| 263 |
+
#ifdef ABSL_HAVE_STD_IS_TRIVIALLY_ASSIGNABLE
|
| 264 |
+
#error ABSL_HAVE_STD_IS_TRIVIALLY_ASSIGNABLE cannot be directly set
|
| 265 |
+
#else
|
| 266 |
+
#define ABSL_HAVE_STD_IS_TRIVIALLY_ASSIGNABLE 1
|
| 267 |
+
#endif
|
| 268 |
+
|
| 269 |
+
// ABSL_HAVE_STD_IS_TRIVIALLY_COPYABLE
|
| 270 |
+
//
|
| 271 |
+
// Checks whether `std::is_trivially_copyable<T>` is supported.
|
| 272 |
+
#ifdef ABSL_HAVE_STD_IS_TRIVIALLY_COPYABLE
|
| 273 |
+
#error ABSL_HAVE_STD_IS_TRIVIALLY_COPYABLE cannot be directly set
|
| 274 |
+
#define ABSL_HAVE_STD_IS_TRIVIALLY_COPYABLE 1
|
| 275 |
+
#endif
|
| 276 |
+
|
| 277 |
+
|
| 278 |
+
// ABSL_HAVE_THREAD_LOCAL
|
| 279 |
+
//
|
| 280 |
+
// DEPRECATED - `thread_local` is available on all supported platforms.
|
| 281 |
+
// Checks whether C++11's `thread_local` storage duration specifier is
|
| 282 |
+
// supported.
|
| 283 |
+
#ifdef ABSL_HAVE_THREAD_LOCAL
|
| 284 |
+
#error ABSL_HAVE_THREAD_LOCAL cannot be directly set
|
| 285 |
+
#else
|
| 286 |
+
#define ABSL_HAVE_THREAD_LOCAL 1
|
| 287 |
+
#endif
|
| 288 |
+
|
| 289 |
+
// ABSL_HAVE_INTRINSIC_INT128
|
| 290 |
+
//
|
| 291 |
+
// Checks whether the __int128 compiler extension for a 128-bit integral type is
|
| 292 |
+
// supported.
|
| 293 |
+
//
|
| 294 |
+
// Note: __SIZEOF_INT128__ is defined by Clang and GCC when __int128 is
|
| 295 |
+
// supported, but we avoid using it in certain cases:
|
| 296 |
+
// * On Clang:
|
| 297 |
+
// * Building using Clang for Windows, where the Clang runtime library has
|
| 298 |
+
// 128-bit support only on LP64 architectures, but Windows is LLP64.
|
| 299 |
+
// * On Nvidia's nvcc:
|
| 300 |
+
// * nvcc also defines __GNUC__ and __SIZEOF_INT128__, but not all versions
|
| 301 |
+
// actually support __int128.
|
| 302 |
+
#ifdef ABSL_HAVE_INTRINSIC_INT128
|
| 303 |
+
#error ABSL_HAVE_INTRINSIC_INT128 cannot be directly set
|
| 304 |
+
#elif defined(__SIZEOF_INT128__)
|
| 305 |
+
#if (defined(__clang__) && !defined(_WIN32)) || \
|
| 306 |
+
(defined(__CUDACC__) && __CUDACC_VER_MAJOR__ >= 9) || \
|
| 307 |
+
(defined(__GNUC__) && !defined(__clang__) && !defined(__CUDACC__))
|
| 308 |
+
#define ABSL_HAVE_INTRINSIC_INT128 1
|
| 309 |
+
#elif defined(__CUDACC__)
|
| 310 |
+
// __CUDACC_VER__ is a full version number before CUDA 9, and is defined to a
|
| 311 |
+
// string explaining that it has been removed starting with CUDA 9. We use
|
| 312 |
+
// nested #ifs because there is no short-circuiting in the preprocessor.
|
| 313 |
+
// NOTE: `__CUDACC__` could be undefined while `__CUDACC_VER__` is defined.
|
| 314 |
+
#if __CUDACC_VER__ >= 70000
|
| 315 |
+
#define ABSL_HAVE_INTRINSIC_INT128 1
|
| 316 |
+
#endif // __CUDACC_VER__ >= 70000
|
| 317 |
+
#endif // defined(__CUDACC__)
|
| 318 |
+
#endif // ABSL_HAVE_INTRINSIC_INT128
|
| 319 |
+
|
| 320 |
+
// ABSL_HAVE_EXCEPTIONS
|
| 321 |
+
//
|
| 322 |
+
// Checks whether the compiler both supports and enables exceptions. Many
|
| 323 |
+
// compilers support a "no exceptions" mode that disables exceptions.
|
| 324 |
+
//
|
| 325 |
+
// Generally, when ABSL_HAVE_EXCEPTIONS is not defined:
|
| 326 |
+
//
|
| 327 |
+
// * Code using `throw` and `try` may not compile.
|
| 328 |
+
// * The `noexcept` specifier will still compile and behave as normal.
|
| 329 |
+
// * The `noexcept` operator may still return `false`.
|
| 330 |
+
//
|
| 331 |
+
// For further details, consult the compiler's documentation.
|
| 332 |
+
#ifdef ABSL_HAVE_EXCEPTIONS
|
| 333 |
+
#error ABSL_HAVE_EXCEPTIONS cannot be directly set.
|
| 334 |
+
#elif ABSL_INTERNAL_HAVE_MIN_CLANG_VERSION(3, 6)
|
| 335 |
+
// Clang >= 3.6
|
| 336 |
+
#if ABSL_HAVE_FEATURE(cxx_exceptions)
|
| 337 |
+
#define ABSL_HAVE_EXCEPTIONS 1
|
| 338 |
+
#endif // ABSL_HAVE_FEATURE(cxx_exceptions)
|
| 339 |
+
#elif defined(__clang__)
|
| 340 |
+
// Clang < 3.6
|
| 341 |
+
// http://releases.llvm.org/3.6.0/tools/clang/docs/ReleaseNotes.html#the-exceptions-macro
|
| 342 |
+
#if defined(__EXCEPTIONS) && ABSL_HAVE_FEATURE(cxx_exceptions)
|
| 343 |
+
#define ABSL_HAVE_EXCEPTIONS 1
|
| 344 |
+
#endif // defined(__EXCEPTIONS) && ABSL_HAVE_FEATURE(cxx_exceptions)
|
| 345 |
+
// Handle remaining special cases and default to exceptions being supported.
|
| 346 |
+
#elif !(defined(__GNUC__) && !defined(__cpp_exceptions)) && \
|
| 347 |
+
!(defined(_MSC_VER) && !defined(_CPPUNWIND))
|
| 348 |
+
#define ABSL_HAVE_EXCEPTIONS 1
|
| 349 |
+
#endif
|
| 350 |
+
|
| 351 |
+
// -----------------------------------------------------------------------------
|
| 352 |
+
// Platform Feature Checks
|
| 353 |
+
// -----------------------------------------------------------------------------
|
| 354 |
+
|
| 355 |
+
// Currently supported operating systems and associated preprocessor
|
| 356 |
+
// symbols:
|
| 357 |
+
//
|
| 358 |
+
// Linux and Linux-derived __linux__
|
| 359 |
+
// Android __ANDROID__ (implies __linux__)
|
| 360 |
+
// Linux (non-Android) __linux__ && !__ANDROID__
|
| 361 |
+
// Darwin (macOS and iOS) __APPLE__
|
| 362 |
+
// Akaros (http://akaros.org) __ros__
|
| 363 |
+
// Windows _WIN32
|
| 364 |
+
// NaCL __native_client__
|
| 365 |
+
// AsmJS __asmjs__
|
| 366 |
+
// WebAssembly (Emscripten) __EMSCRIPTEN__
|
| 367 |
+
// Fuchsia __Fuchsia__
|
| 368 |
+
//
|
| 369 |
+
// Note that since Android defines both __ANDROID__ and __linux__, one
|
| 370 |
+
// may probe for either Linux or Android by simply testing for __linux__.
|
| 371 |
+
|
| 372 |
+
// ABSL_HAVE_MMAP
|
| 373 |
+
//
|
| 374 |
+
// Checks whether the platform has an mmap(2) implementation as defined in
|
| 375 |
+
// POSIX.1-2001.
|
| 376 |
+
#ifdef ABSL_HAVE_MMAP
|
| 377 |
+
#error ABSL_HAVE_MMAP cannot be directly set
|
| 378 |
+
#elif defined(__linux__) || defined(__APPLE__) || defined(__FreeBSD__) || \
|
| 379 |
+
defined(_AIX) || defined(__ros__) || defined(__native_client__) || \
|
| 380 |
+
defined(__asmjs__) || defined(__EMSCRIPTEN__) || defined(__Fuchsia__) || \
|
| 381 |
+
defined(__sun) || defined(__myriad2__) || defined(__HAIKU__) || \
|
| 382 |
+
defined(__OpenBSD__) || defined(__NetBSD__) || defined(__QNX__) || \
|
| 383 |
+
defined(__VXWORKS__) || defined(__hexagon__) || defined(__XTENSA__)
|
| 384 |
+
#define ABSL_HAVE_MMAP 1
|
| 385 |
+
#endif
|
| 386 |
+
|
| 387 |
+
// ABSL_HAVE_PTHREAD_GETSCHEDPARAM
|
| 388 |
+
//
|
| 389 |
+
// Checks whether the platform implements the pthread_(get|set)schedparam(3)
|
| 390 |
+
// functions as defined in POSIX.1-2001.
|
| 391 |
+
#ifdef ABSL_HAVE_PTHREAD_GETSCHEDPARAM
|
| 392 |
+
#error ABSL_HAVE_PTHREAD_GETSCHEDPARAM cannot be directly set
|
| 393 |
+
#elif defined(__linux__) || defined(__APPLE__) || defined(__FreeBSD__) || \
|
| 394 |
+
defined(_AIX) || defined(__ros__) || defined(__OpenBSD__) || \
|
| 395 |
+
defined(__NetBSD__) || defined(__VXWORKS__)
|
| 396 |
+
#define ABSL_HAVE_PTHREAD_GETSCHEDPARAM 1
|
| 397 |
+
#endif
|
| 398 |
+
|
| 399 |
+
// ABSL_HAVE_SCHED_GETCPU
|
| 400 |
+
//
|
| 401 |
+
// Checks whether sched_getcpu is available.
|
| 402 |
+
#ifdef ABSL_HAVE_SCHED_GETCPU
|
| 403 |
+
#error ABSL_HAVE_SCHED_GETCPU cannot be directly set
|
| 404 |
+
#elif defined(__linux__)
|
| 405 |
+
#define ABSL_HAVE_SCHED_GETCPU 1
|
| 406 |
+
#endif
|
| 407 |
+
|
| 408 |
+
// ABSL_HAVE_SCHED_YIELD
|
| 409 |
+
//
|
| 410 |
+
// Checks whether the platform implements sched_yield(2) as defined in
|
| 411 |
+
// POSIX.1-2001.
|
| 412 |
+
#ifdef ABSL_HAVE_SCHED_YIELD
|
| 413 |
+
#error ABSL_HAVE_SCHED_YIELD cannot be directly set
|
| 414 |
+
#elif defined(__linux__) || defined(__ros__) || defined(__native_client__) || \
|
| 415 |
+
defined(__VXWORKS__)
|
| 416 |
+
#define ABSL_HAVE_SCHED_YIELD 1
|
| 417 |
+
#endif
|
| 418 |
+
|
| 419 |
+
// ABSL_HAVE_SEMAPHORE_H
|
| 420 |
+
//
|
| 421 |
+
// Checks whether the platform supports the <semaphore.h> header and sem_init(3)
|
| 422 |
+
// family of functions as standardized in POSIX.1-2001.
|
| 423 |
+
//
|
| 424 |
+
// Note: While Apple provides <semaphore.h> for both iOS and macOS, it is
|
| 425 |
+
// explicitly deprecated and will cause build failures if enabled for those
|
| 426 |
+
// platforms. We side-step the issue by not defining it here for Apple
|
| 427 |
+
// platforms.
|
| 428 |
+
#ifdef ABSL_HAVE_SEMAPHORE_H
|
| 429 |
+
#error ABSL_HAVE_SEMAPHORE_H cannot be directly set
|
| 430 |
+
#elif defined(__linux__) || defined(__ros__) || defined(__VXWORKS__)
|
| 431 |
+
#define ABSL_HAVE_SEMAPHORE_H 1
|
| 432 |
+
#endif
|
| 433 |
+
|
| 434 |
+
// ABSL_HAVE_ALARM
|
| 435 |
+
//
|
| 436 |
+
// Checks whether the platform supports the <signal.h> header and alarm(2)
|
| 437 |
+
// function as standardized in POSIX.1-2001.
|
| 438 |
+
#ifdef ABSL_HAVE_ALARM
|
| 439 |
+
#error ABSL_HAVE_ALARM cannot be directly set
|
| 440 |
+
#elif defined(__GOOGLE_GRTE_VERSION__)
|
| 441 |
+
// feature tests for Google's GRTE
|
| 442 |
+
#define ABSL_HAVE_ALARM 1
|
| 443 |
+
#elif defined(__GLIBC__)
|
| 444 |
+
// feature test for glibc
|
| 445 |
+
#define ABSL_HAVE_ALARM 1
|
| 446 |
+
#elif defined(_MSC_VER)
|
| 447 |
+
// feature tests for Microsoft's library
|
| 448 |
+
#elif defined(__MINGW32__)
|
| 449 |
+
// mingw32 doesn't provide alarm(2):
|
| 450 |
+
// https://osdn.net/projects/mingw/scm/git/mingw-org-wsl/blobs/5.2-trunk/mingwrt/include/unistd.h
|
| 451 |
+
// mingw-w64 provides a no-op implementation:
|
| 452 |
+
// https://sourceforge.net/p/mingw-w64/mingw-w64/ci/master/tree/mingw-w64-crt/misc/alarm.c
|
| 453 |
+
#elif defined(__EMSCRIPTEN__)
|
| 454 |
+
// emscripten doesn't support signals
|
| 455 |
+
#elif defined(__wasi__)
|
| 456 |
+
// WASI doesn't support signals
|
| 457 |
+
#elif defined(__Fuchsia__)
|
| 458 |
+
// Signals don't exist on fuchsia.
|
| 459 |
+
#elif defined(__native_client__)
|
| 460 |
+
// Signals don't exist on hexagon/QuRT
|
| 461 |
+
#elif defined(__hexagon__)
|
| 462 |
+
#else
|
| 463 |
+
// other standard libraries
|
| 464 |
+
#define ABSL_HAVE_ALARM 1
|
| 465 |
+
#endif
|
| 466 |
+
|
| 467 |
+
// ABSL_IS_LITTLE_ENDIAN
|
| 468 |
+
// ABSL_IS_BIG_ENDIAN
|
| 469 |
+
//
|
| 470 |
+
// Checks the endianness of the platform.
|
| 471 |
+
//
|
| 472 |
+
// Notes: uses the built in endian macros provided by GCC (since 4.6) and
|
| 473 |
+
// Clang (since 3.2); see
|
| 474 |
+
// https://gcc.gnu.org/onlinedocs/cpp/Common-Predefined-Macros.html.
|
| 475 |
+
// Otherwise, if _WIN32, assume little endian. Otherwise, bail with an error.
|
| 476 |
+
#if defined(ABSL_IS_BIG_ENDIAN)
|
| 477 |
+
#error "ABSL_IS_BIG_ENDIAN cannot be directly set."
|
| 478 |
+
#endif
|
| 479 |
+
#if defined(ABSL_IS_LITTLE_ENDIAN)
|
| 480 |
+
#error "ABSL_IS_LITTLE_ENDIAN cannot be directly set."
|
| 481 |
+
#endif
|
| 482 |
+
|
| 483 |
+
#if (defined(__BYTE_ORDER__) && defined(__ORDER_LITTLE_ENDIAN__) && \
|
| 484 |
+
__BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__)
|
| 485 |
+
#define ABSL_IS_LITTLE_ENDIAN 1
|
| 486 |
+
#elif defined(__BYTE_ORDER__) && defined(__ORDER_BIG_ENDIAN__) && \
|
| 487 |
+
__BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
|
| 488 |
+
#define ABSL_IS_BIG_ENDIAN 1
|
| 489 |
+
#elif defined(_WIN32)
|
| 490 |
+
#define ABSL_IS_LITTLE_ENDIAN 1
|
| 491 |
+
#else
|
| 492 |
+
#error "absl endian detection needs to be set up for your compiler"
|
| 493 |
+
#endif
|
| 494 |
+
|
| 495 |
+
// macOS < 10.13 and iOS < 12 don't support <any>, <optional>, or <variant>
|
| 496 |
+
// because the libc++ shared library shipped on the system doesn't have the
|
| 497 |
+
// requisite exported symbols. See
|
| 498 |
+
// https://github.com/abseil/abseil-cpp/issues/207 and
|
| 499 |
+
// https://developer.apple.com/documentation/xcode_release_notes/xcode_10_release_notes
|
| 500 |
+
//
|
| 501 |
+
// libc++ spells out the availability requirements in the file
|
| 502 |
+
// llvm-project/libcxx/include/__config via the #define
|
| 503 |
+
// _LIBCPP_AVAILABILITY_BAD_OPTIONAL_ACCESS. The set of versions has been
|
| 504 |
+
// modified a few times, via
|
| 505 |
+
// https://github.com/llvm/llvm-project/commit/7fb40e1569dd66292b647f4501b85517e9247953
|
| 506 |
+
// and
|
| 507 |
+
// https://github.com/llvm/llvm-project/commit/0bc451e7e137c4ccadcd3377250874f641ca514a
|
| 508 |
+
// The second has the actually correct versions, thus, is what we copy here.
|
| 509 |
+
#if defined(__APPLE__) && \
|
| 510 |
+
((defined(__ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__) && \
|
| 511 |
+
__ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__ < 101300) || \
|
| 512 |
+
(defined(__ENVIRONMENT_IPHONE_OS_VERSION_MIN_REQUIRED__) && \
|
| 513 |
+
__ENVIRONMENT_IPHONE_OS_VERSION_MIN_REQUIRED__ < 120000) || \
|
| 514 |
+
(defined(__ENVIRONMENT_WATCH_OS_VERSION_MIN_REQUIRED__) && \
|
| 515 |
+
__ENVIRONMENT_WATCH_OS_VERSION_MIN_REQUIRED__ < 50000) || \
|
| 516 |
+
(defined(__ENVIRONMENT_TV_OS_VERSION_MIN_REQUIRED__) && \
|
| 517 |
+
__ENVIRONMENT_TV_OS_VERSION_MIN_REQUIRED__ < 120000))
|
| 518 |
+
#define ABSL_INTERNAL_APPLE_CXX17_TYPES_UNAVAILABLE 1
|
| 519 |
+
#else
|
| 520 |
+
#define ABSL_INTERNAL_APPLE_CXX17_TYPES_UNAVAILABLE 0
|
| 521 |
+
#endif
|
| 522 |
+
|
| 523 |
+
// ABSL_HAVE_STD_ANY
|
| 524 |
+
//
|
| 525 |
+
// Checks whether C++17 std::any is available.
|
| 526 |
+
#ifdef ABSL_HAVE_STD_ANY
|
| 527 |
+
#error "ABSL_HAVE_STD_ANY cannot be directly set."
|
| 528 |
+
#elif defined(__cpp_lib_any) && __cpp_lib_any >= 201606L
|
| 529 |
+
#define ABSL_HAVE_STD_ANY 1
|
| 530 |
+
#elif defined(ABSL_INTERNAL_CPLUSPLUS_LANG) && \
|
| 531 |
+
ABSL_INTERNAL_CPLUSPLUS_LANG >= 201703L && \
|
| 532 |
+
!ABSL_INTERNAL_APPLE_CXX17_TYPES_UNAVAILABLE
|
| 533 |
+
#define ABSL_HAVE_STD_ANY 1
|
| 534 |
+
#endif
|
| 535 |
+
|
| 536 |
+
// ABSL_HAVE_STD_OPTIONAL
|
| 537 |
+
//
|
| 538 |
+
// Checks whether C++17 std::optional is available.
|
| 539 |
+
#ifdef ABSL_HAVE_STD_OPTIONAL
|
| 540 |
+
#error "ABSL_HAVE_STD_OPTIONAL cannot be directly set."
|
| 541 |
+
#elif defined(__cpp_lib_optional) && __cpp_lib_optional >= 202106L
|
| 542 |
+
#define ABSL_HAVE_STD_OPTIONAL 1
|
| 543 |
+
#elif defined(ABSL_INTERNAL_CPLUSPLUS_LANG) && \
|
| 544 |
+
ABSL_INTERNAL_CPLUSPLUS_LANG >= 201703L && \
|
| 545 |
+
!ABSL_INTERNAL_APPLE_CXX17_TYPES_UNAVAILABLE
|
| 546 |
+
#define ABSL_HAVE_STD_OPTIONAL 1
|
| 547 |
+
#endif
|
| 548 |
+
|
| 549 |
+
// ABSL_HAVE_STD_VARIANT
|
| 550 |
+
//
|
| 551 |
+
// Checks whether C++17 std::variant is available.
|
| 552 |
+
#ifdef ABSL_HAVE_STD_VARIANT
|
| 553 |
+
#error "ABSL_HAVE_STD_VARIANT cannot be directly set."
|
| 554 |
+
#elif defined(__cpp_lib_variant) && __cpp_lib_variant >= 201606L
|
| 555 |
+
#define ABSL_HAVE_STD_VARIANT 1
|
| 556 |
+
#elif defined(ABSL_INTERNAL_CPLUSPLUS_LANG) && \
|
| 557 |
+
ABSL_INTERNAL_CPLUSPLUS_LANG >= 201703L && \
|
| 558 |
+
!ABSL_INTERNAL_APPLE_CXX17_TYPES_UNAVAILABLE
|
| 559 |
+
#define ABSL_HAVE_STD_VARIANT 1
|
| 560 |
+
#endif
|
| 561 |
+
|
| 562 |
+
// ABSL_HAVE_STD_STRING_VIEW
|
| 563 |
+
//
|
| 564 |
+
// Checks whether C++17 std::string_view is available.
|
| 565 |
+
#ifdef ABSL_HAVE_STD_STRING_VIEW
|
| 566 |
+
#error "ABSL_HAVE_STD_STRING_VIEW cannot be directly set."
|
| 567 |
+
#elif defined(__cpp_lib_string_view) && __cpp_lib_string_view >= 201606L
|
| 568 |
+
#define ABSL_HAVE_STD_STRING_VIEW 1
|
| 569 |
+
#elif defined(ABSL_INTERNAL_CPLUSPLUS_LANG) && \
|
| 570 |
+
ABSL_INTERNAL_CPLUSPLUS_LANG >= 201703L
|
| 571 |
+
#define ABSL_HAVE_STD_STRING_VIEW 1
|
| 572 |
+
#endif
|
| 573 |
+
|
| 574 |
+
// ABSL_HAVE_STD_ORDERING
|
| 575 |
+
//
|
| 576 |
+
// Checks whether C++20 std::{partial,weak,strong}_ordering are available.
|
| 577 |
+
//
|
| 578 |
+
// __cpp_lib_three_way_comparison is missing on libc++
|
| 579 |
+
// (https://github.com/llvm/llvm-project/issues/73953) so treat it as defined
|
| 580 |
+
// when building in C++20 mode.
|
| 581 |
+
#ifdef ABSL_HAVE_STD_ORDERING
|
| 582 |
+
#error "ABSL_HAVE_STD_ORDERING cannot be directly set."
|
| 583 |
+
#elif (defined(__cpp_lib_three_way_comparison) && \
|
| 584 |
+
__cpp_lib_three_way_comparison >= 201907L) || \
|
| 585 |
+
(defined(ABSL_INTERNAL_CPLUSPLUS_LANG) && \
|
| 586 |
+
ABSL_INTERNAL_CPLUSPLUS_LANG >= 202002L)
|
| 587 |
+
#define ABSL_HAVE_STD_ORDERING 1
|
| 588 |
+
#endif
|
| 589 |
+
|
| 590 |
+
// ABSL_USES_STD_ANY
|
| 591 |
+
//
|
| 592 |
+
// Indicates whether absl::any is an alias for std::any.
|
| 593 |
+
#if !defined(ABSL_OPTION_USE_STD_ANY)
|
| 594 |
+
#error options.h is misconfigured.
|
| 595 |
+
#elif ABSL_OPTION_USE_STD_ANY == 0 || \
|
| 596 |
+
(ABSL_OPTION_USE_STD_ANY == 2 && !defined(ABSL_HAVE_STD_ANY))
|
| 597 |
+
#undef ABSL_USES_STD_ANY
|
| 598 |
+
#elif ABSL_OPTION_USE_STD_ANY == 1 || \
|
| 599 |
+
(ABSL_OPTION_USE_STD_ANY == 2 && defined(ABSL_HAVE_STD_ANY))
|
| 600 |
+
#define ABSL_USES_STD_ANY 1
|
| 601 |
+
#else
|
| 602 |
+
#error options.h is misconfigured.
|
| 603 |
+
#endif
|
| 604 |
+
|
| 605 |
+
// ABSL_USES_STD_OPTIONAL
|
| 606 |
+
//
|
| 607 |
+
// Indicates whether absl::optional is an alias for std::optional.
|
| 608 |
+
#if !defined(ABSL_OPTION_USE_STD_OPTIONAL)
|
| 609 |
+
#error options.h is misconfigured.
|
| 610 |
+
#elif ABSL_OPTION_USE_STD_OPTIONAL == 0 || \
|
| 611 |
+
(ABSL_OPTION_USE_STD_OPTIONAL == 2 && !defined(ABSL_HAVE_STD_OPTIONAL))
|
| 612 |
+
#undef ABSL_USES_STD_OPTIONAL
|
| 613 |
+
#elif ABSL_OPTION_USE_STD_OPTIONAL == 1 || \
|
| 614 |
+
(ABSL_OPTION_USE_STD_OPTIONAL == 2 && defined(ABSL_HAVE_STD_OPTIONAL))
|
| 615 |
+
#define ABSL_USES_STD_OPTIONAL 1
|
| 616 |
+
#else
|
| 617 |
+
#error options.h is misconfigured.
|
| 618 |
+
#endif
|
| 619 |
+
|
| 620 |
+
// ABSL_USES_STD_VARIANT
|
| 621 |
+
//
|
| 622 |
+
// Indicates whether absl::variant is an alias for std::variant.
|
| 623 |
+
#if !defined(ABSL_OPTION_USE_STD_VARIANT)
|
| 624 |
+
#error options.h is misconfigured.
|
| 625 |
+
#elif ABSL_OPTION_USE_STD_VARIANT == 0 || \
|
| 626 |
+
(ABSL_OPTION_USE_STD_VARIANT == 2 && !defined(ABSL_HAVE_STD_VARIANT))
|
| 627 |
+
#undef ABSL_USES_STD_VARIANT
|
| 628 |
+
#elif ABSL_OPTION_USE_STD_VARIANT == 1 || \
|
| 629 |
+
(ABSL_OPTION_USE_STD_VARIANT == 2 && defined(ABSL_HAVE_STD_VARIANT))
|
| 630 |
+
#define ABSL_USES_STD_VARIANT 1
|
| 631 |
+
#else
|
| 632 |
+
#error options.h is misconfigured.
|
| 633 |
+
#endif
|
| 634 |
+
|
| 635 |
+
// ABSL_USES_STD_STRING_VIEW
|
| 636 |
+
//
|
| 637 |
+
// Indicates whether absl::string_view is an alias for std::string_view.
|
| 638 |
+
#if !defined(ABSL_OPTION_USE_STD_STRING_VIEW)
|
| 639 |
+
#error options.h is misconfigured.
|
| 640 |
+
#elif ABSL_OPTION_USE_STD_STRING_VIEW == 0 || \
|
| 641 |
+
(ABSL_OPTION_USE_STD_STRING_VIEW == 2 && \
|
| 642 |
+
!defined(ABSL_HAVE_STD_STRING_VIEW))
|
| 643 |
+
#undef ABSL_USES_STD_STRING_VIEW
|
| 644 |
+
#elif ABSL_OPTION_USE_STD_STRING_VIEW == 1 || \
|
| 645 |
+
(ABSL_OPTION_USE_STD_STRING_VIEW == 2 && \
|
| 646 |
+
defined(ABSL_HAVE_STD_STRING_VIEW))
|
| 647 |
+
#define ABSL_USES_STD_STRING_VIEW 1
|
| 648 |
+
#else
|
| 649 |
+
#error options.h is misconfigured.
|
| 650 |
+
#endif
|
| 651 |
+
|
| 652 |
+
// ABSL_USES_STD_ORDERING
|
| 653 |
+
//
|
| 654 |
+
// Indicates whether absl::{partial,weak,strong}_ordering are aliases for the
|
| 655 |
+
// std:: ordering types.
|
| 656 |
+
#if !defined(ABSL_OPTION_USE_STD_ORDERING)
|
| 657 |
+
#error options.h is misconfigured.
|
| 658 |
+
#elif ABSL_OPTION_USE_STD_ORDERING == 0 || \
|
| 659 |
+
(ABSL_OPTION_USE_STD_ORDERING == 2 && !defined(ABSL_HAVE_STD_ORDERING))
|
| 660 |
+
#undef ABSL_USES_STD_ORDERING
|
| 661 |
+
#elif ABSL_OPTION_USE_STD_ORDERING == 1 || \
|
| 662 |
+
(ABSL_OPTION_USE_STD_ORDERING == 2 && defined(ABSL_HAVE_STD_ORDERING))
|
| 663 |
+
#define ABSL_USES_STD_ORDERING 1
|
| 664 |
+
#else
|
| 665 |
+
#error options.h is misconfigured.
|
| 666 |
+
#endif
|
| 667 |
+
|
| 668 |
+
// In debug mode, MSVC 2017's std::variant throws a EXCEPTION_ACCESS_VIOLATION
|
| 669 |
+
// SEH exception from emplace for variant<SomeStruct> when constructing the
|
| 670 |
+
// struct can throw. This defeats some of variant_test and
|
| 671 |
+
// variant_exception_safety_test.
|
| 672 |
+
#if defined(_MSC_VER) && _MSC_VER >= 1700 && defined(_DEBUG)
|
| 673 |
+
#define ABSL_INTERNAL_MSVC_2017_DBG_MODE
|
| 674 |
+
#endif
|
| 675 |
+
|
| 676 |
+
// ABSL_INTERNAL_MANGLED_NS
|
| 677 |
+
// ABSL_INTERNAL_MANGLED_BACKREFERENCE
|
| 678 |
+
//
|
| 679 |
+
// Internal macros for building up mangled names in our internal fork of CCTZ.
|
| 680 |
+
// This implementation detail is only needed and provided for the MSVC build.
|
| 681 |
+
//
|
| 682 |
+
// These macros both expand to string literals. ABSL_INTERNAL_MANGLED_NS is
|
| 683 |
+
// the mangled spelling of the `absl` namespace, and
|
| 684 |
+
// ABSL_INTERNAL_MANGLED_BACKREFERENCE is a back-reference integer representing
|
| 685 |
+
// the proper count to skip past the CCTZ fork namespace names. (This number
|
| 686 |
+
// is one larger when there is an inline namespace name to skip.)
|
| 687 |
+
#if defined(_MSC_VER)
|
| 688 |
+
#if ABSL_OPTION_USE_INLINE_NAMESPACE == 0
|
| 689 |
+
#define ABSL_INTERNAL_MANGLED_NS "absl"
|
| 690 |
+
#define ABSL_INTERNAL_MANGLED_BACKREFERENCE "5"
|
| 691 |
+
#else
|
| 692 |
+
#define ABSL_INTERNAL_MANGLED_NS \
|
| 693 |
+
ABSL_INTERNAL_TOKEN_STR(ABSL_OPTION_INLINE_NAMESPACE_NAME) "@absl"
|
| 694 |
+
#define ABSL_INTERNAL_MANGLED_BACKREFERENCE "6"
|
| 695 |
+
#endif
|
| 696 |
+
#endif
|
| 697 |
+
|
| 698 |
+
// ABSL_DLL
|
| 699 |
+
//
|
| 700 |
+
// When building Abseil as a DLL, this macro expands to `__declspec(dllexport)`
|
| 701 |
+
// so we can annotate symbols appropriately as being exported. When used in
|
| 702 |
+
// headers consuming a DLL, this macro expands to `__declspec(dllimport)` so
|
| 703 |
+
// that consumers know the symbol is defined inside the DLL. In all other cases,
|
| 704 |
+
// the macro expands to nothing.
|
| 705 |
+
#if defined(_MSC_VER)
|
| 706 |
+
#if defined(ABSL_BUILD_DLL)
|
| 707 |
+
#define ABSL_DLL __declspec(dllexport)
|
| 708 |
+
#else
|
| 709 |
+
// conda-forge addition: by default, users import the definitions defined here
|
| 710 |
+
#define ABSL_DLL __declspec(dllimport)
|
| 711 |
+
#endif
|
| 712 |
+
#else
|
| 713 |
+
#define ABSL_DLL
|
| 714 |
+
#endif // defined(_MSC_VER)
|
| 715 |
+
|
| 716 |
+
#if defined(_MSC_VER)
|
| 717 |
+
#if defined(ABSL_BUILD_TEST_DLL)
|
| 718 |
+
#define ABSL_TEST_DLL __declspec(dllexport)
|
| 719 |
+
#elif defined(ABSL_CONSUME_TEST_DLL)
|
| 720 |
+
#define ABSL_TEST_DLL __declspec(dllimport)
|
| 721 |
+
#else
|
| 722 |
+
#define ABSL_TEST_DLL
|
| 723 |
+
#endif
|
| 724 |
+
#else
|
| 725 |
+
#define ABSL_TEST_DLL
|
| 726 |
+
#endif // defined(_MSC_VER)
|
| 727 |
+
|
| 728 |
+
// ABSL_HAVE_MEMORY_SANITIZER
|
| 729 |
+
//
|
| 730 |
+
// MemorySanitizer (MSan) is a detector of uninitialized reads. It consists of
|
| 731 |
+
// a compiler instrumentation module and a run-time library.
|
| 732 |
+
#ifdef ABSL_HAVE_MEMORY_SANITIZER
|
| 733 |
+
#error "ABSL_HAVE_MEMORY_SANITIZER cannot be directly set."
|
| 734 |
+
#elif !defined(__native_client__) && ABSL_HAVE_FEATURE(memory_sanitizer)
|
| 735 |
+
#define ABSL_HAVE_MEMORY_SANITIZER 1
|
| 736 |
+
#endif
|
| 737 |
+
|
| 738 |
+
// ABSL_HAVE_THREAD_SANITIZER
|
| 739 |
+
//
|
| 740 |
+
// ThreadSanitizer (TSan) is a fast data race detector.
|
| 741 |
+
#ifdef ABSL_HAVE_THREAD_SANITIZER
|
| 742 |
+
#error "ABSL_HAVE_THREAD_SANITIZER cannot be directly set."
|
| 743 |
+
#elif defined(__SANITIZE_THREAD__)
|
| 744 |
+
#define ABSL_HAVE_THREAD_SANITIZER 1
|
| 745 |
+
#elif ABSL_HAVE_FEATURE(thread_sanitizer)
|
| 746 |
+
#define ABSL_HAVE_THREAD_SANITIZER 1
|
| 747 |
+
#endif
|
| 748 |
+
|
| 749 |
+
// ABSL_HAVE_ADDRESS_SANITIZER
|
| 750 |
+
//
|
| 751 |
+
// AddressSanitizer (ASan) is a fast memory error detector.
|
| 752 |
+
#ifdef ABSL_HAVE_ADDRESS_SANITIZER
|
| 753 |
+
#error "ABSL_HAVE_ADDRESS_SANITIZER cannot be directly set."
|
| 754 |
+
#elif defined(__SANITIZE_ADDRESS__)
|
| 755 |
+
#define ABSL_HAVE_ADDRESS_SANITIZER 1
|
| 756 |
+
#elif ABSL_HAVE_FEATURE(address_sanitizer)
|
| 757 |
+
#define ABSL_HAVE_ADDRESS_SANITIZER 1
|
| 758 |
+
#endif
|
| 759 |
+
|
| 760 |
+
// ABSL_HAVE_HWADDRESS_SANITIZER
|
| 761 |
+
//
|
| 762 |
+
// Hardware-Assisted AddressSanitizer (or HWASAN) is even faster than asan
|
| 763 |
+
// memory error detector which can use CPU features like ARM TBI, Intel LAM or
|
| 764 |
+
// AMD UAI.
|
| 765 |
+
#ifdef ABSL_HAVE_HWADDRESS_SANITIZER
|
| 766 |
+
#error "ABSL_HAVE_HWADDRESS_SANITIZER cannot be directly set."
|
| 767 |
+
#elif defined(__SANITIZE_HWADDRESS__)
|
| 768 |
+
#define ABSL_HAVE_HWADDRESS_SANITIZER 1
|
| 769 |
+
#elif ABSL_HAVE_FEATURE(hwaddress_sanitizer)
|
| 770 |
+
#define ABSL_HAVE_HWADDRESS_SANITIZER 1
|
| 771 |
+
#endif
|
| 772 |
+
|
| 773 |
+
// ABSL_HAVE_DATAFLOW_SANITIZER
|
| 774 |
+
//
|
| 775 |
+
// Dataflow Sanitizer (or DFSAN) is a generalised dynamic data flow analysis.
|
| 776 |
+
#ifdef ABSL_HAVE_DATAFLOW_SANITIZER
|
| 777 |
+
#error "ABSL_HAVE_DATAFLOW_SANITIZER cannot be directly set."
|
| 778 |
+
#elif defined(DATAFLOW_SANITIZER)
|
| 779 |
+
// GCC provides no method for detecting the presence of the standalone
|
| 780 |
+
// DataFlowSanitizer (-fsanitize=dataflow), so GCC users of -fsanitize=dataflow
|
| 781 |
+
// should also use -DDATAFLOW_SANITIZER.
|
| 782 |
+
#define ABSL_HAVE_DATAFLOW_SANITIZER 1
|
| 783 |
+
#elif ABSL_HAVE_FEATURE(dataflow_sanitizer)
|
| 784 |
+
#define ABSL_HAVE_DATAFLOW_SANITIZER 1
|
| 785 |
+
#endif
|
| 786 |
+
|
| 787 |
+
// ABSL_HAVE_LEAK_SANITIZER
|
| 788 |
+
//
|
| 789 |
+
// LeakSanitizer (or lsan) is a detector of memory leaks.
|
| 790 |
+
// https://clang.llvm.org/docs/LeakSanitizer.html
|
| 791 |
+
// https://github.com/google/sanitizers/wiki/AddressSanitizerLeakSanitizer
|
| 792 |
+
//
|
| 793 |
+
// The macro ABSL_HAVE_LEAK_SANITIZER can be used to detect at compile-time
|
| 794 |
+
// whether the LeakSanitizer is potentially available. However, just because the
|
| 795 |
+
// LeakSanitizer is available does not mean it is active. Use the
|
| 796 |
+
// always-available run-time interface in //absl/debugging/leak_check.h for
|
| 797 |
+
// interacting with LeakSanitizer.
|
| 798 |
+
#ifdef ABSL_HAVE_LEAK_SANITIZER
|
| 799 |
+
#error "ABSL_HAVE_LEAK_SANITIZER cannot be directly set."
|
| 800 |
+
#elif defined(LEAK_SANITIZER)
|
| 801 |
+
// GCC provides no method for detecting the presence of the standalone
|
| 802 |
+
// LeakSanitizer (-fsanitize=leak), so GCC users of -fsanitize=leak should also
|
| 803 |
+
// use -DLEAK_SANITIZER.
|
| 804 |
+
#define ABSL_HAVE_LEAK_SANITIZER 1
|
| 805 |
+
// Clang standalone LeakSanitizer (-fsanitize=leak)
|
| 806 |
+
#elif ABSL_HAVE_FEATURE(leak_sanitizer)
|
| 807 |
+
#define ABSL_HAVE_LEAK_SANITIZER 1
|
| 808 |
+
#elif defined(ABSL_HAVE_ADDRESS_SANITIZER)
|
| 809 |
+
// GCC or Clang using the LeakSanitizer integrated into AddressSanitizer.
|
| 810 |
+
#define ABSL_HAVE_LEAK_SANITIZER 1
|
| 811 |
+
#endif
|
| 812 |
+
|
| 813 |
+
// ABSL_HAVE_CLASS_TEMPLATE_ARGUMENT_DEDUCTION
|
| 814 |
+
//
|
| 815 |
+
// Class template argument deduction is a language feature added in C++17.
|
| 816 |
+
#ifdef ABSL_HAVE_CLASS_TEMPLATE_ARGUMENT_DEDUCTION
|
| 817 |
+
#error "ABSL_HAVE_CLASS_TEMPLATE_ARGUMENT_DEDUCTION cannot be directly set."
|
| 818 |
+
#elif defined(__cpp_deduction_guides)
|
| 819 |
+
#define ABSL_HAVE_CLASS_TEMPLATE_ARGUMENT_DEDUCTION 1
|
| 820 |
+
#endif
|
| 821 |
+
|
| 822 |
+
// ABSL_INTERNAL_NEED_REDUNDANT_CONSTEXPR_DECL
|
| 823 |
+
//
|
| 824 |
+
// Prior to C++17, static constexpr variables defined in classes required a
|
| 825 |
+
// separate definition outside of the class body, for example:
|
| 826 |
+
//
|
| 827 |
+
// class Foo {
|
| 828 |
+
// static constexpr int kBar = 0;
|
| 829 |
+
// };
|
| 830 |
+
// constexpr int Foo::kBar;
|
| 831 |
+
//
|
| 832 |
+
// In C++17, these variables defined in classes are considered inline variables,
|
| 833 |
+
// and the extra declaration is redundant. Since some compilers warn on the
|
| 834 |
+
// extra declarations, ABSL_INTERNAL_NEED_REDUNDANT_CONSTEXPR_DECL can be used
|
| 835 |
+
// conditionally ignore them:
|
| 836 |
+
//
|
| 837 |
+
// #ifdef ABSL_INTERNAL_NEED_REDUNDANT_CONSTEXPR_DECL
|
| 838 |
+
// constexpr int Foo::kBar;
|
| 839 |
+
// #endif
|
| 840 |
+
#if defined(ABSL_INTERNAL_CPLUSPLUS_LANG) && \
|
| 841 |
+
ABSL_INTERNAL_CPLUSPLUS_LANG < 201703L
|
| 842 |
+
#define ABSL_INTERNAL_NEED_REDUNDANT_CONSTEXPR_DECL 1
|
| 843 |
+
#endif
|
| 844 |
+
|
| 845 |
+
// `ABSL_INTERNAL_HAS_RTTI` determines whether abseil is being compiled with
|
| 846 |
+
// RTTI support.
|
| 847 |
+
#ifdef ABSL_INTERNAL_HAS_RTTI
|
| 848 |
+
#error ABSL_INTERNAL_HAS_RTTI cannot be directly set
|
| 849 |
+
#elif ABSL_HAVE_FEATURE(cxx_rtti)
|
| 850 |
+
#define ABSL_INTERNAL_HAS_RTTI 1
|
| 851 |
+
#elif defined(__GNUC__) && defined(__GXX_RTTI)
|
| 852 |
+
#define ABSL_INTERNAL_HAS_RTTI 1
|
| 853 |
+
#elif defined(_MSC_VER) && defined(_CPPRTTI)
|
| 854 |
+
#define ABSL_INTERNAL_HAS_RTTI 1
|
| 855 |
+
#elif !defined(__GNUC__) && !defined(_MSC_VER)
|
| 856 |
+
// Unknown compiler, default to RTTI
|
| 857 |
+
#define ABSL_INTERNAL_HAS_RTTI 1
|
| 858 |
+
#endif
|
| 859 |
+
|
| 860 |
+
// `ABSL_INTERNAL_HAS_CXA_DEMANGLE` determines whether `abi::__cxa_demangle` is
|
| 861 |
+
// available.
|
| 862 |
+
#ifdef ABSL_INTERNAL_HAS_CXA_DEMANGLE
|
| 863 |
+
#error ABSL_INTERNAL_HAS_CXA_DEMANGLE cannot be directly set
|
| 864 |
+
#elif defined(OS_ANDROID) && (defined(__i386__) || defined(__x86_64__))
|
| 865 |
+
#define ABSL_INTERNAL_HAS_CXA_DEMANGLE 0
|
| 866 |
+
#elif defined(__GNUC__)
|
| 867 |
+
#define ABSL_INTERNAL_HAS_CXA_DEMANGLE 1
|
| 868 |
+
#elif defined(__clang__) && !defined(_MSC_VER)
|
| 869 |
+
#define ABSL_INTERNAL_HAS_CXA_DEMANGLE 1
|
| 870 |
+
#endif
|
| 871 |
+
|
| 872 |
+
// ABSL_INTERNAL_HAVE_SSE is used for compile-time detection of SSE support.
|
| 873 |
+
// See https://gcc.gnu.org/onlinedocs/gcc/x86-Options.html for an overview of
|
| 874 |
+
// which architectures support the various x86 instruction sets.
|
| 875 |
+
#ifdef ABSL_INTERNAL_HAVE_SSE
|
| 876 |
+
#error ABSL_INTERNAL_HAVE_SSE cannot be directly set
|
| 877 |
+
#elif defined(__SSE__)
|
| 878 |
+
#define ABSL_INTERNAL_HAVE_SSE 1
|
| 879 |
+
#elif (defined(_M_X64) || (defined(_M_IX86_FP) && _M_IX86_FP >= 1)) && \
|
| 880 |
+
!defined(_M_ARM64EC)
|
| 881 |
+
// MSVC only defines _M_IX86_FP for x86 32-bit code, and _M_IX86_FP >= 1
|
| 882 |
+
// indicates that at least SSE was targeted with the /arch:SSE option.
|
| 883 |
+
// All x86-64 processors support SSE, so support can be assumed.
|
| 884 |
+
// https://docs.microsoft.com/en-us/cpp/preprocessor/predefined-macros
|
| 885 |
+
#define ABSL_INTERNAL_HAVE_SSE 1
|
| 886 |
+
#endif
|
| 887 |
+
|
| 888 |
+
// ABSL_INTERNAL_HAVE_SSE2 is used for compile-time detection of SSE2 support.
|
| 889 |
+
// See https://gcc.gnu.org/onlinedocs/gcc/x86-Options.html for an overview of
|
| 890 |
+
// which architectures support the various x86 instruction sets.
|
| 891 |
+
#ifdef ABSL_INTERNAL_HAVE_SSE2
|
| 892 |
+
#error ABSL_INTERNAL_HAVE_SSE2 cannot be directly set
|
| 893 |
+
#elif defined(__SSE2__)
|
| 894 |
+
#define ABSL_INTERNAL_HAVE_SSE2 1
|
| 895 |
+
#elif (defined(_M_X64) || (defined(_M_IX86_FP) && _M_IX86_FP >= 2)) && \
|
| 896 |
+
!defined(_M_ARM64EC)
|
| 897 |
+
// MSVC only defines _M_IX86_FP for x86 32-bit code, and _M_IX86_FP >= 2
|
| 898 |
+
// indicates that at least SSE2 was targeted with the /arch:SSE2 option.
|
| 899 |
+
// All x86-64 processors support SSE2, so support can be assumed.
|
| 900 |
+
// https://docs.microsoft.com/en-us/cpp/preprocessor/predefined-macros
|
| 901 |
+
#define ABSL_INTERNAL_HAVE_SSE2 1
|
| 902 |
+
#endif
|
| 903 |
+
|
| 904 |
+
// ABSL_INTERNAL_HAVE_SSSE3 is used for compile-time detection of SSSE3 support.
|
| 905 |
+
// See https://gcc.gnu.org/onlinedocs/gcc/x86-Options.html for an overview of
|
| 906 |
+
// which architectures support the various x86 instruction sets.
|
| 907 |
+
//
|
| 908 |
+
// MSVC does not have a mode that targets SSSE3 at compile-time. To use SSSE3
|
| 909 |
+
// with MSVC requires either assuming that the code will only every run on CPUs
|
| 910 |
+
// that support SSSE3, otherwise __cpuid() can be used to detect support at
|
| 911 |
+
// runtime and fallback to a non-SSSE3 implementation when SSSE3 is unsupported
|
| 912 |
+
// by the CPU.
|
| 913 |
+
#ifdef ABSL_INTERNAL_HAVE_SSSE3
|
| 914 |
+
#error ABSL_INTERNAL_HAVE_SSSE3 cannot be directly set
|
| 915 |
+
#elif defined(__SSSE3__)
|
| 916 |
+
#define ABSL_INTERNAL_HAVE_SSSE3 1
|
| 917 |
+
#endif
|
| 918 |
+
|
| 919 |
+
// ABSL_INTERNAL_HAVE_ARM_NEON is used for compile-time detection of NEON (ARM
|
| 920 |
+
// SIMD).
|
| 921 |
+
//
|
| 922 |
+
// If __CUDA_ARCH__ is defined, then we are compiling CUDA code in device mode.
|
| 923 |
+
// In device mode, NEON intrinsics are not available, regardless of host
|
| 924 |
+
// platform.
|
| 925 |
+
// https://llvm.org/docs/CompileCudaWithLLVM.html#detecting-clang-vs-nvcc-from-code
|
| 926 |
+
#ifdef ABSL_INTERNAL_HAVE_ARM_NEON
|
| 927 |
+
#error ABSL_INTERNAL_HAVE_ARM_NEON cannot be directly set
|
| 928 |
+
#elif defined(__ARM_NEON) && !(defined(__NVCC__) && defined(__CUDACC__))
|
| 929 |
+
#define ABSL_INTERNAL_HAVE_ARM_NEON 1
|
| 930 |
+
#endif
|
| 931 |
+
|
| 932 |
+
// ABSL_HAVE_CONSTANT_EVALUATED is used for compile-time detection of
|
| 933 |
+
// constant evaluation support through `absl::is_constant_evaluated`.
|
| 934 |
+
#ifdef ABSL_HAVE_CONSTANT_EVALUATED
|
| 935 |
+
#error ABSL_HAVE_CONSTANT_EVALUATED cannot be directly set
|
| 936 |
+
#endif
|
| 937 |
+
#ifdef __cpp_lib_is_constant_evaluated
|
| 938 |
+
#define ABSL_HAVE_CONSTANT_EVALUATED 1
|
| 939 |
+
#elif ABSL_HAVE_BUILTIN(__builtin_is_constant_evaluated)
|
| 940 |
+
#define ABSL_HAVE_CONSTANT_EVALUATED 1
|
| 941 |
+
#endif
|
| 942 |
+
|
| 943 |
+
// ABSL_INTERNAL_CONSTEXPR_SINCE_CXXYY is used to conditionally define constexpr
|
| 944 |
+
// for different C++ versions.
|
| 945 |
+
//
|
| 946 |
+
// These macros are an implementation detail and will be unconditionally removed
|
| 947 |
+
// once the minimum supported C++ version catches up to a given version.
|
| 948 |
+
//
|
| 949 |
+
// For this reason, this symbol is considered INTERNAL and code outside of
|
| 950 |
+
// Abseil must not use it.
|
| 951 |
+
#if defined(ABSL_INTERNAL_CPLUSPLUS_LANG) && \
|
| 952 |
+
ABSL_INTERNAL_CPLUSPLUS_LANG >= 201703L
|
| 953 |
+
#define ABSL_INTERNAL_CONSTEXPR_SINCE_CXX17 constexpr
|
| 954 |
+
#else
|
| 955 |
+
#define ABSL_INTERNAL_CONSTEXPR_SINCE_CXX17
|
| 956 |
+
#endif
|
| 957 |
+
#if defined(ABSL_INTERNAL_CPLUSPLUS_LANG) && \
|
| 958 |
+
ABSL_INTERNAL_CPLUSPLUS_LANG >= 202002L
|
| 959 |
+
#define ABSL_INTERNAL_CONSTEXPR_SINCE_CXX20 constexpr
|
| 960 |
+
#else
|
| 961 |
+
#define ABSL_INTERNAL_CONSTEXPR_SINCE_CXX20
|
| 962 |
+
#endif
|
| 963 |
+
|
| 964 |
+
// ABSL_INTERNAL_EMSCRIPTEN_VERSION combines Emscripten's three version macros
|
| 965 |
+
// into an integer that can be compared against.
|
| 966 |
+
#ifdef ABSL_INTERNAL_EMSCRIPTEN_VERSION
|
| 967 |
+
#error ABSL_INTERNAL_EMSCRIPTEN_VERSION cannot be directly set
|
| 968 |
+
#endif
|
| 969 |
+
#ifdef __EMSCRIPTEN__
|
| 970 |
+
#include <emscripten/version.h>
|
| 971 |
+
#ifdef __EMSCRIPTEN_major__
|
| 972 |
+
#if __EMSCRIPTEN_minor__ >= 1000
|
| 973 |
+
#error __EMSCRIPTEN_minor__ is too big to fit in ABSL_INTERNAL_EMSCRIPTEN_VERSION
|
| 974 |
+
#endif
|
| 975 |
+
#if __EMSCRIPTEN_tiny__ >= 1000
|
| 976 |
+
#error __EMSCRIPTEN_tiny__ is too big to fit in ABSL_INTERNAL_EMSCRIPTEN_VERSION
|
| 977 |
+
#endif
|
| 978 |
+
#define ABSL_INTERNAL_EMSCRIPTEN_VERSION \
|
| 979 |
+
((__EMSCRIPTEN_major__) * 1000000 + (__EMSCRIPTEN_minor__) * 1000 + \
|
| 980 |
+
(__EMSCRIPTEN_tiny__))
|
| 981 |
+
#endif
|
| 982 |
+
#endif
|
| 983 |
+
|
| 984 |
+
#endif // ABSL_BASE_CONFIG_H_
|
include/absl/base/const_init.h
ADDED
|
@@ -0,0 +1,76 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
// Copyright 2017 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 |
+
// -----------------------------------------------------------------------------
|
| 16 |
+
// kConstInit
|
| 17 |
+
// -----------------------------------------------------------------------------
|
| 18 |
+
//
|
| 19 |
+
// A constructor tag used to mark an object as safe for use as a global
|
| 20 |
+
// variable, avoiding the usual lifetime issues that can affect globals.
|
| 21 |
+
|
| 22 |
+
#ifndef ABSL_BASE_CONST_INIT_H_
|
| 23 |
+
#define ABSL_BASE_CONST_INIT_H_
|
| 24 |
+
|
| 25 |
+
#include "absl/base/config.h"
|
| 26 |
+
|
| 27 |
+
// In general, objects with static storage duration (such as global variables)
|
| 28 |
+
// can trigger tricky object lifetime situations. Attempting to access them
|
| 29 |
+
// from the constructors or destructors of other global objects can result in
|
| 30 |
+
// undefined behavior, unless their constructors and destructors are designed
|
| 31 |
+
// with this issue in mind.
|
| 32 |
+
//
|
| 33 |
+
// The normal way to deal with this issue in C++11 is to use constant
|
| 34 |
+
// initialization and trivial destructors.
|
| 35 |
+
//
|
| 36 |
+
// Constant initialization is guaranteed to occur before any other code
|
| 37 |
+
// executes. Constructors that are declared 'constexpr' are eligible for
|
| 38 |
+
// constant initialization. You can annotate a variable declaration with the
|
| 39 |
+
// ABSL_CONST_INIT macro to express this intent. For compilers that support
|
| 40 |
+
// it, this annotation will cause a compilation error for declarations that
|
| 41 |
+
// aren't subject to constant initialization (perhaps because a runtime value
|
| 42 |
+
// was passed as a constructor argument).
|
| 43 |
+
//
|
| 44 |
+
// On program shutdown, lifetime issues can be avoided on global objects by
|
| 45 |
+
// ensuring that they contain trivial destructors. A class has a trivial
|
| 46 |
+
// destructor unless it has a user-defined destructor, a virtual method or base
|
| 47 |
+
// class, or a data member or base class with a non-trivial destructor of its
|
| 48 |
+
// own. Objects with static storage duration and a trivial destructor are not
|
| 49 |
+
// cleaned up on program shutdown, and are thus safe to access from other code
|
| 50 |
+
// running during shutdown.
|
| 51 |
+
//
|
| 52 |
+
// For a few core Abseil classes, we make a best effort to allow for safe global
|
| 53 |
+
// instances, even though these classes have non-trivial destructors. These
|
| 54 |
+
// objects can be created with the absl::kConstInit tag. For example:
|
| 55 |
+
// ABSL_CONST_INIT absl::Mutex global_mutex(absl::kConstInit);
|
| 56 |
+
//
|
| 57 |
+
// The line above declares a global variable of type absl::Mutex which can be
|
| 58 |
+
// accessed at any point during startup or shutdown. global_mutex's destructor
|
| 59 |
+
// will still run, but will not invalidate the object. Note that C++ specifies
|
| 60 |
+
// that accessing an object after its destructor has run results in undefined
|
| 61 |
+
// behavior, but this pattern works on the toolchains we support.
|
| 62 |
+
//
|
| 63 |
+
// The absl::kConstInit tag should only be used to define objects with static
|
| 64 |
+
// or thread_local storage duration.
|
| 65 |
+
|
| 66 |
+
namespace absl {
|
| 67 |
+
ABSL_NAMESPACE_BEGIN
|
| 68 |
+
|
| 69 |
+
enum ConstInitType {
|
| 70 |
+
kConstInit,
|
| 71 |
+
};
|
| 72 |
+
|
| 73 |
+
ABSL_NAMESPACE_END
|
| 74 |
+
} // namespace absl
|
| 75 |
+
|
| 76 |
+
#endif // ABSL_BASE_CONST_INIT_H_
|
include/absl/base/dynamic_annotations.h
ADDED
|
@@ -0,0 +1,480 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
// Copyright 2017 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 |
+
// This file defines dynamic annotations for use with dynamic analysis tool
|
| 16 |
+
// such as valgrind, PIN, etc.
|
| 17 |
+
//
|
| 18 |
+
// Dynamic annotation is a source code annotation that affects the generated
|
| 19 |
+
// code (that is, the annotation is not a comment). Each such annotation is
|
| 20 |
+
// attached to a particular instruction and/or to a particular object (address)
|
| 21 |
+
// in the program.
|
| 22 |
+
//
|
| 23 |
+
// The annotations that should be used by users are macros in all upper-case
|
| 24 |
+
// (e.g., ABSL_ANNOTATE_THREAD_NAME).
|
| 25 |
+
//
|
| 26 |
+
// Actual implementation of these macros may differ depending on the dynamic
|
| 27 |
+
// analysis tool being used.
|
| 28 |
+
//
|
| 29 |
+
// This file supports the following configurations:
|
| 30 |
+
// - Dynamic Annotations enabled (with static thread-safety warnings disabled).
|
| 31 |
+
// In this case, macros expand to functions implemented by Thread Sanitizer,
|
| 32 |
+
// when building with TSan. When not provided an external implementation,
|
| 33 |
+
// dynamic_annotations.cc provides no-op implementations.
|
| 34 |
+
//
|
| 35 |
+
// - Static Clang thread-safety warnings enabled.
|
| 36 |
+
// When building with a Clang compiler that supports thread-safety warnings,
|
| 37 |
+
// a subset of annotations can be statically-checked at compile-time. We
|
| 38 |
+
// expand these macros to static-inline functions that can be analyzed for
|
| 39 |
+
// thread-safety, but afterwards elided when building the final binary.
|
| 40 |
+
//
|
| 41 |
+
// - All annotations are disabled.
|
| 42 |
+
// If neither Dynamic Annotations nor Clang thread-safety warnings are
|
| 43 |
+
// enabled, then all annotation-macros expand to empty.
|
| 44 |
+
|
| 45 |
+
#ifndef ABSL_BASE_DYNAMIC_ANNOTATIONS_H_
|
| 46 |
+
#define ABSL_BASE_DYNAMIC_ANNOTATIONS_H_
|
| 47 |
+
|
| 48 |
+
#include <stddef.h>
|
| 49 |
+
#include <stdint.h>
|
| 50 |
+
|
| 51 |
+
#include "absl/base/attributes.h"
|
| 52 |
+
#include "absl/base/config.h"
|
| 53 |
+
#ifdef __cplusplus
|
| 54 |
+
#include "absl/base/macros.h"
|
| 55 |
+
#endif
|
| 56 |
+
|
| 57 |
+
#ifdef ABSL_HAVE_HWADDRESS_SANITIZER
|
| 58 |
+
#include <sanitizer/hwasan_interface.h>
|
| 59 |
+
#endif
|
| 60 |
+
|
| 61 |
+
// TODO(rogeeff): Remove after the backward compatibility period.
|
| 62 |
+
#include "absl/base/internal/dynamic_annotations.h" // IWYU pragma: export
|
| 63 |
+
|
| 64 |
+
// -------------------------------------------------------------------------
|
| 65 |
+
// Decide which features are enabled.
|
| 66 |
+
|
| 67 |
+
#ifdef ABSL_HAVE_THREAD_SANITIZER
|
| 68 |
+
|
| 69 |
+
#define ABSL_INTERNAL_RACE_ANNOTATIONS_ENABLED 1
|
| 70 |
+
#define ABSL_INTERNAL_READS_ANNOTATIONS_ENABLED 1
|
| 71 |
+
#define ABSL_INTERNAL_WRITES_ANNOTATIONS_ENABLED 1
|
| 72 |
+
#define ABSL_INTERNAL_ANNOTALYSIS_ENABLED 0
|
| 73 |
+
#define ABSL_INTERNAL_READS_WRITES_ANNOTATIONS_ENABLED 1
|
| 74 |
+
|
| 75 |
+
#else
|
| 76 |
+
|
| 77 |
+
#define ABSL_INTERNAL_RACE_ANNOTATIONS_ENABLED 0
|
| 78 |
+
#define ABSL_INTERNAL_READS_ANNOTATIONS_ENABLED 0
|
| 79 |
+
#define ABSL_INTERNAL_WRITES_ANNOTATIONS_ENABLED 0
|
| 80 |
+
|
| 81 |
+
// Clang provides limited support for static thread-safety analysis through a
|
| 82 |
+
// feature called Annotalysis. We configure macro-definitions according to
|
| 83 |
+
// whether Annotalysis support is available. When running in opt-mode, GCC
|
| 84 |
+
// will issue a warning, if these attributes are compiled. Only include them
|
| 85 |
+
// when compiling using Clang.
|
| 86 |
+
|
| 87 |
+
#if defined(__clang__)
|
| 88 |
+
#define ABSL_INTERNAL_ANNOTALYSIS_ENABLED 1
|
| 89 |
+
#if !defined(SWIG)
|
| 90 |
+
#define ABSL_INTERNAL_IGNORE_READS_ATTRIBUTE_ENABLED 1
|
| 91 |
+
#endif
|
| 92 |
+
#else
|
| 93 |
+
#define ABSL_INTERNAL_ANNOTALYSIS_ENABLED 0
|
| 94 |
+
#endif
|
| 95 |
+
|
| 96 |
+
// Read/write annotations are enabled in Annotalysis mode; disabled otherwise.
|
| 97 |
+
#define ABSL_INTERNAL_READS_WRITES_ANNOTATIONS_ENABLED \
|
| 98 |
+
ABSL_INTERNAL_ANNOTALYSIS_ENABLED
|
| 99 |
+
|
| 100 |
+
#endif // ABSL_HAVE_THREAD_SANITIZER
|
| 101 |
+
|
| 102 |
+
#ifdef __cplusplus
|
| 103 |
+
#define ABSL_INTERNAL_BEGIN_EXTERN_C extern "C" {
|
| 104 |
+
#define ABSL_INTERNAL_END_EXTERN_C } // extern "C"
|
| 105 |
+
#define ABSL_INTERNAL_GLOBAL_SCOPED(F) ::F
|
| 106 |
+
#define ABSL_INTERNAL_STATIC_INLINE inline
|
| 107 |
+
#else
|
| 108 |
+
#define ABSL_INTERNAL_BEGIN_EXTERN_C // empty
|
| 109 |
+
#define ABSL_INTERNAL_END_EXTERN_C // empty
|
| 110 |
+
#define ABSL_INTERNAL_GLOBAL_SCOPED(F) F
|
| 111 |
+
#define ABSL_INTERNAL_STATIC_INLINE static inline
|
| 112 |
+
#endif
|
| 113 |
+
|
| 114 |
+
// -------------------------------------------------------------------------
|
| 115 |
+
// Define race annotations.
|
| 116 |
+
|
| 117 |
+
#if ABSL_INTERNAL_RACE_ANNOTATIONS_ENABLED == 1
|
| 118 |
+
// Some of the symbols used in this section (e.g. AnnotateBenignRaceSized) are
|
| 119 |
+
// defined by the compiler-based sanitizer implementation, not by the Abseil
|
| 120 |
+
// library. Therefore they do not use ABSL_INTERNAL_C_SYMBOL.
|
| 121 |
+
|
| 122 |
+
// -------------------------------------------------------------
|
| 123 |
+
// Annotations that suppress errors. It is usually better to express the
|
| 124 |
+
// program's synchronization using the other annotations, but these can be used
|
| 125 |
+
// when all else fails.
|
| 126 |
+
|
| 127 |
+
// Report that we may have a benign race at `pointer`, with size
|
| 128 |
+
// "sizeof(*(pointer))". `pointer` must be a non-void* pointer. Insert at the
|
| 129 |
+
// point where `pointer` has been allocated, preferably close to the point
|
| 130 |
+
// where the race happens. See also ABSL_ANNOTATE_BENIGN_RACE_STATIC.
|
| 131 |
+
#define ABSL_ANNOTATE_BENIGN_RACE(pointer, description) \
|
| 132 |
+
ABSL_INTERNAL_GLOBAL_SCOPED(AnnotateBenignRaceSized) \
|
| 133 |
+
(__FILE__, __LINE__, pointer, sizeof(*(pointer)), description)
|
| 134 |
+
|
| 135 |
+
// Same as ABSL_ANNOTATE_BENIGN_RACE(`address`, `description`), but applies to
|
| 136 |
+
// the memory range [`address`, `address`+`size`).
|
| 137 |
+
#define ABSL_ANNOTATE_BENIGN_RACE_SIZED(address, size, description) \
|
| 138 |
+
ABSL_INTERNAL_GLOBAL_SCOPED(AnnotateBenignRaceSized) \
|
| 139 |
+
(__FILE__, __LINE__, address, size, description)
|
| 140 |
+
|
| 141 |
+
// Enable (`enable`!=0) or disable (`enable`==0) race detection for all threads.
|
| 142 |
+
// This annotation could be useful if you want to skip expensive race analysis
|
| 143 |
+
// during some period of program execution, e.g. during initialization.
|
| 144 |
+
#define ABSL_ANNOTATE_ENABLE_RACE_DETECTION(enable) \
|
| 145 |
+
ABSL_INTERNAL_GLOBAL_SCOPED(AnnotateEnableRaceDetection) \
|
| 146 |
+
(__FILE__, __LINE__, enable)
|
| 147 |
+
|
| 148 |
+
// -------------------------------------------------------------
|
| 149 |
+
// Annotations useful for debugging.
|
| 150 |
+
|
| 151 |
+
// Report the current thread `name` to a race detector.
|
| 152 |
+
#define ABSL_ANNOTATE_THREAD_NAME(name) \
|
| 153 |
+
ABSL_INTERNAL_GLOBAL_SCOPED(AnnotateThreadName)(__FILE__, __LINE__, name)
|
| 154 |
+
|
| 155 |
+
// -------------------------------------------------------------
|
| 156 |
+
// Annotations useful when implementing locks. They are not normally needed by
|
| 157 |
+
// modules that merely use locks. The `lock` argument is a pointer to the lock
|
| 158 |
+
// object.
|
| 159 |
+
|
| 160 |
+
// Report that a lock has been created at address `lock`.
|
| 161 |
+
#define ABSL_ANNOTATE_RWLOCK_CREATE(lock) \
|
| 162 |
+
ABSL_INTERNAL_GLOBAL_SCOPED(AnnotateRWLockCreate)(__FILE__, __LINE__, lock)
|
| 163 |
+
|
| 164 |
+
// Report that a linker initialized lock has been created at address `lock`.
|
| 165 |
+
#ifdef ABSL_HAVE_THREAD_SANITIZER
|
| 166 |
+
#define ABSL_ANNOTATE_RWLOCK_CREATE_STATIC(lock) \
|
| 167 |
+
ABSL_INTERNAL_GLOBAL_SCOPED(AnnotateRWLockCreateStatic) \
|
| 168 |
+
(__FILE__, __LINE__, lock)
|
| 169 |
+
#else
|
| 170 |
+
#define ABSL_ANNOTATE_RWLOCK_CREATE_STATIC(lock) \
|
| 171 |
+
ABSL_ANNOTATE_RWLOCK_CREATE(lock)
|
| 172 |
+
#endif
|
| 173 |
+
|
| 174 |
+
// Report that the lock at address `lock` is about to be destroyed.
|
| 175 |
+
#define ABSL_ANNOTATE_RWLOCK_DESTROY(lock) \
|
| 176 |
+
ABSL_INTERNAL_GLOBAL_SCOPED(AnnotateRWLockDestroy)(__FILE__, __LINE__, lock)
|
| 177 |
+
|
| 178 |
+
// Report that the lock at address `lock` has been acquired.
|
| 179 |
+
// `is_w`=1 for writer lock, `is_w`=0 for reader lock.
|
| 180 |
+
#define ABSL_ANNOTATE_RWLOCK_ACQUIRED(lock, is_w) \
|
| 181 |
+
ABSL_INTERNAL_GLOBAL_SCOPED(AnnotateRWLockAcquired) \
|
| 182 |
+
(__FILE__, __LINE__, lock, is_w)
|
| 183 |
+
|
| 184 |
+
// Report that the lock at address `lock` is about to be released.
|
| 185 |
+
// `is_w`=1 for writer lock, `is_w`=0 for reader lock.
|
| 186 |
+
#define ABSL_ANNOTATE_RWLOCK_RELEASED(lock, is_w) \
|
| 187 |
+
ABSL_INTERNAL_GLOBAL_SCOPED(AnnotateRWLockReleased) \
|
| 188 |
+
(__FILE__, __LINE__, lock, is_w)
|
| 189 |
+
|
| 190 |
+
// Apply ABSL_ANNOTATE_BENIGN_RACE_SIZED to a static variable `static_var`.
|
| 191 |
+
#define ABSL_ANNOTATE_BENIGN_RACE_STATIC(static_var, description) \
|
| 192 |
+
namespace { \
|
| 193 |
+
class static_var##_annotator { \
|
| 194 |
+
public: \
|
| 195 |
+
static_var##_annotator() { \
|
| 196 |
+
ABSL_ANNOTATE_BENIGN_RACE_SIZED(&static_var, sizeof(static_var), \
|
| 197 |
+
#static_var ": " description); \
|
| 198 |
+
} \
|
| 199 |
+
}; \
|
| 200 |
+
static static_var##_annotator the##static_var##_annotator; \
|
| 201 |
+
} // namespace
|
| 202 |
+
|
| 203 |
+
// Function prototypes of annotations provided by the compiler-based sanitizer
|
| 204 |
+
// implementation.
|
| 205 |
+
ABSL_INTERNAL_BEGIN_EXTERN_C
|
| 206 |
+
void AnnotateRWLockCreate(const char* file, int line,
|
| 207 |
+
const volatile void* lock);
|
| 208 |
+
void AnnotateRWLockCreateStatic(const char* file, int line,
|
| 209 |
+
const volatile void* lock);
|
| 210 |
+
void AnnotateRWLockDestroy(const char* file, int line,
|
| 211 |
+
const volatile void* lock);
|
| 212 |
+
void AnnotateRWLockAcquired(const char* file, int line,
|
| 213 |
+
const volatile void* lock, long is_w); // NOLINT
|
| 214 |
+
void AnnotateRWLockReleased(const char* file, int line,
|
| 215 |
+
const volatile void* lock, long is_w); // NOLINT
|
| 216 |
+
void AnnotateBenignRace(const char* file, int line,
|
| 217 |
+
const volatile void* address, const char* description);
|
| 218 |
+
void AnnotateBenignRaceSized(const char* file, int line,
|
| 219 |
+
const volatile void* address, size_t size,
|
| 220 |
+
const char* description);
|
| 221 |
+
void AnnotateThreadName(const char* file, int line, const char* name);
|
| 222 |
+
void AnnotateEnableRaceDetection(const char* file, int line, int enable);
|
| 223 |
+
ABSL_INTERNAL_END_EXTERN_C
|
| 224 |
+
|
| 225 |
+
#else // ABSL_INTERNAL_RACE_ANNOTATIONS_ENABLED == 0
|
| 226 |
+
|
| 227 |
+
#define ABSL_ANNOTATE_RWLOCK_CREATE(lock) // empty
|
| 228 |
+
#define ABSL_ANNOTATE_RWLOCK_CREATE_STATIC(lock) // empty
|
| 229 |
+
#define ABSL_ANNOTATE_RWLOCK_DESTROY(lock) // empty
|
| 230 |
+
#define ABSL_ANNOTATE_RWLOCK_ACQUIRED(lock, is_w) // empty
|
| 231 |
+
#define ABSL_ANNOTATE_RWLOCK_RELEASED(lock, is_w) // empty
|
| 232 |
+
#define ABSL_ANNOTATE_BENIGN_RACE(address, description) // empty
|
| 233 |
+
#define ABSL_ANNOTATE_BENIGN_RACE_SIZED(address, size, description) // empty
|
| 234 |
+
#define ABSL_ANNOTATE_THREAD_NAME(name) // empty
|
| 235 |
+
#define ABSL_ANNOTATE_ENABLE_RACE_DETECTION(enable) // empty
|
| 236 |
+
#define ABSL_ANNOTATE_BENIGN_RACE_STATIC(static_var, description) // empty
|
| 237 |
+
|
| 238 |
+
#endif // ABSL_INTERNAL_RACE_ANNOTATIONS_ENABLED
|
| 239 |
+
|
| 240 |
+
// -------------------------------------------------------------------------
|
| 241 |
+
// Define memory annotations.
|
| 242 |
+
|
| 243 |
+
#ifdef ABSL_HAVE_MEMORY_SANITIZER
|
| 244 |
+
|
| 245 |
+
#include <sanitizer/msan_interface.h>
|
| 246 |
+
|
| 247 |
+
#define ABSL_ANNOTATE_MEMORY_IS_INITIALIZED(address, size) \
|
| 248 |
+
__msan_unpoison(address, size)
|
| 249 |
+
|
| 250 |
+
#define ABSL_ANNOTATE_MEMORY_IS_UNINITIALIZED(address, size) \
|
| 251 |
+
__msan_allocated_memory(address, size)
|
| 252 |
+
|
| 253 |
+
#else // !defined(ABSL_HAVE_MEMORY_SANITIZER)
|
| 254 |
+
|
| 255 |
+
#define ABSL_ANNOTATE_MEMORY_IS_INITIALIZED(address, size) // empty
|
| 256 |
+
#define ABSL_ANNOTATE_MEMORY_IS_UNINITIALIZED(address, size) // empty
|
| 257 |
+
|
| 258 |
+
#endif // ABSL_HAVE_MEMORY_SANITIZER
|
| 259 |
+
|
| 260 |
+
// -------------------------------------------------------------------------
|
| 261 |
+
// Define IGNORE_READS_BEGIN/_END attributes.
|
| 262 |
+
|
| 263 |
+
#if defined(ABSL_INTERNAL_IGNORE_READS_ATTRIBUTE_ENABLED)
|
| 264 |
+
|
| 265 |
+
#define ABSL_INTERNAL_IGNORE_READS_BEGIN_ATTRIBUTE \
|
| 266 |
+
__attribute((exclusive_lock_function("*")))
|
| 267 |
+
#define ABSL_INTERNAL_IGNORE_READS_END_ATTRIBUTE \
|
| 268 |
+
__attribute((unlock_function("*")))
|
| 269 |
+
|
| 270 |
+
#else // !defined(ABSL_INTERNAL_IGNORE_READS_ATTRIBUTE_ENABLED)
|
| 271 |
+
|
| 272 |
+
#define ABSL_INTERNAL_IGNORE_READS_BEGIN_ATTRIBUTE // empty
|
| 273 |
+
#define ABSL_INTERNAL_IGNORE_READS_END_ATTRIBUTE // empty
|
| 274 |
+
|
| 275 |
+
#endif // defined(ABSL_INTERNAL_IGNORE_READS_ATTRIBUTE_ENABLED)
|
| 276 |
+
|
| 277 |
+
// -------------------------------------------------------------------------
|
| 278 |
+
// Define IGNORE_READS_BEGIN/_END annotations.
|
| 279 |
+
|
| 280 |
+
#if ABSL_INTERNAL_READS_ANNOTATIONS_ENABLED == 1
|
| 281 |
+
// Some of the symbols used in this section (e.g. AnnotateIgnoreReadsBegin) are
|
| 282 |
+
// defined by the compiler-based implementation, not by the Abseil
|
| 283 |
+
// library. Therefore they do not use ABSL_INTERNAL_C_SYMBOL.
|
| 284 |
+
|
| 285 |
+
// Request the analysis tool to ignore all reads in the current thread until
|
| 286 |
+
// ABSL_ANNOTATE_IGNORE_READS_END is called. Useful to ignore intentional racey
|
| 287 |
+
// reads, while still checking other reads and all writes.
|
| 288 |
+
// See also ABSL_ANNOTATE_UNPROTECTED_READ.
|
| 289 |
+
#define ABSL_ANNOTATE_IGNORE_READS_BEGIN() \
|
| 290 |
+
ABSL_INTERNAL_GLOBAL_SCOPED(AnnotateIgnoreReadsBegin) \
|
| 291 |
+
(__FILE__, __LINE__)
|
| 292 |
+
|
| 293 |
+
// Stop ignoring reads.
|
| 294 |
+
#define ABSL_ANNOTATE_IGNORE_READS_END() \
|
| 295 |
+
ABSL_INTERNAL_GLOBAL_SCOPED(AnnotateIgnoreReadsEnd) \
|
| 296 |
+
(__FILE__, __LINE__)
|
| 297 |
+
|
| 298 |
+
// Function prototypes of annotations provided by the compiler-based sanitizer
|
| 299 |
+
// implementation.
|
| 300 |
+
ABSL_INTERNAL_BEGIN_EXTERN_C
|
| 301 |
+
void AnnotateIgnoreReadsBegin(const char* file, int line)
|
| 302 |
+
ABSL_INTERNAL_IGNORE_READS_BEGIN_ATTRIBUTE;
|
| 303 |
+
void AnnotateIgnoreReadsEnd(const char* file,
|
| 304 |
+
int line) ABSL_INTERNAL_IGNORE_READS_END_ATTRIBUTE;
|
| 305 |
+
ABSL_INTERNAL_END_EXTERN_C
|
| 306 |
+
|
| 307 |
+
#elif defined(ABSL_INTERNAL_ANNOTALYSIS_ENABLED)
|
| 308 |
+
|
| 309 |
+
// When Annotalysis is enabled without Dynamic Annotations, the use of
|
| 310 |
+
// static-inline functions allows the annotations to be read at compile-time,
|
| 311 |
+
// while still letting the compiler elide the functions from the final build.
|
| 312 |
+
//
|
| 313 |
+
// TODO(delesley) -- The exclusive lock here ignores writes as well, but
|
| 314 |
+
// allows IGNORE_READS_AND_WRITES to work properly.
|
| 315 |
+
|
| 316 |
+
#define ABSL_ANNOTATE_IGNORE_READS_BEGIN() \
|
| 317 |
+
ABSL_INTERNAL_GLOBAL_SCOPED( \
|
| 318 |
+
ABSL_INTERNAL_C_SYMBOL(AbslInternalAnnotateIgnoreReadsBegin)) \
|
| 319 |
+
()
|
| 320 |
+
|
| 321 |
+
#define ABSL_ANNOTATE_IGNORE_READS_END() \
|
| 322 |
+
ABSL_INTERNAL_GLOBAL_SCOPED( \
|
| 323 |
+
ABSL_INTERNAL_C_SYMBOL(AbslInternalAnnotateIgnoreReadsEnd)) \
|
| 324 |
+
()
|
| 325 |
+
|
| 326 |
+
ABSL_INTERNAL_STATIC_INLINE void ABSL_INTERNAL_C_SYMBOL(
|
| 327 |
+
AbslInternalAnnotateIgnoreReadsBegin)()
|
| 328 |
+
ABSL_INTERNAL_IGNORE_READS_BEGIN_ATTRIBUTE {}
|
| 329 |
+
|
| 330 |
+
ABSL_INTERNAL_STATIC_INLINE void ABSL_INTERNAL_C_SYMBOL(
|
| 331 |
+
AbslInternalAnnotateIgnoreReadsEnd)()
|
| 332 |
+
ABSL_INTERNAL_IGNORE_READS_END_ATTRIBUTE {}
|
| 333 |
+
|
| 334 |
+
#else
|
| 335 |
+
|
| 336 |
+
#define ABSL_ANNOTATE_IGNORE_READS_BEGIN() // empty
|
| 337 |
+
#define ABSL_ANNOTATE_IGNORE_READS_END() // empty
|
| 338 |
+
|
| 339 |
+
#endif
|
| 340 |
+
|
| 341 |
+
// -------------------------------------------------------------------------
|
| 342 |
+
// Define IGNORE_WRITES_BEGIN/_END annotations.
|
| 343 |
+
|
| 344 |
+
#if ABSL_INTERNAL_WRITES_ANNOTATIONS_ENABLED == 1
|
| 345 |
+
|
| 346 |
+
// Similar to ABSL_ANNOTATE_IGNORE_READS_BEGIN, but ignore writes instead.
|
| 347 |
+
#define ABSL_ANNOTATE_IGNORE_WRITES_BEGIN() \
|
| 348 |
+
ABSL_INTERNAL_GLOBAL_SCOPED(AnnotateIgnoreWritesBegin)(__FILE__, __LINE__)
|
| 349 |
+
|
| 350 |
+
// Stop ignoring writes.
|
| 351 |
+
#define ABSL_ANNOTATE_IGNORE_WRITES_END() \
|
| 352 |
+
ABSL_INTERNAL_GLOBAL_SCOPED(AnnotateIgnoreWritesEnd)(__FILE__, __LINE__)
|
| 353 |
+
|
| 354 |
+
// Function prototypes of annotations provided by the compiler-based sanitizer
|
| 355 |
+
// implementation.
|
| 356 |
+
ABSL_INTERNAL_BEGIN_EXTERN_C
|
| 357 |
+
void AnnotateIgnoreWritesBegin(const char* file, int line);
|
| 358 |
+
void AnnotateIgnoreWritesEnd(const char* file, int line);
|
| 359 |
+
ABSL_INTERNAL_END_EXTERN_C
|
| 360 |
+
|
| 361 |
+
#else
|
| 362 |
+
|
| 363 |
+
#define ABSL_ANNOTATE_IGNORE_WRITES_BEGIN() // empty
|
| 364 |
+
#define ABSL_ANNOTATE_IGNORE_WRITES_END() // empty
|
| 365 |
+
|
| 366 |
+
#endif
|
| 367 |
+
|
| 368 |
+
// -------------------------------------------------------------------------
|
| 369 |
+
// Define the ABSL_ANNOTATE_IGNORE_READS_AND_WRITES_* annotations using the more
|
| 370 |
+
// primitive annotations defined above.
|
| 371 |
+
//
|
| 372 |
+
// Instead of doing
|
| 373 |
+
// ABSL_ANNOTATE_IGNORE_READS_BEGIN();
|
| 374 |
+
// ... = x;
|
| 375 |
+
// ABSL_ANNOTATE_IGNORE_READS_END();
|
| 376 |
+
// one can use
|
| 377 |
+
// ... = ABSL_ANNOTATE_UNPROTECTED_READ(x);
|
| 378 |
+
|
| 379 |
+
#if defined(ABSL_INTERNAL_READS_WRITES_ANNOTATIONS_ENABLED)
|
| 380 |
+
|
| 381 |
+
// Start ignoring all memory accesses (both reads and writes).
|
| 382 |
+
#define ABSL_ANNOTATE_IGNORE_READS_AND_WRITES_BEGIN() \
|
| 383 |
+
do { \
|
| 384 |
+
ABSL_ANNOTATE_IGNORE_READS_BEGIN(); \
|
| 385 |
+
ABSL_ANNOTATE_IGNORE_WRITES_BEGIN(); \
|
| 386 |
+
} while (0)
|
| 387 |
+
|
| 388 |
+
// Stop ignoring both reads and writes.
|
| 389 |
+
#define ABSL_ANNOTATE_IGNORE_READS_AND_WRITES_END() \
|
| 390 |
+
do { \
|
| 391 |
+
ABSL_ANNOTATE_IGNORE_WRITES_END(); \
|
| 392 |
+
ABSL_ANNOTATE_IGNORE_READS_END(); \
|
| 393 |
+
} while (0)
|
| 394 |
+
|
| 395 |
+
#ifdef __cplusplus
|
| 396 |
+
// ABSL_ANNOTATE_UNPROTECTED_READ is the preferred way to annotate racey reads.
|
| 397 |
+
#define ABSL_ANNOTATE_UNPROTECTED_READ(x) \
|
| 398 |
+
absl::base_internal::AnnotateUnprotectedRead(x)
|
| 399 |
+
|
| 400 |
+
namespace absl {
|
| 401 |
+
ABSL_NAMESPACE_BEGIN
|
| 402 |
+
namespace base_internal {
|
| 403 |
+
|
| 404 |
+
template <typename T>
|
| 405 |
+
inline T AnnotateUnprotectedRead(const volatile T& x) { // NOLINT
|
| 406 |
+
ABSL_ANNOTATE_IGNORE_READS_BEGIN();
|
| 407 |
+
T res = x;
|
| 408 |
+
ABSL_ANNOTATE_IGNORE_READS_END();
|
| 409 |
+
return res;
|
| 410 |
+
}
|
| 411 |
+
|
| 412 |
+
} // namespace base_internal
|
| 413 |
+
ABSL_NAMESPACE_END
|
| 414 |
+
} // namespace absl
|
| 415 |
+
#endif
|
| 416 |
+
|
| 417 |
+
#else
|
| 418 |
+
|
| 419 |
+
#define ABSL_ANNOTATE_IGNORE_READS_AND_WRITES_BEGIN() // empty
|
| 420 |
+
#define ABSL_ANNOTATE_IGNORE_READS_AND_WRITES_END() // empty
|
| 421 |
+
#define ABSL_ANNOTATE_UNPROTECTED_READ(x) (x)
|
| 422 |
+
|
| 423 |
+
#endif
|
| 424 |
+
|
| 425 |
+
// -------------------------------------------------------------------------
|
| 426 |
+
// Address sanitizer annotations
|
| 427 |
+
|
| 428 |
+
#ifdef ABSL_HAVE_ADDRESS_SANITIZER
|
| 429 |
+
// Describe the current state of a contiguous container such as e.g.
|
| 430 |
+
// std::vector or std::string. For more details see
|
| 431 |
+
// sanitizer/common_interface_defs.h, which is provided by the compiler.
|
| 432 |
+
#include <sanitizer/common_interface_defs.h>
|
| 433 |
+
|
| 434 |
+
#define ABSL_ANNOTATE_CONTIGUOUS_CONTAINER(beg, end, old_mid, new_mid) \
|
| 435 |
+
__sanitizer_annotate_contiguous_container(beg, end, old_mid, new_mid)
|
| 436 |
+
#define ABSL_ADDRESS_SANITIZER_REDZONE(name) \
|
| 437 |
+
struct { \
|
| 438 |
+
alignas(8) char x[8]; \
|
| 439 |
+
} name
|
| 440 |
+
|
| 441 |
+
#else
|
| 442 |
+
|
| 443 |
+
#define ABSL_ANNOTATE_CONTIGUOUS_CONTAINER(beg, end, old_mid, new_mid) // empty
|
| 444 |
+
#define ABSL_ADDRESS_SANITIZER_REDZONE(name) static_assert(true, "")
|
| 445 |
+
|
| 446 |
+
#endif // ABSL_HAVE_ADDRESS_SANITIZER
|
| 447 |
+
|
| 448 |
+
// -------------------------------------------------------------------------
|
| 449 |
+
// HWAddress sanitizer annotations
|
| 450 |
+
|
| 451 |
+
#ifdef __cplusplus
|
| 452 |
+
namespace absl {
|
| 453 |
+
#ifdef ABSL_HAVE_HWADDRESS_SANITIZER
|
| 454 |
+
// Under HWASAN changes the tag of the pointer.
|
| 455 |
+
template <typename T>
|
| 456 |
+
T* HwasanTagPointer(T* ptr, uintptr_t tag) {
|
| 457 |
+
return reinterpret_cast<T*>(__hwasan_tag_pointer(ptr, tag));
|
| 458 |
+
}
|
| 459 |
+
#else
|
| 460 |
+
template <typename T>
|
| 461 |
+
T* HwasanTagPointer(T* ptr, uintptr_t) {
|
| 462 |
+
return ptr;
|
| 463 |
+
}
|
| 464 |
+
#endif
|
| 465 |
+
} // namespace absl
|
| 466 |
+
#endif
|
| 467 |
+
|
| 468 |
+
// -------------------------------------------------------------------------
|
| 469 |
+
// Undefine the macros intended only for this file.
|
| 470 |
+
|
| 471 |
+
#undef ABSL_INTERNAL_RACE_ANNOTATIONS_ENABLED
|
| 472 |
+
#undef ABSL_INTERNAL_READS_ANNOTATIONS_ENABLED
|
| 473 |
+
#undef ABSL_INTERNAL_WRITES_ANNOTATIONS_ENABLED
|
| 474 |
+
#undef ABSL_INTERNAL_ANNOTALYSIS_ENABLED
|
| 475 |
+
#undef ABSL_INTERNAL_READS_WRITES_ANN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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
// Copyright 2017 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_BASE_INTERNAL_ATOMIC_HOOK_H_
|
| 16 |
+
#define ABSL_BASE_INTERNAL_ATOMIC_HOOK_H_
|
| 17 |
+
|
| 18 |
+
#include <atomic>
|
| 19 |
+
#include <cassert>
|
| 20 |
+
#include <cstdint>
|
| 21 |
+
#include <utility>
|
| 22 |
+
|
| 23 |
+
#include "absl/base/attributes.h"
|
| 24 |
+
#include "absl/base/config.h"
|
| 25 |
+
|
| 26 |
+
#if defined(_MSC_VER) && !defined(__clang__)
|
| 27 |
+
#define ABSL_HAVE_WORKING_CONSTEXPR_STATIC_INIT 0
|
| 28 |
+
#else
|
| 29 |
+
#define ABSL_HAVE_WORKING_CONSTEXPR_STATIC_INIT 1
|
| 30 |
+
#endif
|
| 31 |
+
|
| 32 |
+
#if defined(_MSC_VER)
|
| 33 |
+
#define ABSL_HAVE_WORKING_ATOMIC_POINTER 0
|
| 34 |
+
#else
|
| 35 |
+
#define ABSL_HAVE_WORKING_ATOMIC_POINTER 1
|
| 36 |
+
#endif
|
| 37 |
+
|
| 38 |
+
namespace absl {
|
| 39 |
+
ABSL_NAMESPACE_BEGIN
|
| 40 |
+
namespace base_internal {
|
| 41 |
+
|
| 42 |
+
template <typename T>
|
| 43 |
+
class AtomicHook;
|
| 44 |
+
|
| 45 |
+
// To workaround AtomicHook not being constant-initializable on some platforms,
|
| 46 |
+
// prefer to annotate instances with `ABSL_INTERNAL_ATOMIC_HOOK_ATTRIBUTES`
|
| 47 |
+
// instead of `ABSL_CONST_INIT`.
|
| 48 |
+
#if ABSL_HAVE_WORKING_CONSTEXPR_STATIC_INIT
|
| 49 |
+
#define ABSL_INTERNAL_ATOMIC_HOOK_ATTRIBUTES ABSL_CONST_INIT
|
| 50 |
+
#else
|
| 51 |
+
#define ABSL_INTERNAL_ATOMIC_HOOK_ATTRIBUTES
|
| 52 |
+
#endif
|
| 53 |
+
|
| 54 |
+
// `AtomicHook` is a helper class, templatized on a raw function pointer type,
|
| 55 |
+
// for implementing Abseil customization hooks. It is a callable object that
|
| 56 |
+
// dispatches to the registered hook. Objects of type `AtomicHook` must have
|
| 57 |
+
// static or thread storage duration.
|
| 58 |
+
//
|
| 59 |
+
// A default constructed object performs a no-op (and returns a default
|
| 60 |
+
// constructed object) if no hook has been registered.
|
| 61 |
+
//
|
| 62 |
+
// Hooks can be pre-registered via constant initialization, for example:
|
| 63 |
+
//
|
| 64 |
+
// ABSL_INTERNAL_ATOMIC_HOOK_ATTRIBUTES static AtomicHook<void(*)()>
|
| 65 |
+
// my_hook(DefaultAction);
|
| 66 |
+
//
|
| 67 |
+
// and then changed at runtime via a call to `Store()`.
|
| 68 |
+
//
|
| 69 |
+
// Reads and writes guarantee memory_order_acquire/memory_order_release
|
| 70 |
+
// semantics.
|
| 71 |
+
template <typename ReturnType, typename... Args>
|
| 72 |
+
class AtomicHook<ReturnType (*)(Args...)> {
|
| 73 |
+
public:
|
| 74 |
+
using FnPtr = ReturnType (*)(Args...);
|
| 75 |
+
|
| 76 |
+
// Constructs an object that by default performs a no-op (and
|
| 77 |
+
// returns a default constructed object) when no hook as been registered.
|
| 78 |
+
constexpr AtomicHook() : AtomicHook(DummyFunction) {}
|
| 79 |
+
|
| 80 |
+
// Constructs an object that by default dispatches to/returns the
|
| 81 |
+
// pre-registered default_fn when no hook has been registered at runtime.
|
| 82 |
+
#if ABSL_HAVE_WORKING_ATOMIC_POINTER && ABSL_HAVE_WORKING_CONSTEXPR_STATIC_INIT
|
| 83 |
+
explicit constexpr AtomicHook(FnPtr default_fn)
|
| 84 |
+
: hook_(default_fn), default_fn_(default_fn) {}
|
| 85 |
+
#elif ABSL_HAVE_WORKING_CONSTEXPR_STATIC_INIT
|
| 86 |
+
explicit constexpr AtomicHook(FnPtr default_fn)
|
| 87 |
+
: hook_(kUninitialized), default_fn_(default_fn) {}
|
| 88 |
+
#else
|
| 89 |
+
// As of January 2020, on all known versions of MSVC this constructor runs in
|
| 90 |
+
// the global constructor sequence. If `Store()` is called by a dynamic
|
| 91 |
+
// initializer, we want to preserve the value, even if this constructor runs
|
| 92 |
+
// after the call to `Store()`. If not, `hook_` will be
|
| 93 |
+
// zero-initialized by the linker and we have no need to set it.
|
| 94 |
+
// https://developercommunity.visualstudio.com/content/problem/336946/class-with-constexpr-constructor-not-using-static.html
|
| 95 |
+
explicit constexpr AtomicHook(FnPtr default_fn)
|
| 96 |
+
: /* hook_(deliberately omitted), */ default_fn_(default_fn) {
|
| 97 |
+
static_assert(kUninitialized == 0, "here we rely on zero-initialization");
|
| 98 |
+
}
|
| 99 |
+
#endif
|
| 100 |
+
|
| 101 |
+
// Stores the provided function pointer as the value for this hook.
|
| 102 |
+
//
|
| 103 |
+
// This is intended to be called once. Multiple calls are legal only if the
|
| 104 |
+
// same function pointer is provided for each call. The store is implemented
|
| 105 |
+
// as a memory_order_release operation, and read accesses are implemented as
|
| 106 |
+
// memory_order_acquire.
|
| 107 |
+
void Store(FnPtr fn) {
|
| 108 |
+
bool success = DoStore(fn);
|
| 109 |
+
static_cast<void>(success);
|
| 110 |
+
assert(success);
|
| 111 |
+
}
|
| 112 |
+
|
| 113 |
+
// Invokes the registered callback. If no callback has yet been registered, a
|
| 114 |
+
// default-constructed object of the appropriate type is returned instead.
|
| 115 |
+
template <typename... CallArgs>
|
| 116 |
+
ReturnType operator()(CallArgs&&... args) const {
|
| 117 |
+
return DoLoad()(std::forward<CallArgs>(args)...);
|
| 118 |
+
}
|
| 119 |
+
|
| 120 |
+
// Returns the registered callback, or nullptr if none has been registered.
|
| 121 |
+
// Useful if client code needs to conditionalize behavior based on whether a
|
| 122 |
+
// callback was registered.
|
| 123 |
+
//
|
| 124 |
+
// Note that atomic_hook.Load()() and atomic_hook() have different semantics:
|
| 125 |
+
// operator()() will perform a no-op if no callback was registered, while
|
| 126 |
+
// Load()() will dereference a null function pointer. Prefer operator()() to
|
| 127 |
+
// Load()() unless you must conditionalize behavior on whether a hook was
|
| 128 |
+
// registered.
|
| 129 |
+
FnPtr Load() const {
|
| 130 |
+
FnPtr ptr = DoLoad();
|
| 131 |
+
return (ptr == DummyFunction) ? nullptr : ptr;
|
| 132 |
+
}
|
| 133 |
+
|
| 134 |
+
private:
|
| 135 |
+
static ReturnType DummyFunction(Args...) {
|
| 136 |
+
return ReturnType();
|
| 137 |
+
}
|
| 138 |
+
|
| 139 |
+
// Current versions of MSVC (as of September 2017) have a broken
|
| 140 |
+
// implementation of std::atomic<T*>: Its constructor attempts to do the
|
| 141 |
+
// equivalent of a reinterpret_cast in a constexpr context, which is not
|
| 142 |
+
// allowed.
|
| 143 |
+
//
|
| 144 |
+
// This causes an issue when building with LLVM under Windows. To avoid this,
|
| 145 |
+
// we use a less-efficient, intptr_t-based implementation on Windows.
|
| 146 |
+
#if ABSL_HAVE_WORKING_ATOMIC_POINTER
|
| 147 |
+
// Return the stored value, or DummyFunction if no value has been stored.
|
| 148 |
+
FnPtr DoLoad() const { return hook_.load(std::memory_order_acquire); }
|
| 149 |
+
|
| 150 |
+
// Store the given value. Returns false if a different value was already
|
| 151 |
+
// stored to this object.
|
| 152 |
+
bool DoStore(FnPtr fn) {
|
| 153 |
+
assert(fn);
|
| 154 |
+
FnPtr expected = default_fn_;
|
| 155 |
+
const bool store_succeeded = hook_.compare_exchange_strong(
|
| 156 |
+
expected, fn, std::memory_order_acq_rel, std::memory_order_acquire);
|
| 157 |
+
const bool same_value_already_stored = (expected == fn);
|
| 158 |
+
return store_succeeded || same_value_already_stored;
|
| 159 |
+
}
|
| 160 |
+
|
| 161 |
+
std::atomic<FnPtr> hook_;
|
| 162 |
+
#else // !ABSL_HAVE_WORKING_ATOMIC_POINTER
|
| 163 |
+
// Use a sentinel value unlikely to be the address of an actual function.
|
| 164 |
+
static constexpr intptr_t kUninitialized = 0;
|
| 165 |
+
|
| 166 |
+
static_assert(sizeof(intptr_t) >= sizeof(FnPtr),
|
| 167 |
+
"intptr_t can't contain a function pointer");
|
| 168 |
+
|
| 169 |
+
FnPtr DoLoad() const {
|
| 170 |
+
const intptr_t value = hook_.load(std::memory_order_acquire);
|
| 171 |
+
if (value == kUninitialized) {
|
| 172 |
+
return default_fn_;
|
| 173 |
+
}
|
| 174 |
+
return reinterpret_cast<FnPtr>(value);
|
| 175 |
+
}
|
| 176 |
+
|
| 177 |
+
bool DoStore(FnPtr fn) {
|
| 178 |
+
assert(fn);
|
| 179 |
+
const auto value = reinterpret_cast<intptr_t>(fn);
|
| 180 |
+
intptr_t expected = kUninitialized;
|
| 181 |
+
const bool store_succeeded = hook_.compare_exchange_strong(
|
| 182 |
+
expected, value, std::memory_order_acq_rel, std::memory_order_acquire);
|
| 183 |
+
const bool same_value_already_stored = (expected == value);
|
| 184 |
+
return store_succeeded || same_value_already_stored;
|
| 185 |
+
}
|
| 186 |
+
|
| 187 |
+
std::atomic<intptr_t> hook_;
|
| 188 |
+
#endif
|
| 189 |
+
|
| 190 |
+
const FnPtr default_fn_;
|
| 191 |
+
};
|
| 192 |
+
|
| 193 |
+
#undef ABSL_HAVE_WORKING_ATOMIC_POINTER
|
| 194 |
+
#undef ABSL_HAVE_WORKING_CONSTEXPR_STATIC_INIT
|
| 195 |
+
|
| 196 |
+
} // namespace base_internal
|
| 197 |
+
ABSL_NAMESPACE_END
|
| 198 |
+
} // namespace absl
|
| 199 |
+
|
| 200 |
+
#endif // ABSL_BASE_INTERNAL_ATOMIC_HOOK_H_
|
include/absl/base/internal/atomic_hook_test_helper.h
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
// Copyright 2017 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_BASE_INTERNAL_ATOMIC_HOOK_TEST_HELPER_H_
|
| 16 |
+
#define ABSL_BASE_INTERNAL_ATOMIC_HOOK_TEST_HELPER_H_
|
| 17 |
+
|
| 18 |
+
#include "absl/base/internal/atomic_hook.h"
|
| 19 |
+
|
| 20 |
+
namespace absl {
|
| 21 |
+
ABSL_NAMESPACE_BEGIN
|
| 22 |
+
namespace atomic_hook_internal {
|
| 23 |
+
|
| 24 |
+
using VoidF = void (*)();
|
| 25 |
+
extern absl::base_internal::AtomicHook<VoidF> func;
|
| 26 |
+
extern int default_func_calls;
|
| 27 |
+
void DefaultFunc();
|
| 28 |
+
void RegisterFunc(VoidF func);
|
| 29 |
+
|
| 30 |
+
} // namespace atomic_hook_internal
|
| 31 |
+
ABSL_NAMESPACE_END
|
| 32 |
+
} // namespace absl
|
| 33 |
+
|
| 34 |
+
#endif // ABSL_BASE_INTERNAL_ATOMIC_HOOK_TEST_HELPER_H_
|
include/absl/base/internal/cycleclock.h
ADDED
|
@@ -0,0 +1,144 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
//
|
| 2 |
+
// Copyright 2017 The Abseil Authors.
|
| 3 |
+
//
|
| 4 |
+
// Licensed under the Apache License, Version 2.0 (the "License");
|
| 5 |
+
// you may not use this file except in compliance with the License.
|
| 6 |
+
// You may obtain a copy of the License at
|
| 7 |
+
//
|
| 8 |
+
// https://www.apache.org/licenses/LICENSE-2.0
|
| 9 |
+
//
|
| 10 |
+
// Unless required by applicable law or agreed to in writing, software
|
| 11 |
+
// distributed under the License is distributed on an "AS IS" BASIS,
|
| 12 |
+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
| 13 |
+
// See the License for the specific language governing permissions and
|
| 14 |
+
// limitations under the License.
|
| 15 |
+
//
|
| 16 |
+
|
| 17 |
+
// -----------------------------------------------------------------------------
|
| 18 |
+
// File: cycleclock.h
|
| 19 |
+
// -----------------------------------------------------------------------------
|
| 20 |
+
//
|
| 21 |
+
// This header file defines a `CycleClock`, which yields the value and frequency
|
| 22 |
+
// of a cycle counter that increments at a rate that is approximately constant.
|
| 23 |
+
//
|
| 24 |
+
// NOTE:
|
| 25 |
+
//
|
| 26 |
+
// The cycle counter frequency is not necessarily related to the core clock
|
| 27 |
+
// frequency and should not be treated as such. That is, `CycleClock` cycles are
|
| 28 |
+
// not necessarily "CPU cycles" and code should not rely on that behavior, even
|
| 29 |
+
// if experimentally observed.
|
| 30 |
+
//
|
| 31 |
+
// An arbitrary offset may have been added to the counter at power on.
|
| 32 |
+
//
|
| 33 |
+
// On some platforms, the rate and offset of the counter may differ
|
| 34 |
+
// slightly when read from different CPUs of a multiprocessor. Usually,
|
| 35 |
+
// we try to ensure that the operating system adjusts values periodically
|
| 36 |
+
// so that values agree approximately. If you need stronger guarantees,
|
| 37 |
+
// consider using alternate interfaces.
|
| 38 |
+
//
|
| 39 |
+
// The CPU is not required to maintain the ordering of a cycle counter read
|
| 40 |
+
// with respect to surrounding instructions.
|
| 41 |
+
|
| 42 |
+
#ifndef ABSL_BASE_INTERNAL_CYCLECLOCK_H_
|
| 43 |
+
#define ABSL_BASE_INTERNAL_CYCLECLOCK_H_
|
| 44 |
+
|
| 45 |
+
#include <atomic>
|
| 46 |
+
#include <cstdint>
|
| 47 |
+
|
| 48 |
+
#include "absl/base/attributes.h"
|
| 49 |
+
#include "absl/base/config.h"
|
| 50 |
+
#include "absl/base/internal/cycleclock_config.h"
|
| 51 |
+
#include "absl/base/internal/unscaledcycleclock.h"
|
| 52 |
+
|
| 53 |
+
namespace absl {
|
| 54 |
+
ABSL_NAMESPACE_BEGIN
|
| 55 |
+
namespace base_internal {
|
| 56 |
+
|
| 57 |
+
using CycleClockSourceFunc = int64_t (*)();
|
| 58 |
+
|
| 59 |
+
// -----------------------------------------------------------------------------
|
| 60 |
+
// CycleClock
|
| 61 |
+
// -----------------------------------------------------------------------------
|
| 62 |
+
class CycleClock {
|
| 63 |
+
public:
|
| 64 |
+
// CycleClock::Now()
|
| 65 |
+
//
|
| 66 |
+
// Returns the value of a cycle counter that counts at a rate that is
|
| 67 |
+
// approximately constant.
|
| 68 |
+
static int64_t Now();
|
| 69 |
+
|
| 70 |
+
// CycleClock::Frequency()
|
| 71 |
+
//
|
| 72 |
+
// Returns the amount by which `CycleClock::Now()` increases per second. Note
|
| 73 |
+
// that this value may not necessarily match the core CPU clock frequency.
|
| 74 |
+
static double Frequency();
|
| 75 |
+
|
| 76 |
+
private:
|
| 77 |
+
#if ABSL_USE_UNSCALED_CYCLECLOCK
|
| 78 |
+
static CycleClockSourceFunc LoadCycleClockSource();
|
| 79 |
+
|
| 80 |
+
static constexpr int32_t kShift = kCycleClockShift;
|
| 81 |
+
static constexpr double kFrequencyScale = kCycleClockFrequencyScale;
|
| 82 |
+
|
| 83 |
+
ABSL_CONST_INIT static std::atomic<CycleClockSourceFunc> cycle_clock_source_;
|
| 84 |
+
#endif // ABSL_USE_UNSCALED_CYCLECLOC
|
| 85 |
+
|
| 86 |
+
CycleClock() = delete; // no instances
|
| 87 |
+
CycleClock(const CycleClock&) = delete;
|
| 88 |
+
CycleClock& operator=(const CycleClock&) = delete;
|
| 89 |
+
|
| 90 |
+
friend class CycleClockSource;
|
| 91 |
+
};
|
| 92 |
+
|
| 93 |
+
class CycleClockSource {
|
| 94 |
+
private:
|
| 95 |
+
// CycleClockSource::Register()
|
| 96 |
+
//
|
| 97 |
+
// Register a function that provides an alternate source for the unscaled CPU
|
| 98 |
+
// cycle count value. The source function must be async signal safe, must not
|
| 99 |
+
// call CycleClock::Now(), and must have a frequency that matches that of the
|
| 100 |
+
// unscaled clock used by CycleClock. A nullptr value resets CycleClock to use
|
| 101 |
+
// the default source.
|
| 102 |
+
static void Register(CycleClockSourceFunc source);
|
| 103 |
+
};
|
| 104 |
+
|
| 105 |
+
#if ABSL_USE_UNSCALED_CYCLECLOCK
|
| 106 |
+
|
| 107 |
+
inline CycleClockSourceFunc CycleClock::LoadCycleClockSource() {
|
| 108 |
+
#if !defined(__x86_64__)
|
| 109 |
+
// Optimize for the common case (no callback) by first doing a relaxed load;
|
| 110 |
+
// this is significantly faster on non-x86 platforms.
|
| 111 |
+
if (cycle_clock_source_.load(std::memory_order_relaxed) == nullptr) {
|
| 112 |
+
return nullptr;
|
| 113 |
+
}
|
| 114 |
+
#endif // !defined(__x86_64__)
|
| 115 |
+
|
| 116 |
+
// This corresponds to the store(std::memory_order_release) in
|
| 117 |
+
// CycleClockSource::Register, and makes sure that any updates made prior to
|
| 118 |
+
// registering the callback are visible to this thread before the callback
|
| 119 |
+
// is invoked.
|
| 120 |
+
return cycle_clock_source_.load(std::memory_order_acquire);
|
| 121 |
+
}
|
| 122 |
+
|
| 123 |
+
// Accessing globals in inlined code in Window DLLs is problematic.
|
| 124 |
+
#ifndef _WIN32
|
| 125 |
+
inline int64_t CycleClock::Now() {
|
| 126 |
+
auto fn = LoadCycleClockSource();
|
| 127 |
+
if (fn == nullptr) {
|
| 128 |
+
return base_internal::UnscaledCycleClock::Now() >> kShift;
|
| 129 |
+
}
|
| 130 |
+
return fn() >> kShift;
|
| 131 |
+
}
|
| 132 |
+
#endif
|
| 133 |
+
|
| 134 |
+
inline double CycleClock::Frequency() {
|
| 135 |
+
return kFrequencyScale * base_internal::UnscaledCycleClock::Frequency();
|
| 136 |
+
}
|
| 137 |
+
|
| 138 |
+
#endif // ABSL_USE_UNSCALED_CYCLECLOCK
|
| 139 |
+
|
| 140 |
+
} // namespace base_internal
|
| 141 |
+
ABSL_NAMESPACE_END
|
| 142 |
+
} // namespace absl
|
| 143 |
+
|
| 144 |
+
#endif // ABSL_BASE_INTERNAL_CYCLECLOCK_H_
|
include/absl/base/internal/cycleclock_config.h
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
// Copyright 2022 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_BASE_INTERNAL_CYCLECLOCK_CONFIG_H_
|
| 16 |
+
#define ABSL_BASE_INTERNAL_CYCLECLOCK_CONFIG_H_
|
| 17 |
+
|
| 18 |
+
#include <cstdint>
|
| 19 |
+
|
| 20 |
+
#include "absl/base/config.h"
|
| 21 |
+
#include "absl/base/internal/inline_variable.h"
|
| 22 |
+
#include "absl/base/internal/unscaledcycleclock_config.h"
|
| 23 |
+
|
| 24 |
+
namespace absl {
|
| 25 |
+
ABSL_NAMESPACE_BEGIN
|
| 26 |
+
namespace base_internal {
|
| 27 |
+
|
| 28 |
+
#if ABSL_USE_UNSCALED_CYCLECLOCK
|
| 29 |
+
#ifdef NDEBUG
|
| 30 |
+
#ifdef ABSL_INTERNAL_UNSCALED_CYCLECLOCK_FREQUENCY_IS_CPU_FREQUENCY
|
| 31 |
+
// Not debug mode and the UnscaledCycleClock frequency is the CPU
|
| 32 |
+
// frequency. Scale the CycleClock to prevent overflow if someone
|
| 33 |
+
// tries to represent the time as cycles since the Unix epoch.
|
| 34 |
+
ABSL_INTERNAL_INLINE_CONSTEXPR(int32_t, kCycleClockShift, 1);
|
| 35 |
+
#else
|
| 36 |
+
// Not debug mode and the UnscaledCycleClock isn't operating at the
|
| 37 |
+
// raw CPU frequency. There is no need to do any scaling, so don't
|
| 38 |
+
// needlessly sacrifice precision.
|
| 39 |
+
ABSL_INTERNAL_INLINE_CONSTEXPR(int32_t, kCycleClockShift, 0);
|
| 40 |
+
#endif
|
| 41 |
+
#else // NDEBUG
|
| 42 |
+
// In debug mode use a different shift to discourage depending on a
|
| 43 |
+
// particular shift value.
|
| 44 |
+
ABSL_INTERNAL_INLINE_CONSTEXPR(int32_t, kCycleClockShift, 2);
|
| 45 |
+
#endif // NDEBUG
|
| 46 |
+
|
| 47 |
+
ABSL_INTERNAL_INLINE_CONSTEXPR(double, kCycleClockFrequencyScale,
|
| 48 |
+
1.0 / (1 << kCycleClockShift));
|
| 49 |
+
#endif // ABSL_USE_UNSCALED_CYCLECLOC
|
| 50 |
+
|
| 51 |
+
} // namespace base_internal
|
| 52 |
+
ABSL_NAMESPACE_END
|
| 53 |
+
} // namespace absl
|
| 54 |
+
|
| 55 |
+
#endif // ABSL_BASE_INTERNAL_CYCLECLOCK_CONFIG_H_
|
include/absl/base/internal/direct_mmap.h
ADDED
|
@@ -0,0 +1,170 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
// Copyright 2017 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 |
+
// Functions for directly invoking mmap() via syscall, avoiding the case where
|
| 16 |
+
// mmap() has been locally overridden.
|
| 17 |
+
|
| 18 |
+
#ifndef ABSL_BASE_INTERNAL_DIRECT_MMAP_H_
|
| 19 |
+
#define ABSL_BASE_INTERNAL_DIRECT_MMAP_H_
|
| 20 |
+
|
| 21 |
+
#include "absl/base/config.h"
|
| 22 |
+
|
| 23 |
+
#ifdef ABSL_HAVE_MMAP
|
| 24 |
+
|
| 25 |
+
#include <sys/mman.h>
|
| 26 |
+
|
| 27 |
+
#ifdef __linux__
|
| 28 |
+
|
| 29 |
+
#include <sys/types.h>
|
| 30 |
+
#ifdef __BIONIC__
|
| 31 |
+
#include <sys/syscall.h>
|
| 32 |
+
#else
|
| 33 |
+
#include <syscall.h>
|
| 34 |
+
#endif
|
| 35 |
+
|
| 36 |
+
#include <linux/unistd.h>
|
| 37 |
+
#include <unistd.h>
|
| 38 |
+
#include <cerrno>
|
| 39 |
+
#include <cstdarg>
|
| 40 |
+
#include <cstdint>
|
| 41 |
+
|
| 42 |
+
#ifdef __mips__
|
| 43 |
+
// Include definitions of the ABI currently in use.
|
| 44 |
+
#if defined(__BIONIC__) || !defined(__GLIBC__)
|
| 45 |
+
// Android doesn't have sgidefs.h, but does have asm/sgidefs.h, which has the
|
| 46 |
+
// definitions we need.
|
| 47 |
+
#include <asm/sgidefs.h>
|
| 48 |
+
#else
|
| 49 |
+
#include <sgidefs.h>
|
| 50 |
+
#endif // __BIONIC__ || !__GLIBC__
|
| 51 |
+
#endif // __mips__
|
| 52 |
+
|
| 53 |
+
// SYS_mmap and SYS_munmap are not defined in Android.
|
| 54 |
+
#ifdef __BIONIC__
|
| 55 |
+
extern "C" void* __mmap2(void*, size_t, int, int, int, size_t);
|
| 56 |
+
#if defined(__NR_mmap) && !defined(SYS_mmap)
|
| 57 |
+
#define SYS_mmap __NR_mmap
|
| 58 |
+
#endif
|
| 59 |
+
#ifndef SYS_munmap
|
| 60 |
+
#define SYS_munmap __NR_munmap
|
| 61 |
+
#endif
|
| 62 |
+
#endif // __BIONIC__
|
| 63 |
+
|
| 64 |
+
#if defined(__NR_mmap2) && !defined(SYS_mmap2)
|
| 65 |
+
#define SYS_mmap2 __NR_mmap2
|
| 66 |
+
#endif
|
| 67 |
+
|
| 68 |
+
namespace absl {
|
| 69 |
+
ABSL_NAMESPACE_BEGIN
|
| 70 |
+
namespace base_internal {
|
| 71 |
+
|
| 72 |
+
// Platform specific logic extracted from
|
| 73 |
+
// https://chromium.googlesource.com/linux-syscall-support/+/master/linux_syscall_support.h
|
| 74 |
+
inline void* DirectMmap(void* start, size_t length, int prot, int flags, int fd,
|
| 75 |
+
off_t offset) noexcept {
|
| 76 |
+
#if defined(__i386__) || defined(__ARM_ARCH_3__) || defined(__ARM_EABI__) || \
|
| 77 |
+
defined(__m68k__) || defined(__sh__) || \
|
| 78 |
+
(defined(__hppa__) && !defined(__LP64__)) || \
|
| 79 |
+
(defined(__mips__) && _MIPS_SIM == _MIPS_SIM_ABI32) || \
|
| 80 |
+
(defined(__PPC__) && !defined(__PPC64__)) || \
|
| 81 |
+
(defined(__riscv) && __riscv_xlen == 32) || \
|
| 82 |
+
(defined(__s390__) && !defined(__s390x__)) || \
|
| 83 |
+
(defined(__sparc__) && !defined(__arch64__))
|
| 84 |
+
// On these architectures, implement mmap with mmap2.
|
| 85 |
+
static int pagesize = 0;
|
| 86 |
+
if (pagesize == 0) {
|
| 87 |
+
#if defined(__wasm__) || defined(__asmjs__)
|
| 88 |
+
pagesize = getpagesize();
|
| 89 |
+
#else
|
| 90 |
+
pagesize = sysconf(_SC_PAGESIZE);
|
| 91 |
+
#endif
|
| 92 |
+
}
|
| 93 |
+
if (offset < 0 || offset % pagesize != 0) {
|
| 94 |
+
errno = EINVAL;
|
| 95 |
+
return MAP_FAILED;
|
| 96 |
+
}
|
| 97 |
+
#ifdef __BIONIC__
|
| 98 |
+
// SYS_mmap2 has problems on Android API level <= 16.
|
| 99 |
+
// Workaround by invoking __mmap2() instead.
|
| 100 |
+
return __mmap2(start, length, prot, flags, fd,
|
| 101 |
+
static_cast<size_t>(offset / pagesize));
|
| 102 |
+
#else
|
| 103 |
+
return reinterpret_cast<void*>(
|
| 104 |
+
syscall(SYS_mmap2, start, length, prot, flags, fd,
|
| 105 |
+
static_cast<unsigned long>(offset / pagesize))); // NOLINT
|
| 106 |
+
#endif
|
| 107 |
+
#elif defined(__s390x__)
|
| 108 |
+
// On s390x, mmap() arguments are passed in memory.
|
| 109 |
+
unsigned long buf[6] = {reinterpret_cast<unsigned long>(start), // NOLINT
|
| 110 |
+
static_cast<unsigned long>(length), // NOLINT
|
| 111 |
+
static_cast<unsigned long>(prot), // NOLINT
|
| 112 |
+
static_cast<unsigned long>(flags), // NOLINT
|
| 113 |
+
static_cast<unsigned long>(fd), // NOLINT
|
| 114 |
+
static_cast<unsigned long>(offset)}; // NOLINT
|
| 115 |
+
return reinterpret_cast<void*>(syscall(SYS_mmap, buf));
|
| 116 |
+
#elif defined(__x86_64__)
|
| 117 |
+
// The x32 ABI has 32 bit longs, but the syscall interface is 64 bit.
|
| 118 |
+
// We need to explicitly cast to an unsigned 64 bit type to avoid implicit
|
| 119 |
+
// sign extension. We can't cast pointers directly because those are
|
| 120 |
+
// 32 bits, and gcc will dump ugly warnings about casting from a pointer
|
| 121 |
+
// to an integer of a different size. We also need to make sure __off64_t
|
| 122 |
+
// isn't truncated to 32-bits under x32.
|
| 123 |
+
#define MMAP_SYSCALL_ARG(x) ((uint64_t)(uintptr_t)(x))
|
| 124 |
+
return reinterpret_cast<void*>(
|
| 125 |
+
syscall(SYS_mmap, MMAP_SYSCALL_ARG(start), MMAP_SYSCALL_ARG(length),
|
| 126 |
+
MMAP_SYSCALL_ARG(prot), MMAP_SYSCALL_ARG(flags),
|
| 127 |
+
MMAP_SYSCALL_ARG(fd), static_cast<uint64_t>(offset)));
|
| 128 |
+
#undef MMAP_SYSCALL_ARG
|
| 129 |
+
#else // Remaining 64-bit aritectures.
|
| 130 |
+
static_assert(sizeof(unsigned long) == 8, "Platform is not 64-bit");
|
| 131 |
+
return reinterpret_cast<void*>(
|
| 132 |
+
syscall(SYS_mmap, start, length, prot, flags, fd, offset));
|
| 133 |
+
#endif
|
| 134 |
+
}
|
| 135 |
+
|
| 136 |
+
inline int DirectMunmap(void* start, size_t length) {
|
| 137 |
+
return static_cast<int>(syscall(SYS_munmap, start, length));
|
| 138 |
+
}
|
| 139 |
+
|
| 140 |
+
} // namespace base_internal
|
| 141 |
+
ABSL_NAMESPACE_END
|
| 142 |
+
} // namespace absl
|
| 143 |
+
|
| 144 |
+
#else // !__linux__
|
| 145 |
+
|
| 146 |
+
// For non-linux platforms where we have mmap, just dispatch directly to the
|
| 147 |
+
// actual mmap()/munmap() methods.
|
| 148 |
+
|
| 149 |
+
namespace absl {
|
| 150 |
+
ABSL_NAMESPACE_BEGIN
|
| 151 |
+
namespace base_internal {
|
| 152 |
+
|
| 153 |
+
inline void* DirectMmap(void* start, size_t length, int prot, int flags, int fd,
|
| 154 |
+
off_t offset) {
|
| 155 |
+
return mmap(start, length, prot, flags, fd, offset);
|
| 156 |
+
}
|
| 157 |
+
|
| 158 |
+
inline int DirectMunmap(void* start, size_t length) {
|
| 159 |
+
return munmap(start, length);
|
| 160 |
+
}
|
| 161 |
+
|
| 162 |
+
} // namespace base_internal
|
| 163 |
+
ABSL_NAMESPACE_END
|
| 164 |
+
} // namespace absl
|
| 165 |
+
|
| 166 |
+
#endif // __linux__
|
| 167 |
+
|
| 168 |
+
#endif // ABSL_HAVE_MMAP
|
| 169 |
+
|
| 170 |
+
#endif // ABSL_BASE_INTERNAL_DIRECT_MMAP_H_
|
include/absl/base/internal/dynamic_annotations.h
ADDED
|
@@ -0,0 +1,398 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
// Copyright 2017 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 |
+
// This file defines dynamic annotations for use with dynamic analysis tool
|
| 16 |
+
// such as valgrind, PIN, etc.
|
| 17 |
+
//
|
| 18 |
+
// Dynamic annotation is a source code annotation that affects the generated
|
| 19 |
+
// code (that is, the annotation is not a comment). Each such annotation is
|
| 20 |
+
// attached to a particular instruction and/or to a particular object (address)
|
| 21 |
+
// in the program.
|
| 22 |
+
//
|
| 23 |
+
// The annotations that should be used by users are macros in all upper-case
|
| 24 |
+
// (e.g., ANNOTATE_THREAD_NAME).
|
| 25 |
+
//
|
| 26 |
+
// Actual implementation of these macros may differ depending on the dynamic
|
| 27 |
+
// analysis tool being used.
|
| 28 |
+
//
|
| 29 |
+
// This file supports the following configurations:
|
| 30 |
+
// - Dynamic Annotations enabled (with static thread-safety warnings disabled).
|
| 31 |
+
// In this case, macros expand to functions implemented by Thread Sanitizer,
|
| 32 |
+
// when building with TSan. When not provided an external implementation,
|
| 33 |
+
// dynamic_annotations.cc provides no-op implementations.
|
| 34 |
+
//
|
| 35 |
+
// - Static Clang thread-safety warnings enabled.
|
| 36 |
+
// When building with a Clang compiler that supports thread-safety warnings,
|
| 37 |
+
// a subset of annotations can be statically-checked at compile-time. We
|
| 38 |
+
// expand these macros to static-inline functions that can be analyzed for
|
| 39 |
+
// thread-safety, but afterwards elided when building the final binary.
|
| 40 |
+
//
|
| 41 |
+
// - All annotations are disabled.
|
| 42 |
+
// If neither Dynamic Annotations nor Clang thread-safety warnings are
|
| 43 |
+
// enabled, then all annotation-macros expand to empty.
|
| 44 |
+
|
| 45 |
+
#ifndef ABSL_BASE_INTERNAL_DYNAMIC_ANNOTATIONS_H_
|
| 46 |
+
#define ABSL_BASE_INTERNAL_DYNAMIC_ANNOTATIONS_H_
|
| 47 |
+
|
| 48 |
+
#include <stddef.h>
|
| 49 |
+
|
| 50 |
+
#include "absl/base/config.h"
|
| 51 |
+
|
| 52 |
+
// -------------------------------------------------------------------------
|
| 53 |
+
// Decide which features are enabled
|
| 54 |
+
|
| 55 |
+
#ifndef DYNAMIC_ANNOTATIONS_ENABLED
|
| 56 |
+
#define DYNAMIC_ANNOTATIONS_ENABLED 0
|
| 57 |
+
#endif
|
| 58 |
+
|
| 59 |
+
#if defined(__clang__) && !defined(SWIG)
|
| 60 |
+
#define ABSL_INTERNAL_IGNORE_READS_ATTRIBUTE_ENABLED 1
|
| 61 |
+
#endif
|
| 62 |
+
|
| 63 |
+
#if DYNAMIC_ANNOTATIONS_ENABLED != 0
|
| 64 |
+
|
| 65 |
+
#define ABSL_INTERNAL_RACE_ANNOTATIONS_ENABLED 1
|
| 66 |
+
#define ABSL_INTERNAL_READS_ANNOTATIONS_ENABLED 1
|
| 67 |
+
#define ABSL_INTERNAL_WRITES_ANNOTATIONS_ENABLED 1
|
| 68 |
+
#define ABSL_INTERNAL_ANNOTALYSIS_ENABLED 0
|
| 69 |
+
#define ABSL_INTERNAL_READS_WRITES_ANNOTATIONS_ENABLED 1
|
| 70 |
+
|
| 71 |
+
#else
|
| 72 |
+
|
| 73 |
+
#define ABSL_INTERNAL_RACE_ANNOTATIONS_ENABLED 0
|
| 74 |
+
#define ABSL_INTERNAL_READS_ANNOTATIONS_ENABLED 0
|
| 75 |
+
#define ABSL_INTERNAL_WRITES_ANNOTATIONS_ENABLED 0
|
| 76 |
+
|
| 77 |
+
// Clang provides limited support for static thread-safety analysis through a
|
| 78 |
+
// feature called Annotalysis. We configure macro-definitions according to
|
| 79 |
+
// whether Annotalysis support is available. When running in opt-mode, GCC
|
| 80 |
+
// will issue a warning, if these attributes are compiled. Only include them
|
| 81 |
+
// when compiling using Clang.
|
| 82 |
+
|
| 83 |
+
// ANNOTALYSIS_ENABLED == 1 when IGNORE_READ_ATTRIBUTE_ENABLED == 1
|
| 84 |
+
#define ABSL_INTERNAL_ANNOTALYSIS_ENABLED \
|
| 85 |
+
defined(ABSL_INTERNAL_IGNORE_READS_ATTRIBUTE_ENABLED)
|
| 86 |
+
// Read/write annotations are enabled in Annotalysis mode; disabled otherwise.
|
| 87 |
+
#define ABSL_INTERNAL_READS_WRITES_ANNOTATIONS_ENABLED \
|
| 88 |
+
ABSL_INTERNAL_ANNOTALYSIS_ENABLED
|
| 89 |
+
#endif
|
| 90 |
+
|
| 91 |
+
// Memory annotations are also made available to LLVM's Memory Sanitizer
|
| 92 |
+
#if defined(ABSL_HAVE_MEMORY_SANITIZER) && !defined(__native_client__)
|
| 93 |
+
#define ABSL_INTERNAL_MEMORY_ANNOTATIONS_ENABLED 1
|
| 94 |
+
#endif
|
| 95 |
+
|
| 96 |
+
#ifndef ABSL_INTERNAL_MEMORY_ANNOTATIONS_ENABLED
|
| 97 |
+
#define ABSL_INTERNAL_MEMORY_ANNOTATIONS_ENABLED 0
|
| 98 |
+
#endif
|
| 99 |
+
|
| 100 |
+
#ifdef __cplusplus
|
| 101 |
+
#define ABSL_INTERNAL_BEGIN_EXTERN_C extern "C" {
|
| 102 |
+
#define ABSL_INTERNAL_END_EXTERN_C } // extern "C"
|
| 103 |
+
#define ABSL_INTERNAL_GLOBAL_SCOPED(F) ::F
|
| 104 |
+
#define ABSL_INTERNAL_STATIC_INLINE inline
|
| 105 |
+
#else
|
| 106 |
+
#define ABSL_INTERNAL_BEGIN_EXTERN_C // empty
|
| 107 |
+
#define ABSL_INTERNAL_END_EXTERN_C // empty
|
| 108 |
+
#define ABSL_INTERNAL_GLOBAL_SCOPED(F) F
|
| 109 |
+
#define ABSL_INTERNAL_STATIC_INLINE static inline
|
| 110 |
+
#endif
|
| 111 |
+
|
| 112 |
+
// -------------------------------------------------------------------------
|
| 113 |
+
// Define race annotations.
|
| 114 |
+
|
| 115 |
+
#if ABSL_INTERNAL_RACE_ANNOTATIONS_ENABLED == 1
|
| 116 |
+
|
| 117 |
+
// -------------------------------------------------------------
|
| 118 |
+
// Annotations that suppress errors. It is usually better to express the
|
| 119 |
+
// program's synchronization using the other annotations, but these can be used
|
| 120 |
+
// when all else fails.
|
| 121 |
+
|
| 122 |
+
// Report that we may have a benign race at `pointer`, with size
|
| 123 |
+
// "sizeof(*(pointer))". `pointer` must be a non-void* pointer. Insert at the
|
| 124 |
+
// point where `pointer` has been allocated, preferably close to the point
|
| 125 |
+
// where the race happens. See also ANNOTATE_BENIGN_RACE_STATIC.
|
| 126 |
+
#define ANNOTATE_BENIGN_RACE(pointer, description) \
|
| 127 |
+
ABSL_INTERNAL_GLOBAL_SCOPED(AnnotateBenignRaceSized) \
|
| 128 |
+
(__FILE__, __LINE__, pointer, sizeof(*(pointer)), description)
|
| 129 |
+
|
| 130 |
+
// Same as ANNOTATE_BENIGN_RACE(`address`, `description`), but applies to
|
| 131 |
+
// the memory range [`address`, `address`+`size`).
|
| 132 |
+
#define ANNOTATE_BENIGN_RACE_SIZED(address, size, description) \
|
| 133 |
+
ABSL_INTERNAL_GLOBAL_SCOPED(AnnotateBenignRaceSized) \
|
| 134 |
+
(__FILE__, __LINE__, address, size, description)
|
| 135 |
+
|
| 136 |
+
// Enable (`enable`!=0) or disable (`enable`==0) race detection for all threads.
|
| 137 |
+
// This annotation could be useful if you want to skip expensive race analysis
|
| 138 |
+
// during some period of program execution, e.g. during initialization.
|
| 139 |
+
#define ANNOTATE_ENABLE_RACE_DETECTION(enable) \
|
| 140 |
+
ABSL_INTERNAL_GLOBAL_SCOPED(AnnotateEnableRaceDetection) \
|
| 141 |
+
(__FILE__, __LINE__, enable)
|
| 142 |
+
|
| 143 |
+
// -------------------------------------------------------------
|
| 144 |
+
// Annotations useful for debugging.
|
| 145 |
+
|
| 146 |
+
// Report the current thread `name` to a race detector.
|
| 147 |
+
#define ANNOTATE_THREAD_NAME(name) \
|
| 148 |
+
ABSL_INTERNAL_GLOBAL_SCOPED(AnnotateThreadName)(__FILE__, __LINE__, name)
|
| 149 |
+
|
| 150 |
+
// -------------------------------------------------------------
|
| 151 |
+
// Annotations useful when implementing locks. They are not normally needed by
|
| 152 |
+
// modules that merely use locks. The `lock` argument is a pointer to the lock
|
| 153 |
+
// object.
|
| 154 |
+
|
| 155 |
+
// Report that a lock has been created at address `lock`.
|
| 156 |
+
#define ANNOTATE_RWLOCK_CREATE(lock) \
|
| 157 |
+
ABSL_INTERNAL_GLOBAL_SCOPED(AnnotateRWLockCreate)(__FILE__, __LINE__, lock)
|
| 158 |
+
|
| 159 |
+
// Report that a linker initialized lock has been created at address `lock`.
|
| 160 |
+
#ifdef ABSL_HAVE_THREAD_SANITIZER
|
| 161 |
+
#define ANNOTATE_RWLOCK_CREATE_STATIC(lock) \
|
| 162 |
+
ABSL_INTERNAL_GLOBAL_SCOPED(AnnotateRWLockCreateStatic) \
|
| 163 |
+
(__FILE__, __LINE__, lock)
|
| 164 |
+
#else
|
| 165 |
+
#define ANNOTATE_RWLOCK_CREATE_STATIC(lock) ANNOTATE_RWLOCK_CREATE(lock)
|
| 166 |
+
#endif
|
| 167 |
+
|
| 168 |
+
// Report that the lock at address `lock` is about to be destroyed.
|
| 169 |
+
#define ANNOTATE_RWLOCK_DESTROY(lock) \
|
| 170 |
+
ABSL_INTERNAL_GLOBAL_SCOPED(AnnotateRWLockDestroy)(__FILE__, __LINE__, lock)
|
| 171 |
+
|
| 172 |
+
// Report that the lock at address `lock` has been acquired.
|
| 173 |
+
// `is_w`=1 for writer lock, `is_w`=0 for reader lock.
|
| 174 |
+
#define ANNOTATE_RWLOCK_ACQUIRED(lock, is_w) \
|
| 175 |
+
ABSL_INTERNAL_GLOBAL_SCOPED(AnnotateRWLockAcquired) \
|
| 176 |
+
(__FILE__, __LINE__, lock, is_w)
|
| 177 |
+
|
| 178 |
+
// Report that the lock at address `lock` is about to be released.
|
| 179 |
+
// `is_w`=1 for writer lock, `is_w`=0 for reader lock.
|
| 180 |
+
#define ANNOTATE_RWLOCK_RELEASED(lock, is_w) \
|
| 181 |
+
ABSL_INTERNAL_GLOBAL_SCOPED(AnnotateRWLockReleased) \
|
| 182 |
+
(__FILE__, __LINE__, lock, is_w)
|
| 183 |
+
|
| 184 |
+
// Apply ANNOTATE_BENIGN_RACE_SIZED to a static variable `static_var`.
|
| 185 |
+
#define ANNOTATE_BENIGN_RACE_STATIC(static_var, description) \
|
| 186 |
+
namespace { \
|
| 187 |
+
class static_var##_annotator { \
|
| 188 |
+
public: \
|
| 189 |
+
static_var##_annotator() { \
|
| 190 |
+
ANNOTATE_BENIGN_RACE_SIZED(&static_var, sizeof(static_var), \
|
| 191 |
+
#static_var ": " description); \
|
| 192 |
+
} \
|
| 193 |
+
}; \
|
| 194 |
+
static static_var##_annotator the##static_var##_annotator; \
|
| 195 |
+
} // namespace
|
| 196 |
+
|
| 197 |
+
#else // ABSL_INTERNAL_RACE_ANNOTATIONS_ENABLED == 0
|
| 198 |
+
|
| 199 |
+
#define ANNOTATE_RWLOCK_CREATE(lock) // empty
|
| 200 |
+
#define ANNOTATE_RWLOCK_CREATE_STATIC(lock) // empty
|
| 201 |
+
#define ANNOTATE_RWLOCK_DESTROY(lock) // empty
|
| 202 |
+
#define ANNOTATE_RWLOCK_ACQUIRED(lock, is_w) // empty
|
| 203 |
+
#define ANNOTATE_RWLOCK_RELEASED(lock, is_w) // empty
|
| 204 |
+
#define ANNOTATE_BENIGN_RACE(address, description) // empty
|
| 205 |
+
#define ANNOTATE_BENIGN_RACE_SIZED(address, size, description) // empty
|
| 206 |
+
#define ANNOTATE_THREAD_NAME(name) // empty
|
| 207 |
+
#define ANNOTATE_ENABLE_RACE_DETECTION(enable) // empty
|
| 208 |
+
#define ANNOTATE_BENIGN_RACE_STATIC(static_var, description) // empty
|
| 209 |
+
|
| 210 |
+
#endif // ABSL_INTERNAL_RACE_ANNOTATIONS_ENABLED
|
| 211 |
+
|
| 212 |
+
// -------------------------------------------------------------------------
|
| 213 |
+
// Define memory annotations.
|
| 214 |
+
|
| 215 |
+
#if ABSL_INTERNAL_MEMORY_ANNOTATIONS_ENABLED == 1
|
| 216 |
+
|
| 217 |
+
#include <sanitizer/msan_interface.h>
|
| 218 |
+
|
| 219 |
+
#define ANNOTATE_MEMORY_IS_INITIALIZED(address, size) \
|
| 220 |
+
__msan_unpoison(address, size)
|
| 221 |
+
|
| 222 |
+
#define ANNOTATE_MEMORY_IS_UNINITIALIZED(address, size) \
|
| 223 |
+
__msan_allocated_memory(address, size)
|
| 224 |
+
|
| 225 |
+
#else // ABSL_INTERNAL_MEMORY_ANNOTATIONS_ENABLED == 0
|
| 226 |
+
|
| 227 |
+
#if DYNAMIC_ANNOTATIONS_ENABLED == 1
|
| 228 |
+
#define ANNOTATE_MEMORY_IS_INITIALIZED(address, size) \
|
| 229 |
+
do { \
|
| 230 |
+
(void)(address); \
|
| 231 |
+
(void)(size); \
|
| 232 |
+
} while (0)
|
| 233 |
+
#define ANNOTATE_MEMORY_IS_UNINITIALIZED(address, size) \
|
| 234 |
+
do { \
|
| 235 |
+
(void)(address); \
|
| 236 |
+
(void)(size); \
|
| 237 |
+
} while (0)
|
| 238 |
+
#else
|
| 239 |
+
#define ANNOTATE_MEMORY_IS_INITIALIZED(address, size) // empty
|
| 240 |
+
#define ANNOTATE_MEMORY_IS_UNINITIALIZED(address, size) // empty
|
| 241 |
+
#endif
|
| 242 |
+
|
| 243 |
+
#endif // ABSL_INTERNAL_MEMORY_ANNOTATIONS_ENABLED
|
| 244 |
+
|
| 245 |
+
// -------------------------------------------------------------------------
|
| 246 |
+
// Define IGNORE_READS_BEGIN/_END attributes.
|
| 247 |
+
|
| 248 |
+
#if defined(ABSL_INTERNAL_IGNORE_READS_ATTRIBUTE_ENABLED)
|
| 249 |
+
|
| 250 |
+
#define ABSL_INTERNAL_IGNORE_READS_BEGIN_ATTRIBUTE \
|
| 251 |
+
__attribute((exclusive_lock_function("*")))
|
| 252 |
+
#define ABSL_INTERNAL_IGNORE_READS_END_ATTRIBUTE \
|
| 253 |
+
__attribute((unlock_function("*")))
|
| 254 |
+
|
| 255 |
+
#else // !defined(ABSL_INTERNAL_IGNORE_READS_ATTRIBUTE_ENABLED)
|
| 256 |
+
|
| 257 |
+
#define ABSL_INTERNAL_IGNORE_READS_BEGIN_ATTRIBUTE // empty
|
| 258 |
+
#define ABSL_INTERNAL_IGNORE_READS_END_ATTRIBUTE // empty
|
| 259 |
+
|
| 260 |
+
#endif // defined(ABSL_INTERNAL_IGNORE_READS_ATTRIBUTE_ENABLED)
|
| 261 |
+
|
| 262 |
+
// -------------------------------------------------------------------------
|
| 263 |
+
// Define IGNORE_READS_BEGIN/_END annotations.
|
| 264 |
+
|
| 265 |
+
#if ABSL_INTERNAL_READS_ANNOTATIONS_ENABLED == 1
|
| 266 |
+
|
| 267 |
+
// Request the analysis tool to ignore all reads in the current thread until
|
| 268 |
+
// ANNOTATE_IGNORE_READS_END is called. Useful to ignore intentional racey
|
| 269 |
+
// reads, while still checking other reads and all writes.
|
| 270 |
+
// See also ANNOTATE_UNPROTECTED_READ.
|
| 271 |
+
#define ANNOTATE_IGNORE_READS_BEGIN() \
|
| 272 |
+
ABSL_INTERNAL_GLOBAL_SCOPED(AnnotateIgnoreReadsBegin)(__FILE__, __LINE__)
|
| 273 |
+
|
| 274 |
+
// Stop ignoring reads.
|
| 275 |
+
#define ANNOTATE_IGNORE_READS_END() \
|
| 276 |
+
ABSL_INTERNAL_GLOBAL_SCOPED(AnnotateIgnoreReadsEnd)(__FILE__, __LINE__)
|
| 277 |
+
|
| 278 |
+
#elif defined(ABSL_INTERNAL_ANNOTALYSIS_ENABLED)
|
| 279 |
+
|
| 280 |
+
// When Annotalysis is enabled without Dynamic Annotations, the use of
|
| 281 |
+
// static-inline functions allows the annotations to be read at compile-time,
|
| 282 |
+
// while still letting the compiler elide the functions from the final build.
|
| 283 |
+
//
|
| 284 |
+
// TODO(delesley) -- The exclusive lock here ignores writes as well, but
|
| 285 |
+
// allows IGNORE_READS_AND_WRITES to work properly.
|
| 286 |
+
|
| 287 |
+
#define ANNOTATE_IGNORE_READS_BEGIN() \
|
| 288 |
+
ABSL_INTERNAL_GLOBAL_SCOPED(AbslInternalAnnotateIgnoreReadsBegin)()
|
| 289 |
+
|
| 290 |
+
#define ANNOTATE_IGNORE_READS_END() \
|
| 291 |
+
ABSL_INTERNAL_GLOBAL_SCOPED(AbslInternalAnnotateIgnoreReadsEnd)()
|
| 292 |
+
|
| 293 |
+
#else
|
| 294 |
+
|
| 295 |
+
#define ANNOTATE_IGNORE_READS_BEGIN() // empty
|
| 296 |
+
#define ANNOTATE_IGNORE_READS_END() // empty
|
| 297 |
+
|
| 298 |
+
#endif
|
| 299 |
+
|
| 300 |
+
// -------------------------------------------------------------------------
|
| 301 |
+
// Define IGNORE_WRITES_BEGIN/_END annotations.
|
| 302 |
+
|
| 303 |
+
#if ABSL_INTERNAL_WRITES_ANNOTATIONS_ENABLED == 1
|
| 304 |
+
|
| 305 |
+
// Similar to ANNOTATE_IGNORE_READS_BEGIN, but ignore writes instead.
|
| 306 |
+
#define ANNOTATE_IGNORE_WRITES_BEGIN() \
|
| 307 |
+
ABSL_INTERNAL_GLOBAL_SCOPED(AnnotateIgnoreWritesBegin)(__FILE__, __LINE__)
|
| 308 |
+
|
| 309 |
+
// Stop ignoring writes.
|
| 310 |
+
#define ANNOTATE_IGNORE_WRITES_END() \
|
| 311 |
+
ABSL_INTERNAL_GLOBAL_SCOPED(AnnotateIgnoreWritesEnd)(__FILE__, __LINE__)
|
| 312 |
+
|
| 313 |
+
#else
|
| 314 |
+
|
| 315 |
+
#define ANNOTATE_IGNORE_WRITES_BEGIN() // empty
|
| 316 |
+
#define ANNOTATE_IGNORE_WRITES_END() // empty
|
| 317 |
+
|
| 318 |
+
#endif
|
| 319 |
+
|
| 320 |
+
// -------------------------------------------------------------------------
|
| 321 |
+
// Define the ANNOTATE_IGNORE_READS_AND_WRITES_* annotations using the more
|
| 322 |
+
// primitive annotations defined above.
|
| 323 |
+
//
|
| 324 |
+
// Instead of doing
|
| 325 |
+
// ANNOTATE_IGNORE_READS_BEGIN();
|
| 326 |
+
// ... = x;
|
| 327 |
+
// ANNOTATE_IGNORE_READS_END();
|
| 328 |
+
// one can use
|
| 329 |
+
// ... = ANNOTATE_UNPROTECTED_READ(x);
|
| 330 |
+
|
| 331 |
+
#if defined(ABSL_INTERNAL_READS_WRITES_ANNOTATIONS_ENABLED)
|
| 332 |
+
|
| 333 |
+
// Start ignoring all memory accesses (both reads and writes).
|
| 334 |
+
#define ANNOTATE_IGNORE_READS_AND_WRITES_BEGIN() \
|
| 335 |
+
do { \
|
| 336 |
+
ANNOTATE_IGNORE_READS_BEGIN(); \
|
| 337 |
+
ANNOTATE_IGNORE_WRITES_BEGIN(); \
|
| 338 |
+
} while (0)
|
| 339 |
+
|
| 340 |
+
// Stop ignoring both reads and writes.
|
| 341 |
+
#define ANNOTATE_IGNORE_READS_AND_WRITES_END() \
|
| 342 |
+
do { \
|
| 343 |
+
ANNOTATE_IGNORE_WRITES_END(); \
|
| 344 |
+
ANNOTATE_IGNORE_READS_END(); \
|
| 345 |
+
} while (0)
|
| 346 |
+
|
| 347 |
+
#ifdef __cplusplus
|
| 348 |
+
// ANNOTATE_UNPROTECTED_READ is the preferred way to annotate racey reads.
|
| 349 |
+
#define ANNOTATE_UNPROTECTED_READ(x) \
|
| 350 |
+
absl::base_internal::AnnotateUnprotectedRead(x)
|
| 351 |
+
|
| 352 |
+
#endif
|
| 353 |
+
|
| 354 |
+
#else
|
| 355 |
+
|
| 356 |
+
#define ANNOTATE_IGNORE_READS_AND_WRITES_BEGIN() // empty
|
| 357 |
+
#define ANNOTATE_IGNORE_READS_AND_WRITES_END() // empty
|
| 358 |
+
#define ANNOTATE_UNPROTECTED_READ(x) (x)
|
| 359 |
+
|
| 360 |
+
#endif
|
| 361 |
+
|
| 362 |
+
// -------------------------------------------------------------------------
|
| 363 |
+
// Address sanitizer annotations
|
| 364 |
+
|
| 365 |
+
#ifdef ABSL_HAVE_ADDRESS_SANITIZER
|
| 366 |
+
// Describe the current state of a contiguous container such as e.g.
|
| 367 |
+
// std::vector or std::string. For more details see
|
| 368 |
+
// sanitizer/common_interface_defs.h, which is provided by the compiler.
|
| 369 |
+
#include <sanitizer/common_interface_defs.h>
|
| 370 |
+
|
| 371 |
+
#define ANNOTATE_CONTIGUOUS_CONTAINER(beg, end, old_mid, new_mid) \
|
| 372 |
+
__sanitizer_annotate_contiguous_container(beg, end, old_mid, new_mid)
|
| 373 |
+
#define ADDRESS_SANITIZER_REDZONE(name) \
|
| 374 |
+
struct { \
|
| 375 |
+
char x[8] __attribute__((aligned(8))); \
|
| 376 |
+
} name
|
| 377 |
+
|
| 378 |
+
#else
|
| 379 |
+
|
| 380 |
+
#define ANNOTATE_CONTIGUOUS_CONTAINER(beg, end, old_mid, new_mid)
|
| 381 |
+
#define ADDRESS_SANITIZER_REDZONE(name) static_assert(true, "")
|
| 382 |
+
|
| 383 |
+
#endif // ABSL_HAVE_ADDRESS_SANITIZER
|
| 384 |
+
|
| 385 |
+
// -------------------------------------------------------------------------
|
| 386 |
+
// Undefine the macros intended only for this file.
|
| 387 |
+
|
| 388 |
+
#undef ABSL_INTERNAL_RACE_ANNOTATIONS_ENABLED
|
| 389 |
+
#undef ABSL_INTERNAL_MEMORY_ANNOTATIONS_ENABLED
|
| 390 |
+
#undef ABSL_INTERNAL_READS_ANNOTATIONS_ENABLED
|
| 391 |
+
#undef ABSL_INTERNAL_WRITES_ANNOTATIONS_ENABLED
|
| 392 |
+
#undef ABSL_INTERNAL_ANNOTALYSIS_ENABLED
|
| 393 |
+
#undef ABSL_INTERNAL_READS_WRITES_ANNOTATIONS_ENABLED
|
| 394 |
+
#undef ABSL_INTERNAL_BEGIN_EXTERN_C
|
| 395 |
+
#undef ABSL_INTERNAL_END_EXTERN_C
|
| 396 |
+
#undef ABSL_INTERNAL_STATIC_INLINE
|
| 397 |
+
|
| 398 |
+
#endif // ABSL_BASE_INTERNAL_DYNAMIC_ANNOTATIONS_H_
|
include/absl/base/internal/endian.h
ADDED
|
@@ -0,0 +1,283 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
// Copyright 2017 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 |
+
|
| 16 |
+
#ifndef ABSL_BASE_INTERNAL_ENDIAN_H_
|
| 17 |
+
#define ABSL_BASE_INTERNAL_ENDIAN_H_
|
| 18 |
+
|
| 19 |
+
#include <cstdint>
|
| 20 |
+
#include <cstdlib>
|
| 21 |
+
|
| 22 |
+
#include "absl/base/casts.h"
|
| 23 |
+
#include "absl/base/config.h"
|
| 24 |
+
#include "absl/base/internal/unaligned_access.h"
|
| 25 |
+
#include "absl/base/nullability.h"
|
| 26 |
+
#include "absl/base/port.h"
|
| 27 |
+
|
| 28 |
+
namespace absl {
|
| 29 |
+
ABSL_NAMESPACE_BEGIN
|
| 30 |
+
|
| 31 |
+
inline uint64_t gbswap_64(uint64_t host_int) {
|
| 32 |
+
#if ABSL_HAVE_BUILTIN(__builtin_bswap64) || defined(__GNUC__)
|
| 33 |
+
return __builtin_bswap64(host_int);
|
| 34 |
+
#elif defined(_MSC_VER)
|
| 35 |
+
return _byteswap_uint64(host_int);
|
| 36 |
+
#else
|
| 37 |
+
return (((host_int & uint64_t{0xFF}) << 56) |
|
| 38 |
+
((host_int & uint64_t{0xFF00}) << 40) |
|
| 39 |
+
((host_int & uint64_t{0xFF0000}) << 24) |
|
| 40 |
+
((host_int & uint64_t{0xFF000000}) << 8) |
|
| 41 |
+
((host_int & uint64_t{0xFF00000000}) >> 8) |
|
| 42 |
+
((host_int & uint64_t{0xFF0000000000}) >> 24) |
|
| 43 |
+
((host_int & uint64_t{0xFF000000000000}) >> 40) |
|
| 44 |
+
((host_int & uint64_t{0xFF00000000000000}) >> 56));
|
| 45 |
+
#endif
|
| 46 |
+
}
|
| 47 |
+
|
| 48 |
+
inline uint32_t gbswap_32(uint32_t host_int) {
|
| 49 |
+
#if ABSL_HAVE_BUILTIN(__builtin_bswap32) || defined(__GNUC__)
|
| 50 |
+
return __builtin_bswap32(host_int);
|
| 51 |
+
#elif defined(_MSC_VER)
|
| 52 |
+
return _byteswap_ulong(host_int);
|
| 53 |
+
#else
|
| 54 |
+
return (((host_int & uint32_t{0xFF}) << 24) |
|
| 55 |
+
((host_int & uint32_t{0xFF00}) << 8) |
|
| 56 |
+
((host_int & uint32_t{0xFF0000}) >> 8) |
|
| 57 |
+
((host_int & uint32_t{0xFF000000}) >> 24));
|
| 58 |
+
#endif
|
| 59 |
+
}
|
| 60 |
+
|
| 61 |
+
inline uint16_t gbswap_16(uint16_t host_int) {
|
| 62 |
+
#if ABSL_HAVE_BUILTIN(__builtin_bswap16) || defined(__GNUC__)
|
| 63 |
+
return __builtin_bswap16(host_int);
|
| 64 |
+
#elif defined(_MSC_VER)
|
| 65 |
+
return _byteswap_ushort(host_int);
|
| 66 |
+
#else
|
| 67 |
+
return (((host_int & uint16_t{0xFF}) << 8) |
|
| 68 |
+
((host_int & uint16_t{0xFF00}) >> 8));
|
| 69 |
+
#endif
|
| 70 |
+
}
|
| 71 |
+
|
| 72 |
+
#ifdef ABSL_IS_LITTLE_ENDIAN
|
| 73 |
+
|
| 74 |
+
// Portable definitions for htonl (host-to-network) and friends on little-endian
|
| 75 |
+
// architectures.
|
| 76 |
+
inline uint16_t ghtons(uint16_t x) { return gbswap_16(x); }
|
| 77 |
+
inline uint32_t ghtonl(uint32_t x) { return gbswap_32(x); }
|
| 78 |
+
inline uint64_t ghtonll(uint64_t x) { return gbswap_64(x); }
|
| 79 |
+
|
| 80 |
+
#elif defined ABSL_IS_BIG_ENDIAN
|
| 81 |
+
|
| 82 |
+
// Portable definitions for htonl (host-to-network) etc on big-endian
|
| 83 |
+
// architectures. These definitions are simpler since the host byte order is the
|
| 84 |
+
// same as network byte order.
|
| 85 |
+
inline uint16_t ghtons(uint16_t x) { return x; }
|
| 86 |
+
inline uint32_t ghtonl(uint32_t x) { return x; }
|
| 87 |
+
inline uint64_t ghtonll(uint64_t x) { return x; }
|
| 88 |
+
|
| 89 |
+
#else
|
| 90 |
+
#error \
|
| 91 |
+
"Unsupported byte order: Either ABSL_IS_BIG_ENDIAN or " \
|
| 92 |
+
"ABSL_IS_LITTLE_ENDIAN must be defined"
|
| 93 |
+
#endif // byte order
|
| 94 |
+
|
| 95 |
+
inline uint16_t gntohs(uint16_t x) { return ghtons(x); }
|
| 96 |
+
inline uint32_t gntohl(uint32_t x) { return ghtonl(x); }
|
| 97 |
+
inline uint64_t gntohll(uint64_t x) { return ghtonll(x); }
|
| 98 |
+
|
| 99 |
+
// Utilities to convert numbers between the current hosts's native byte
|
| 100 |
+
// order and little-endian byte order
|
| 101 |
+
//
|
| 102 |
+
// Load/Store methods are alignment safe
|
| 103 |
+
namespace little_endian {
|
| 104 |
+
// Conversion functions.
|
| 105 |
+
#ifdef ABSL_IS_LITTLE_ENDIAN
|
| 106 |
+
|
| 107 |
+
inline uint16_t FromHost16(uint16_t x) { return x; }
|
| 108 |
+
inline uint16_t ToHost16(uint16_t x) { return x; }
|
| 109 |
+
|
| 110 |
+
inline uint32_t FromHost32(uint32_t x) { return x; }
|
| 111 |
+
inline uint32_t ToHost32(uint32_t x) { return x; }
|
| 112 |
+
|
| 113 |
+
inline uint64_t FromHost64(uint64_t x) { return x; }
|
| 114 |
+
inline uint64_t ToHost64(uint64_t x) { return x; }
|
| 115 |
+
|
| 116 |
+
inline constexpr bool IsLittleEndian() { return true; }
|
| 117 |
+
|
| 118 |
+
#elif defined ABSL_IS_BIG_ENDIAN
|
| 119 |
+
|
| 120 |
+
inline uint16_t FromHost16(uint16_t x) { return gbswap_16(x); }
|
| 121 |
+
inline uint16_t ToHost16(uint16_t x) { return gbswap_16(x); }
|
| 122 |
+
|
| 123 |
+
inline uint32_t FromHost32(uint32_t x) { return gbswap_32(x); }
|
| 124 |
+
inline uint32_t ToHost32(uint32_t x) { return gbswap_32(x); }
|
| 125 |
+
|
| 126 |
+
inline uint64_t FromHost64(uint64_t x) { return gbswap_64(x); }
|
| 127 |
+
inline uint64_t ToHost64(uint64_t x) { return gbswap_64(x); }
|
| 128 |
+
|
| 129 |
+
inline constexpr bool IsLittleEndian() { return false; }
|
| 130 |
+
|
| 131 |
+
#endif /* ENDIAN */
|
| 132 |
+
|
| 133 |
+
inline uint8_t FromHost(uint8_t x) { return x; }
|
| 134 |
+
inline uint16_t FromHost(uint16_t x) { return FromHost16(x); }
|
| 135 |
+
inline uint32_t FromHost(uint32_t x) { return FromHost32(x); }
|
| 136 |
+
inline uint64_t FromHost(uint64_t x) { return FromHost64(x); }
|
| 137 |
+
inline uint8_t ToHost(uint8_t x) { return x; }
|
| 138 |
+
inline uint16_t ToHost(uint16_t x) { return ToHost16(x); }
|
| 139 |
+
inline uint32_t ToHost(uint32_t x) { return ToHost32(x); }
|
| 140 |
+
inline uint64_t ToHost(uint64_t x) { return ToHost64(x); }
|
| 141 |
+
|
| 142 |
+
inline int8_t FromHost(int8_t x) { return x; }
|
| 143 |
+
inline int16_t FromHost(int16_t x) {
|
| 144 |
+
return bit_cast<int16_t>(FromHost16(bit_cast<uint16_t>(x)));
|
| 145 |
+
}
|
| 146 |
+
inline int32_t FromHost(int32_t x) {
|
| 147 |
+
return bit_cast<int32_t>(FromHost32(bit_cast<uint32_t>(x)));
|
| 148 |
+
}
|
| 149 |
+
inline int64_t FromHost(int64_t x) {
|
| 150 |
+
return bit_cast<int64_t>(FromHost64(bit_cast<uint64_t>(x)));
|
| 151 |
+
}
|
| 152 |
+
inline int8_t ToHost(int8_t x) { return x; }
|
| 153 |
+
inline int16_t ToHost(int16_t x) {
|
| 154 |
+
return bit_cast<int16_t>(ToHost16(bit_cast<uint16_t>(x)));
|
| 155 |
+
}
|
| 156 |
+
inline int32_t ToHost(int32_t x) {
|
| 157 |
+
return bit_cast<int32_t>(ToHost32(bit_cast<uint32_t>(x)));
|
| 158 |
+
}
|
| 159 |
+
inline int64_t ToHost(int64_t x) {
|
| 160 |
+
return bit_cast<int64_t>(ToHost64(bit_cast<uint64_t>(x)));
|
| 161 |
+
}
|
| 162 |
+
|
| 163 |
+
// Functions to do unaligned loads and stores in little-endian order.
|
| 164 |
+
inline uint16_t Load16(absl::Nonnull<const void *> p) {
|
| 165 |
+
return ToHost16(ABSL_INTERNAL_UNALIGNED_LOAD16(p));
|
| 166 |
+
}
|
| 167 |
+
|
| 168 |
+
inline void Store16(absl::Nonnull<void *> p, uint16_t v) {
|
| 169 |
+
ABSL_INTERNAL_UNALIGNED_STORE16(p, FromHost16(v));
|
| 170 |
+
}
|
| 171 |
+
|
| 172 |
+
inline uint32_t Load32(absl::Nonnull<const void *> p) {
|
| 173 |
+
return ToHost32(ABSL_INTERNAL_UNALIGNED_LOAD32(p));
|
| 174 |
+
}
|
| 175 |
+
|
| 176 |
+
inline void Store32(absl::Nonnull<void *> p, uint32_t v) {
|
| 177 |
+
ABSL_INTERNAL_UNALIGNED_STORE32(p, FromHost32(v));
|
| 178 |
+
}
|
| 179 |
+
|
| 180 |
+
inline uint64_t Load64(absl::Nonnull<const void *> p) {
|
| 181 |
+
return ToHost64(ABSL_INTERNAL_UNALIGNED_LOAD64(p));
|
| 182 |
+
}
|
| 183 |
+
|
| 184 |
+
inline void Store64(absl::Nonnull<void *> p, uint64_t v) {
|
| 185 |
+
ABSL_INTERNAL_UNALIGNED_STORE64(p, FromHost64(v));
|
| 186 |
+
}
|
| 187 |
+
|
| 188 |
+
} // namespace little_endian
|
| 189 |
+
|
| 190 |
+
// Utilities to convert numbers between the current hosts's native byte
|
| 191 |
+
// order and big-endian byte order (same as network byte order)
|
| 192 |
+
//
|
| 193 |
+
// Load/Store methods are alignment safe
|
| 194 |
+
namespace big_endian {
|
| 195 |
+
#ifdef ABSL_IS_LITTLE_ENDIAN
|
| 196 |
+
|
| 197 |
+
inline uint16_t FromHost16(uint16_t x) { return gbswap_16(x); }
|
| 198 |
+
inline uint16_t ToHost16(uint16_t x) { return gbswap_16(x); }
|
| 199 |
+
|
| 200 |
+
inline uint32_t FromHost32(uint32_t x) { return gbswap_32(x); }
|
| 201 |
+
inline uint32_t ToHost32(uint32_t x) { return gbswap_32(x); }
|
| 202 |
+
|
| 203 |
+
inline uint64_t FromHost64(uint64_t x) { return gbswap_64(x); }
|
| 204 |
+
inline uint64_t ToHost64(uint64_t x) { return gbswap_64(x); }
|
| 205 |
+
|
| 206 |
+
inline constexpr bool IsLittleEndian() { return true; }
|
| 207 |
+
|
| 208 |
+
#elif defined ABSL_IS_BIG_ENDIAN
|
| 209 |
+
|
| 210 |
+
inline uint16_t FromHost16(uint16_t x) { return x; }
|
| 211 |
+
inline uint16_t ToHost16(uint16_t x) { return x; }
|
| 212 |
+
|
| 213 |
+
inline uint32_t FromHost32(uint32_t x) { return x; }
|
| 214 |
+
inline uint32_t ToHost32(uint32_t x) { return x; }
|
| 215 |
+
|
| 216 |
+
inline uint64_t FromHost64(uint64_t x) { return x; }
|
| 217 |
+
inline uint64_t ToHost64(uint64_t x) { return x; }
|
| 218 |
+
|
| 219 |
+
inline constexpr bool IsLittleEndian() { return false; }
|
| 220 |
+
|
| 221 |
+
#endif /* ENDIAN */
|
| 222 |
+
|
| 223 |
+
inline uint8_t FromHost(uint8_t x) { return x; }
|
| 224 |
+
inline uint16_t FromHost(uint16_t x) { return FromHost16(x); }
|
| 225 |
+
inline uint32_t FromHost(uint32_t x) { return FromHost32(x); }
|
| 226 |
+
inline uint64_t FromHost(uint64_t x) { return FromHost64(x); }
|
| 227 |
+
inline uint8_t ToHost(uint8_t x) { return x; }
|
| 228 |
+
inline uint16_t ToHost(uint16_t x) { return ToHost16(x); }
|
| 229 |
+
inline uint32_t ToHost(uint32_t x) { return ToHost32(x); }
|
| 230 |
+
inline uint64_t ToHost(uint64_t x) { return ToHost64(x); }
|
| 231 |
+
|
| 232 |
+
inline int8_t FromHost(int8_t x) { return x; }
|
| 233 |
+
inline int16_t FromHost(int16_t x) {
|
| 234 |
+
return bit_cast<int16_t>(FromHost16(bit_cast<uint16_t>(x)));
|
| 235 |
+
}
|
| 236 |
+
inline int32_t FromHost(int32_t x) {
|
| 237 |
+
return bit_cast<int32_t>(FromHost32(bit_cast<uint32_t>(x)));
|
| 238 |
+
}
|
| 239 |
+
inline int64_t FromHost(int64_t x) {
|
| 240 |
+
return bit_cast<int64_t>(FromHost64(bit_cast<uint64_t>(x)));
|
| 241 |
+
}
|
| 242 |
+
inline int8_t ToHost(int8_t x) { return x; }
|
| 243 |
+
inline int16_t ToHost(int16_t x) {
|
| 244 |
+
return bit_cast<int16_t>(ToHost16(bit_cast<uint16_t>(x)));
|
| 245 |
+
}
|
| 246 |
+
inline int32_t ToHost(int32_t x) {
|
| 247 |
+
return bit_cast<int32_t>(ToHost32(bit_cast<uint32_t>(x)));
|
| 248 |
+
}
|
| 249 |
+
inline int64_t ToHost(int64_t x) {
|
| 250 |
+
return bit_cast<int64_t>(ToHost64(bit_cast<uint64_t>(x)));
|
| 251 |
+
}
|
| 252 |
+
|
| 253 |
+
// Functions to do unaligned loads and stores in big-endian order.
|
| 254 |
+
inline uint16_t Load16(absl::Nonnull<const void *> p) {
|
| 255 |
+
return ToHost16(ABSL_INTERNAL_UNALIGNED_LOAD16(p));
|
| 256 |
+
}
|
| 257 |
+
|
| 258 |
+
inline void Store16(absl::Nonnull<void *> p, uint16_t v) {
|
| 259 |
+
ABSL_INTERNAL_UNALIGNED_STORE16(p, FromHost16(v));
|
| 260 |
+
}
|
| 261 |
+
|
| 262 |
+
inline uint32_t Load32(absl::Nonnull<const void *> p) {
|
| 263 |
+
return ToHost32(ABSL_INTERNAL_UNALIGNED_LOAD32(p));
|
| 264 |
+
}
|
| 265 |
+
|
| 266 |
+
inline void Store32(absl::Nonnull<void *>p, uint32_t v) {
|
| 267 |
+
ABSL_INTERNAL_UNALIGNED_STORE32(p, FromHost32(v));
|
| 268 |
+
}
|
| 269 |
+
|
| 270 |
+
inline uint64_t Load64(absl::Nonnull<const void *> p) {
|
| 271 |
+
return ToHost64(ABSL_INTERNAL_UNALIGNED_LOAD64(p));
|
| 272 |
+
}
|
| 273 |
+
|
| 274 |
+
inline void Store64(absl::Nonnull<void *> p, uint64_t v) {
|
| 275 |
+
ABSL_INTERNAL_UNALIGNED_STORE64(p, FromHost64(v));
|
| 276 |
+
}
|
| 277 |
+
|
| 278 |
+
} // namespace big_endian
|
| 279 |
+
|
| 280 |
+
ABSL_NAMESPACE_END
|
| 281 |
+
} // namespace absl
|
| 282 |
+
|
| 283 |
+
#endif // ABSL_BASE_INTERNAL_ENDIAN_H_
|
include/absl/base/internal/errno_saver.h
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
// Copyright 2017 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_BASE_INTERNAL_ERRNO_SAVER_H_
|
| 16 |
+
#define ABSL_BASE_INTERNAL_ERRNO_SAVER_H_
|
| 17 |
+
|
| 18 |
+
#include <cerrno>
|
| 19 |
+
|
| 20 |
+
#include "absl/base/config.h"
|
| 21 |
+
|
| 22 |
+
namespace absl {
|
| 23 |
+
ABSL_NAMESPACE_BEGIN
|
| 24 |
+
namespace base_internal {
|
| 25 |
+
|
| 26 |
+
// `ErrnoSaver` captures the value of `errno` upon construction and restores it
|
| 27 |
+
// upon deletion. It is used in low-level code and must be super fast. Do not
|
| 28 |
+
// add instrumentation, even in debug modes.
|
| 29 |
+
class ErrnoSaver {
|
| 30 |
+
public:
|
| 31 |
+
ErrnoSaver() : saved_errno_(errno) {}
|
| 32 |
+
~ErrnoSaver() { errno = saved_errno_; }
|
| 33 |
+
int operator()() const { return saved_errno_; }
|
| 34 |
+
|
| 35 |
+
private:
|
| 36 |
+
const int saved_errno_;
|
| 37 |
+
};
|
| 38 |
+
|
| 39 |
+
} // namespace base_internal
|
| 40 |
+
ABSL_NAMESPACE_END
|
| 41 |
+
} // namespace absl
|
| 42 |
+
|
| 43 |
+
#endif // ABSL_BASE_INTERNAL_ERRNO_SAVER_H_
|
include/absl/base/internal/exception_safety_testing.h
ADDED
|
@@ -0,0 +1,1109 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
// Copyright 2017 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 |
+
// Utilities for testing exception-safety
|
| 16 |
+
|
| 17 |
+
#ifndef ABSL_BASE_INTERNAL_EXCEPTION_SAFETY_TESTING_H_
|
| 18 |
+
#define ABSL_BASE_INTERNAL_EXCEPTION_SAFETY_TESTING_H_
|
| 19 |
+
|
| 20 |
+
#include "absl/base/config.h"
|
| 21 |
+
|
| 22 |
+
#ifdef ABSL_HAVE_EXCEPTIONS
|
| 23 |
+
|
| 24 |
+
#include <cstddef>
|
| 25 |
+
#include <cstdint>
|
| 26 |
+
#include <functional>
|
| 27 |
+
#include <initializer_list>
|
| 28 |
+
#include <iosfwd>
|
| 29 |
+
#include <string>
|
| 30 |
+
#include <tuple>
|
| 31 |
+
#include <unordered_map>
|
| 32 |
+
|
| 33 |
+
#include "gtest/gtest.h"
|
| 34 |
+
#include "absl/base/internal/pretty_function.h"
|
| 35 |
+
#include "absl/memory/memory.h"
|
| 36 |
+
#include "absl/meta/type_traits.h"
|
| 37 |
+
#include "absl/strings/string_view.h"
|
| 38 |
+
#include "absl/strings/substitute.h"
|
| 39 |
+
#include "absl/utility/utility.h"
|
| 40 |
+
|
| 41 |
+
namespace testing {
|
| 42 |
+
|
| 43 |
+
enum class TypeSpec;
|
| 44 |
+
enum class AllocSpec;
|
| 45 |
+
|
| 46 |
+
constexpr TypeSpec operator|(TypeSpec a, TypeSpec b) {
|
| 47 |
+
using T = absl::underlying_type_t<TypeSpec>;
|
| 48 |
+
return static_cast<TypeSpec>(static_cast<T>(a) | static_cast<T>(b));
|
| 49 |
+
}
|
| 50 |
+
|
| 51 |
+
constexpr TypeSpec operator&(TypeSpec a, TypeSpec b) {
|
| 52 |
+
using T = absl::underlying_type_t<TypeSpec>;
|
| 53 |
+
return static_cast<TypeSpec>(static_cast<T>(a) & static_cast<T>(b));
|
| 54 |
+
}
|
| 55 |
+
|
| 56 |
+
constexpr AllocSpec operator|(AllocSpec a, AllocSpec b) {
|
| 57 |
+
using T = absl::underlying_type_t<AllocSpec>;
|
| 58 |
+
return static_cast<AllocSpec>(static_cast<T>(a) | static_cast<T>(b));
|
| 59 |
+
}
|
| 60 |
+
|
| 61 |
+
constexpr AllocSpec operator&(AllocSpec a, AllocSpec b) {
|
| 62 |
+
using T = absl::underlying_type_t<AllocSpec>;
|
| 63 |
+
return static_cast<AllocSpec>(static_cast<T>(a) & static_cast<T>(b));
|
| 64 |
+
}
|
| 65 |
+
|
| 66 |
+
namespace exceptions_internal {
|
| 67 |
+
|
| 68 |
+
std::string GetSpecString(TypeSpec);
|
| 69 |
+
std::string GetSpecString(AllocSpec);
|
| 70 |
+
|
| 71 |
+
struct NoThrowTag {};
|
| 72 |
+
struct StrongGuaranteeTagType {};
|
| 73 |
+
|
| 74 |
+
// A simple exception class. We throw this so that test code can catch
|
| 75 |
+
// exceptions specifically thrown by ThrowingValue.
|
| 76 |
+
class TestException {
|
| 77 |
+
public:
|
| 78 |
+
explicit TestException(absl::string_view msg) : msg_(msg) {}
|
| 79 |
+
virtual ~TestException() {}
|
| 80 |
+
virtual const char* what() const noexcept { return msg_.c_str(); }
|
| 81 |
+
|
| 82 |
+
private:
|
| 83 |
+
std::string msg_;
|
| 84 |
+
};
|
| 85 |
+
|
| 86 |
+
// TestBadAllocException exists because allocation functions must throw an
|
| 87 |
+
// exception which can be caught by a handler of std::bad_alloc. We use a child
|
| 88 |
+
// class of std::bad_alloc so we can customise the error message, and also
|
| 89 |
+
// derive from TestException so we don't accidentally end up catching an actual
|
| 90 |
+
// bad_alloc exception in TestExceptionSafety.
|
| 91 |
+
class TestBadAllocException : public std::bad_alloc, public TestException {
|
| 92 |
+
public:
|
| 93 |
+
explicit TestBadAllocException(absl::string_view msg) : TestException(msg) {}
|
| 94 |
+
using TestException::what;
|
| 95 |
+
};
|
| 96 |
+
|
| 97 |
+
extern int countdown;
|
| 98 |
+
|
| 99 |
+
// Allows the countdown variable to be set manually (defaulting to the initial
|
| 100 |
+
// value of 0)
|
| 101 |
+
inline void SetCountdown(int i = 0) { countdown = i; }
|
| 102 |
+
// Sets the countdown to the terminal value -1
|
| 103 |
+
inline void UnsetCountdown() { SetCountdown(-1); }
|
| 104 |
+
|
| 105 |
+
void MaybeThrow(absl::string_view msg, bool throw_bad_alloc = false);
|
| 106 |
+
|
| 107 |
+
testing::AssertionResult FailureMessage(const TestException& e,
|
| 108 |
+
int countdown) noexcept;
|
| 109 |
+
|
| 110 |
+
struct TrackedAddress {
|
| 111 |
+
bool is_alive;
|
| 112 |
+
std::string description;
|
| 113 |
+
};
|
| 114 |
+
|
| 115 |
+
// Inspects the constructions and destructions of anything inheriting from
|
| 116 |
+
// TrackedObject. This allows us to safely "leak" TrackedObjects, as
|
| 117 |
+
// ConstructorTracker will destroy everything left over in its destructor.
|
| 118 |
+
class ConstructorTracker {
|
| 119 |
+
public:
|
| 120 |
+
explicit ConstructorTracker(int count) : countdown_(count) {
|
| 121 |
+
assert(current_tracker_instance_ == nullptr);
|
| 122 |
+
current_tracker_instance_ = this;
|
| 123 |
+
}
|
| 124 |
+
|
| 125 |
+
~ConstructorTracker() {
|
| 126 |
+
assert(current_tracker_instance_ == this);
|
| 127 |
+
current_tracker_instance_ = nullptr;
|
| 128 |
+
|
| 129 |
+
for (auto& it : address_map_) {
|
| 130 |
+
void* address = it.first;
|
| 131 |
+
TrackedAddress& tracked_address = it.second;
|
| 132 |
+
if (tracked_address.is_alive) {
|
| 133 |
+
ADD_FAILURE() << ErrorMessage(address, tracked_address.description,
|
| 134 |
+
countdown_, "Object was not destroyed.");
|
| 135 |
+
}
|
| 136 |
+
}
|
| 137 |
+
}
|
| 138 |
+
|
| 139 |
+
static void ObjectConstructed(void* address, std::string description) {
|
| 140 |
+
if (!CurrentlyTracking()) return;
|
| 141 |
+
|
| 142 |
+
TrackedAddress& tracked_address =
|
| 143 |
+
current_tracker_instance_->address_map_[address];
|
| 144 |
+
if (tracked_address.is_alive) {
|
| 145 |
+
ADD_FAILURE() << ErrorMessage(
|
| 146 |
+
address, tracked_address.description,
|
| 147 |
+
current_tracker_instance_->countdown_,
|
| 148 |
+
"Object was re-constructed. Current object was constructed by " +
|
| 149 |
+
description);
|
| 150 |
+
}
|
| 151 |
+
tracked_address = {true, std::move(description)};
|
| 152 |
+
}
|
| 153 |
+
|
| 154 |
+
static void ObjectDestructed(void* address) {
|
| 155 |
+
if (!CurrentlyTracking()) return;
|
| 156 |
+
|
| 157 |
+
auto it = current_tracker_instance_->address_map_.find(address);
|
| 158 |
+
// Not tracked. Ignore.
|
| 159 |
+
if (it == current_tracker_instance_->address_map_.end()) return;
|
| 160 |
+
|
| 161 |
+
TrackedAddress& tracked_address = it->second;
|
| 162 |
+
if (!tracked_address.is_alive) {
|
| 163 |
+
ADD_FAILURE() << ErrorMessage(address, tracked_address.description,
|
| 164 |
+
current_tracker_instance_->countdown_,
|
| 165 |
+
"Object was re-destroyed.");
|
| 166 |
+
}
|
| 167 |
+
tracked_address.is_alive = false;
|
| 168 |
+
}
|
| 169 |
+
|
| 170 |
+
private:
|
| 171 |
+
static bool CurrentlyTracking() {
|
| 172 |
+
return current_tracker_instance_ != nullptr;
|
| 173 |
+
}
|
| 174 |
+
|
| 175 |
+
static std::string ErrorMessage(void* address,
|
| 176 |
+
const std::string& address_description,
|
| 177 |
+
int countdown,
|
| 178 |
+
const std::string& error_description) {
|
| 179 |
+
return absl::Substitute(
|
| 180 |
+
"With coundtown at $0:\n"
|
| 181 |
+
" $1\n"
|
| 182 |
+
" Object originally constructed by $2\n"
|
| 183 |
+
" Object address: $3\n",
|
| 184 |
+
countdown, error_description, address_description, address);
|
| 185 |
+
}
|
| 186 |
+
|
| 187 |
+
std::unordered_map<void*, TrackedAddress> address_map_;
|
| 188 |
+
int countdown_;
|
| 189 |
+
|
| 190 |
+
static ConstructorTracker* current_tracker_instance_;
|
| 191 |
+
};
|
| 192 |
+
|
| 193 |
+
class TrackedObject {
|
| 194 |
+
public:
|
| 195 |
+
TrackedObject(const TrackedObject&) = delete;
|
| 196 |
+
TrackedObject(TrackedObject&&) = delete;
|
| 197 |
+
|
| 198 |
+
protected:
|
| 199 |
+
explicit TrackedObject(std::string description) {
|
| 200 |
+
ConstructorTracker::ObjectConstructed(this, std::move(description));
|
| 201 |
+
}
|
| 202 |
+
|
| 203 |
+
~TrackedObject() noexcept { ConstructorTracker::ObjectDestructed(this); }
|
| 204 |
+
};
|
| 205 |
+
} // namespace exceptions_internal
|
| 206 |
+
|
| 207 |
+
extern exceptions_internal::NoThrowTag nothrow_ctor;
|
| 208 |
+
|
| 209 |
+
extern exceptions_internal::StrongGuaranteeTagType strong_guarantee;
|
| 210 |
+
|
| 211 |
+
// A test class which is convertible to bool. The conversion can be
|
| 212 |
+
// instrumented to throw at a controlled time.
|
| 213 |
+
class ThrowingBool {
|
| 214 |
+
public:
|
| 215 |
+
ThrowingBool(bool b) noexcept : b_(b) {} // NOLINT(runtime/explicit)
|
| 216 |
+
operator bool() const { // NOLINT
|
| 217 |
+
exceptions_internal::MaybeThrow(ABSL_PRETTY_FUNCTION);
|
| 218 |
+
return b_;
|
| 219 |
+
}
|
| 220 |
+
|
| 221 |
+
private:
|
| 222 |
+
bool b_;
|
| 223 |
+
};
|
| 224 |
+
|
| 225 |
+
/*
|
| 226 |
+
* Configuration enum for the ThrowingValue type that defines behavior for the
|
| 227 |
+
* lifetime of the instance. Use testing::nothrow_ctor to prevent the integer
|
| 228 |
+
* constructor from throwing.
|
| 229 |
+
*
|
| 230 |
+
* kEverythingThrows: Every operation can throw an exception
|
| 231 |
+
* kNoThrowCopy: Copy construction and copy assignment will not throw
|
| 232 |
+
* kNoThrowMove: Move construction and move assignment will not throw
|
| 233 |
+
* kNoThrowNew: Overloaded operators new and new[] will not throw
|
| 234 |
+
*/
|
| 235 |
+
enum class TypeSpec {
|
| 236 |
+
kEverythingThrows = 0,
|
| 237 |
+
kNoThrowCopy = 1,
|
| 238 |
+
kNoThrowMove = 1 << 1,
|
| 239 |
+
kNoThrowNew = 1 << 2,
|
| 240 |
+
};
|
| 241 |
+
|
| 242 |
+
/*
|
| 243 |
+
* A testing class instrumented to throw an exception at a controlled time.
|
| 244 |
+
*
|
| 245 |
+
* ThrowingValue implements a slightly relaxed version of the Regular concept --
|
| 246 |
+
* that is it's a value type with the expected semantics. It also implements
|
| 247 |
+
* arithmetic operations. It doesn't implement member and pointer operators
|
| 248 |
+
* like operator-> or operator[].
|
| 249 |
+
*
|
| 250 |
+
* ThrowingValue can be instrumented to have certain operations be noexcept by
|
| 251 |
+
* using compile-time bitfield template arguments. That is, to make an
|
| 252 |
+
* ThrowingValue which has noexcept move construction/assignment and noexcept
|
| 253 |
+
* copy construction/assignment, use the following:
|
| 254 |
+
* ThrowingValue<testing::kNoThrowMove | testing::kNoThrowCopy> my_thrwr{val};
|
| 255 |
+
*/
|
| 256 |
+
template <TypeSpec Spec = TypeSpec::kEverythingThrows>
|
| 257 |
+
class ThrowingValue : private exceptions_internal::TrackedObject {
|
| 258 |
+
static constexpr bool IsSpecified(TypeSpec spec) {
|
| 259 |
+
return static_cast<bool>(Spec & spec);
|
| 260 |
+
}
|
| 261 |
+
|
| 262 |
+
static constexpr int kDefaultValue = 0;
|
| 263 |
+
static constexpr int kBadValue = 938550620;
|
| 264 |
+
|
| 265 |
+
public:
|
| 266 |
+
ThrowingValue() : TrackedObject(GetInstanceString(kDefaultValue)) {
|
| 267 |
+
exceptions_internal::MaybeThrow(ABSL_PRETTY_FUNCTION);
|
| 268 |
+
dummy_ = kDefaultValue;
|
| 269 |
+
}
|
| 270 |
+
|
| 271 |
+
ThrowingValue(const ThrowingValue& other) noexcept(
|
| 272 |
+
IsSpecified(TypeSpec::kNoThrowCopy))
|
| 273 |
+
: TrackedObject(GetInstanceString(other.dummy_)) {
|
| 274 |
+
if (!IsSpecified(TypeSpec::kNoThrowCopy)) {
|
| 275 |
+
exceptions_internal::MaybeThrow(ABSL_PRETTY_FUNCTION);
|
| 276 |
+
}
|
| 277 |
+
dummy_ = other.dummy_;
|
| 278 |
+
}
|
| 279 |
+
|
| 280 |
+
ThrowingValue(ThrowingValue&& other) noexcept(
|
| 281 |
+
IsSpecified(TypeSpec::kNoThrowMove))
|
| 282 |
+
: TrackedObject(GetInstanceString(other.dummy_)) {
|
| 283 |
+
if (!IsSpecified(TypeSpec::kNoThrowMove)) {
|
| 284 |
+
exceptions_internal::MaybeThrow(ABSL_PRETTY_FUNCTION);
|
| 285 |
+
}
|
| 286 |
+
dummy_ = other.dummy_;
|
| 287 |
+
}
|
| 288 |
+
|
| 289 |
+
explicit ThrowingValue(int i) : TrackedObject(GetInstanceString(i)) {
|
| 290 |
+
exceptions_internal::MaybeThrow(ABSL_PRETTY_FUNCTION);
|
| 291 |
+
dummy_ = i;
|
| 292 |
+
}
|
| 293 |
+
|
| 294 |
+
ThrowingValue(int i, exceptions_internal::NoThrowTag) noexcept
|
| 295 |
+
: TrackedObject(GetInstanceString(i)), dummy_(i) {}
|
| 296 |
+
|
| 297 |
+
// absl expects nothrow destructors
|
| 298 |
+
~ThrowingValue() noexcept = default;
|
| 299 |
+
|
| 300 |
+
ThrowingValue& operator=(const ThrowingValue& other) noexcept(
|
| 301 |
+
IsSpecified(TypeSpec::kNoThrowCopy)) {
|
| 302 |
+
dummy_ = kBadValue;
|
| 303 |
+
if (!IsSpecified(TypeSpec::kNoThrowCopy)) {
|
| 304 |
+
exceptions_internal::MaybeThrow(ABSL_PRETTY_FUNCTION);
|
| 305 |
+
}
|
| 306 |
+
dummy_ = other.dummy_;
|
| 307 |
+
return *this;
|
| 308 |
+
}
|
| 309 |
+
|
| 310 |
+
ThrowingValue& operator=(ThrowingValue&& other) noexcept(
|
| 311 |
+
IsSpecified(TypeSpec::kNoThrowMove)) {
|
| 312 |
+
dummy_ = kBadValue;
|
| 313 |
+
if (!IsSpecified(TypeSpec::kNoThrowMove)) {
|
| 314 |
+
exceptions_internal::MaybeThrow(ABSL_PRETTY_FUNCTION);
|
| 315 |
+
}
|
| 316 |
+
dummy_ = other.dummy_;
|
| 317 |
+
return *this;
|
| 318 |
+
}
|
| 319 |
+
|
| 320 |
+
// Arithmetic Operators
|
| 321 |
+
ThrowingValue operator+(const ThrowingValue& other) const {
|
| 322 |
+
exceptions_internal::MaybeThrow(ABSL_PRETTY_FUNCTION);
|
| 323 |
+
return ThrowingValue(dummy_ + other.dummy_, nothrow_ctor);
|
| 324 |
+
}
|
| 325 |
+
|
| 326 |
+
ThrowingValue operator+() const {
|
| 327 |
+
exceptions_internal::MaybeThrow(ABSL_PRETTY_FUNCTION);
|
| 328 |
+
return ThrowingValue(dummy_, nothrow_ctor);
|
| 329 |
+
}
|
| 330 |
+
|
| 331 |
+
ThrowingValue operator-(const ThrowingValue& other) const {
|
| 332 |
+
exceptions_internal::MaybeThrow(ABSL_PRETTY_FUNCTION);
|
| 333 |
+
return ThrowingValue(dummy_ - other.dummy_, nothrow_ctor);
|
| 334 |
+
}
|
| 335 |
+
|
| 336 |
+
ThrowingValue operator-() const {
|
| 337 |
+
exceptions_internal::MaybeThrow(ABSL_PRETTY_FUNCTION);
|
| 338 |
+
return ThrowingValue(-dummy_, nothrow_ctor);
|
| 339 |
+
}
|
| 340 |
+
|
| 341 |
+
ThrowingValue& operator++() {
|
| 342 |
+
exceptions_internal::MaybeThrow(ABSL_PRETTY_FUNCTION);
|
| 343 |
+
++dummy_;
|
| 344 |
+
return *this;
|
| 345 |
+
}
|
| 346 |
+
|
| 347 |
+
ThrowingValue operator++(int) {
|
| 348 |
+
exceptions_internal::MaybeThrow(ABSL_PRETTY_FUNCTION);
|
| 349 |
+
auto out = ThrowingValue(dummy_, nothrow_ctor);
|
| 350 |
+
++dummy_;
|
| 351 |
+
return out;
|
| 352 |
+
}
|
| 353 |
+
|
| 354 |
+
ThrowingValue& operator--() {
|
| 355 |
+
exceptions_internal::MaybeThrow(ABSL_PRETTY_FUNCTION);
|
| 356 |
+
--dummy_;
|
| 357 |
+
return *this;
|
| 358 |
+
}
|
| 359 |
+
|
| 360 |
+
ThrowingValue operator--(int) {
|
| 361 |
+
exceptions_internal::MaybeThrow(ABSL_PRETTY_FUNCTION);
|
| 362 |
+
auto out = ThrowingValue(dummy_, nothrow_ctor);
|
| 363 |
+
--dummy_;
|
| 364 |
+
return out;
|
| 365 |
+
}
|
| 366 |
+
|
| 367 |
+
ThrowingValue operator*(const ThrowingValue& other) const {
|
| 368 |
+
exceptions_internal::MaybeThrow(ABSL_PRETTY_FUNCTION);
|
| 369 |
+
return ThrowingValue(dummy_ * other.dummy_, nothrow_ctor);
|
| 370 |
+
}
|
| 371 |
+
|
| 372 |
+
ThrowingValue operator/(const ThrowingValue& other) const {
|
| 373 |
+
exceptions_internal::MaybeThrow(ABSL_PRETTY_FUNCTION);
|
| 374 |
+
return ThrowingValue(dummy_ / other.dummy_, nothrow_ctor);
|
| 375 |
+
}
|
| 376 |
+
|
| 377 |
+
ThrowingValue operator%(const ThrowingValue& other) const {
|
| 378 |
+
exceptions_internal::MaybeThrow(ABSL_PRETTY_FUNCTION);
|
| 379 |
+
return ThrowingValue(dummy_ % other.dummy_, nothrow_ctor);
|
| 380 |
+
}
|
| 381 |
+
|
| 382 |
+
ThrowingValue operator<<(int shift) const {
|
| 383 |
+
exceptions_internal::MaybeThrow(ABSL_PRETTY_FUNCTION);
|
| 384 |
+
return ThrowingValue(dummy_ << shift, nothrow_ctor);
|
| 385 |
+
}
|
| 386 |
+
|
| 387 |
+
ThrowingValue operator>>(int shift) const {
|
| 388 |
+
exceptions_internal::MaybeThrow(ABSL_PRETTY_FUNCTION);
|
| 389 |
+
return ThrowingValue(dummy_ >> shift, nothrow_ctor);
|
| 390 |
+
}
|
| 391 |
+
|
| 392 |
+
// Comparison Operators
|
| 393 |
+
// NOTE: We use `ThrowingBool` instead of `bool` because most STL
|
| 394 |
+
// types/containers requires T to be convertible to bool.
|
| 395 |
+
friend ThrowingBool operator==(const ThrowingValue& a,
|
| 396 |
+
const ThrowingValue& b) {
|
| 397 |
+
exceptions_internal::MaybeThrow(ABSL_PRETTY_FUNCTION);
|
| 398 |
+
return a.dummy_ == b.dummy_;
|
| 399 |
+
}
|
| 400 |
+
friend ThrowingBool operator!=(const ThrowingValue& a,
|
| 401 |
+
const ThrowingValue& b) {
|
| 402 |
+
exceptions_internal::MaybeThrow(ABSL_PRETTY_FUNCTION);
|
| 403 |
+
return a.dummy_ != b.dummy_;
|
| 404 |
+
}
|
| 405 |
+
friend ThrowingBool operator<(const ThrowingValue& a,
|
| 406 |
+
const ThrowingValue& b) {
|
| 407 |
+
exceptions_internal::MaybeThrow(ABSL_PRETTY_FUNCTION);
|
| 408 |
+
return a.dummy_ < b.dummy_;
|
| 409 |
+
}
|
| 410 |
+
friend ThrowingBool operator<=(const ThrowingValue& a,
|
| 411 |
+
const ThrowingValue& b) {
|
| 412 |
+
exceptions_internal::MaybeThrow(ABSL_PRETTY_FUNCTION);
|
| 413 |
+
return a.dummy_ <= b.dummy_;
|
| 414 |
+
}
|
| 415 |
+
friend ThrowingBool operator>(const ThrowingValue& a,
|
| 416 |
+
const ThrowingValue& b) {
|
| 417 |
+
exceptions_internal::MaybeThrow(ABSL_PRETTY_FUNCTION);
|
| 418 |
+
return a.dummy_ > b.dummy_;
|
| 419 |
+
}
|
| 420 |
+
friend ThrowingBool operator>=(const ThrowingValue& a,
|
| 421 |
+
const ThrowingValue& b) {
|
| 422 |
+
exceptions_internal::MaybeThrow(ABSL_PRETTY_FUNCTION);
|
| 423 |
+
return a.dummy_ >= b.dummy_;
|
| 424 |
+
}
|
| 425 |
+
|
| 426 |
+
// Logical Operators
|
| 427 |
+
ThrowingBool operator!() const {
|
| 428 |
+
exceptions_internal::MaybeThrow(ABSL_PRETTY_FUNCTION);
|
| 429 |
+
return !dummy_;
|
| 430 |
+
}
|
| 431 |
+
|
| 432 |
+
ThrowingBool operator&&(const ThrowingValue& other) const {
|
| 433 |
+
exceptions_internal::MaybeThrow(ABSL_PRETTY_FUNCTION);
|
| 434 |
+
return dummy_ && other.dummy_;
|
| 435 |
+
}
|
| 436 |
+
|
| 437 |
+
ThrowingBool operator||(const ThrowingValue& other) const {
|
| 438 |
+
exceptions_internal::MaybeThrow(ABSL_PRETTY_FUNCTION);
|
| 439 |
+
return dummy_ || other.dummy_;
|
| 440 |
+
}
|
| 441 |
+
|
| 442 |
+
// Bitwise Logical Operators
|
| 443 |
+
ThrowingValue operator~() const {
|
| 444 |
+
exceptions_internal::MaybeThrow(ABSL_PRETTY_FUNCTION);
|
| 445 |
+
return ThrowingValue(~dummy_, nothrow_ctor);
|
| 446 |
+
}
|
| 447 |
+
|
| 448 |
+
ThrowingValue operator&(const ThrowingValue& other) const {
|
| 449 |
+
exceptions_internal::MaybeThrow(ABSL_PRETTY_FUNCTION);
|
| 450 |
+
return ThrowingValue(dummy_ & other.dummy_, nothrow_ctor);
|
| 451 |
+
}
|
| 452 |
+
|
| 453 |
+
ThrowingValue operator|(const ThrowingValue& other) const {
|
| 454 |
+
exceptions_internal::MaybeThrow(ABSL_PRETTY_FUNCTION);
|
| 455 |
+
return ThrowingValue(dummy_ | other.dummy_, nothrow_ctor);
|
| 456 |
+
}
|
| 457 |
+
|
| 458 |
+
ThrowingValue operator^(const ThrowingValue& other) const {
|
| 459 |
+
exceptions_internal::MaybeThrow(ABSL_PRETTY_FUNCTION);
|
| 460 |
+
return ThrowingValue(dummy_ ^ other.dummy_, nothrow_ctor);
|
| 461 |
+
}
|
| 462 |
+
|
| 463 |
+
// Compound Assignment operators
|
| 464 |
+
ThrowingValue& operator+=(const ThrowingValue& other) {
|
| 465 |
+
exceptions_internal::MaybeThrow(ABSL_PRETTY_FUNCTION);
|
| 466 |
+
dummy_ += other.dummy_;
|
| 467 |
+
return *this;
|
| 468 |
+
}
|
| 469 |
+
|
| 470 |
+
ThrowingValue& operator-=(const ThrowingValue& other) {
|
| 471 |
+
exceptions_internal::MaybeThrow(ABSL_PRETTY_FUNCTION);
|
| 472 |
+
dummy_ -= other.dummy_;
|
| 473 |
+
return *this;
|
| 474 |
+
}
|
| 475 |
+
|
| 476 |
+
ThrowingValue& operator*=(const ThrowingValue& other) {
|
| 477 |
+
exceptions_internal::MaybeThrow(ABSL_PRETTY_FUNCTION);
|
| 478 |
+
dummy_ *= other.dummy_;
|
| 479 |
+
return *this;
|
| 480 |
+
}
|
| 481 |
+
|
| 482 |
+
ThrowingValue& operator/=(const ThrowingValue& other) {
|
| 483 |
+
exceptions_internal::MaybeThrow(ABSL_PRETTY_FUNCTION);
|
| 484 |
+
dummy_ /= other.dummy_;
|
| 485 |
+
return *this;
|
| 486 |
+
}
|
| 487 |
+
|
| 488 |
+
ThrowingValue& operator%=(const ThrowingValue& other) {
|
| 489 |
+
exceptions_internal::MaybeThrow(ABSL_PRETTY_FUNCTION);
|
| 490 |
+
dummy_ %= other.dummy_;
|
| 491 |
+
return *this;
|
| 492 |
+
}
|
| 493 |
+
|
| 494 |
+
ThrowingValue& operator&=(const ThrowingValue& other) {
|
| 495 |
+
exceptions_internal::MaybeThrow(ABSL_PRETTY_FUNCTION);
|
| 496 |
+
dummy_ &= other.dummy_;
|
| 497 |
+
return *this;
|
| 498 |
+
}
|
| 499 |
+
|
| 500 |
+
ThrowingValue& operator|=(const ThrowingValue& other) {
|
| 501 |
+
exceptions_internal::MaybeThrow(ABSL_PRETTY_FUNCTION);
|
| 502 |
+
dummy_ |= other.dummy_;
|
| 503 |
+
return *this;
|
| 504 |
+
}
|
| 505 |
+
|
| 506 |
+
ThrowingValue& operator^=(const ThrowingValue& other) {
|
| 507 |
+
exceptions_internal::MaybeThrow(ABSL_PRETTY_FUNCTION);
|
| 508 |
+
dummy_ ^= other.dummy_;
|
| 509 |
+
return *this;
|
| 510 |
+
}
|
| 511 |
+
|
| 512 |
+
ThrowingValue& operator<<=(int shift) {
|
| 513 |
+
exceptions_internal::MaybeThrow(ABSL_PRETTY_FUNCTION);
|
| 514 |
+
dummy_ <<= shift;
|
| 515 |
+
return *this;
|
| 516 |
+
}
|
| 517 |
+
|
| 518 |
+
ThrowingValue& operator>>=(int shift) {
|
| 519 |
+
exceptions_internal::MaybeThrow(ABSL_PRETTY_FUNCTION);
|
| 520 |
+
dummy_ >>= shift;
|
| 521 |
+
return *this;
|
| 522 |
+
}
|
| 523 |
+
|
| 524 |
+
// Pointer operators
|
| 525 |
+
void operator&() const = delete; // NOLINT(runtime/operator)
|
| 526 |
+
|
| 527 |
+
// Stream operators
|
| 528 |
+
friend std::ostream& operator<<(std::ostream& os, const ThrowingValue& tv) {
|
| 529 |
+
exceptions_internal::MaybeThrow(ABSL_PRETTY_FUNCTION);
|
| 530 |
+
return os << GetInstanceString(tv.dummy_);
|
| 531 |
+
}
|
| 532 |
+
|
| 533 |
+
friend std::istream& operator>>(std::istream& is, const ThrowingValue&) {
|
| 534 |
+
exceptions_internal::MaybeThrow(ABSL_PRETTY_FUNCTION);
|
| 535 |
+
return is;
|
| 536 |
+
}
|
| 537 |
+
|
| 538 |
+
// Memory management operators
|
| 539 |
+
static void* operator new(size_t s) noexcept(
|
| 540 |
+
IsSpecified(TypeSpec::kNoThrowNew)) {
|
| 541 |
+
if (!IsSpecified(TypeSpec::kNoThrowNew)) {
|
| 542 |
+
exceptions_internal::MaybeThrow(ABSL_PRETTY_FUNCTION, true);
|
| 543 |
+
}
|
| 544 |
+
return ::operator new(s);
|
| 545 |
+
}
|
| 546 |
+
|
| 547 |
+
static void* operator new[](size_t s) noexcept(
|
| 548 |
+
IsSpecified(TypeSpec::kNoThrowNew)) {
|
| 549 |
+
if (!IsSpecified(TypeSpec::kNoThrowNew)) {
|
| 550 |
+
exceptions_internal::MaybeThrow(ABSL_PRETTY_FUNCTION, true);
|
| 551 |
+
}
|
| 552 |
+
return ::operator new[](s);
|
| 553 |
+
}
|
| 554 |
+
|
| 555 |
+
template <typename... Args>
|
| 556 |
+
static void* operator new(size_t s, Args&&... args) noexcept(
|
| 557 |
+
IsSpecified(TypeSpec::kNoThrowNew)) {
|
| 558 |
+
if (!IsSpecified(TypeSpec::kNoThrowNew)) {
|
| 559 |
+
exceptions_internal::MaybeThrow(ABSL_PRETTY_FUNCTION, true);
|
| 560 |
+
}
|
| 561 |
+
return ::operator new(s, std::forward<Args>(args)...);
|
| 562 |
+
}
|
| 563 |
+
|
| 564 |
+
template <typename... Args>
|
| 565 |
+
static void* operator new[](size_t s, Args&&... args) noexcept(
|
| 566 |
+
IsSpecified(TypeSpec::kNoThrowNew)) {
|
| 567 |
+
if (!IsSpecified(TypeSpec::kNoThrowNew)) {
|
| 568 |
+
exceptions_internal::MaybeThrow(ABSL_PRETTY_FUNCTION, true);
|
| 569 |
+
}
|
| 570 |
+
return ::operator new[](s, std::forward<Args>(args)...);
|
| 571 |
+
}
|
| 572 |
+
|
| 573 |
+
// Abseil doesn't support throwing overloaded operator delete. These are
|
| 574 |
+
// provided so a throwing operator-new can clean up after itself.
|
| 575 |
+
void operator delete(void* p) noexcept { ::operator delete(p); }
|
| 576 |
+
|
| 577 |
+
template <typename... Args>
|
| 578 |
+
void operator delete(void* p, Args&&... args) noexcept {
|
| 579 |
+
::operator delete(p, std::forward<Args>(args)...);
|
| 580 |
+
}
|
| 581 |
+
|
| 582 |
+
void operator delete[](void* p) noexcept { return ::operator delete[](p); }
|
| 583 |
+
|
| 584 |
+
template <typename... Args>
|
| 585 |
+
void operator delete[](void* p, Args&&... args) noexcept {
|
| 586 |
+
return ::operator delete[](p, std::forward<Args>(args)...);
|
| 587 |
+
}
|
| 588 |
+
|
| 589 |
+
// Non-standard access to the actual contained value. No need for this to
|
| 590 |
+
// throw.
|
| 591 |
+
int& Get() noexcept { return dummy_; }
|
| 592 |
+
const int& Get() const noexcept { return dummy_; }
|
| 593 |
+
|
| 594 |
+
private:
|
| 595 |
+
static std::string GetInstanceString(int dummy) {
|
| 596 |
+
return absl::StrCat("ThrowingValue<",
|
| 597 |
+
exceptions_internal::GetSpecString(Spec), ">(", dummy,
|
| 598 |
+
")");
|
| 599 |
+
}
|
| 600 |
+
|
| 601 |
+
int dummy_;
|
| 602 |
+
};
|
| 603 |
+
// While not having to do with exceptions, explicitly delete comma operator, to
|
| 604 |
+
// make sure we don't use it on user-supplied types.
|
| 605 |
+
template <TypeSpec Spec, typename T>
|
| 606 |
+
void operator,(const ThrowingValue<Spec>&, T&&) = delete;
|
| 607 |
+
template <TypeSpec Spec, typename T>
|
| 608 |
+
void operator,(T&&, const ThrowingValue<Spec>&) = delete;
|
| 609 |
+
|
| 610 |
+
/*
|
| 611 |
+
* Configuration enum for the ThrowingAllocator type that defines behavior for
|
| 612 |
+
* the lifetime of the instance.
|
| 613 |
+
*
|
| 614 |
+
* kEverythingThrows: Calls to the member functions may throw
|
| 615 |
+
* kNoThrowAllocate: Calls to the member functions will not throw
|
| 616 |
+
*/
|
| 617 |
+
enum class AllocSpec {
|
| 618 |
+
kEverythingThrows = 0,
|
| 619 |
+
kNoThrowAllocate = 1,
|
| 620 |
+
};
|
| 621 |
+
|
| 622 |
+
/*
|
| 623 |
+
* An allocator type which is instrumented to throw at a controlled time, or not
|
| 624 |
+
* to throw, using AllocSpec. The supported settings are the default of every
|
| 625 |
+
* function which is allowed to throw in a conforming allocator possibly
|
| 626 |
+
* throwing, or nothing throws, in line with the ABSL_ALLOCATOR_THROWS
|
| 627 |
+
* configuration macro.
|
| 628 |
+
*/
|
| 629 |
+
template <typename T, AllocSpec Spec = AllocSpec::kEverythingThrows>
|
| 630 |
+
class ThrowingAllocator : private exceptions_internal::TrackedObject {
|
| 631 |
+
static constexpr bool IsSpecified(AllocSpec spec) {
|
| 632 |
+
return static_cast<bool>(Spec & spec);
|
| 633 |
+
}
|
| 634 |
+
|
| 635 |
+
public:
|
| 636 |
+
using pointer = T*;
|
| 637 |
+
using const_pointer = const T*;
|
| 638 |
+
using reference = T&;
|
| 639 |
+
using const_reference = const T&;
|
| 640 |
+
using void_pointer = void*;
|
| 641 |
+
using const_void_pointer = const void*;
|
| 642 |
+
using value_type = T;
|
| 643 |
+
using size_type = size_t;
|
| 644 |
+
using difference_type = ptrdiff_t;
|
| 645 |
+
|
| 646 |
+
using is_nothrow =
|
| 647 |
+
std::integral_constant<bool, Spec == AllocSpec::kNoThrowAllocate>;
|
| 648 |
+
using propagate_on_container_copy_assignment = std::true_type;
|
| 649 |
+
using propagate_on_container_move_assignment = std::true_type;
|
| 650 |
+
using propagate_on_container_swap = std::true_type;
|
| 651 |
+
using is_always_equal = std::false_type;
|
| 652 |
+
|
| 653 |
+
ThrowingAllocator() : TrackedObject(GetInstanceString(next_id_)) {
|
| 654 |
+
exceptions_internal::MaybeThrow(ABSL_PRETTY_FUNCTION);
|
| 655 |
+
dummy_ = std::make_shared<const int>(next_id_++);
|
| 656 |
+
}
|
| 657 |
+
|
| 658 |
+
template <typename U>
|
| 659 |
+
ThrowingAllocator(const ThrowingAllocator<U, Spec>& other) noexcept // NOLINT
|
| 660 |
+
: TrackedObject(GetInstanceString(*other.State())),
|
| 661 |
+
dummy_(other.State()) {}
|
| 662 |
+
|
| 663 |
+
// According to C++11 standard [17.6.3.5], Table 28, the move/copy ctors of
|
| 664 |
+
// allocator shall not exit via an exception, thus they are marked noexcept.
|
| 665 |
+
ThrowingAllocator(const ThrowingAllocator& other) noexcept
|
| 666 |
+
: TrackedObject(GetInstanceString(*other.State())),
|
| 667 |
+
dummy_(other.State()) {}
|
| 668 |
+
|
| 669 |
+
template <typename U>
|
| 670 |
+
ThrowingAllocator(ThrowingAllocator<U, Spec>&& other) noexcept // NOLINT
|
| 671 |
+
: TrackedObject(GetInstanceString(*other.State())),
|
| 672 |
+
dummy_(std::move(other.State())) {}
|
| 673 |
+
|
| 674 |
+
ThrowingAllocator(ThrowingAllocator&& other) noexcept
|
| 675 |
+
: TrackedObject(GetInstanceString(*other.State())),
|
| 676 |
+
dummy_(std::move(other.State())) {}
|
| 677 |
+
|
| 678 |
+
~ThrowingAllocator() noexcept = default;
|
| 679 |
+
|
| 680 |
+
ThrowingAllocator& operator=(const ThrowingAllocator& other) noexcept {
|
| 681 |
+
dummy_ = other.State();
|
| 682 |
+
return *this;
|
| 683 |
+
}
|
| 684 |
+
|
| 685 |
+
template <typename U>
|
| 686 |
+
ThrowingAllocator& operator=(
|
| 687 |
+
const ThrowingAllocator<U, Spec>& other) noexcept {
|
| 688 |
+
dummy_ = other.State();
|
| 689 |
+
return *this;
|
| 690 |
+
}
|
| 691 |
+
|
| 692 |
+
template <typename U>
|
| 693 |
+
ThrowingAllocator& operator=(ThrowingAllocator<U, Spec>&& other) noexcept {
|
| 694 |
+
dummy_ = std::move(other.State());
|
| 695 |
+
return *this;
|
| 696 |
+
}
|
| 697 |
+
|
| 698 |
+
template <typename U>
|
| 699 |
+
struct rebind {
|
| 700 |
+
using other = ThrowingAllocator<U, Spec>;
|
| 701 |
+
};
|
| 702 |
+
|
| 703 |
+
pointer allocate(size_type n) noexcept(
|
| 704 |
+
IsSpecified(AllocSpec::kNoThrowAllocate)) {
|
| 705 |
+
ReadStateAndMaybeThrow(ABSL_PRETTY_FUNCTION);
|
| 706 |
+
return static_cast<pointer>(::operator new(n * sizeof(T)));
|
| 707 |
+
}
|
| 708 |
+
|
| 709 |
+
pointer allocate(size_type n, const_void_pointer) noexcept(
|
| 710 |
+
IsSpecified(AllocSpec::kNoThrowAllocate)) {
|
| 711 |
+
return allocate(n);
|
| 712 |
+
}
|
| 713 |
+
|
| 714 |
+
void deallocate(pointer ptr, size_type) noexcept {
|
| 715 |
+
ReadState();
|
| 716 |
+
::operator delete(static_cast<void*>(ptr));
|
| 717 |
+
}
|
| 718 |
+
|
| 719 |
+
template <typename U, typename... Args>
|
| 720 |
+
void construct(U* ptr, Args&&... args) noexcept(
|
| 721 |
+
IsSpecified(AllocSpec::kNoThrowAllocate)) {
|
| 722 |
+
ReadStateAndMaybeThrow(ABSL_PRETTY_FUNCTION);
|
| 723 |
+
::new (static_cast<void*>(ptr)) U(std::forward<Args>(args)...);
|
| 724 |
+
}
|
| 725 |
+
|
| 726 |
+
template <typename U>
|
| 727 |
+
void destroy(U* p) noexcept {
|
| 728 |
+
ReadState();
|
| 729 |
+
p->~U();
|
| 730 |
+
}
|
| 731 |
+
|
| 732 |
+
size_type max_size() const noexcept {
|
| 733 |
+
return (std::numeric_limits<difference_type>::max)() / sizeof(value_type);
|
| 734 |
+
}
|
| 735 |
+
|
| 736 |
+
ThrowingAllocator select_on_container_copy_construction() noexcept(
|
| 737 |
+
IsSpecified(AllocSpec::kNoThrowAllocate)) {
|
| 738 |
+
ReadStateAndMaybeThrow(ABSL_PRETTY_FUNCTION);
|
| 739 |
+
return *this;
|
| 740 |
+
}
|
| 741 |
+
|
| 742 |
+
template <typename U>
|
| 743 |
+
bool operator==(const ThrowingAllocator<U, Spec>& other) const noexcept {
|
| 744 |
+
return dummy_ == other.dummy_;
|
| 745 |
+
}
|
| 746 |
+
|
| 747 |
+
template <typename U>
|
| 748 |
+
bool operator!=(const ThrowingAllocator<U, Spec>& other) const noexcept {
|
| 749 |
+
return dummy_ != other.dummy_;
|
| 750 |
+
}
|
| 751 |
+
|
| 752 |
+
template <typename, AllocSpec>
|
| 753 |
+
friend class ThrowingAllocator;
|
| 754 |
+
|
| 755 |
+
private:
|
| 756 |
+
static std::string GetInstanceString(int dummy) {
|
| 757 |
+
return absl::StrCat("ThrowingAllocator<",
|
| 758 |
+
exceptions_internal::GetSpecString(Spec), ">(", dummy,
|
| 759 |
+
")");
|
| 760 |
+
}
|
| 761 |
+
|
| 762 |
+
const std::shared_ptr<const int>& State() const { return dummy_; }
|
| 763 |
+
std::shared_ptr<const int>& State() { return dummy_; }
|
| 764 |
+
|
| 765 |
+
void ReadState() {
|
| 766 |
+
// we know that this will never be true, but the compiler doesn't, so this
|
| 767 |
+
// should safely force a read of the value.
|
| 768 |
+
if (*dummy_ < 0) std::abort();
|
| 769 |
+
}
|
| 770 |
+
|
| 771 |
+
void ReadStateAndMaybeThrow(absl::string_view msg) const {
|
| 772 |
+
if (!IsSpecified(AllocSpec::kNoThrowAllocate)) {
|
| 773 |
+
exceptions_internal::MaybeThrow(
|
| 774 |
+
absl::Substitute("Allocator id $0 threw from $1", *dummy_, msg));
|
| 775 |
+
}
|
| 776 |
+
}
|
| 777 |
+
|
| 778 |
+
static int next_id_;
|
| 779 |
+
std::shared_ptr<const int> dummy_;
|
| 780 |
+
};
|
| 781 |
+
|
| 782 |
+
template <typename T, AllocSpec Spec>
|
| 783 |
+
int ThrowingAllocator<T, Spec>::next_id_ = 0;
|
| 784 |
+
|
| 785 |
+
// Tests for resource leaks by attempting to construct a T using args repeatedly
|
| 786 |
+
// until successful, using the countdown method. Side effects can then be
|
| 787 |
+
// tested for resource leaks.
|
| 788 |
+
template <typename T, typename... Args>
|
| 789 |
+
void TestThrowingCtor(Args&&... args) {
|
| 790 |
+
struct Cleanup {
|
| 791 |
+
~Cleanup() { exceptions_internal::UnsetCountdown(); }
|
| 792 |
+
} c;
|
| 793 |
+
for (int count = 0;; ++count) {
|
| 794 |
+
exceptions_internal::ConstructorTracker ct(count);
|
| 795 |
+
exceptions_internal::SetCountdown(count);
|
| 796 |
+
try {
|
| 797 |
+
T temp(std::forward<Args>(args)...);
|
| 798 |
+
static_cast<void>(temp);
|
| 799 |
+
break;
|
| 800 |
+
} catch (const exceptions_internal::TestException&) {
|
| 801 |
+
}
|
| 802 |
+
}
|
| 803 |
+
}
|
| 804 |
+
|
| 805 |
+
// Tests the nothrow guarantee of the provided nullary operation. If the an
|
| 806 |
+
// exception is thrown, the result will be AssertionFailure(). Otherwise, it
|
| 807 |
+
// will be AssertionSuccess().
|
| 808 |
+
template <typename Operation>
|
| 809 |
+
testing::AssertionResult TestNothrowOp(const Operation& operation) {
|
| 810 |
+
struct Cleanup {
|
| 811 |
+
Cleanup() { exceptions_internal::SetCountdown(); }
|
| 812 |
+
~Cleanup() { exceptions_internal::UnsetCountdown(); }
|
| 813 |
+
} c;
|
| 814 |
+
try {
|
| 815 |
+
operation();
|
| 816 |
+
return testing::AssertionSuccess();
|
| 817 |
+
} catch (const exceptions_internal::TestException&) {
|
| 818 |
+
return testing::AssertionFailure()
|
| 819 |
+
<< "TestException thrown during call to operation() when nothrow "
|
| 820 |
+
"guarantee was expected.";
|
| 821 |
+
} catch (...) {
|
| 822 |
+
return testing::AssertionFailure()
|
| 823 |
+
<< "Unknown exception thrown during call to operation() when "
|
| 824 |
+
"nothrow guarantee was expected.";
|
| 825 |
+
}
|
| 826 |
+
}
|
| 827 |
+
|
| 828 |
+
namespace exceptions_internal {
|
| 829 |
+
|
| 830 |
+
// Dummy struct for ExceptionSafetyTestBuilder<> partial state.
|
| 831 |
+
struct UninitializedT {};
|
| 832 |
+
|
| 833 |
+
template <typename T>
|
| 834 |
+
class DefaultFactory {
|
| 835 |
+
public:
|
| 836 |
+
explicit DefaultFactory(const T& t) : t_(t) {}
|
| 837 |
+
std::unique_ptr<T> operator()() const { return absl::make_unique<T>(t_); }
|
| 838 |
+
|
| 839 |
+
private:
|
| 840 |
+
T t_;
|
| 841 |
+
};
|
| 842 |
+
|
| 843 |
+
template <size_t LazyContractsCount, typename LazyFactory,
|
| 844 |
+
typename LazyOperation>
|
| 845 |
+
using EnableIfTestable = typename absl::enable_if_t<
|
| 846 |
+
LazyContractsCount != 0 &&
|
| 847 |
+
!std::is_same<LazyFactory, UninitializedT>::value &&
|
| 848 |
+
!std::is_same<LazyOperation, UninitializedT>::value>;
|
| 849 |
+
|
| 850 |
+
template <typename Factory = UninitializedT,
|
| 851 |
+
typename Operation = UninitializedT, typename... Contracts>
|
| 852 |
+
class ExceptionSafetyTestBuilder;
|
| 853 |
+
|
| 854 |
+
} // namespace exceptions_internal
|
| 855 |
+
|
| 856 |
+
/*
|
| 857 |
+
* Constructs an empty ExceptionSafetyTestBuilder. All
|
| 858 |
+
* ExceptionSafetyTestBuilder objects are immutable and all With[thing] mutation
|
| 859 |
+
* methods return new instances of ExceptionSafetyTestBuilder.
|
| 860 |
+
*
|
| 861 |
+
* In order to test a T for exception safety, a factory for that T, a testable
|
| 862 |
+
* operation, and at least one contract callback returning an assertion
|
| 863 |
+
* result must be applied using the respective methods.
|
| 864 |
+
*/
|
| 865 |
+
exceptions_internal::ExceptionSafetyTestBuilder<> MakeExceptionSafetyTester();
|
| 866 |
+
|
| 867 |
+
namespace exceptions_internal {
|
| 868 |
+
template <typename T>
|
| 869 |
+
struct IsUniquePtr : std::false_type {};
|
| 870 |
+
|
| 871 |
+
template <typename T, typename D>
|
| 872 |
+
struct IsUniquePtr<std::unique_ptr<T, D>> : std::true_type {};
|
| 873 |
+
|
| 874 |
+
template <typename Factory>
|
| 875 |
+
struct FactoryPtrTypeHelper {
|
| 876 |
+
using type = decltype(std::declval<const Factory&>()());
|
| 877 |
+
|
| 878 |
+
static_assert(IsUniquePtr<type>::value, "Factories must return a unique_ptr");
|
| 879 |
+
};
|
| 880 |
+
|
| 881 |
+
template <typename Factory>
|
| 882 |
+
using FactoryPtrType = typename FactoryPtrTypeHelper<Factory>::type;
|
| 883 |
+
|
| 884 |
+
template <typename Factory>
|
| 885 |
+
using FactoryElementType = typename FactoryPtrType<Factory>::element_type;
|
| 886 |
+
|
| 887 |
+
template <typename T>
|
| 888 |
+
class ExceptionSafetyTest {
|
| 889 |
+
using Factory = std::function<std::unique_ptr<T>()>;
|
| 890 |
+
using Operation = std::function<void(T*)>;
|
| 891 |
+
using Contract = std::function<AssertionResult(T*)>;
|
| 892 |
+
|
| 893 |
+
public:
|
| 894 |
+
template <typename... Contracts>
|
| 895 |
+
explicit ExceptionSafetyTest(const Factory& f, const Operation& op,
|
| 896 |
+
const Contracts&... contracts)
|
| 897 |
+
: factory_(f), operation_(op), contracts_{WrapContract(contracts)...} {}
|
| 898 |
+
|
| 899 |
+
AssertionResult Test() const {
|
| 900 |
+
for (int count = 0;; ++count) {
|
| 901 |
+
exceptions_internal::ConstructorTracker ct(count);
|
| 902 |
+
|
| 903 |
+
for (const auto& contract : contracts_) {
|
| 904 |
+
auto t_ptr = factory_();
|
| 905 |
+
try {
|
| 906 |
+
SetCountdown(count);
|
| 907 |
+
operation_(t_ptr.get());
|
| 908 |
+
// Unset for the case that the operation throws no exceptions, which
|
| 909 |
+
// would leave the countdown set and break the *next* exception safety
|
| 910 |
+
// test after this one.
|
| 911 |
+
UnsetCountdown();
|
| 912 |
+
return AssertionSuccess();
|
| 913 |
+
} catch (const exceptions_internal::TestException& e) {
|
| 914 |
+
if (!contract(t_ptr.get())) {
|
| 915 |
+
return AssertionFailure() << e.what() << " failed contract check";
|
| 916 |
+
}
|
| 917 |
+
}
|
| 918 |
+
}
|
| 919 |
+
}
|
| 920 |
+
}
|
| 921 |
+
|
| 922 |
+
private:
|
| 923 |
+
template <typename ContractFn>
|
| 924 |
+
Contract WrapContract(const ContractFn& contract) {
|
| 925 |
+
return [contract](T* t_ptr) { return AssertionResult(contract(t_ptr)); };
|
| 926 |
+
}
|
| 927 |
+
|
| 928 |
+
Contract WrapContract(StrongGuaranteeTagType) {
|
| 929 |
+
return [this](T* t_ptr) { return AssertionResult(*factory_() == *t_ptr); };
|
| 930 |
+
}
|
| 931 |
+
|
| 932 |
+
Factory factory_;
|
| 933 |
+
Operation operation_;
|
| 934 |
+
std::vector<Contract> contracts_;
|
| 935 |
+
};
|
| 936 |
+
|
| 937 |
+
/*
|
| 938 |
+
* Builds a tester object that tests if performing a operation on a T follows
|
| 939 |
+
* exception safety guarantees. Verification is done via contract assertion
|
| 940 |
+
* callbacks applied to T instances post-throw.
|
| 941 |
+
*
|
| 942 |
+
* Template parameters for ExceptionSafetyTestBuilder:
|
| 943 |
+
*
|
| 944 |
+
* - Factory: The factory object (passed in via tester.WithFactory(...) or
|
| 945 |
+
* tester.WithInitialValue(...)) must be invocable with the signature
|
| 946 |
+
* `std::unique_ptr<T> operator()() const` where T is the type being tested.
|
| 947 |
+
* It is used for reliably creating identical T instances to test on.
|
| 948 |
+
*
|
| 949 |
+
* - Operation: The operation object (passed in via tester.WithOperation(...)
|
| 950 |
+
* or tester.Test(...)) must be invocable with the signature
|
| 951 |
+
* `void operator()(T*) const` where T is the type being tested. It is used
|
| 952 |
+
* for performing steps on a T instance that may throw and that need to be
|
| 953 |
+
* checked for exception safety. Each call to the operation will receive a
|
| 954 |
+
* fresh T instance so it's free to modify and destroy the T instances as it
|
| 955 |
+
* pleases.
|
| 956 |
+
*
|
| 957 |
+
* - Contracts...: The contract assertion callback objects (passed in via
|
| 958 |
+
* tester.WithContracts(...)) must be invocable with the signature
|
| 959 |
+
* `testing::AssertionResult operator()(T*) const` where T is the type being
|
| 960 |
+
* tested. Contract assertion callbacks are provided T instances post-throw.
|
| 961 |
+
* They must return testing::AssertionSuccess when the type contracts of the
|
| 962 |
+
* provided T instance hold. If the type contracts of the T instance do not
|
| 963 |
+
* hold, they must return testing::AssertionFailure. Execution order of
|
| 964 |
+
* Contracts... is unspecified. They will each individually get a fresh T
|
| 965 |
+
* instance so they are free to modify and destroy the T instances as they
|
| 966 |
+
* please.
|
| 967 |
+
*/
|
| 968 |
+
template <typename Factory, typename Operation, typename... Contracts>
|
| 969 |
+
class ExceptionSafetyTestBuilder {
|
| 970 |
+
public:
|
| 971 |
+
/*
|
| 972 |
+
* Returns a new ExceptionSafetyTestBuilder with an included T factory based
|
| 973 |
+
* on the provided T instance. The existing factory will not be included in
|
| 974 |
+
* the newly created tester instance. The created factory returns a new T
|
| 975 |
+
* instance by copy-constructing the provided const T& t.
|
| 976 |
+
*
|
| 977 |
+
* Preconditions for tester.WithInitialValue(const T& t):
|
| 978 |
+
*
|
| 979 |
+
* - The const T& t object must be copy-constructible where T is the type
|
| 980 |
+
* being tested. For non-copy-constructible objects, use the method
|
| 981 |
+
* tester.WithFactory(...).
|
| 982 |
+
*/
|
| 983 |
+
template <typename T>
|
| 984 |
+
ExceptionSafetyTestBuilder<DefaultFactory<T>, Operation, Contracts...>
|
| 985 |
+
WithInitialValue(const T& t) const {
|
| 986 |
+
return WithFactory(DefaultFactory<T>(t));
|
| 987 |
+
}
|
| 988 |
+
|
| 989 |
+
/*
|
| 990 |
+
* Returns a new ExceptionSafetyTestBuilder with the provided T factory
|
| 991 |
+
* included. The existing factory will not be included in the newly-created
|
| 992 |
+
* tester instance. This method is intended for use with types lacking a copy
|
| 993 |
+
* constructor. Types that can be copy-constructed should instead use the
|
| 994 |
+
* method tester.WithInitialValue(...).
|
| 995 |
+
*/
|
| 996 |
+
template <typename NewFactory>
|
| 997 |
+
ExceptionSafetyTestBuilder<absl::decay_t<NewFactory>, Operation, Contracts...>
|
| 998 |
+
WithFactory(const NewFactory& new_factory) const {
|
| 999 |
+
return {new_factory, operation_, contracts_};
|
| 1000 |
+
}
|
| 1001 |
+
|
| 1002 |
+
/*
|
| 1003 |
+
* Returns a new ExceptionSafetyTestBuilder with the provided testable
|
| 1004 |
+
* operation included. The existing operation will not be included in the
|
| 1005 |
+
* newly created tester.
|
| 1006 |
+
*/
|
| 1007 |
+
template <typename NewOperation>
|
| 1008 |
+
ExceptionSafetyTestBuilder<Factory, absl::decay_t<NewOperation>, Contracts...>
|
| 1009 |
+
WithOperation(const NewOperation& new_operation) const {
|
| 1010 |
+
return {factory_, new_operation, contracts_};
|
| 1011 |
+
}
|
| 1012 |
+
|
| 1013 |
+
/*
|
| 1014 |
+
* Returns a new ExceptionSafetyTestBuilder with the provided MoreContracts...
|
| 1015 |
+
* combined with the Contracts... that were already included in the instance
|
| 1016 |
+
* on which the method was called. Contracts... cannot be removed or replaced
|
| 1017 |
+
* once added to an ExceptionSafetyTestBuilder instance. A fresh object must
|
| 1018 |
+
* be created in order to get an empty Contracts... list.
|
| 1019 |
+
*
|
| 1020 |
+
* In addition to passing in custom contract assertion callbacks, this method
|
| 1021 |
+
* accepts `testing::strong_guarantee` as an argument which checks T instances
|
| 1022 |
+
* post-throw against freshly created T instances via operator== to verify
|
| 1023 |
+
* that any state changes made during the execution of the operation were
|
| 1024 |
+
* properly rolled back.
|
| 1025 |
+
*/
|
| 1026 |
+
template <typename... MoreContracts>
|
| 1027 |
+
ExceptionSafetyTestBuilder<Factory, Operation, Contracts...,
|
| 1028 |
+
absl::decay_t<MoreContracts>...>
|
| 1029 |
+
WithContracts(const MoreContracts&... more_contracts) const {
|
| 1030 |
+
return {
|
| 1031 |
+
factory_, operation_,
|
| 1032 |
+
std::tuple_cat(contracts_, std::tuple<absl::decay_t<MoreContracts>...>(
|
| 1033 |
+
more_contracts...))};
|
| 1034 |
+
}
|
| 1035 |
+
|
| 1036 |
+
/*
|
| 1037 |
+
* Returns a testing::AssertionResult that is the reduced result of the
|
| 1038 |
+
* exception safety algorithm. The algorithm short circuits and returns
|
| 1039 |
+
* AssertionFailure after the first contract callback returns an
|
| 1040 |
+
* AssertionFailure. Otherwise, if all contract callbacks return an
|
| 1041 |
+
* AssertionSuccess, the reduced result is AssertionSuccess.
|
| 1042 |
+
*
|
| 1043 |
+
* The passed-in testable operation will not be saved in a new tester instance
|
| 1044 |
+
* nor will it modify/replace the existing tester instance. This is useful
|
| 1045 |
+
* when each operation being tested is unique and does not need to be reused.
|
| 1046 |
+
*
|
| 1047 |
+
* Preconditions for tester.Test(const NewOperation& new_operation):
|
| 1048 |
+
*
|
| 1049 |
+
* - May only be called after at least one contract assertion callback and a
|
| 1050 |
+
* factory or initial value have been provided.
|
| 1051 |
+
*/
|
| 1052 |
+
template <
|
| 1053 |
+
typename NewOperation,
|
| 1054 |
+
typename = EnableIfTestable<sizeof...(Contracts), Factory, NewOperation>>
|
| 1055 |
+
testing::AssertionResult Test(const NewOperation& new_operation) const {
|
| 1056 |
+
return TestImpl(new_operation, absl::index_sequence_for<Contracts...>());
|
| 1057 |
+
}
|
| 1058 |
+
|
| 1059 |
+
/*
|
| 1060 |
+
* Returns a testing::AssertionResult that is the reduced result of the
|
| 1061 |
+
* exception safety algorithm. The algorithm short circuits and returns
|
| 1062 |
+
* AssertionFailure after the first contract callback returns an
|
| 1063 |
+
* AssertionFailure. Otherwise, if all contract callbacks return an
|
| 1064 |
+
* AssertionSuccess, the reduced result is AssertionSuccess.
|
| 1065 |
+
*
|
| 1066 |
+
* Preconditions for tester.Test():
|
| 1067 |
+
*
|
| 1068 |
+
* - May only be called after at least one contract assertion callback, a
|
| 1069 |
+
* factory or initial value and a testable operation have been provided.
|
| 1070 |
+
*/
|
| 1071 |
+
template <
|
| 1072 |
+
typename LazyOperation = Operation,
|
| 1073 |
+
typename = EnableIfTestable<sizeof...(Contracts), Factory, LazyOperation>>
|
| 1074 |
+
testing::AssertionResult Test() const {
|
| 1075 |
+
return Test(operation_);
|
| 1076 |
+
}
|
| 1077 |
+
|
| 1078 |
+
private:
|
| 1079 |
+
template <typename, typename, typename...>
|
| 1080 |
+
friend class ExceptionSafetyTestBuilder;
|
| 1081 |
+
|
| 1082 |
+
friend ExceptionSafetyTestBuilder<> testing::MakeExceptionSafetyTester();
|
| 1083 |
+
|
| 1084 |
+
ExceptionSafetyTestBuilder() {}
|
| 1085 |
+
|
| 1086 |
+
ExceptionSafetyTestBuilder(const Factory& f, const Operation& o,
|
| 1087 |
+
const std::tuple<Contracts...>& i)
|
| 1088 |
+
: factory_(f), operation_(o), contracts_(i) {}
|
| 1089 |
+
|
| 1090 |
+
template <typename SelectedOperation, size_t... Indices>
|
| 1091 |
+
testing::AssertionResult TestImpl(SelectedOperation selected_operation,
|
| 1092 |
+
absl::index_sequence<Indices...>) const {
|
| 1093 |
+
return ExceptionSafetyTest<FactoryElementType<Factory>>(
|
| 1094 |
+
factory_, selected_operation, std::get<Indices>(contracts_)...)
|
| 1095 |
+
.Test();
|
| 1096 |
+
}
|
| 1097 |
+
|
| 1098 |
+
Factory factory_;
|
| 1099 |
+
Operation operation_;
|
| 1100 |
+
std::tuple<Contracts...> contracts_;
|
| 1101 |
+
};
|
| 1102 |
+
|
| 1103 |
+
} // namespace exceptions_internal
|
| 1104 |
+
|
| 1105 |
+
} // namespace testing
|
| 1106 |
+
|
| 1107 |
+
#endif // ABSL_HAVE_EXCEPTIONS
|
| 1108 |
+
|
| 1109 |
+
#endif // ABSL_BASE_INTERNAL_EXCEPTION_SAFETY_TESTING_H_
|
include/absl/base/internal/exception_testing.h
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
// Copyright 2017 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 |
+
// Testing utilities for ABSL types which throw exceptions.
|
| 16 |
+
|
| 17 |
+
#ifndef ABSL_BASE_INTERNAL_EXCEPTION_TESTING_H_
|
| 18 |
+
#define ABSL_BASE_INTERNAL_EXCEPTION_TESTING_H_
|
| 19 |
+
|
| 20 |
+
#include "gtest/gtest.h"
|
| 21 |
+
#include "absl/base/config.h"
|
| 22 |
+
|
| 23 |
+
// ABSL_BASE_INTERNAL_EXPECT_FAIL tests either for a specified thrown exception
|
| 24 |
+
// if exceptions are enabled, or for death with a specified text in the error
|
| 25 |
+
// message
|
| 26 |
+
#ifdef ABSL_HAVE_EXCEPTIONS
|
| 27 |
+
|
| 28 |
+
#define ABSL_BASE_INTERNAL_EXPECT_FAIL(expr, exception_t, text) \
|
| 29 |
+
EXPECT_THROW(expr, exception_t)
|
| 30 |
+
|
| 31 |
+
#elif defined(__ANDROID__)
|
| 32 |
+
// Android asserts do not log anywhere that gtest can currently inspect.
|
| 33 |
+
// So we expect exit, but cannot match the message.
|
| 34 |
+
#define ABSL_BASE_INTERNAL_EXPECT_FAIL(expr, exception_t, text) \
|
| 35 |
+
EXPECT_DEATH(expr, ".*")
|
| 36 |
+
#else
|
| 37 |
+
#define ABSL_BASE_INTERNAL_EXPECT_FAIL(expr, exception_t, text) \
|
| 38 |
+
EXPECT_DEATH_IF_SUPPORTED(expr, text)
|
| 39 |
+
|
| 40 |
+
#endif
|
| 41 |
+
|
| 42 |
+
#endif // ABSL_BASE_INTERNAL_EXCEPTION_TESTING_H_
|
include/absl/base/internal/fast_type_id.h
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
//
|
| 2 |
+
// Copyright 2020 The Abseil Authors.
|
| 3 |
+
//
|
| 4 |
+
// Licensed under the Apache License, Version 2.0 (the "License");
|
| 5 |
+
// you may not use this file except in compliance with the License.
|
| 6 |
+
// You may obtain a copy of the License at
|
| 7 |
+
//
|
| 8 |
+
// https://www.apache.org/licenses/LICENSE-2.0
|
| 9 |
+
//
|
| 10 |
+
// Unless required by applicable law or agreed to in writing, software
|
| 11 |
+
// distributed under the License is distributed on an "AS IS" BASIS,
|
| 12 |
+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
| 13 |
+
// See the License for the specific language governing permissions and
|
| 14 |
+
// limitations under the License.
|
| 15 |
+
//
|
| 16 |
+
|
| 17 |
+
#ifndef ABSL_BASE_INTERNAL_FAST_TYPE_ID_H_
|
| 18 |
+
#define ABSL_BASE_INTERNAL_FAST_TYPE_ID_H_
|
| 19 |
+
|
| 20 |
+
#include "absl/base/config.h"
|
| 21 |
+
|
| 22 |
+
namespace absl {
|
| 23 |
+
ABSL_NAMESPACE_BEGIN
|
| 24 |
+
namespace base_internal {
|
| 25 |
+
|
| 26 |
+
template <typename Type>
|
| 27 |
+
struct FastTypeTag {
|
| 28 |
+
constexpr static char dummy_var = 0;
|
| 29 |
+
};
|
| 30 |
+
|
| 31 |
+
#ifdef ABSL_INTERNAL_NEED_REDUNDANT_CONSTEXPR_DECL
|
| 32 |
+
template <typename Type>
|
| 33 |
+
constexpr char FastTypeTag<Type>::dummy_var;
|
| 34 |
+
#endif
|
| 35 |
+
|
| 36 |
+
// FastTypeId<Type>() evaluates at compile/link-time to a unique pointer for the
|
| 37 |
+
// passed-in type. These are meant to be good match for keys into maps or
|
| 38 |
+
// straight up comparisons.
|
| 39 |
+
using FastTypeIdType = const void*;
|
| 40 |
+
|
| 41 |
+
template <typename Type>
|
| 42 |
+
constexpr inline FastTypeIdType FastTypeId() {
|
| 43 |
+
return &FastTypeTag<Type>::dummy_var;
|
| 44 |
+
}
|
| 45 |
+
|
| 46 |
+
} // namespace base_internal
|
| 47 |
+
ABSL_NAMESPACE_END
|
| 48 |
+
} // namespace absl
|
| 49 |
+
|
| 50 |
+
#endif // ABSL_BASE_INTERNAL_FAST_TYPE_ID_H_
|
include/absl/base/internal/hide_ptr.h
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
// Copyright 2018 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_BASE_INTERNAL_HIDE_PTR_H_
|
| 16 |
+
#define ABSL_BASE_INTERNAL_HIDE_PTR_H_
|
| 17 |
+
|
| 18 |
+
#include <cstdint>
|
| 19 |
+
|
| 20 |
+
#include "absl/base/config.h"
|
| 21 |
+
|
| 22 |
+
namespace absl {
|
| 23 |
+
ABSL_NAMESPACE_BEGIN
|
| 24 |
+
namespace base_internal {
|
| 25 |
+
|
| 26 |
+
// Arbitrary value with high bits set. Xor'ing with it is unlikely
|
| 27 |
+
// to map one valid pointer to another valid pointer.
|
| 28 |
+
constexpr uintptr_t HideMask() {
|
| 29 |
+
return (uintptr_t{0xF03A5F7BU} << (sizeof(uintptr_t) - 4) * 8) | 0xF03A5F7BU;
|
| 30 |
+
}
|
| 31 |
+
|
| 32 |
+
// Hide a pointer from the leak checker. For internal use only.
|
| 33 |
+
// Differs from absl::IgnoreLeak(ptr) in that absl::IgnoreLeak(ptr) causes ptr
|
| 34 |
+
// and all objects reachable from ptr to be ignored by the leak checker.
|
| 35 |
+
template <class T>
|
| 36 |
+
inline uintptr_t HidePtr(T* ptr) {
|
| 37 |
+
return reinterpret_cast<uintptr_t>(ptr) ^ HideMask();
|
| 38 |
+
}
|
| 39 |
+
|
| 40 |
+
// Return a pointer that has been hidden from the leak checker.
|
| 41 |
+
// For internal use only.
|
| 42 |
+
template <class T>
|
| 43 |
+
inline T* UnhidePtr(uintptr_t hidden) {
|
| 44 |
+
return reinterpret_cast<T*>(hidden ^ HideMask());
|
| 45 |
+
}
|
| 46 |
+
|
| 47 |
+
} // namespace base_internal
|
| 48 |
+
ABSL_NAMESPACE_END
|
| 49 |
+
} // namespace absl
|
| 50 |
+
|
| 51 |
+
#endif // ABSL_BASE_INTERNAL_HIDE_PTR_H_
|
include/absl/base/internal/identity.h
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
// Copyright 2017 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 |
+
|
| 16 |
+
#ifndef ABSL_BASE_INTERNAL_IDENTITY_H_
|
| 17 |
+
#define ABSL_BASE_INTERNAL_IDENTITY_H_
|
| 18 |
+
|
| 19 |
+
#include "absl/base/config.h"
|
| 20 |
+
|
| 21 |
+
namespace absl {
|
| 22 |
+
ABSL_NAMESPACE_BEGIN
|
| 23 |
+
namespace internal {
|
| 24 |
+
|
| 25 |
+
// This is a back-fill of C++20's `std::type_identity`.
|
| 26 |
+
template <typename T>
|
| 27 |
+
struct type_identity {
|
| 28 |
+
typedef T type;
|
| 29 |
+
};
|
| 30 |
+
|
| 31 |
+
// This is a back-fill of C++20's `std::type_identity_t`.
|
| 32 |
+
template <typename T>
|
| 33 |
+
using type_identity_t = typename type_identity<T>::type;
|
| 34 |
+
|
| 35 |
+
} // namespace internal
|
| 36 |
+
ABSL_NAMESPACE_END
|
| 37 |
+
} // namespace absl
|
| 38 |
+
|
| 39 |
+
#endif // ABSL_BASE_INTERNAL_IDENTITY_H_
|
include/absl/base/internal/inline_variable.h
ADDED
|
@@ -0,0 +1,108 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
// Copyright 2017 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_BASE_INTERNAL_INLINE_VARIABLE_H_
|
| 16 |
+
#define ABSL_BASE_INTERNAL_INLINE_VARIABLE_H_
|
| 17 |
+
|
| 18 |
+
#include <type_traits>
|
| 19 |
+
|
| 20 |
+
#include "absl/base/internal/identity.h"
|
| 21 |
+
|
| 22 |
+
// File:
|
| 23 |
+
// This file define a macro that allows the creation of or emulation of C++17
|
| 24 |
+
// inline variables based on whether or not the feature is supported.
|
| 25 |
+
|
| 26 |
+
////////////////////////////////////////////////////////////////////////////////
|
| 27 |
+
// Macro: ABSL_INTERNAL_INLINE_CONSTEXPR(type, name, init)
|
| 28 |
+
//
|
| 29 |
+
// Description:
|
| 30 |
+
// Expands to the equivalent of an inline constexpr instance of the specified
|
| 31 |
+
// `type` and `name`, initialized to the value `init`. If the compiler being
|
| 32 |
+
// used is detected as supporting actual inline variables as a language
|
| 33 |
+
// feature, then the macro expands to an actual inline variable definition.
|
| 34 |
+
//
|
| 35 |
+
// Requires:
|
| 36 |
+
// `type` is a type that is usable in an extern variable declaration.
|
| 37 |
+
//
|
| 38 |
+
// Requires: `name` is a valid identifier
|
| 39 |
+
//
|
| 40 |
+
// Requires:
|
| 41 |
+
// `init` is an expression that can be used in the following definition:
|
| 42 |
+
// constexpr type name = init;
|
| 43 |
+
//
|
| 44 |
+
// Usage:
|
| 45 |
+
//
|
| 46 |
+
// // Equivalent to: `inline constexpr size_t variant_npos = -1;`
|
| 47 |
+
// ABSL_INTERNAL_INLINE_CONSTEXPR(size_t, variant_npos, -1);
|
| 48 |
+
//
|
| 49 |
+
// Differences in implementation:
|
| 50 |
+
// For a direct, language-level inline variable, decltype(name) will be the
|
| 51 |
+
// type that was specified along with const qualification, whereas for
|
| 52 |
+
// emulated inline variables, decltype(name) may be different (in practice
|
| 53 |
+
// it will likely be a reference type).
|
| 54 |
+
////////////////////////////////////////////////////////////////////////////////
|
| 55 |
+
|
| 56 |
+
#ifdef __cpp_inline_variables
|
| 57 |
+
|
| 58 |
+
// Clang's -Wmissing-variable-declarations option erroneously warned that
|
| 59 |
+
// inline constexpr objects need to be pre-declared. This has now been fixed,
|
| 60 |
+
// but we will need to support this workaround for people building with older
|
| 61 |
+
// versions of clang.
|
| 62 |
+
//
|
| 63 |
+
// Bug: https://bugs.llvm.org/show_bug.cgi?id=35862
|
| 64 |
+
//
|
| 65 |
+
// Note:
|
| 66 |
+
// type_identity_t is used here so that the const and name are in the
|
| 67 |
+
// appropriate place for pointer types, reference types, function pointer
|
| 68 |
+
// types, etc..
|
| 69 |
+
#if defined(__clang__)
|
| 70 |
+
#define ABSL_INTERNAL_EXTERN_DECL(type, name) \
|
| 71 |
+
extern const ::absl::internal::type_identity_t<type> name;
|
| 72 |
+
#else // Otherwise, just define the macro to do nothing.
|
| 73 |
+
#define ABSL_INTERNAL_EXTERN_DECL(type, name)
|
| 74 |
+
#endif // defined(__clang__)
|
| 75 |
+
|
| 76 |
+
// See above comment at top of file for details.
|
| 77 |
+
#define ABSL_INTERNAL_INLINE_CONSTEXPR(type, name, init) \
|
| 78 |
+
ABSL_INTERNAL_EXTERN_DECL(type, name) \
|
| 79 |
+
inline constexpr ::absl::internal::type_identity_t<type> name = init
|
| 80 |
+
|
| 81 |
+
#else
|
| 82 |
+
|
| 83 |
+
// See above comment at top of file for details.
|
| 84 |
+
//
|
| 85 |
+
// Note:
|
| 86 |
+
// type_identity_t is used here so that the const and name are in the
|
| 87 |
+
// appropriate place for pointer types, reference types, function pointer
|
| 88 |
+
// types, etc..
|
| 89 |
+
#define ABSL_INTERNAL_INLINE_CONSTEXPR(var_type, name, init) \
|
| 90 |
+
template <class /*AbslInternalDummy*/ = void> \
|
| 91 |
+
struct AbslInternalInlineVariableHolder##name { \
|
| 92 |
+
static constexpr ::absl::internal::type_identity_t<var_type> kInstance = \
|
| 93 |
+
init; \
|
| 94 |
+
}; \
|
| 95 |
+
\
|
| 96 |
+
template <class AbslInternalDummy> \
|
| 97 |
+
constexpr ::absl::internal::type_identity_t<var_type> \
|
| 98 |
+
AbslInternalInlineVariableHolder##name<AbslInternalDummy>::kInstance; \
|
| 99 |
+
\
|
| 100 |
+
static constexpr const ::absl::internal::type_identity_t<var_type>& \
|
| 101 |
+
name = /* NOLINT */ \
|
| 102 |
+
AbslInternalInlineVariableHolder##name<>::kInstance; \
|
| 103 |
+
static_assert(sizeof(void (*)(decltype(name))) != 0, \
|
| 104 |
+
"Silence unused variable warnings.")
|
| 105 |
+
|
| 106 |
+
#endif // __cpp_inline_variables
|
| 107 |
+
|
| 108 |
+
#endif // ABSL_BASE_INTERNAL_INLINE_VARIABLE_H_
|
include/absl/base/internal/inline_variable_testing.h
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
// Copyright 2017 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_BASE_INTERNAL_INLINE_VARIABLE_TESTING_H_
|
| 16 |
+
#define ABSL_BASE_INTERNAL_INLINE_VARIABLE_TESTING_H_
|
| 17 |
+
|
| 18 |
+
#include "absl/base/internal/inline_variable.h"
|
| 19 |
+
|
| 20 |
+
namespace absl {
|
| 21 |
+
ABSL_NAMESPACE_BEGIN
|
| 22 |
+
namespace inline_variable_testing_internal {
|
| 23 |
+
|
| 24 |
+
struct Foo {
|
| 25 |
+
int value = 5;
|
| 26 |
+
};
|
| 27 |
+
|
| 28 |
+
ABSL_INTERNAL_INLINE_CONSTEXPR(Foo, inline_variable_foo, {});
|
| 29 |
+
ABSL_INTERNAL_INLINE_CONSTEXPR(Foo, other_inline_variable_foo, {});
|
| 30 |
+
|
| 31 |
+
ABSL_INTERNAL_INLINE_CONSTEXPR(int, inline_variable_int, 5);
|
| 32 |
+
ABSL_INTERNAL_INLINE_CONSTEXPR(int, other_inline_variable_int, 5);
|
| 33 |
+
|
| 34 |
+
ABSL_INTERNAL_INLINE_CONSTEXPR(void(*)(), inline_variable_fun_ptr, nullptr);
|
| 35 |
+
|
| 36 |
+
const Foo& get_foo_a();
|
| 37 |
+
const Foo& get_foo_b();
|
| 38 |
+
|
| 39 |
+
const int& get_int_a();
|
| 40 |
+
const int& get_int_b();
|
| 41 |
+
|
| 42 |
+
} // namespace inline_variable_testing_internal
|
| 43 |
+
ABSL_NAMESPACE_END
|
| 44 |
+
} // namespace absl
|
| 45 |
+
|
| 46 |
+
#endif // ABSL_BASE_INTERNAL_INLINE_VARIABLE_TESTING_H_
|
include/absl/base/internal/invoke.h
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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
// Copyright 2017 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 |
+
// absl::base_internal::invoke(f, args...) is an implementation of
|
| 16 |
+
// INVOKE(f, args...) from section [func.require] of the C++ standard.
|
| 17 |
+
// When compiled as C++17 and later versions, it is implemented as an alias of
|
| 18 |
+
// std::invoke.
|
| 19 |
+
//
|
| 20 |
+
// [func.require]
|
| 21 |
+
// Define INVOKE (f, t1, t2, ..., tN) as follows:
|
| 22 |
+
// 1. (t1.*f)(t2, ..., tN) when f is a pointer to a member function of a class T
|
| 23 |
+
// and t1 is an object of type T or a reference to an object of type T or a
|
| 24 |
+
// reference to an object of a type derived from T;
|
| 25 |
+
// 2. ((*t1).*f)(t2, ..., tN) when f is a pointer to a member function of a
|
| 26 |
+
// class T and t1 is not one of the types described in the previous item;
|
| 27 |
+
// 3. t1.*f when N == 1 and f is a pointer to member data of a class T and t1 is
|
| 28 |
+
// an object of type T or a reference to an object of type T or a reference
|
| 29 |
+
// to an object of a type derived from T;
|
| 30 |
+
// 4. (*t1).*f when N == 1 and f is a pointer to member data of a class T and t1
|
| 31 |
+
// is not one of the types described in the previous item;
|
| 32 |
+
// 5. f(t1, t2, ..., tN) in all other cases.
|
| 33 |
+
//
|
| 34 |
+
// The implementation is SFINAE-friendly: substitution failure within invoke()
|
| 35 |
+
// isn't an error.
|
| 36 |
+
|
| 37 |
+
#ifndef ABSL_BASE_INTERNAL_INVOKE_H_
|
| 38 |
+
#define ABSL_BASE_INTERNAL_INVOKE_H_
|
| 39 |
+
|
| 40 |
+
#include "absl/base/config.h"
|
| 41 |
+
|
| 42 |
+
#if ABSL_INTERNAL_CPLUSPLUS_LANG >= 201703L
|
| 43 |
+
|
| 44 |
+
#include <functional>
|
| 45 |
+
|
| 46 |
+
namespace absl {
|
| 47 |
+
ABSL_NAMESPACE_BEGIN
|
| 48 |
+
namespace base_internal {
|
| 49 |
+
|
| 50 |
+
using std::invoke;
|
| 51 |
+
using std::invoke_result_t;
|
| 52 |
+
using std::is_invocable_r;
|
| 53 |
+
|
| 54 |
+
} // namespace base_internal
|
| 55 |
+
ABSL_NAMESPACE_END
|
| 56 |
+
} // namespace absl
|
| 57 |
+
|
| 58 |
+
#else // ABSL_INTERNAL_CPLUSPLUS_LANG >= 201703L
|
| 59 |
+
|
| 60 |
+
#include <algorithm>
|
| 61 |
+
#include <type_traits>
|
| 62 |
+
#include <utility>
|
| 63 |
+
|
| 64 |
+
#include "absl/meta/type_traits.h"
|
| 65 |
+
|
| 66 |
+
// The following code is internal implementation detail. See the comment at the
|
| 67 |
+
// top of this file for the API documentation.
|
| 68 |
+
|
| 69 |
+
namespace absl {
|
| 70 |
+
ABSL_NAMESPACE_BEGIN
|
| 71 |
+
namespace base_internal {
|
| 72 |
+
|
| 73 |
+
// The five classes below each implement one of the clauses from the definition
|
| 74 |
+
// of INVOKE. The inner class template Accept<F, Args...> checks whether the
|
| 75 |
+
// clause is applicable; static function template Invoke(f, args...) does the
|
| 76 |
+
// invocation.
|
| 77 |
+
//
|
| 78 |
+
// By separating the clause selection logic from invocation we make sure that
|
| 79 |
+
// Invoke() does exactly what the standard says.
|
| 80 |
+
|
| 81 |
+
template <typename Derived>
|
| 82 |
+
struct StrippedAccept {
|
| 83 |
+
template <typename... Args>
|
| 84 |
+
struct Accept : Derived::template AcceptImpl<typename std::remove_cv<
|
| 85 |
+
typename std::remove_reference<Args>::type>::type...> {};
|
| 86 |
+
};
|
| 87 |
+
|
| 88 |
+
// (t1.*f)(t2, ..., tN) when f is a pointer to a member function of a class T
|
| 89 |
+
// and t1 is an object of type T or a reference to an object of type T or a
|
| 90 |
+
// reference to an object of a type derived from T.
|
| 91 |
+
struct MemFunAndRef : StrippedAccept<MemFunAndRef> {
|
| 92 |
+
template <typename... Args>
|
| 93 |
+
struct AcceptImpl : std::false_type {};
|
| 94 |
+
|
| 95 |
+
template <typename MemFunType, typename C, typename Obj, typename... Args>
|
| 96 |
+
struct AcceptImpl<MemFunType C::*, Obj, Args...>
|
| 97 |
+
: std::integral_constant<bool, std::is_base_of<C, Obj>::value &&
|
| 98 |
+
absl::is_function<MemFunType>::value> {
|
| 99 |
+
};
|
| 100 |
+
|
| 101 |
+
template <typename MemFun, typename Obj, typename... Args>
|
| 102 |
+
static decltype((std::declval<Obj>().*
|
| 103 |
+
std::declval<MemFun>())(std::declval<Args>()...))
|
| 104 |
+
Invoke(MemFun&& mem_fun, Obj&& obj, Args&&... args) {
|
| 105 |
+
// Ignore bogus GCC warnings on this line.
|
| 106 |
+
// See https://gcc.gnu.org/bugzilla/show_bug.cgi?id=101436 for similar example.
|
| 107 |
+
#if ABSL_INTERNAL_HAVE_MIN_GNUC_VERSION(11, 0)
|
| 108 |
+
#pragma GCC diagnostic push
|
| 109 |
+
#pragma GCC diagnostic ignored "-Warray-bounds"
|
| 110 |
+
#pragma GCC diagnostic ignored "-Wmaybe-uninitialized"
|
| 111 |
+
#endif
|
| 112 |
+
return (std::forward<Obj>(obj).*
|
| 113 |
+
std::forward<MemFun>(mem_fun))(std::forward<Args>(args)...);
|
| 114 |
+
#if ABSL_INTERNAL_HAVE_MIN_GNUC_VERSION(11, 0)
|
| 115 |
+
#pragma GCC diagnostic pop
|
| 116 |
+
#endif
|
| 117 |
+
}
|
| 118 |
+
};
|
| 119 |
+
|
| 120 |
+
// ((*t1).*f)(t2, ..., tN) when f is a pointer to a member function of a
|
| 121 |
+
// class T and t1 is not one of the types described in the previous item.
|
| 122 |
+
struct MemFunAndPtr : StrippedAccept<MemFunAndPtr> {
|
| 123 |
+
template <typename... Args>
|
| 124 |
+
struct AcceptImpl : std::false_type {};
|
| 125 |
+
|
| 126 |
+
template <typename MemFunType, typename C, typename Ptr, typename... Args>
|
| 127 |
+
struct AcceptImpl<MemFunType C::*, Ptr, Args...>
|
| 128 |
+
: std::integral_constant<bool, !std::is_base_of<C, Ptr>::value &&
|
| 129 |
+
absl::is_function<MemFunType>::value> {
|
| 130 |
+
};
|
| 131 |
+
|
| 132 |
+
template <typename MemFun, typename Ptr, typename... Args>
|
| 133 |
+
static decltype(((*std::declval<Ptr>()).*
|
| 134 |
+
std::declval<MemFun>())(std::declval<Args>()...))
|
| 135 |
+
Invoke(MemFun&& mem_fun, Ptr&& ptr, Args&&... args) {
|
| 136 |
+
return ((*std::forward<Ptr>(ptr)).*
|
| 137 |
+
std::forward<MemFun>(mem_fun))(std::forward<Args>(args)...);
|
| 138 |
+
}
|
| 139 |
+
};
|
| 140 |
+
|
| 141 |
+
// t1.*f when N == 1 and f is a pointer to member data of a class T and t1 is
|
| 142 |
+
// an object of type T or a reference to an object of type T or a reference
|
| 143 |
+
// to an object of a type derived from T.
|
| 144 |
+
struct DataMemAndRef : StrippedAccept<DataMemAndRef> {
|
| 145 |
+
template <typename... Args>
|
| 146 |
+
struct AcceptImpl : std::false_type {};
|
| 147 |
+
|
| 148 |
+
template <typename R, typename C, typename Obj>
|
| 149 |
+
struct AcceptImpl<R C::*, Obj>
|
| 150 |
+
: std::integral_constant<bool, std::is_base_of<C, Obj>::value &&
|
| 151 |
+
!absl::is_function<R>::value> {};
|
| 152 |
+
|
| 153 |
+
template <typename DataMem, typename Ref>
|
| 154 |
+
static decltype(std::declval<Ref>().*std::declval<DataMem>()) Invoke(
|
| 155 |
+
DataMem&& data_mem, Ref&& ref) {
|
| 156 |
+
return std::forward<Ref>(ref).*std::forward<DataMem>(data_mem);
|
| 157 |
+
}
|
| 158 |
+
};
|
| 159 |
+
|
| 160 |
+
// (*t1).*f when N == 1 and f is a pointer to member data of a class T and t1
|
| 161 |
+
// is not one of the types described in the previous item.
|
| 162 |
+
struct DataMemAndPtr : StrippedAccept<DataMemAndPtr> {
|
| 163 |
+
template <typename... Args>
|
| 164 |
+
struct AcceptImpl : std::false_type {};
|
| 165 |
+
|
| 166 |
+
template <typename R, typename C, typename Ptr>
|
| 167 |
+
struct AcceptImpl<R C::*, Ptr>
|
| 168 |
+
: std::integral_constant<bool, !std::is_base_of<C, Ptr>::value &&
|
| 169 |
+
!absl::is_function<R>::value> {};
|
| 170 |
+
|
| 171 |
+
template <typename DataMem, typename Ptr>
|
| 172 |
+
static decltype((*std::declval<Ptr>()).*std::declval<DataMem>()) Invoke(
|
| 173 |
+
DataMem&& data_mem, Ptr&& ptr) {
|
| 174 |
+
return (*std::forward<Ptr>(ptr)).*std::forward<DataMem>(data_mem);
|
| 175 |
+
}
|
| 176 |
+
};
|
| 177 |
+
|
| 178 |
+
// f(t1, t2, ..., tN) in all other cases.
|
| 179 |
+
struct Callable {
|
| 180 |
+
// Callable doesn't have Accept because it's the last clause that gets picked
|
| 181 |
+
// when none of the previous clauses are applicable.
|
| 182 |
+
template <typename F, typename... Args>
|
| 183 |
+
static decltype(std::declval<F>()(std::declval<Args>()...)) Invoke(
|
| 184 |
+
F&& f, Args&&... args) {
|
| 185 |
+
return std::forward<F>(f)(std::forward<Args>(args)...);
|
| 186 |
+
}
|
| 187 |
+
};
|
| 188 |
+
|
| 189 |
+
// Resolves to the first matching clause.
|
| 190 |
+
template <typename... Args>
|
| 191 |
+
struct Invoker {
|
| 192 |
+
typedef typename std::conditional<
|
| 193 |
+
MemFunAndRef::Accept<Args...>::value, MemFunAndRef,
|
| 194 |
+
typename std::conditional<
|
| 195 |
+
MemFunAndPtr::Accept<Args...>::value, MemFunAndPtr,
|
| 196 |
+
typename std::conditional<
|
| 197 |
+
DataMemAndRef::Accept<Args...>::value, DataMemAndRef,
|
| 198 |
+
typename std::conditional<DataMemAndPtr::Accept<Args...>::value,
|
| 199 |
+
DataMemAndPtr, Callable>::type>::type>::
|
| 200 |
+
type>::type type;
|
| 201 |
+
};
|
| 202 |
+
|
| 203 |
+
// The result type of Invoke<F, Args...>.
|
| 204 |
+
template <typename F, typename... Args>
|
| 205 |
+
using invoke_result_t = decltype(Invoker<F, Args...>::type::Invoke(
|
| 206 |
+
std::declval<F>(), std::declval<Args>()...));
|
| 207 |
+
|
| 208 |
+
// Invoke(f, args...) is an implementation of INVOKE(f, args...) from section
|
| 209 |
+
// [func.require] of the C++ standard.
|
| 210 |
+
template <typename F, typename... Args>
|
| 211 |
+
invoke_result_t<F, Args...> invoke(F&& f, Args&&... args) {
|
| 212 |
+
return Invoker<F, Args...>::type::Invoke(std::forward<F>(f),
|
| 213 |
+
std::forward<Args>(args)...);
|
| 214 |
+
}
|
| 215 |
+
|
| 216 |
+
template <typename AlwaysVoid, typename, typename, typename...>
|
| 217 |
+
struct IsInvocableRImpl : std::false_type {};
|
| 218 |
+
|
| 219 |
+
template <typename R, typename F, typename... Args>
|
| 220 |
+
struct IsInvocableRImpl<
|
| 221 |
+
absl::void_t<absl::base_internal::invoke_result_t<F, Args...> >, R, F,
|
| 222 |
+
Args...>
|
| 223 |
+
: std::integral_constant<
|
| 224 |
+
bool,
|
| 225 |
+
std::is_convertible<absl::base_internal::invoke_result_t<F, Args...>,
|
| 226 |
+
R>::value ||
|
| 227 |
+
std::is_void<R>::value> {};
|
| 228 |
+
|
| 229 |
+
// Type trait whose member `value` is true if invoking `F` with `Args` is valid,
|
| 230 |
+
// and either the return type is convertible to `R`, or `R` is void.
|
| 231 |
+
// C++11-compatible version of `std::is_invocable_r`.
|
| 232 |
+
template <typename R, typename F, typename... Args>
|
| 233 |
+
using is_invocable_r = IsInvocableRImpl<void, R, F, Args...>;
|
| 234 |
+
|
| 235 |
+
} // namespace base_internal
|
| 236 |
+
ABSL_NAMESPACE_END
|
| 237 |
+
} // namespace absl
|
| 238 |
+
|
| 239 |
+
#endif // ABSL_INTERNAL_CPLUSPLUS_LANG >= 201703L
|
| 240 |
+
|
| 241 |
+
#endif // ABSL_BASE_INTERNAL_INVOKE_H_
|
include/absl/base/internal/low_level_alloc.h
ADDED
|
@@ -0,0 +1,127 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
// Copyright 2017 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 |
+
|
| 16 |
+
#ifndef ABSL_BASE_INTERNAL_LOW_LEVEL_ALLOC_H_
|
| 17 |
+
#define ABSL_BASE_INTERNAL_LOW_LEVEL_ALLOC_H_
|
| 18 |
+
|
| 19 |
+
// A simple thread-safe memory allocator that does not depend on
|
| 20 |
+
// mutexes or thread-specific data. It is intended to be used
|
| 21 |
+
// sparingly, and only when malloc() would introduce an unwanted
|
| 22 |
+
// dependency, such as inside the heap-checker, or the Mutex
|
| 23 |
+
// implementation.
|
| 24 |
+
|
| 25 |
+
// IWYU pragma: private, include "base/low_level_alloc.h"
|
| 26 |
+
|
| 27 |
+
#include <sys/types.h>
|
| 28 |
+
|
| 29 |
+
#include <cstdint>
|
| 30 |
+
|
| 31 |
+
#include "absl/base/attributes.h"
|
| 32 |
+
#include "absl/base/config.h"
|
| 33 |
+
|
| 34 |
+
// LowLevelAlloc requires that the platform support low-level
|
| 35 |
+
// allocation of virtual memory. Platforms lacking this cannot use
|
| 36 |
+
// LowLevelAlloc.
|
| 37 |
+
#ifdef ABSL_LOW_LEVEL_ALLOC_MISSING
|
| 38 |
+
#error ABSL_LOW_LEVEL_ALLOC_MISSING cannot be directly set
|
| 39 |
+
#elif !defined(ABSL_HAVE_MMAP) && !defined(_WIN32)
|
| 40 |
+
#define ABSL_LOW_LEVEL_ALLOC_MISSING 1
|
| 41 |
+
#endif
|
| 42 |
+
|
| 43 |
+
// Using LowLevelAlloc with kAsyncSignalSafe isn't supported on Windows or
|
| 44 |
+
// asm.js / WebAssembly.
|
| 45 |
+
// See https://kripken.github.io/emscripten-site/docs/porting/pthreads.html
|
| 46 |
+
// for more information.
|
| 47 |
+
#ifdef ABSL_LOW_LEVEL_ALLOC_ASYNC_SIGNAL_SAFE_MISSING
|
| 48 |
+
#error ABSL_LOW_LEVEL_ALLOC_ASYNC_SIGNAL_SAFE_MISSING cannot be directly set
|
| 49 |
+
#elif defined(_WIN32) || defined(__asmjs__) || defined(__wasm__) || \
|
| 50 |
+
defined(__hexagon__)
|
| 51 |
+
#define ABSL_LOW_LEVEL_ALLOC_ASYNC_SIGNAL_SAFE_MISSING 1
|
| 52 |
+
#endif
|
| 53 |
+
|
| 54 |
+
#include <cstddef>
|
| 55 |
+
|
| 56 |
+
#include "absl/base/port.h"
|
| 57 |
+
|
| 58 |
+
namespace absl {
|
| 59 |
+
ABSL_NAMESPACE_BEGIN
|
| 60 |
+
namespace base_internal {
|
| 61 |
+
|
| 62 |
+
class LowLevelAlloc {
|
| 63 |
+
public:
|
| 64 |
+
struct Arena; // an arena from which memory may be allocated
|
| 65 |
+
|
| 66 |
+
// Returns a pointer to a block of at least "request" bytes
|
| 67 |
+
// that have been newly allocated from the specific arena.
|
| 68 |
+
// for Alloc() call the DefaultArena() is used.
|
| 69 |
+
// Returns 0 if passed request==0.
|
| 70 |
+
// Does not return 0 under other circumstances; it crashes if memory
|
| 71 |
+
// is not available.
|
| 72 |
+
static void *Alloc(size_t request) ABSL_ATTRIBUTE_SECTION(malloc_hook);
|
| 73 |
+
static void *AllocWithArena(size_t request, Arena *arena)
|
| 74 |
+
ABSL_ATTRIBUTE_SECTION(malloc_hook);
|
| 75 |
+
|
| 76 |
+
// Deallocates a region of memory that was previously allocated with
|
| 77 |
+
// Alloc(). Does nothing if passed 0. "s" must be either 0,
|
| 78 |
+
// or must have been returned from a call to Alloc() and not yet passed to
|
| 79 |
+
// Free() since that call to Alloc(). The space is returned to the arena
|
| 80 |
+
// from which it was allocated.
|
| 81 |
+
static void Free(void *s) ABSL_ATTRIBUTE_SECTION(malloc_hook);
|
| 82 |
+
|
| 83 |
+
// ABSL_ATTRIBUTE_SECTION(malloc_hook) for Alloc* and Free
|
| 84 |
+
// are to put all callers of MallocHook::Invoke* in this module
|
| 85 |
+
// into special section,
|
| 86 |
+
// so that MallocHook::GetCallerStackTrace can function accurately.
|
| 87 |
+
|
| 88 |
+
// Create a new arena.
|
| 89 |
+
// The root metadata for the new arena is allocated in the
|
| 90 |
+
// meta_data_arena; the DefaultArena() can be passed for meta_data_arena.
|
| 91 |
+
// These values may be ored into flags:
|
| 92 |
+
enum {
|
| 93 |
+
// Report calls to Alloc() and Free() via the MallocHook interface.
|
| 94 |
+
// Set in the DefaultArena.
|
| 95 |
+
kCallMallocHook = 0x0001,
|
| 96 |
+
|
| 97 |
+
#ifndef ABSL_LOW_LEVEL_ALLOC_ASYNC_SIGNAL_SAFE_MISSING
|
| 98 |
+
// Make calls to Alloc(), Free() be async-signal-safe. Not set in
|
| 99 |
+
// DefaultArena(). Not supported on all platforms.
|
| 100 |
+
kAsyncSignalSafe = 0x0002,
|
| 101 |
+
#endif
|
| 102 |
+
};
|
| 103 |
+
// Construct a new arena. The allocation of the underlying metadata honors
|
| 104 |
+
// the provided flags. For example, the call NewArena(kAsyncSignalSafe)
|
| 105 |
+
// is itself async-signal-safe, as well as generatating an arena that provides
|
| 106 |
+
// async-signal-safe Alloc/Free.
|
| 107 |
+
static Arena *NewArena(uint32_t flags);
|
| 108 |
+
|
| 109 |
+
// Destroys an arena allocated by NewArena and returns true,
|
| 110 |
+
// provided no allocated blocks remain in the arena.
|
| 111 |
+
// If allocated blocks remain in the arena, does nothing and
|
| 112 |
+
// returns false.
|
| 113 |
+
// It is illegal to attempt to destroy the DefaultArena().
|
| 114 |
+
static bool DeleteArena(Arena *arena);
|
| 115 |
+
|
| 116 |
+
// The default arena that always exists.
|
| 117 |
+
static Arena *DefaultArena();
|
| 118 |
+
|
| 119 |
+
private:
|
| 120 |
+
LowLevelAlloc(); // no instances
|
| 121 |
+
};
|
| 122 |
+
|
| 123 |
+
} // namespace base_internal
|
| 124 |
+
ABSL_NAMESPACE_END
|
| 125 |
+
} // namespace absl
|
| 126 |
+
|
| 127 |
+
#endif // ABSL_BASE_INTERNAL_LOW_LEVEL_ALLOC_H_
|
include/absl/base/internal/low_level_scheduling.h
ADDED
|
@@ -0,0 +1,134 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
// Copyright 2017 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 |
+
// Core interfaces and definitions used by by low-level interfaces such as
|
| 16 |
+
// SpinLock.
|
| 17 |
+
|
| 18 |
+
#ifndef ABSL_BASE_INTERNAL_LOW_LEVEL_SCHEDULING_H_
|
| 19 |
+
#define ABSL_BASE_INTERNAL_LOW_LEVEL_SCHEDULING_H_
|
| 20 |
+
|
| 21 |
+
#include "absl/base/internal/raw_logging.h"
|
| 22 |
+
#include "absl/base/internal/scheduling_mode.h"
|
| 23 |
+
#include "absl/base/macros.h"
|
| 24 |
+
|
| 25 |
+
// The following two declarations exist so SchedulingGuard may friend them with
|
| 26 |
+
// the appropriate language linkage. These callbacks allow libc internals, such
|
| 27 |
+
// as function level statics, to schedule cooperatively when locking.
|
| 28 |
+
extern "C" bool __google_disable_rescheduling(void);
|
| 29 |
+
extern "C" void __google_enable_rescheduling(bool disable_result);
|
| 30 |
+
|
| 31 |
+
namespace absl {
|
| 32 |
+
ABSL_NAMESPACE_BEGIN
|
| 33 |
+
class CondVar;
|
| 34 |
+
class Mutex;
|
| 35 |
+
|
| 36 |
+
namespace synchronization_internal {
|
| 37 |
+
int MutexDelay(int32_t c, int mode);
|
| 38 |
+
} // namespace synchronization_internal
|
| 39 |
+
|
| 40 |
+
namespace base_internal {
|
| 41 |
+
|
| 42 |
+
class SchedulingHelper; // To allow use of SchedulingGuard.
|
| 43 |
+
class SpinLock; // To allow use of SchedulingGuard.
|
| 44 |
+
|
| 45 |
+
// SchedulingGuard
|
| 46 |
+
// Provides guard semantics that may be used to disable cooperative rescheduling
|
| 47 |
+
// of the calling thread within specific program blocks. This is used to
|
| 48 |
+
// protect resources (e.g. low-level SpinLocks or Domain code) that cooperative
|
| 49 |
+
// scheduling depends on.
|
| 50 |
+
//
|
| 51 |
+
// Domain implementations capable of rescheduling in reaction to involuntary
|
| 52 |
+
// kernel thread actions (e.g blocking due to a pagefault or syscall) must
|
| 53 |
+
// guarantee that an annotated thread is not allowed to (cooperatively)
|
| 54 |
+
// reschedule until the annotated region is complete.
|
| 55 |
+
//
|
| 56 |
+
// It is an error to attempt to use a cooperatively scheduled resource (e.g.
|
| 57 |
+
// Mutex) within a rescheduling-disabled region.
|
| 58 |
+
//
|
| 59 |
+
// All methods are async-signal safe.
|
| 60 |
+
class SchedulingGuard {
|
| 61 |
+
public:
|
| 62 |
+
// Returns true iff the calling thread may be cooperatively rescheduled.
|
| 63 |
+
static bool ReschedulingIsAllowed();
|
| 64 |
+
SchedulingGuard(const SchedulingGuard&) = delete;
|
| 65 |
+
SchedulingGuard& operator=(const SchedulingGuard&) = delete;
|
| 66 |
+
|
| 67 |
+
private:
|
| 68 |
+
// Disable cooperative rescheduling of the calling thread. It may still
|
| 69 |
+
// initiate scheduling operations (e.g. wake-ups), however, it may not itself
|
| 70 |
+
// reschedule. Nestable. The returned result is opaque, clients should not
|
| 71 |
+
// attempt to interpret it.
|
| 72 |
+
// REQUIRES: Result must be passed to a pairing EnableScheduling().
|
| 73 |
+
static bool DisableRescheduling();
|
| 74 |
+
|
| 75 |
+
// Marks the end of a rescheduling disabled region, previously started by
|
| 76 |
+
// DisableRescheduling().
|
| 77 |
+
// REQUIRES: Pairs with innermost call (and result) of DisableRescheduling().
|
| 78 |
+
static void EnableRescheduling(bool disable_result);
|
| 79 |
+
|
| 80 |
+
// A scoped helper for {Disable, Enable}Rescheduling().
|
| 81 |
+
// REQUIRES: destructor must run in same thread as constructor.
|
| 82 |
+
struct ScopedDisable {
|
| 83 |
+
ScopedDisable() { disabled = SchedulingGuard::DisableRescheduling(); }
|
| 84 |
+
~ScopedDisable() { SchedulingGuard::EnableRescheduling(disabled); }
|
| 85 |
+
|
| 86 |
+
bool disabled;
|
| 87 |
+
};
|
| 88 |
+
|
| 89 |
+
// A scoped helper to enable rescheduling temporarily.
|
| 90 |
+
// REQUIRES: destructor must run in same thread as constructor.
|
| 91 |
+
class ScopedEnable {
|
| 92 |
+
public:
|
| 93 |
+
ScopedEnable();
|
| 94 |
+
~ScopedEnable();
|
| 95 |
+
|
| 96 |
+
private:
|
| 97 |
+
int scheduling_disabled_depth_;
|
| 98 |
+
};
|
| 99 |
+
|
| 100 |
+
// Access to SchedulingGuard is explicitly permitted.
|
| 101 |
+
friend class absl::CondVar;
|
| 102 |
+
friend class absl::Mutex;
|
| 103 |
+
friend class SchedulingHelper;
|
| 104 |
+
friend class SpinLock;
|
| 105 |
+
friend int absl::synchronization_internal::MutexDelay(int32_t c, int mode);
|
| 106 |
+
};
|
| 107 |
+
|
| 108 |
+
//------------------------------------------------------------------------------
|
| 109 |
+
// End of public interfaces.
|
| 110 |
+
//------------------------------------------------------------------------------
|
| 111 |
+
|
| 112 |
+
inline bool SchedulingGuard::ReschedulingIsAllowed() {
|
| 113 |
+
return false;
|
| 114 |
+
}
|
| 115 |
+
|
| 116 |
+
inline bool SchedulingGuard::DisableRescheduling() {
|
| 117 |
+
return false;
|
| 118 |
+
}
|
| 119 |
+
|
| 120 |
+
inline void SchedulingGuard::EnableRescheduling(bool /* disable_result */) {
|
| 121 |
+
return;
|
| 122 |
+
}
|
| 123 |
+
|
| 124 |
+
inline SchedulingGuard::ScopedEnable::ScopedEnable()
|
| 125 |
+
: scheduling_disabled_depth_(0) {}
|
| 126 |
+
inline SchedulingGuard::ScopedEnable::~ScopedEnable() {
|
| 127 |
+
ABSL_RAW_CHECK(scheduling_disabled_depth_ == 0, "disable unused warning");
|
| 128 |
+
}
|
| 129 |
+
|
| 130 |
+
} // namespace base_internal
|
| 131 |
+
ABSL_NAMESPACE_END
|
| 132 |
+
} // namespace absl
|
| 133 |
+
|
| 134 |
+
#endif // ABSL_BASE_INTERNAL_LOW_LEVEL_SCHEDULING_H_
|
include/absl/base/internal/nullability_impl.h
ADDED
|
@@ -0,0 +1,69 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
// Copyright 2023 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_BASE_INTERNAL_NULLABILITY_IMPL_H_
|
| 16 |
+
#define ABSL_BASE_INTERNAL_NULLABILITY_IMPL_H_
|
| 17 |
+
|
| 18 |
+
#include <memory>
|
| 19 |
+
#include <type_traits>
|
| 20 |
+
|
| 21 |
+
#include "absl/base/attributes.h"
|
| 22 |
+
#include "absl/base/config.h"
|
| 23 |
+
#include "absl/meta/type_traits.h"
|
| 24 |
+
|
| 25 |
+
namespace absl {
|
| 26 |
+
ABSL_NAMESPACE_BEGIN
|
| 27 |
+
namespace nullability_internal {
|
| 28 |
+
|
| 29 |
+
template <typename T>
|
| 30 |
+
using NullableImpl
|
| 31 |
+
#if ABSL_HAVE_CPP_ATTRIBUTE(clang::annotate)
|
| 32 |
+
[[clang::annotate("Nullable")]]
|
| 33 |
+
#endif
|
| 34 |
+
// Don't add the _Nullable attribute in Objective-C compiles. Many Objective-C
|
| 35 |
+
// projects enable the `-Wnullable-to-nonnull-conversion warning`, which is
|
| 36 |
+
// liable to produce false positives.
|
| 37 |
+
#if ABSL_HAVE_FEATURE(nullability_on_classes) && !defined(__OBJC__)
|
| 38 |
+
= T _Nullable;
|
| 39 |
+
#else
|
| 40 |
+
= T;
|
| 41 |
+
#endif
|
| 42 |
+
|
| 43 |
+
template <typename T>
|
| 44 |
+
using NonnullImpl
|
| 45 |
+
#if ABSL_HAVE_CPP_ATTRIBUTE(clang::annotate)
|
| 46 |
+
[[clang::annotate("Nonnull")]]
|
| 47 |
+
#endif
|
| 48 |
+
#if ABSL_HAVE_FEATURE(nullability_on_classes) && !defined(__OBJC__)
|
| 49 |
+
= T _Nonnull;
|
| 50 |
+
#else
|
| 51 |
+
= T;
|
| 52 |
+
#endif
|
| 53 |
+
|
| 54 |
+
template <typename T>
|
| 55 |
+
using NullabilityUnknownImpl
|
| 56 |
+
#if ABSL_HAVE_CPP_ATTRIBUTE(clang::annotate)
|
| 57 |
+
[[clang::annotate("Nullability_Unspecified")]]
|
| 58 |
+
#endif
|
| 59 |
+
#if ABSL_HAVE_FEATURE(nullability_on_classes) && !defined(__OBJC__)
|
| 60 |
+
= T _Null_unspecified;
|
| 61 |
+
#else
|
| 62 |
+
= T;
|
| 63 |
+
#endif
|
| 64 |
+
|
| 65 |
+
} // namespace nullability_internal
|
| 66 |
+
ABSL_NAMESPACE_END
|
| 67 |
+
} // namespace absl
|
| 68 |
+
|
| 69 |
+
#endif // ABSL_BASE_INTERNAL_NULLABILITY_IMPL_H_
|
include/absl/base/internal/per_thread_tls.h
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
// Copyright 2017 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_BASE_INTERNAL_PER_THREAD_TLS_H_
|
| 16 |
+
#define ABSL_BASE_INTERNAL_PER_THREAD_TLS_H_
|
| 17 |
+
|
| 18 |
+
// This header defines two macros:
|
| 19 |
+
//
|
| 20 |
+
// If the platform supports thread-local storage:
|
| 21 |
+
//
|
| 22 |
+
// * ABSL_PER_THREAD_TLS_KEYWORD is the C keyword needed to declare a
|
| 23 |
+
// thread-local variable
|
| 24 |
+
// * ABSL_PER_THREAD_TLS is 1
|
| 25 |
+
//
|
| 26 |
+
// Otherwise:
|
| 27 |
+
//
|
| 28 |
+
// * ABSL_PER_THREAD_TLS_KEYWORD is empty
|
| 29 |
+
// * ABSL_PER_THREAD_TLS is 0
|
| 30 |
+
//
|
| 31 |
+
// Microsoft C supports thread-local storage.
|
| 32 |
+
// GCC supports it if the appropriate version of glibc is available,
|
| 33 |
+
// which the programmer can indicate by defining ABSL_HAVE_TLS
|
| 34 |
+
|
| 35 |
+
#include "absl/base/port.h" // For ABSL_HAVE_TLS
|
| 36 |
+
|
| 37 |
+
#if defined(ABSL_PER_THREAD_TLS)
|
| 38 |
+
#error ABSL_PER_THREAD_TLS cannot be directly set
|
| 39 |
+
#elif defined(ABSL_PER_THREAD_TLS_KEYWORD)
|
| 40 |
+
#error ABSL_PER_THREAD_TLS_KEYWORD cannot be directly set
|
| 41 |
+
#elif defined(ABSL_HAVE_TLS)
|
| 42 |
+
#define ABSL_PER_THREAD_TLS_KEYWORD __thread
|
| 43 |
+
#define ABSL_PER_THREAD_TLS 1
|
| 44 |
+
#elif defined(_MSC_VER)
|
| 45 |
+
#define ABSL_PER_THREAD_TLS_KEYWORD __declspec(thread)
|
| 46 |
+
#define ABSL_PER_THREAD_TLS 1
|
| 47 |
+
#else
|
| 48 |
+
#define ABSL_PER_THREAD_TLS_KEYWORD
|
| 49 |
+
#define ABSL_PER_THREAD_TLS 0
|
| 50 |
+
#endif
|
| 51 |
+
|
| 52 |
+
#endif // ABSL_BASE_INTERNAL_PER_THREAD_TLS_H_
|
include/absl/base/internal/poison.h
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
// Copyright 2024 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_BASE_INTERNAL_POISON_H_
|
| 16 |
+
#define ABSL_BASE_INTERNAL_POISON_H_
|
| 17 |
+
|
| 18 |
+
#include <cstdint>
|
| 19 |
+
|
| 20 |
+
#include "absl/base/config.h"
|
| 21 |
+
|
| 22 |
+
namespace absl {
|
| 23 |
+
ABSL_NAMESPACE_BEGIN
|
| 24 |
+
namespace base_internal {
|
| 25 |
+
|
| 26 |
+
inline void* GetBadPointerInternal() {
|
| 27 |
+
// A likely bad pointer. Pointers are required to have high bits that are all
|
| 28 |
+
// zero or all one for certain 64-bit CPUs. This pointer value will hopefully
|
| 29 |
+
// cause a crash on dereference and also be clearly recognizable as invalid.
|
| 30 |
+
constexpr uint64_t kBadPtr = 0xBAD0BAD0BAD0BAD0;
|
| 31 |
+
auto ret = reinterpret_cast<void*>(static_cast<uintptr_t>(kBadPtr));
|
| 32 |
+
#ifndef _MSC_VER // MSVC doesn't support inline asm with `volatile`.
|
| 33 |
+
// Try to prevent the compiler from optimizing out the undefined behavior.
|
| 34 |
+
asm volatile("" : : "r"(ret) :); // NOLINT
|
| 35 |
+
#endif
|
| 36 |
+
return ret;
|
| 37 |
+
}
|
| 38 |
+
|
| 39 |
+
void* InitializePoisonedPointerInternal();
|
| 40 |
+
|
| 41 |
+
inline void* get_poisoned_pointer() {
|
| 42 |
+
#if defined(NDEBUG) && !defined(ABSL_HAVE_ADDRESS_SANITIZER) && \
|
| 43 |
+
!defined(ABSL_HAVE_MEMORY_SANITIZER)
|
| 44 |
+
// In optimized non-sanitized builds, avoid the function-local static because
|
| 45 |
+
// of the codegen and runtime cost.
|
| 46 |
+
return GetBadPointerInternal();
|
| 47 |
+
#else
|
| 48 |
+
// Non-optimized builds may use more robust implementation. Note that we can't
|
| 49 |
+
// use a static global because Chromium doesn't allow non-constinit globals.
|
| 50 |
+
static void* ptr = InitializePoisonedPointerInternal();
|
| 51 |
+
return ptr;
|
| 52 |
+
#endif
|
| 53 |
+
}
|
| 54 |
+
|
| 55 |
+
} // namespace base_internal
|
| 56 |
+
ABSL_NAMESPACE_END
|
| 57 |
+
} // namespace absl
|
| 58 |
+
|
| 59 |
+
#endif // ABSL_BASE_INTERNAL_POISON_H_
|
include/absl/base/internal/pretty_function.h
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
// Copyright 2017 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_BASE_INTERNAL_PRETTY_FUNCTION_H_
|
| 16 |
+
#define ABSL_BASE_INTERNAL_PRETTY_FUNCTION_H_
|
| 17 |
+
|
| 18 |
+
// ABSL_PRETTY_FUNCTION
|
| 19 |
+
//
|
| 20 |
+
// In C++11, __func__ gives the undecorated name of the current function. That
|
| 21 |
+
// is, "main", not "int main()". Various compilers give extra macros to get the
|
| 22 |
+
// decorated function name, including return type and arguments, to
|
| 23 |
+
// differentiate between overload sets. ABSL_PRETTY_FUNCTION is a portable
|
| 24 |
+
// version of these macros which forwards to the correct macro on each compiler.
|
| 25 |
+
#if defined(_MSC_VER)
|
| 26 |
+
#define ABSL_PRETTY_FUNCTION __FUNCSIG__
|
| 27 |
+
#elif defined(__GNUC__)
|
| 28 |
+
#define ABSL_PRETTY_FUNCTION __PRETTY_FUNCTION__
|
| 29 |
+
#else
|
| 30 |
+
#error "Unsupported compiler"
|
| 31 |
+
#endif
|
| 32 |
+
|
| 33 |
+
#endif // ABSL_BASE_INTERNAL_PRETTY_FUNCTION_H_
|
include/absl/base/internal/raw_logging.h
ADDED
|
@@ -0,0 +1,217 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
// Copyright 2017 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 |
+
// Thread-safe logging routines that do not allocate any memory or
|
| 16 |
+
// acquire any locks, and can therefore be used by low-level memory
|
| 17 |
+
// allocation, synchronization, and signal-handling code.
|
| 18 |
+
|
| 19 |
+
#ifndef ABSL_BASE_INTERNAL_RAW_LOGGING_H_
|
| 20 |
+
#define ABSL_BASE_INTERNAL_RAW_LOGGING_H_
|
| 21 |
+
|
| 22 |
+
#include <string>
|
| 23 |
+
|
| 24 |
+
#include "absl/base/attributes.h"
|
| 25 |
+
#include "absl/base/config.h"
|
| 26 |
+
#include "absl/base/internal/atomic_hook.h"
|
| 27 |
+
#include "absl/base/log_severity.h"
|
| 28 |
+
#include "absl/base/macros.h"
|
| 29 |
+
#include "absl/base/optimization.h"
|
| 30 |
+
#include "absl/base/port.h"
|
| 31 |
+
|
| 32 |
+
// This is similar to LOG(severity) << format..., but
|
| 33 |
+
// * it is to be used ONLY by low-level modules that can't use normal LOG()
|
| 34 |
+
// * it is designed to be a low-level logger that does not allocate any
|
| 35 |
+
// memory and does not need any locks, hence:
|
| 36 |
+
// * it logs straight and ONLY to STDERR w/o buffering
|
| 37 |
+
// * it uses an explicit printf-format and arguments list
|
| 38 |
+
// * it will silently chop off really long message strings
|
| 39 |
+
// Usage example:
|
| 40 |
+
// ABSL_RAW_LOG(ERROR, "Failed foo with %i: %s", status, error);
|
| 41 |
+
// This will print an almost standard log line like this to stderr only:
|
| 42 |
+
// E0821 211317 file.cc:123] RAW: Failed foo with 22: bad_file
|
| 43 |
+
|
| 44 |
+
#define ABSL_RAW_LOG(severity, ...) \
|
| 45 |
+
do { \
|
| 46 |
+
constexpr const char* absl_raw_log_internal_basename = \
|
| 47 |
+
::absl::raw_log_internal::Basename(__FILE__, sizeof(__FILE__) - 1); \
|
| 48 |
+
::absl::raw_log_internal::RawLog(ABSL_RAW_LOG_INTERNAL_##severity, \
|
| 49 |
+
absl_raw_log_internal_basename, __LINE__, \
|
| 50 |
+
__VA_ARGS__); \
|
| 51 |
+
ABSL_RAW_LOG_INTERNAL_MAYBE_UNREACHABLE_##severity; \
|
| 52 |
+
} while (0)
|
| 53 |
+
|
| 54 |
+
// Similar to CHECK(condition) << message, but for low-level modules:
|
| 55 |
+
// we use only ABSL_RAW_LOG that does not allocate memory.
|
| 56 |
+
// We do not want to provide args list here to encourage this usage:
|
| 57 |
+
// if (!cond) ABSL_RAW_LOG(FATAL, "foo ...", hard_to_compute_args);
|
| 58 |
+
// so that the args are not computed when not needed.
|
| 59 |
+
#define ABSL_RAW_CHECK(condition, message) \
|
| 60 |
+
do { \
|
| 61 |
+
if (ABSL_PREDICT_FALSE(!(condition))) { \
|
| 62 |
+
ABSL_RAW_LOG(FATAL, "Check %s failed: %s", #condition, message); \
|
| 63 |
+
} \
|
| 64 |
+
} while (0)
|
| 65 |
+
|
| 66 |
+
// ABSL_INTERNAL_LOG and ABSL_INTERNAL_CHECK work like the RAW variants above,
|
| 67 |
+
// except that if the richer log library is linked into the binary, we dispatch
|
| 68 |
+
// to that instead. This is potentially useful for internal logging and
|
| 69 |
+
// assertions, where we are using RAW_LOG neither for its async-signal-safety
|
| 70 |
+
// nor for its non-allocating nature, but rather because raw logging has very
|
| 71 |
+
// few other dependencies.
|
| 72 |
+
//
|
| 73 |
+
// The API is a subset of the above: each macro only takes two arguments. Use
|
| 74 |
+
// StrCat if you need to build a richer message.
|
| 75 |
+
#define ABSL_INTERNAL_LOG(severity, message) \
|
| 76 |
+
do { \
|
| 77 |
+
constexpr const char* absl_raw_log_internal_filename = __FILE__; \
|
| 78 |
+
::absl::raw_log_internal::internal_log_function( \
|
| 79 |
+
ABSL_RAW_LOG_INTERNAL_##severity, absl_raw_log_internal_filename, \
|
| 80 |
+
__LINE__, message); \
|
| 81 |
+
ABSL_RAW_LOG_INTERNAL_MAYBE_UNREACHABLE_##severity; \
|
| 82 |
+
} while (0)
|
| 83 |
+
|
| 84 |
+
#define ABSL_INTERNAL_CHECK(condition, message) \
|
| 85 |
+
do { \
|
| 86 |
+
if (ABSL_PREDICT_FALSE(!(condition))) { \
|
| 87 |
+
std::string death_message = "Check " #condition " failed: "; \
|
| 88 |
+
death_message += std::string(message); \
|
| 89 |
+
ABSL_INTERNAL_LOG(FATAL, death_message); \
|
| 90 |
+
} \
|
| 91 |
+
} while (0)
|
| 92 |
+
|
| 93 |
+
#ifndef NDEBUG
|
| 94 |
+
|
| 95 |
+
#define ABSL_RAW_DLOG(severity, ...) ABSL_RAW_LOG(severity, __VA_ARGS__)
|
| 96 |
+
#define ABSL_RAW_DCHECK(condition, message) ABSL_RAW_CHECK(condition, message)
|
| 97 |
+
|
| 98 |
+
#else // NDEBUG
|
| 99 |
+
|
| 100 |
+
#define ABSL_RAW_DLOG(severity, ...) \
|
| 101 |
+
while (false) ABSL_RAW_LOG(severity, __VA_ARGS__)
|
| 102 |
+
#define ABSL_RAW_DCHECK(condition, message) \
|
| 103 |
+
while (false) ABSL_RAW_CHECK(condition, message)
|
| 104 |
+
|
| 105 |
+
#endif // NDEBUG
|
| 106 |
+
|
| 107 |
+
#define ABSL_RAW_LOG_INTERNAL_INFO ::absl::LogSeverity::kInfo
|
| 108 |
+
#define ABSL_RAW_LOG_INTERNAL_WARNING ::absl::LogSeverity::kWarning
|
| 109 |
+
#define ABSL_RAW_LOG_INTERNAL_ERROR ::absl::LogSeverity::kError
|
| 110 |
+
#define ABSL_RAW_LOG_INTERNAL_FATAL ::absl::LogSeverity::kFatal
|
| 111 |
+
#define ABSL_RAW_LOG_INTERNAL_DFATAL ::absl::kLogDebugFatal
|
| 112 |
+
#define ABSL_RAW_LOG_INTERNAL_LEVEL(severity) \
|
| 113 |
+
::absl::NormalizeLogSeverity(severity)
|
| 114 |
+
|
| 115 |
+
#define ABSL_RAW_LOG_INTERNAL_MAYBE_UNREACHABLE_INFO
|
| 116 |
+
#define ABSL_RAW_LOG_INTERNAL_MAYBE_UNREACHABLE_WARNING
|
| 117 |
+
#define ABSL_RAW_LOG_INTERNAL_MAYBE_UNREACHABLE_ERROR
|
| 118 |
+
#define ABSL_RAW_LOG_INTERNAL_MAYBE_UNREACHABLE_FATAL ABSL_UNREACHABLE()
|
| 119 |
+
#define ABSL_RAW_LOG_INTERNAL_MAYBE_UNREACHABLE_DFATAL
|
| 120 |
+
#define ABSL_RAW_LOG_INTERNAL_MAYBE_UNREACHABLE_LEVEL(severity)
|
| 121 |
+
|
| 122 |
+
namespace absl {
|
| 123 |
+
ABSL_NAMESPACE_BEGIN
|
| 124 |
+
namespace raw_log_internal {
|
| 125 |
+
|
| 126 |
+
// Helper function to implement ABSL_RAW_LOG
|
| 127 |
+
// Logs format... at "severity" level, reporting it
|
| 128 |
+
// as called from file:line.
|
| 129 |
+
// This does not allocate memory or acquire locks.
|
| 130 |
+
void RawLog(absl::LogSeverity severity, const char* file, int line,
|
| 131 |
+
const char* format, ...) ABSL_PRINTF_ATTRIBUTE(4, 5);
|
| 132 |
+
|
| 133 |
+
// Writes the provided buffer directly to stderr, in a signal-safe, low-level
|
| 134 |
+
// manner. Preserves errno.
|
| 135 |
+
void AsyncSignalSafeWriteError(const char* s, size_t len);
|
| 136 |
+
|
| 137 |
+
// compile-time function to get the "base" filename, that is, the part of
|
| 138 |
+
// a filename after the last "/" or "\" path separator. The search starts at
|
| 139 |
+
// the end of the string; the second parameter is the length of the string.
|
| 140 |
+
constexpr const char* Basename(const char* fname, int offset) {
|
| 141 |
+
return offset == 0 || fname[offset - 1] == '/' || fname[offset - 1] == '\\'
|
| 142 |
+
? fname + offset
|
| 143 |
+
: Basename(fname, offset - 1);
|
| 144 |
+
}
|
| 145 |
+
|
| 146 |
+
// For testing only.
|
| 147 |
+
// Returns true if raw logging is fully supported. When it is not
|
| 148 |
+
// fully supported, no messages will be emitted, but a log at FATAL
|
| 149 |
+
// severity will cause an abort.
|
| 150 |
+
//
|
| 151 |
+
// TODO(gfalcon): Come up with a better name for this method.
|
| 152 |
+
bool RawLoggingFullySupported();
|
| 153 |
+
|
| 154 |
+
// Function type for a raw_log customization hook for suppressing messages
|
| 155 |
+
// by severity, and for writing custom prefixes on non-suppressed messages.
|
| 156 |
+
//
|
| 157 |
+
// The installed hook is called for every raw log invocation. The message will
|
| 158 |
+
// be logged to stderr only if the hook returns true. FATAL errors will cause
|
| 159 |
+
// the process to abort, even if writing to stderr is suppressed. The hook is
|
| 160 |
+
// also provided with an output buffer, where it can write a custom log message
|
| 161 |
+
// prefix.
|
| 162 |
+
//
|
| 163 |
+
// The raw_log system does not allocate memory or grab locks. User-provided
|
| 164 |
+
// hooks must avoid these operations, and must not throw exceptions.
|
| 165 |
+
//
|
| 166 |
+
// 'severity' is the severity level of the message being written.
|
| 167 |
+
// 'file' and 'line' are the file and line number where the ABSL_RAW_LOG macro
|
| 168 |
+
// was located.
|
| 169 |
+
// 'buf' and 'buf_size' are pointers to the buffer and buffer size. If the
|
| 170 |
+
// hook writes a prefix, it must increment *buf and decrement *buf_size
|
| 171 |
+
// accordingly.
|
| 172 |
+
using LogFilterAndPrefixHook = bool (*)(absl::LogSeverity severity,
|
| 173 |
+
const char* file, int line, char** buf,
|
| 174 |
+
int* buf_size);
|
| 175 |
+
|
| 176 |
+
// Function type for a raw_log customization hook called to abort a process
|
| 177 |
+
// when a FATAL message is logged. If the provided AbortHook() returns, the
|
| 178 |
+
// logging system will call abort().
|
| 179 |
+
//
|
| 180 |
+
// 'file' and 'line' are the file and line number where the ABSL_RAW_LOG macro
|
| 181 |
+
// was located.
|
| 182 |
+
// The NUL-terminated logged message lives in the buffer between 'buf_start'
|
| 183 |
+
// and 'buf_end'. 'prefix_end' points to the first non-prefix character of the
|
| 184 |
+
// buffer (as written by the LogFilterAndPrefixHook.)
|
| 185 |
+
//
|
| 186 |
+
// The lifetime of the filename and message buffers will not end while the
|
| 187 |
+
// process remains alive.
|
| 188 |
+
using AbortHook = void (*)(const char* file, int line, const char* buf_start,
|
| 189 |
+
const char* prefix_end, const char* buf_end);
|
| 190 |
+
|
| 191 |
+
// Internal logging function for ABSL_INTERNAL_LOG to dispatch to.
|
| 192 |
+
//
|
| 193 |
+
// TODO(gfalcon): When string_view no longer depends on base, change this
|
| 194 |
+
// interface to take its message as a string_view instead.
|
| 195 |
+
using InternalLogFunction = void (*)(absl::LogSeverity severity,
|
| 196 |
+
const char* file, int line,
|
| 197 |
+
const std::string& message);
|
| 198 |
+
|
| 199 |
+
ABSL_INTERNAL_ATOMIC_HOOK_ATTRIBUTES ABSL_DLL extern base_internal::AtomicHook<
|
| 200 |
+
InternalLogFunction>
|
| 201 |
+
internal_log_function;
|
| 202 |
+
|
| 203 |
+
// Registers hooks of the above types. Only a single hook of each type may be
|
| 204 |
+
// registered. It is an error to call these functions multiple times with
|
| 205 |
+
// different input arguments.
|
| 206 |
+
//
|
| 207 |
+
// These functions are safe to call at any point during initialization; they do
|
| 208 |
+
// not block or malloc, and are async-signal safe.
|
| 209 |
+
void RegisterLogFilterAndPrefixHook(LogFilterAndPrefixHook func);
|
| 210 |
+
void RegisterAbortHook(AbortHook func);
|
| 211 |
+
void RegisterInternalLogFunction(InternalLogFunction func);
|
| 212 |
+
|
| 213 |
+
} // namespace raw_log_internal
|
| 214 |
+
ABSL_NAMESPACE_END
|
| 215 |
+
} // namespace absl
|
| 216 |
+
|
| 217 |
+
#endif // ABSL_BASE_INTERNAL_RAW_LOGGING_H_
|
include/absl/base/internal/scheduling_mode.h
ADDED
|
@@ -0,0 +1,58 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
// Copyright 2017 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 |
+
// Core interfaces and definitions used by by low-level interfaces such as
|
| 16 |
+
// SpinLock.
|
| 17 |
+
|
| 18 |
+
#ifndef ABSL_BASE_INTERNAL_SCHEDULING_MODE_H_
|
| 19 |
+
#define ABSL_BASE_INTERNAL_SCHEDULING_MODE_H_
|
| 20 |
+
|
| 21 |
+
#include "absl/base/config.h"
|
| 22 |
+
|
| 23 |
+
namespace absl {
|
| 24 |
+
ABSL_NAMESPACE_BEGIN
|
| 25 |
+
namespace base_internal {
|
| 26 |
+
|
| 27 |
+
// Used to describe how a thread may be scheduled. Typically associated with
|
| 28 |
+
// the declaration of a resource supporting synchronized access.
|
| 29 |
+
//
|
| 30 |
+
// SCHEDULE_COOPERATIVE_AND_KERNEL:
|
| 31 |
+
// Specifies that when waiting, a cooperative thread (e.g. a Fiber) may
|
| 32 |
+
// reschedule (using base::scheduling semantics); allowing other cooperative
|
| 33 |
+
// threads to proceed.
|
| 34 |
+
//
|
| 35 |
+
// SCHEDULE_KERNEL_ONLY: (Also described as "non-cooperative")
|
| 36 |
+
// Specifies that no cooperative scheduling semantics may be used, even if the
|
| 37 |
+
// current thread is itself cooperatively scheduled. This means that
|
| 38 |
+
// cooperative threads will NOT allow other cooperative threads to execute in
|
| 39 |
+
// their place while waiting for a resource of this type. Host operating system
|
| 40 |
+
// semantics (e.g. a futex) may still be used.
|
| 41 |
+
//
|
| 42 |
+
// When optional, clients should strongly prefer SCHEDULE_COOPERATIVE_AND_KERNEL
|
| 43 |
+
// by default. SCHEDULE_KERNEL_ONLY should only be used for resources on which
|
| 44 |
+
// base::scheduling (e.g. the implementation of a Scheduler) may depend.
|
| 45 |
+
//
|
| 46 |
+
// NOTE: Cooperative resources may not be nested below non-cooperative ones.
|
| 47 |
+
// This means that it is invalid to to acquire a SCHEDULE_COOPERATIVE_AND_KERNEL
|
| 48 |
+
// resource if a SCHEDULE_KERNEL_ONLY resource is already held.
|
| 49 |
+
enum SchedulingMode {
|
| 50 |
+
SCHEDULE_KERNEL_ONLY = 0, // Allow scheduling only the host OS.
|
| 51 |
+
SCHEDULE_COOPERATIVE_AND_KERNEL, // Also allow cooperative scheduling.
|
| 52 |
+
};
|
| 53 |
+
|
| 54 |
+
} // namespace base_internal
|
| 55 |
+
ABSL_NAMESPACE_END
|
| 56 |
+
} // namespace absl
|
| 57 |
+
|
| 58 |
+
#endif // ABSL_BASE_INTERNAL_SCHEDULING_MODE_H_
|
include/absl/base/internal/scoped_set_env.h
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
//
|
| 2 |
+
// Copyright 2019 The Abseil Authors.
|
| 3 |
+
//
|
| 4 |
+
// Licensed under the Apache License, Version 2.0 (the "License");
|
| 5 |
+
// you may not use this file except in compliance with the License.
|
| 6 |
+
// You may obtain a copy of the License at
|
| 7 |
+
//
|
| 8 |
+
// https://www.apache.org/licenses/LICENSE-2.0
|
| 9 |
+
//
|
| 10 |
+
// Unless required by applicable law or agreed to in writing, software
|
| 11 |
+
// distributed under the License is distributed on an "AS IS" BASIS,
|
| 12 |
+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
| 13 |
+
// See the License for the specific language governing permissions and
|
| 14 |
+
// limitations under the License.
|
| 15 |
+
//
|
| 16 |
+
|
| 17 |
+
#ifndef ABSL_BASE_INTERNAL_SCOPED_SET_ENV_H_
|
| 18 |
+
#define ABSL_BASE_INTERNAL_SCOPED_SET_ENV_H_
|
| 19 |
+
|
| 20 |
+
#include <string>
|
| 21 |
+
|
| 22 |
+
#include "absl/base/config.h"
|
| 23 |
+
|
| 24 |
+
namespace absl {
|
| 25 |
+
ABSL_NAMESPACE_BEGIN
|
| 26 |
+
namespace base_internal {
|
| 27 |
+
|
| 28 |
+
class ScopedSetEnv {
|
| 29 |
+
public:
|
| 30 |
+
ScopedSetEnv(const char* var_name, const char* new_value);
|
| 31 |
+
~ScopedSetEnv();
|
| 32 |
+
|
| 33 |
+
private:
|
| 34 |
+
std::string var_name_;
|
| 35 |
+
std::string old_value_;
|
| 36 |
+
|
| 37 |
+
// True if the environment variable was initially not set.
|
| 38 |
+
bool was_unset_;
|
| 39 |
+
};
|
| 40 |
+
|
| 41 |
+
} // namespace base_internal
|
| 42 |
+
ABSL_NAMESPACE_END
|
| 43 |
+
} // namespace absl
|
| 44 |
+
|
| 45 |
+
#endif // ABSL_BASE_INTERNAL_SCOPED_SET_ENV_H_
|
include/absl/base/internal/spinlock.h
ADDED
|
@@ -0,0 +1,275 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
//
|
| 2 |
+
// Copyright 2017 The Abseil Authors.
|
| 3 |
+
//
|
| 4 |
+
// Licensed under the Apache License, Version 2.0 (the "License");
|
| 5 |
+
// you may not use this file except in compliance with the License.
|
| 6 |
+
// You may obtain a copy of the License at
|
| 7 |
+
//
|
| 8 |
+
// https://www.apache.org/licenses/LICENSE-2.0
|
| 9 |
+
//
|
| 10 |
+
// Unless required by applicable law or agreed to in writing, software
|
| 11 |
+
// distributed under the License is distributed on an "AS IS" BASIS,
|
| 12 |
+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
| 13 |
+
// See the License for the specific language governing permissions and
|
| 14 |
+
// limitations under the License.
|
| 15 |
+
//
|
| 16 |
+
|
| 17 |
+
// Most users requiring mutual exclusion should use Mutex.
|
| 18 |
+
// SpinLock is provided for use in two situations:
|
| 19 |
+
// - for use by Abseil internal code that Mutex itself depends on
|
| 20 |
+
// - for async signal safety (see below)
|
| 21 |
+
|
| 22 |
+
// SpinLock with a base_internal::SchedulingMode::SCHEDULE_KERNEL_ONLY is async
|
| 23 |
+
// signal safe. If a spinlock is used within a signal handler, all code that
|
| 24 |
+
// acquires the lock must ensure that the signal cannot arrive while they are
|
| 25 |
+
// holding the lock. Typically, this is done by blocking the signal.
|
| 26 |
+
//
|
| 27 |
+
// Threads waiting on a SpinLock may be woken in an arbitrary order.
|
| 28 |
+
|
| 29 |
+
#ifndef ABSL_BASE_INTERNAL_SPINLOCK_H_
|
| 30 |
+
#define ABSL_BASE_INTERNAL_SPINLOCK_H_
|
| 31 |
+
|
| 32 |
+
#include <atomic>
|
| 33 |
+
#include <cstdint>
|
| 34 |
+
|
| 35 |
+
#include "absl/base/attributes.h"
|
| 36 |
+
#include "absl/base/const_init.h"
|
| 37 |
+
#include "absl/base/dynamic_annotations.h"
|
| 38 |
+
#include "absl/base/internal/low_level_scheduling.h"
|
| 39 |
+
#include "absl/base/internal/raw_logging.h"
|
| 40 |
+
#include "absl/base/internal/scheduling_mode.h"
|
| 41 |
+
#include "absl/base/internal/tsan_mutex_interface.h"
|
| 42 |
+
#include "absl/base/thread_annotations.h"
|
| 43 |
+
|
| 44 |
+
namespace tcmalloc {
|
| 45 |
+
namespace tcmalloc_internal {
|
| 46 |
+
|
| 47 |
+
class AllocationGuardSpinLockHolder;
|
| 48 |
+
|
| 49 |
+
} // namespace tcmalloc_internal
|
| 50 |
+
} // namespace tcmalloc
|
| 51 |
+
|
| 52 |
+
namespace absl {
|
| 53 |
+
ABSL_NAMESPACE_BEGIN
|
| 54 |
+
namespace base_internal {
|
| 55 |
+
|
| 56 |
+
class ABSL_LOCKABLE ABSL_ATTRIBUTE_WARN_UNUSED SpinLock {
|
| 57 |
+
public:
|
| 58 |
+
SpinLock() : lockword_(kSpinLockCooperative) {
|
| 59 |
+
ABSL_TSAN_MUTEX_CREATE(this, __tsan_mutex_not_static);
|
| 60 |
+
}
|
| 61 |
+
|
| 62 |
+
// Constructors that allow non-cooperative spinlocks to be created for use
|
| 63 |
+
// inside thread schedulers. Normal clients should not use these.
|
| 64 |
+
explicit SpinLock(base_internal::SchedulingMode mode);
|
| 65 |
+
|
| 66 |
+
// Constructor for global SpinLock instances. See absl/base/const_init.h.
|
| 67 |
+
constexpr SpinLock(absl::ConstInitType, base_internal::SchedulingMode mode)
|
| 68 |
+
: lockword_(IsCooperative(mode) ? kSpinLockCooperative : 0) {}
|
| 69 |
+
|
| 70 |
+
// For global SpinLock instances prefer trivial destructor when possible.
|
| 71 |
+
// Default but non-trivial destructor in some build configurations causes an
|
| 72 |
+
// extra static initializer.
|
| 73 |
+
#ifdef ABSL_INTERNAL_HAVE_TSAN_INTERFACE
|
| 74 |
+
~SpinLock() { ABSL_TSAN_MUTEX_DESTROY(this, __tsan_mutex_not_static); }
|
| 75 |
+
#else
|
| 76 |
+
~SpinLock() = default;
|
| 77 |
+
#endif
|
| 78 |
+
|
| 79 |
+
// Acquire this SpinLock.
|
| 80 |
+
inline void Lock() ABSL_EXCLUSIVE_LOCK_FUNCTION() {
|
| 81 |
+
ABSL_TSAN_MUTEX_PRE_LOCK(this, 0);
|
| 82 |
+
if (!TryLockImpl()) {
|
| 83 |
+
SlowLock();
|
| 84 |
+
}
|
| 85 |
+
ABSL_TSAN_MUTEX_POST_LOCK(this, 0, 0);
|
| 86 |
+
}
|
| 87 |
+
|
| 88 |
+
// Try to acquire this SpinLock without blocking and return true if the
|
| 89 |
+
// acquisition was successful. If the lock was not acquired, false is
|
| 90 |
+
// returned. If this SpinLock is free at the time of the call, TryLock
|
| 91 |
+
// will return true with high probability.
|
| 92 |
+
ABSL_MUST_USE_RESULT inline bool TryLock()
|
| 93 |
+
ABSL_EXCLUSIVE_TRYLOCK_FUNCTION(true) {
|
| 94 |
+
ABSL_TSAN_MUTEX_PRE_LOCK(this, __tsan_mutex_try_lock);
|
| 95 |
+
bool res = TryLockImpl();
|
| 96 |
+
ABSL_TSAN_MUTEX_POST_LOCK(
|
| 97 |
+
this, __tsan_mutex_try_lock | (res ? 0 : __tsan_mutex_try_lock_failed),
|
| 98 |
+
0);
|
| 99 |
+
return res;
|
| 100 |
+
}
|
| 101 |
+
|
| 102 |
+
// Release this SpinLock, which must be held by the calling thread.
|
| 103 |
+
inline void Unlock() ABSL_UNLOCK_FUNCTION() {
|
| 104 |
+
ABSL_TSAN_MUTEX_PRE_UNLOCK(this, 0);
|
| 105 |
+
uint32_t lock_value = lockword_.load(std::memory_order_relaxed);
|
| 106 |
+
lock_value = lockword_.exchange(lock_value & kSpinLockCooperative,
|
| 107 |
+
std::memory_order_release);
|
| 108 |
+
|
| 109 |
+
if ((lock_value & kSpinLockDisabledScheduling) != 0) {
|
| 110 |
+
base_internal::SchedulingGuard::EnableRescheduling(true);
|
| 111 |
+
}
|
| 112 |
+
if ((lock_value & kWaitTimeMask) != 0) {
|
| 113 |
+
// Collect contentionz profile info, and speed the wakeup of any waiter.
|
| 114 |
+
// The wait_cycles value indicates how long this thread spent waiting
|
| 115 |
+
// for the lock.
|
| 116 |
+
SlowUnlock(lock_value);
|
| 117 |
+
}
|
| 118 |
+
ABSL_TSAN_MUTEX_POST_UNLOCK(this, 0);
|
| 119 |
+
}
|
| 120 |
+
|
| 121 |
+
// Determine if the lock is held. When the lock is held by the invoking
|
| 122 |
+
// thread, true will always be returned. Intended to be used as
|
| 123 |
+
// CHECK(lock.IsHeld()).
|
| 124 |
+
ABSL_MUST_USE_RESULT inline bool IsHeld() const {
|
| 125 |
+
return (lockword_.load(std::memory_order_relaxed) & kSpinLockHeld) != 0;
|
| 126 |
+
}
|
| 127 |
+
|
| 128 |
+
// Return immediately if this thread holds the SpinLock exclusively.
|
| 129 |
+
// Otherwise, report an error by crashing with a diagnostic.
|
| 130 |
+
inline void AssertHeld() const ABSL_ASSERT_EXCLUSIVE_LOCK() {
|
| 131 |
+
if (!IsHeld()) {
|
| 132 |
+
ABSL_RAW_LOG(FATAL, "thread should hold the lock on SpinLock");
|
| 133 |
+
}
|
| 134 |
+
}
|
| 135 |
+
|
| 136 |
+
protected:
|
| 137 |
+
// These should not be exported except for testing.
|
| 138 |
+
|
| 139 |
+
// Store number of cycles between wait_start_time and wait_end_time in a
|
| 140 |
+
// lock value.
|
| 141 |
+
static uint32_t EncodeWaitCycles(int64_t wait_start_time,
|
| 142 |
+
int64_t wait_end_time);
|
| 143 |
+
|
| 144 |
+
// Extract number of wait cycles in a lock value.
|
| 145 |
+
static int64_t DecodeWaitCycles(uint32_t lock_value);
|
| 146 |
+
|
| 147 |
+
// Provide access to protected method above. Use for testing only.
|
| 148 |
+
friend struct SpinLockTest;
|
| 149 |
+
friend class tcmalloc::tcmalloc_internal::AllocationGuardSpinLockHolder;
|
| 150 |
+
|
| 151 |
+
private:
|
| 152 |
+
// lockword_ is used to store the following:
|
| 153 |
+
//
|
| 154 |
+
// bit[0] encodes whether a lock is being held.
|
| 155 |
+
// bit[1] encodes whether a lock uses cooperative scheduling.
|
| 156 |
+
// bit[2] encodes whether the current lock holder disabled scheduling when
|
| 157 |
+
// acquiring the lock. Only set when kSpinLockHeld is also set.
|
| 158 |
+
// bit[3:31] encodes time a lock spent on waiting as a 29-bit unsigned int.
|
| 159 |
+
// This is set by the lock holder to indicate how long it waited on
|
| 160 |
+
// the lock before eventually acquiring it. The number of cycles is
|
| 161 |
+
// encoded as a 29-bit unsigned int, or in the case that the current
|
| 162 |
+
// holder did not wait but another waiter is queued, the LSB
|
| 163 |
+
// (kSpinLockSleeper) is set. The implementation does not explicitly
|
| 164 |
+
// track the number of queued waiters beyond this. It must always be
|
| 165 |
+
// assumed that waiters may exist if the current holder was required to
|
| 166 |
+
// queue.
|
| 167 |
+
//
|
| 168 |
+
// Invariant: if the lock is not held, the value is either 0 or
|
| 169 |
+
// kSpinLockCooperative.
|
| 170 |
+
static constexpr uint32_t kSpinLockHeld = 1;
|
| 171 |
+
static constexpr uint32_t kSpinLockCooperative = 2;
|
| 172 |
+
static constexpr uint32_t kSpinLockDisabledScheduling = 4;
|
| 173 |
+
static constexpr uint32_t kSpinLockSleeper = 8;
|
| 174 |
+
// Includes kSpinLockSleeper.
|
| 175 |
+
static constexpr uint32_t kWaitTimeMask =
|
| 176 |
+
~(kSpinLockHeld | kSpinLockCooperative | kSpinLockDisabledScheduling);
|
| 177 |
+
|
| 178 |
+
// Returns true if the provided scheduling mode is cooperative.
|
| 179 |
+
static constexpr bool IsCooperative(
|
| 180 |
+
base_internal::SchedulingMode scheduling_mode) {
|
| 181 |
+
return scheduling_mode == base_internal::SCHEDULE_COOPERATIVE_AND_KERNEL;
|
| 182 |
+
}
|
| 183 |
+
|
| 184 |
+
bool IsCooperative() const {
|
| 185 |
+
return lockword_.load(std::memory_order_relaxed) & kSpinLockCooperative;
|
| 186 |
+
}
|
| 187 |
+
|
| 188 |
+
uint32_t TryLockInternal(uint32_t lock_value, uint32_t wait_cycles);
|
| 189 |
+
void SlowLock() ABSL_ATTRIBUTE_COLD;
|
| 190 |
+
void SlowUnlock(uint32_t lock_value) ABSL_ATTRIBUTE_COLD;
|
| 191 |
+
uint32_t SpinLoop();
|
| 192 |
+
|
| 193 |
+
inline bool TryLockImpl() {
|
| 194 |
+
uint32_t lock_value = lockword_.load(std::memory_order_relaxed);
|
| 195 |
+
return (TryLockInternal(lock_value, 0) & kSpinLockHeld) == 0;
|
| 196 |
+
}
|
| 197 |
+
|
| 198 |
+
std::atomic<uint32_t> lockword_;
|
| 199 |
+
|
| 200 |
+
SpinLock(const SpinLock&) = delete;
|
| 201 |
+
SpinLock& operator=(const SpinLock&) = delete;
|
| 202 |
+
};
|
| 203 |
+
|
| 204 |
+
// Corresponding locker object that arranges to acquire a spinlock for
|
| 205 |
+
// the duration of a C++ scope.
|
| 206 |
+
//
|
| 207 |
+
// TODO(b/176172494): Use only [[nodiscard]] when baseline is raised.
|
| 208 |
+
// TODO(b/6695610): Remove forward declaration when #ifdef is no longer needed.
|
| 209 |
+
#if ABSL_HAVE_CPP_ATTRIBUTE(nodiscard)
|
| 210 |
+
class [[nodiscard]] SpinLockHolder;
|
| 211 |
+
#else
|
| 212 |
+
class ABSL_MUST_USE_RESULT ABSL_ATTRIBUTE_TRIVIAL_ABI SpinLockHolder;
|
| 213 |
+
#endif
|
| 214 |
+
|
| 215 |
+
class ABSL_SCOPED_LOCKABLE SpinLockHolder {
|
| 216 |
+
public:
|
| 217 |
+
inline explicit SpinLockHolder(SpinLock* l) ABSL_EXCLUSIVE_LOCK_FUNCTION(l)
|
| 218 |
+
: lock_(l) {
|
| 219 |
+
l->Lock();
|
| 220 |
+
}
|
| 221 |
+
inline ~SpinLockHolder() ABSL_UNLOCK_FUNCTION() { lock_->Unlock(); }
|
| 222 |
+
|
| 223 |
+
SpinLockHolder(const SpinLockHolder&) = delete;
|
| 224 |
+
SpinLockHolder& operator=(const SpinLockHolder&) = delete;
|
| 225 |
+
|
| 226 |
+
private:
|
| 227 |
+
SpinLock* lock_;
|
| 228 |
+
};
|
| 229 |
+
|
| 230 |
+
// Register a hook for profiling support.
|
| 231 |
+
//
|
| 232 |
+
// The function pointer registered here will be called whenever a spinlock is
|
| 233 |
+
// contended. The callback is given an opaque handle to the contended spinlock
|
| 234 |
+
// and the number of wait cycles. This is thread-safe, but only a single
|
| 235 |
+
// profiler can be registered. It is an error to call this function multiple
|
| 236 |
+
// times with different arguments.
|
| 237 |
+
void RegisterSpinLockProfiler(void (*fn)(const void* lock,
|
| 238 |
+
int64_t wait_cycles));
|
| 239 |
+
|
| 240 |
+
//------------------------------------------------------------------------------
|
| 241 |
+
// Public interface ends here.
|
| 242 |
+
//------------------------------------------------------------------------------
|
| 243 |
+
|
| 244 |
+
// If (result & kSpinLockHeld) == 0, then *this was successfully locked.
|
| 245 |
+
// Otherwise, returns last observed value for lockword_.
|
| 246 |
+
inline uint32_t SpinLock::TryLockInternal(uint32_t lock_value,
|
| 247 |
+
uint32_t wait_cycles) {
|
| 248 |
+
if ((lock_value & kSpinLockHeld) != 0) {
|
| 249 |
+
return lock_value;
|
| 250 |
+
}
|
| 251 |
+
|
| 252 |
+
uint32_t sched_disabled_bit = 0;
|
| 253 |
+
if ((lock_value & kSpinLockCooperative) == 0) {
|
| 254 |
+
// For non-cooperative locks we must make sure we mark ourselves as
|
| 255 |
+
// non-reschedulable before we attempt to CompareAndSwap.
|
| 256 |
+
if (base_internal::SchedulingGuard::DisableRescheduling()) {
|
| 257 |
+
sched_disabled_bit = kSpinLockDisabledScheduling;
|
| 258 |
+
}
|
| 259 |
+
}
|
| 260 |
+
|
| 261 |
+
if (!lockword_.compare_exchange_strong(
|
| 262 |
+
lock_value,
|
| 263 |
+
kSpinLockHeld | lock_value | wait_cycles | sched_disabled_bit,
|
| 264 |
+
std::memory_order_acquire, std::memory_order_relaxed)) {
|
| 265 |
+
base_internal::SchedulingGuard::EnableRescheduling(sched_disabled_bit != 0);
|
| 266 |
+
}
|
| 267 |
+
|
| 268 |
+
return lock_value;
|
| 269 |
+
}
|
| 270 |
+
|
| 271 |
+
} // namespace base_internal
|
| 272 |
+
ABSL_NAMESPACE_END
|
| 273 |
+
} // namespace absl
|
| 274 |
+
|
| 275 |
+
#endif // ABSL_BASE_INTERNAL_SPINLOCK_H_
|
include/absl/base/internal/spinlock_akaros.inc
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
// Copyright 2017 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 |
+
// This file is an Akaros-specific part of spinlock_wait.cc
|
| 16 |
+
|
| 17 |
+
#include <atomic>
|
| 18 |
+
|
| 19 |
+
#include "absl/base/internal/scheduling_mode.h"
|
| 20 |
+
|
| 21 |
+
extern "C" {
|
| 22 |
+
|
| 23 |
+
ABSL_ATTRIBUTE_WEAK void ABSL_INTERNAL_C_SYMBOL(AbslInternalSpinLockDelay)(
|
| 24 |
+
std::atomic<uint32_t>* /* lock_word */, uint32_t /* value */,
|
| 25 |
+
int /* loop */, absl::base_internal::SchedulingMode /* mode */) {
|
| 26 |
+
// In Akaros, one must take care not to call anything that could cause a
|
| 27 |
+
// malloc(), a blocking system call, or a uthread_yield() while holding a
|
| 28 |
+
// spinlock. Our callers assume will not call into libraries or other
|
| 29 |
+
// arbitrary code.
|
| 30 |
+
}
|
| 31 |
+
|
| 32 |
+
ABSL_ATTRIBUTE_WEAK void ABSL_INTERNAL_C_SYMBOL(AbslInternalSpinLockWake)(
|
| 33 |
+
std::atomic<uint32_t>* /* lock_word */, bool /* all */) {}
|
| 34 |
+
|
| 35 |
+
} // extern "C"
|
include/absl/base/internal/spinlock_linux.inc
ADDED
|
@@ -0,0 +1,71 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
// Copyright 2018 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 |
+
// This file is a Linux-specific part of spinlock_wait.cc
|
| 16 |
+
|
| 17 |
+
#include <linux/futex.h>
|
| 18 |
+
#include <sys/syscall.h>
|
| 19 |
+
#include <unistd.h>
|
| 20 |
+
|
| 21 |
+
#include <atomic>
|
| 22 |
+
#include <climits>
|
| 23 |
+
#include <cstdint>
|
| 24 |
+
#include <ctime>
|
| 25 |
+
|
| 26 |
+
#include "absl/base/attributes.h"
|
| 27 |
+
#include "absl/base/internal/errno_saver.h"
|
| 28 |
+
|
| 29 |
+
// The SpinLock lockword is `std::atomic<uint32_t>`. Here we assert that
|
| 30 |
+
// `std::atomic<uint32_t>` is bitwise equivalent of the `int` expected
|
| 31 |
+
// by SYS_futex. We also assume that reads/writes done to the lockword
|
| 32 |
+
// by SYS_futex have rational semantics with regard to the
|
| 33 |
+
// std::atomic<> API. C++ provides no guarantees of these assumptions,
|
| 34 |
+
// but they are believed to hold in practice.
|
| 35 |
+
static_assert(sizeof(std::atomic<uint32_t>) == sizeof(int),
|
| 36 |
+
"SpinLock lockword has the wrong size for a futex");
|
| 37 |
+
|
| 38 |
+
// Some Android headers are missing these definitions even though they
|
| 39 |
+
// support these futex operations.
|
| 40 |
+
#ifdef __BIONIC__
|
| 41 |
+
#ifndef SYS_futex
|
| 42 |
+
#define SYS_futex __NR_futex
|
| 43 |
+
#endif
|
| 44 |
+
#ifndef FUTEX_PRIVATE_FLAG
|
| 45 |
+
#define FUTEX_PRIVATE_FLAG 128
|
| 46 |
+
#endif
|
| 47 |
+
#endif
|
| 48 |
+
|
| 49 |
+
#if defined(__NR_futex_time64) && !defined(SYS_futex_time64)
|
| 50 |
+
#define SYS_futex_time64 __NR_futex_time64
|
| 51 |
+
#endif
|
| 52 |
+
|
| 53 |
+
#if defined(SYS_futex_time64) && !defined(SYS_futex)
|
| 54 |
+
#define SYS_futex SYS_futex_time64
|
| 55 |
+
#endif
|
| 56 |
+
|
| 57 |
+
extern "C" {
|
| 58 |
+
|
| 59 |
+
ABSL_ATTRIBUTE_WEAK void ABSL_INTERNAL_C_SYMBOL(AbslInternalSpinLockDelay)(
|
| 60 |
+
std::atomic<uint32_t> *w, uint32_t value, int,
|
| 61 |
+
absl::base_internal::SchedulingMode) {
|
| 62 |
+
absl::base_internal::ErrnoSaver errno_saver;
|
| 63 |
+
syscall(SYS_futex, w, FUTEX_WAIT | FUTEX_PRIVATE_FLAG, value, nullptr);
|
| 64 |
+
}
|
| 65 |
+
|
| 66 |
+
ABSL_ATTRIBUTE_WEAK void ABSL_INTERNAL_C_SYMBOL(AbslInternalSpinLockWake)(
|
| 67 |
+
std::atomic<uint32_t> *w, bool all) {
|
| 68 |
+
syscall(SYS_futex, w, FUTEX_WAKE | FUTEX_PRIVATE_FLAG, all ? INT_MAX : 1, 0);
|
| 69 |
+
}
|
| 70 |
+
|
| 71 |
+
} // extern "C"
|
include/absl/base/internal/spinlock_posix.inc
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
// Copyright 2017 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 |
+
// This file is a Posix-specific part of spinlock_wait.cc
|
| 16 |
+
|
| 17 |
+
#include <sched.h>
|
| 18 |
+
|
| 19 |
+
#include <atomic>
|
| 20 |
+
#include <ctime>
|
| 21 |
+
|
| 22 |
+
#include "absl/base/internal/errno_saver.h"
|
| 23 |
+
#include "absl/base/internal/scheduling_mode.h"
|
| 24 |
+
#include "absl/base/port.h"
|
| 25 |
+
|
| 26 |
+
extern "C" {
|
| 27 |
+
|
| 28 |
+
ABSL_ATTRIBUTE_WEAK void ABSL_INTERNAL_C_SYMBOL(AbslInternalSpinLockDelay)(
|
| 29 |
+
std::atomic<uint32_t>* /* lock_word */, uint32_t /* value */, int loop,
|
| 30 |
+
absl::base_internal::SchedulingMode /* mode */) {
|
| 31 |
+
absl::base_internal::ErrnoSaver errno_saver;
|
| 32 |
+
if (loop == 0) {
|
| 33 |
+
} else if (loop == 1) {
|
| 34 |
+
sched_yield();
|
| 35 |
+
} else {
|
| 36 |
+
struct timespec tm;
|
| 37 |
+
tm.tv_sec = 0;
|
| 38 |
+
tm.tv_nsec = absl::base_internal::SpinLockSuggestedDelayNS(loop);
|
| 39 |
+
nanosleep(&tm, nullptr);
|
| 40 |
+
}
|
| 41 |
+
}
|
| 42 |
+
|
| 43 |
+
ABSL_ATTRIBUTE_WEAK void ABSL_INTERNAL_C_SYMBOL(AbslInternalSpinLockWake)(
|
| 44 |
+
std::atomic<uint32_t>* /* lock_word */, bool /* all */) {}
|
| 45 |
+
|
| 46 |
+
} // extern "C"
|
include/absl/base/internal/spinlock_wait.h
ADDED
|
@@ -0,0 +1,95 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
// Copyright 2017 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_BASE_INTERNAL_SPINLOCK_WAIT_H_
|
| 16 |
+
#define ABSL_BASE_INTERNAL_SPINLOCK_WAIT_H_
|
| 17 |
+
|
| 18 |
+
// Operations to make atomic transitions on a word, and to allow
|
| 19 |
+
// waiting for those transitions to become possible.
|
| 20 |
+
|
| 21 |
+
#include <stdint.h>
|
| 22 |
+
#include <atomic>
|
| 23 |
+
|
| 24 |
+
#include "absl/base/internal/scheduling_mode.h"
|
| 25 |
+
|
| 26 |
+
namespace absl {
|
| 27 |
+
ABSL_NAMESPACE_BEGIN
|
| 28 |
+
namespace base_internal {
|
| 29 |
+
|
| 30 |
+
// SpinLockWait() waits until it can perform one of several transitions from
|
| 31 |
+
// "from" to "to". It returns when it performs a transition where done==true.
|
| 32 |
+
struct SpinLockWaitTransition {
|
| 33 |
+
uint32_t from;
|
| 34 |
+
uint32_t to;
|
| 35 |
+
bool done;
|
| 36 |
+
};
|
| 37 |
+
|
| 38 |
+
// Wait until *w can transition from trans[i].from to trans[i].to for some i
|
| 39 |
+
// satisfying 0<=i<n && trans[i].done, atomically make the transition,
|
| 40 |
+
// then return the old value of *w. Make any other atomic transitions
|
| 41 |
+
// where !trans[i].done, but continue waiting.
|
| 42 |
+
//
|
| 43 |
+
// Wakeups for threads blocked on SpinLockWait do not respect priorities.
|
| 44 |
+
uint32_t SpinLockWait(std::atomic<uint32_t> *w, int n,
|
| 45 |
+
const SpinLockWaitTransition trans[],
|
| 46 |
+
SchedulingMode scheduling_mode);
|
| 47 |
+
|
| 48 |
+
// If possible, wake some thread that has called SpinLockDelay(w, ...). If `all`
|
| 49 |
+
// is true, wake all such threads. On some systems, this may be a no-op; on
|
| 50 |
+
// those systems, threads calling SpinLockDelay() will always wake eventually
|
| 51 |
+
// even if SpinLockWake() is never called.
|
| 52 |
+
void SpinLockWake(std::atomic<uint32_t> *w, bool all);
|
| 53 |
+
|
| 54 |
+
// Wait for an appropriate spin delay on iteration "loop" of a
|
| 55 |
+
// spin loop on location *w, whose previously observed value was "value".
|
| 56 |
+
// SpinLockDelay() may do nothing, may yield the CPU, may sleep a clock tick,
|
| 57 |
+
// or may wait for a call to SpinLockWake(w).
|
| 58 |
+
void SpinLockDelay(std::atomic<uint32_t> *w, uint32_t value, int loop,
|
| 59 |
+
base_internal::SchedulingMode scheduling_mode);
|
| 60 |
+
|
| 61 |
+
// Helper used by AbslInternalSpinLockDelay.
|
| 62 |
+
// Returns a suggested delay in nanoseconds for iteration number "loop".
|
| 63 |
+
int SpinLockSuggestedDelayNS(int loop);
|
| 64 |
+
|
| 65 |
+
} // namespace base_internal
|
| 66 |
+
ABSL_NAMESPACE_END
|
| 67 |
+
} // namespace absl
|
| 68 |
+
|
| 69 |
+
// In some build configurations we pass --detect-odr-violations to the
|
| 70 |
+
// gold linker. This causes it to flag weak symbol overrides as ODR
|
| 71 |
+
// violations. Because ODR only applies to C++ and not C,
|
| 72 |
+
// --detect-odr-violations ignores symbols not mangled with C++ names.
|
| 73 |
+
// By changing our extension points to be extern "C", we dodge this
|
| 74 |
+
// check.
|
| 75 |
+
extern "C" {
|
| 76 |
+
void ABSL_INTERNAL_C_SYMBOL(AbslInternalSpinLockWake)(std::atomic<uint32_t> *w,
|
| 77 |
+
bool all);
|
| 78 |
+
void ABSL_INTERNAL_C_SYMBOL(AbslInternalSpinLockDelay)(
|
| 79 |
+
std::atomic<uint32_t> *w, uint32_t value, int loop,
|
| 80 |
+
absl::base_internal::SchedulingMode scheduling_mode);
|
| 81 |
+
}
|
| 82 |
+
|
| 83 |
+
inline void absl::base_internal::SpinLockWake(std::atomic<uint32_t> *w,
|
| 84 |
+
bool all) {
|
| 85 |
+
ABSL_INTERNAL_C_SYMBOL(AbslInternalSpinLockWake)(w, all);
|
| 86 |
+
}
|
| 87 |
+
|
| 88 |
+
inline void absl::base_internal::SpinLockDelay(
|
| 89 |
+
std::atomic<uint32_t> *w, uint32_t value, int loop,
|
| 90 |
+
absl::base_internal::SchedulingMode scheduling_mode) {
|
| 91 |
+
ABSL_INTERNAL_C_SYMBOL(AbslInternalSpinLockDelay)
|
| 92 |
+
(w, value, loop, scheduling_mode);
|
| 93 |
+
}
|
| 94 |
+
|
| 95 |
+
#endif // ABSL_BASE_INTERNAL_SPINLOCK_WAIT_H_
|
include/absl/base/internal/spinlock_win32.inc
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
// Copyright 2017 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 |
+
// This file is a Win32-specific part of spinlock_wait.cc
|
| 16 |
+
|
| 17 |
+
#include <windows.h>
|
| 18 |
+
#include <atomic>
|
| 19 |
+
#include "absl/base/internal/scheduling_mode.h"
|
| 20 |
+
|
| 21 |
+
extern "C" {
|
| 22 |
+
|
| 23 |
+
void ABSL_INTERNAL_C_SYMBOL(AbslInternalSpinLockDelay)(
|
| 24 |
+
std::atomic<uint32_t>* /* lock_word */, uint32_t /* value */, int loop,
|
| 25 |
+
absl::base_internal::SchedulingMode /* mode */) {
|
| 26 |
+
if (loop == 0) {
|
| 27 |
+
} else if (loop == 1) {
|
| 28 |
+
Sleep(0);
|
| 29 |
+
} else {
|
| 30 |
+
// SpinLockSuggestedDelayNS() always returns a positive integer, so this
|
| 31 |
+
// static_cast is safe.
|
| 32 |
+
Sleep(static_cast<DWORD>(
|
| 33 |
+
absl::base_internal::SpinLockSuggestedDelayNS(loop) / 1000000));
|
| 34 |
+
}
|
| 35 |
+
}
|
| 36 |
+
|
| 37 |
+
void ABSL_INTERNAL_C_SYMBOL(AbslInternalSpinLockWake)(
|
| 38 |
+
std::atomic<uint32_t>* /* lock_word */, bool /* all */) {}
|
| 39 |
+
|
| 40 |
+
} // extern "C"
|
include/absl/base/internal/strerror.h
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
// Copyright 2020 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_BASE_INTERNAL_STRERROR_H_
|
| 16 |
+
#define ABSL_BASE_INTERNAL_STRERROR_H_
|
| 17 |
+
|
| 18 |
+
#include <string>
|
| 19 |
+
|
| 20 |
+
#include "absl/base/config.h"
|
| 21 |
+
|
| 22 |
+
namespace absl {
|
| 23 |
+
ABSL_NAMESPACE_BEGIN
|
| 24 |
+
namespace base_internal {
|
| 25 |
+
|
| 26 |
+
// A portable and thread-safe alternative to C89's `strerror`.
|
| 27 |
+
//
|
| 28 |
+
// The C89 specification of `strerror` is not suitable for use in a
|
| 29 |
+
// multi-threaded application as the returned string may be changed by calls to
|
| 30 |
+
// `strerror` from another thread. The many non-stdlib alternatives differ
|
| 31 |
+
// enough in their names, availability, and semantics to justify this wrapper
|
| 32 |
+
// around them. `errno` will not be modified by a call to `absl::StrError`.
|
| 33 |
+
std::string StrError(int errnum);
|
| 34 |
+
|
| 35 |
+
} // namespace base_internal
|
| 36 |
+
ABSL_NAMESPACE_END
|
| 37 |
+
} // namespace absl
|
| 38 |
+
|
| 39 |
+
#endif // ABSL_BASE_INTERNAL_STRERROR_H_
|
include/absl/base/internal/sysinfo.h
ADDED
|
@@ -0,0 +1,74 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
// Copyright 2017 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 |
+
// This file includes routines to find out characteristics
|
| 16 |
+
// of the machine a program is running on. It is undoubtedly
|
| 17 |
+
// system-dependent.
|
| 18 |
+
|
| 19 |
+
// Functions listed here that accept a pid_t as an argument act on the
|
| 20 |
+
// current process if the pid_t argument is 0
|
| 21 |
+
// All functions here are thread-hostile due to file caching unless
|
| 22 |
+
// commented otherwise.
|
| 23 |
+
|
| 24 |
+
#ifndef ABSL_BASE_INTERNAL_SYSINFO_H_
|
| 25 |
+
#define ABSL_BASE_INTERNAL_SYSINFO_H_
|
| 26 |
+
|
| 27 |
+
#ifndef _WIN32
|
| 28 |
+
#include <sys/types.h>
|
| 29 |
+
#endif
|
| 30 |
+
|
| 31 |
+
#include <cstdint>
|
| 32 |
+
|
| 33 |
+
#include "absl/base/config.h"
|
| 34 |
+
#include "absl/base/port.h"
|
| 35 |
+
|
| 36 |
+
namespace absl {
|
| 37 |
+
ABSL_NAMESPACE_BEGIN
|
| 38 |
+
namespace base_internal {
|
| 39 |
+
|
| 40 |
+
// Nominal core processor cycles per second of each processor. This is _not_
|
| 41 |
+
// necessarily the frequency of the CycleClock counter (see cycleclock.h)
|
| 42 |
+
// Thread-safe.
|
| 43 |
+
double NominalCPUFrequency();
|
| 44 |
+
|
| 45 |
+
// Number of logical processors (hyperthreads) in system. Thread-safe.
|
| 46 |
+
int NumCPUs();
|
| 47 |
+
|
| 48 |
+
// Return the thread id of the current thread, as told by the system.
|
| 49 |
+
// No two currently-live threads implemented by the OS shall have the same ID.
|
| 50 |
+
// Thread ids of exited threads may be reused. Multiple user-level threads
|
| 51 |
+
// may have the same thread ID if multiplexed on the same OS thread.
|
| 52 |
+
//
|
| 53 |
+
// On Linux, you may send a signal to the resulting ID with kill(). However,
|
| 54 |
+
// it is recommended for portability that you use pthread_kill() instead.
|
| 55 |
+
#ifdef _WIN32
|
| 56 |
+
// On Windows, process id and thread id are of the same type according to the
|
| 57 |
+
// return types of GetProcessId() and GetThreadId() are both DWORD, an unsigned
|
| 58 |
+
// 32-bit type.
|
| 59 |
+
using pid_t = uint32_t;
|
| 60 |
+
#endif
|
| 61 |
+
pid_t GetTID();
|
| 62 |
+
|
| 63 |
+
// Like GetTID(), but caches the result in thread-local storage in order
|
| 64 |
+
// to avoid unnecessary system calls. Note that there are some cases where
|
| 65 |
+
// one must call through to GetTID directly, which is why this exists as a
|
| 66 |
+
// separate function. For example, GetCachedTID() is not safe to call in
|
| 67 |
+
// an asynchronous signal-handling context nor right after a call to fork().
|
|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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
// Copyright 2017 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 |
+
// Each active thread has an ThreadIdentity that may represent the thread in
|
| 16 |
+
// various level interfaces. ThreadIdentity objects are never deallocated.
|
| 17 |
+
// When a thread terminates, its ThreadIdentity object may be reused for a
|
| 18 |
+
// thread created later.
|
| 19 |
+
|
| 20 |
+
#ifndef ABSL_BASE_INTERNAL_THREAD_IDENTITY_H_
|
| 21 |
+
#define ABSL_BASE_INTERNAL_THREAD_IDENTITY_H_
|
| 22 |
+
|
| 23 |
+
#ifndef _WIN32
|
| 24 |
+
#include <pthread.h>
|
| 25 |
+
// Defines __GOOGLE_GRTE_VERSION__ (via glibc-specific features.h) when
|
| 26 |
+
// supported.
|
| 27 |
+
#include <unistd.h>
|
| 28 |
+
#endif
|
| 29 |
+
|
| 30 |
+
#include <atomic>
|
| 31 |
+
#include <cstdint>
|
| 32 |
+
|
| 33 |
+
#include "absl/base/config.h"
|
| 34 |
+
#include "absl/base/internal/per_thread_tls.h"
|
| 35 |
+
#include "absl/base/optimization.h"
|
| 36 |
+
|
| 37 |
+
namespace absl {
|
| 38 |
+
ABSL_NAMESPACE_BEGIN
|
| 39 |
+
|
| 40 |
+
struct SynchLocksHeld;
|
| 41 |
+
struct SynchWaitParams;
|
| 42 |
+
|
| 43 |
+
namespace base_internal {
|
| 44 |
+
|
| 45 |
+
class SpinLock;
|
| 46 |
+
struct ThreadIdentity;
|
| 47 |
+
|
| 48 |
+
// Used by the implementation of absl::Mutex and absl::CondVar.
|
| 49 |
+
struct PerThreadSynch {
|
| 50 |
+
// The internal representation of absl::Mutex and absl::CondVar rely
|
| 51 |
+
// on the alignment of PerThreadSynch. Both store the address of the
|
| 52 |
+
// PerThreadSynch in the high-order bits of their internal state,
|
| 53 |
+
// which means the low kLowZeroBits of the address of PerThreadSynch
|
| 54 |
+
// must be zero.
|
| 55 |
+
static constexpr int kLowZeroBits = 8;
|
| 56 |
+
static constexpr int kAlignment = 1 << kLowZeroBits;
|
| 57 |
+
|
| 58 |
+
// Returns the associated ThreadIdentity.
|
| 59 |
+
// This can be implemented as a cast because we guarantee
|
| 60 |
+
// PerThreadSynch is the first element of ThreadIdentity.
|
| 61 |
+
ThreadIdentity* thread_identity() {
|
| 62 |
+
return reinterpret_cast<ThreadIdentity*>(this);
|
| 63 |
+
}
|
| 64 |
+
|
| 65 |
+
PerThreadSynch* next; // Circular waiter queue; initialized to 0.
|
| 66 |
+
PerThreadSynch* skip; // If non-zero, all entries in Mutex queue
|
| 67 |
+
// up to and including "skip" have same
|
| 68 |
+
// condition as this, and will be woken later
|
| 69 |
+
bool may_skip; // if false while on mutex queue, a mutex unlocker
|
| 70 |
+
// is using this PerThreadSynch as a terminator. Its
|
| 71 |
+
// skip field must not be filled in because the loop
|
| 72 |
+
// might then skip over the terminator.
|
| 73 |
+
bool wake; // This thread is to be woken from a Mutex.
|
| 74 |
+
// If "x" is on a waiter list for a mutex, "x->cond_waiter" is true iff the
|
| 75 |
+
// waiter is waiting on the mutex as part of a CV Wait or Mutex Await.
|
| 76 |
+
//
|
| 77 |
+
// The value of "x->cond_waiter" is meaningless if "x" is not on a
|
| 78 |
+
// Mutex waiter list.
|
| 79 |
+
bool cond_waiter;
|
| 80 |
+
bool maybe_unlocking; // Valid at head of Mutex waiter queue;
|
| 81 |
+
// true if UnlockSlow could be searching
|
| 82 |
+
// for a waiter to wake. Used for an optimization
|
| 83 |
+
// in Enqueue(). true is always a valid value.
|
| 84 |
+
// Can be reset to false when the unlocker or any
|
| 85 |
+
// writer releases the lock, or a reader fully
|
| 86 |
+
// releases the lock. It may not be set to false
|
| 87 |
+
// by a reader that decrements the count to
|
| 88 |
+
// non-zero. protected by mutex spinlock
|
| 89 |
+
bool suppress_fatal_errors; // If true, try to proceed even in the face
|
| 90 |
+
// of broken invariants. This is used within
|
| 91 |
+
// fatal signal handlers to improve the
|
| 92 |
+
// chances of debug logging information being
|
| 93 |
+
// output successfully.
|
| 94 |
+
int priority; // Priority of thread (updated every so often).
|
| 95 |
+
|
| 96 |
+
// State values:
|
| 97 |
+
// kAvailable: This PerThreadSynch is available.
|
| 98 |
+
// kQueued: This PerThreadSynch is unavailable, it's currently queued on a
|
| 99 |
+
// Mutex or CondVar waistlist.
|
| 100 |
+
//
|
| 101 |
+
// Transitions from kQueued to kAvailable require a release
|
| 102 |
+
// barrier. This is needed as a waiter may use "state" to
|
| 103 |
+
// independently observe that it's no longer queued.
|
| 104 |
+
//
|
| 105 |
+
// Transitions from kAvailable to kQueued require no barrier, they
|
| 106 |
+
// are externally ordered by the Mutex.
|
| 107 |
+
enum State { kAvailable, kQueued };
|
| 108 |
+
std::atomic<State> state;
|
| 109 |
+
|
| 110 |
+
// The wait parameters of the current wait. waitp is null if the
|
| 111 |
+
// thread is not waiting. Transitions from null to non-null must
|
| 112 |
+
// occur before the enqueue commit point (state = kQueued in
|
| 113 |
+
// Enqueue() and CondVarEnqueue()). Transitions from non-null to
|
| 114 |
+
// null must occur after the wait is finished (state = kAvailable in
|
| 115 |
+
// Mutex::Block() and CondVar::WaitCommon()). This field may be
|
| 116 |
+
// changed only by the thread that describes this PerThreadSynch. A
|
| 117 |
+
// special case is Fer(), which calls Enqueue() on another thread,
|
| 118 |
+
// but with an identical SynchWaitParams pointer, thus leaving the
|
| 119 |
+
// pointer unchanged.
|
| 120 |
+
SynchWaitParams* waitp;
|
| 121 |
+
|
| 122 |
+
intptr_t readers; // Number of readers in mutex.
|
| 123 |
+
|
| 124 |
+
// When priority will next be read (cycles).
|
| 125 |
+
int64_t next_priority_read_cycles;
|
| 126 |
+
|
| 127 |
+
// Locks held; used during deadlock detection.
|
| 128 |
+
// Allocated in Synch_GetAllLocks() and freed in ReclaimThreadIdentity().
|
| 129 |
+
SynchLocksHeld* all_locks;
|
| 130 |
+
};
|
| 131 |
+
|
| 132 |
+
// The instances of this class are allocated in NewThreadIdentity() with an
|
| 133 |
+
// alignment of PerThreadSynch::kAlignment and never destroyed. Initialization
|
| 134 |
+
// should happen in OneTimeInitThreadIdentity().
|
| 135 |
+
//
|
| 136 |
+
// Instances may be reused by new threads - fields should be reset in
|
| 137 |
+
// ResetThreadIdentityBetweenReuse().
|
| 138 |
+
//
|
| 139 |
+
// NOTE: The layout of fields in this structure is critical, please do not
|
| 140 |
+
// add, remove, or modify the field placements without fully auditing the
|
| 141 |
+
// layout.
|
| 142 |
+
struct ThreadIdentity {
|
| 143 |
+
// Must be the first member. The Mutex implementation requires that
|
| 144 |
+
// the PerThreadSynch object associated with each thread is
|
| 145 |
+
// PerThreadSynch::kAlignment aligned. We provide this alignment on
|
| 146 |
+
// ThreadIdentity itself.
|
| 147 |
+
PerThreadSynch per_thread_synch;
|
| 148 |
+
|
| 149 |
+
// Private: Reserved for absl::synchronization_internal::Waiter.
|
| 150 |
+
struct WaiterState {
|
| 151 |
+
alignas(void*) char data[256];
|
| 152 |
+
} waiter_state;
|
| 153 |
+
|
| 154 |
+
// Used by PerThreadSem::{Get,Set}ThreadBlockedCounter().
|
| 155 |
+
std::atomic<int>* blocked_count_ptr;
|
| 156 |
+
|
| 157 |
+
// The following variables are mostly read/written just by the
|
| 158 |
+
// thread itself. The only exception is that these are read by
|
| 159 |
+
// a ticker thread as a hint.
|
| 160 |
+
std::atomic<int> ticker; // Tick counter, incremented once per second.
|
| 161 |
+
std::atomic<int> wait_start; // Ticker value when thread started waiting.
|
| 162 |
+
std::atomic<bool> is_idle; // Has thread become idle yet?
|
| 163 |
+
|
| 164 |
+
ThreadIdentity* next;
|
| 165 |
+
};
|
| 166 |
+
|
| 167 |
+
// Returns the ThreadIdentity object representing the calling thread; guaranteed
|
| 168 |
+
// to be unique for its lifetime. The returned object will remain valid for the
|
| 169 |
+
// program's lifetime; although it may be re-assigned to a subsequent thread.
|
| 170 |
+
// If one does not exist, return nullptr instead.
|
| 171 |
+
//
|
| 172 |
+
// Does not malloc(*), and is async-signal safe.
|
| 173 |
+
// [*] Technically pthread_setspecific() does malloc on first use; however this
|
| 174 |
+
// is handled internally within tcmalloc's initialization already. Note that
|
| 175 |
+
// darwin does *not* use tcmalloc, so this can catch you if using MallocHooks
|
| 176 |
+
// on Apple platforms. Whatever function is calling your MallocHooks will need
|
| 177 |
+
// to watch for recursion on Apple platforms.
|
| 178 |
+
//
|
| 179 |
+
// New ThreadIdentity objects can be constructed and associated with a thread
|
| 180 |
+
// by calling GetOrCreateCurrentThreadIdentity() in per-thread-sem.h.
|
| 181 |
+
ThreadIdentity* CurrentThreadIdentityIfPresent();
|
| 182 |
+
|
| 183 |
+
using ThreadIdentityReclaimerFunction = void (*)(void*);
|
| 184 |
+
|
| 185 |
+
// Sets the current thread identity to the given value. 'reclaimer' is a
|
| 186 |
+
// pointer to the global function for cleaning up instances on thread
|
| 187 |
+
// destruction.
|
| 188 |
+
void SetCurrentThreadIdentity(ThreadIdentity* identity,
|
| 189 |
+
ThreadIdentityReclaimerFunction reclaimer);
|
| 190 |
+
|
| 191 |
+
// Removes the currently associated ThreadIdentity from the running thread.
|
| 192 |
+
// This must be called from inside the ThreadIdentityReclaimerFunction, and only
|
| 193 |
+
// from that function.
|
| 194 |
+
void ClearCurrentThreadIdentity();
|
| 195 |
+
|
| 196 |
+
// May be chosen at compile time via: -DABSL_FORCE_THREAD_IDENTITY_MODE=<mode
|
| 197 |
+
// index>
|
| 198 |
+
#ifdef ABSL_THREAD_IDENTITY_MODE_USE_POSIX_SETSPECIFIC
|
| 199 |
+
#error ABSL_THREAD_IDENTITY_MODE_USE_POSIX_SETSPECIFIC cannot be directly set
|
| 200 |
+
#else
|
| 201 |
+
#define ABSL_THREAD_IDENTITY_MODE_USE_POSIX_SETSPECIFIC 0
|
| 202 |
+
#endif
|
| 203 |
+
|
| 204 |
+
#ifdef ABSL_THREAD_IDENTITY_MODE_USE_TLS
|
| 205 |
+
#error ABSL_THREAD_IDENTITY_MODE_USE_TLS cannot be directly set
|
| 206 |
+
#else
|
| 207 |
+
#define ABSL_THREAD_IDENTITY_MODE_USE_TLS 1
|
| 208 |
+
#endif
|
| 209 |
+
|
| 210 |
+
#ifdef ABSL_THREAD_IDENTITY_MODE_USE_CPP11
|
| 211 |
+
#error ABSL_THREAD_IDENTITY_MODE_USE_CPP11 cannot be directly set
|
| 212 |
+
#else
|
| 213 |
+
#define ABSL_THREAD_IDENTITY_MODE_USE_CPP11 2
|
| 214 |
+
#endif
|
| 215 |
+
|
| 216 |
+
#ifdef ABSL_THREAD_IDENTITY_MODE
|
| 217 |
+
#error ABSL_THREAD_IDENTITY_MODE cannot be directly set
|
| 218 |
+
#elif defined(ABSL_FORCE_THREAD_IDENTITY_MODE)
|
| 219 |
+
#define ABSL_THREAD_IDENTITY_MODE ABSL_FORCE_THREAD_IDENTITY_MODE
|
| 220 |
+
#elif defined(_WIN32) && !defined(__MINGW32__)
|
| 221 |
+
#define ABSL_THREAD_IDENTITY_MODE ABSL_THREAD_IDENTITY_MODE_USE_CPP11
|
| 222 |
+
#elif defined(__APPLE__) && defined(ABSL_HAVE_THREAD_LOCAL)
|
| 223 |
+
#define ABSL_THREAD_IDENTITY_MODE ABSL_THREAD_IDENTITY_MODE_USE_CPP11
|
| 224 |
+
#elif ABSL_PER_THREAD_TLS && defined(__GOOGLE_GRTE_VERSION__) && \
|
| 225 |
+
(__GOOGLE_GRTE_VERSION__ >= 20140228L)
|
| 226 |
+
// Support for async-safe TLS was specifically added in GRTEv4. It's not
|
| 227 |
+
// present in the upstream eglibc.
|
| 228 |
+
// Note: Current default for production systems.
|
| 229 |
+
#define ABSL_THREAD_IDENTITY_MODE ABSL_THREAD_IDENTITY_MODE_USE_TLS
|
| 230 |
+
#else
|
| 231 |
+
#define ABSL_THREAD_IDENTITY_MODE \
|
| 232 |
+
ABSL_THREAD_IDENTITY_MODE_USE_POSIX_SETSPECIFIC
|
| 233 |
+
#endif
|
| 234 |
+
|
| 235 |
+
#if ABSL_THREAD_IDENTITY_MODE == ABSL_THREAD_IDENTITY_MODE_USE_TLS || \
|
| 236 |
+
ABSL_THREAD_IDENTITY_MODE == ABSL_THREAD_IDENTITY_MODE_USE_CPP11
|
| 237 |
+
|
| 238 |
+
#if ABSL_PER_THREAD_TLS
|
| 239 |
+
ABSL_CONST_INIT extern ABSL_PER_THREAD_TLS_KEYWORD ThreadIdentity*
|
| 240 |
+
thread_identity_ptr;
|
| 241 |
+
#elif defined(ABSL_HAVE_THREAD_LOCAL)
|
| 242 |
+
ABSL_CONST_INIT extern thread_local ThreadIdentity* thread_identity_ptr;
|
| 243 |
+
#else
|
| 244 |
+
#error Thread-local storage not detected on this platform
|
| 245 |
+
#endif
|
| 246 |
+
|
| 247 |
+
// thread_local variables cannot be in headers exposed by DLLs or in certain
|
| 248 |
+
// build configurations on Apple platforms. However, it is important for
|
| 249 |
+
// performance reasons in general that `CurrentThreadIdentityIfPresent` be
|
| 250 |
+
// inlined. In the other cases we opt to have the function not be inlined. Note
|
| 251 |
+
// that `CurrentThreadIdentityIfPresent` is declared above so we can exclude
|
| 252 |
+
// this entire inline definition.
|
| 253 |
+
#if !defined(__APPLE__) && !defined(ABSL_BUILD_DLL) && \
|
| 254 |
+
!defined(ABSL_CONSUME_DLL)
|
| 255 |
+
#define ABSL_INTERNAL_INLINE_CURRENT_THREAD_IDENTITY_IF_PRESENT 1
|
| 256 |
+
#endif
|
| 257 |
+
|
| 258 |
+
#ifdef ABSL_INTERNAL_INLINE_CURRENT_THREAD_IDENTITY_IF_PRESENT
|
| 259 |
+
inline ThreadIdentity* CurrentThreadIdentityIfPresent() {
|
| 260 |
+
return thread_identity_ptr;
|
| 261 |
+
}
|
| 262 |
+
#endif
|
| 263 |
+
|
| 264 |
+
#elif ABSL_THREAD_IDENTITY_MODE != \
|
| 265 |
+
ABSL_THREAD_IDENTITY_MODE_USE_POSIX_SETSPECIFIC
|
| 266 |
+
#error Unknown ABSL_THREAD_IDENTITY_MODE
|
| 267 |
+
#endif
|
| 268 |
+
|
| 269 |
+
} // namespace base_internal
|
| 270 |
+
ABSL_NAMESPACE_END
|
| 271 |
+
} // namespace absl
|
| 272 |
+
|
| 273 |
+
#endif // ABSL_BASE_INTERNAL_THREAD_IDENTITY_H_
|
include/absl/base/internal/throw_delegate.h
ADDED
|
@@ -0,0 +1,75 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
//
|
| 2 |
+
// Copyright 2017 The Abseil Authors.
|
| 3 |
+
//
|
| 4 |
+
// Licensed under the Apache License, Version 2.0 (the "License");
|
| 5 |
+
// you may not use this file except in compliance with the License.
|
| 6 |
+
// You may obtain a copy of the License at
|
| 7 |
+
//
|
| 8 |
+
// https://www.apache.org/licenses/LICENSE-2.0
|
| 9 |
+
//
|
| 10 |
+
// Unless required by applicable law or agreed to in writing, software
|
| 11 |
+
// distributed under the License is distributed on an "AS IS" BASIS,
|
| 12 |
+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
| 13 |
+
// See the License for the specific language governing permissions and
|
| 14 |
+
// limitations under the License.
|
| 15 |
+
//
|
| 16 |
+
|
| 17 |
+
#ifndef ABSL_BASE_INTERNAL_THROW_DELEGATE_H_
|
| 18 |
+
#define ABSL_BASE_INTERNAL_THROW_DELEGATE_H_
|
| 19 |
+
|
| 20 |
+
#include <string>
|
| 21 |
+
|
| 22 |
+
#include "absl/base/config.h"
|
| 23 |
+
|
| 24 |
+
namespace absl {
|
| 25 |
+
ABSL_NAMESPACE_BEGIN
|
| 26 |
+
namespace base_internal {
|
| 27 |
+
|
| 28 |
+
// Helper functions that allow throwing exceptions consistently from anywhere.
|
| 29 |
+
// The main use case is for header-based libraries (eg templates), as they will
|
| 30 |
+
// be built by many different targets with their own compiler options.
|
| 31 |
+
// In particular, this will allow a safe way to throw exceptions even if the
|
| 32 |
+
// caller is compiled with -fno-exceptions. This is intended for implementing
|
| 33 |
+
// things like map<>::at(), which the standard documents as throwing an
|
| 34 |
+
// exception on error.
|
| 35 |
+
//
|
| 36 |
+
// Using other techniques like #if tricks could lead to ODR violations.
|
| 37 |
+
//
|
| 38 |
+
// You shouldn't use it unless you're writing code that you know will be built
|
| 39 |
+
// both with and without exceptions and you need to conform to an interface
|
| 40 |
+
// that uses exceptions.
|
| 41 |
+
|
| 42 |
+
[[noreturn]] void ThrowStdLogicError(const std::string& what_arg);
|
| 43 |
+
[[noreturn]] void ThrowStdLogicError(const char* what_arg);
|
| 44 |
+
[[noreturn]] void ThrowStdInvalidArgument(const std::string& what_arg);
|
| 45 |
+
[[noreturn]] void ThrowStdInvalidArgument(const char* what_arg);
|
| 46 |
+
[[noreturn]] void ThrowStdDomainError(const std::string& what_arg);
|
| 47 |
+
[[noreturn]] void ThrowStdDomainError(const char* what_arg);
|
| 48 |
+
[[noreturn]] void ThrowStdLengthError(const std::string& what_arg);
|
| 49 |
+
[[noreturn]] void ThrowStdLengthError(const char* what_arg);
|
| 50 |
+
[[noreturn]] void ThrowStdOutOfRange(const std::string& what_arg);
|
| 51 |
+
[[noreturn]] void ThrowStdOutOfRange(const char* what_arg);
|
| 52 |
+
[[noreturn]] void ThrowStdRuntimeError(const std::string& what_arg);
|
| 53 |
+
[[noreturn]] void ThrowStdRuntimeError(const char* what_arg);
|
| 54 |
+
[[noreturn]] void ThrowStdRangeError(const std::string& what_arg);
|
| 55 |
+
[[noreturn]] void ThrowStdRangeError(const char* what_arg);
|
| 56 |
+
[[noreturn]] void ThrowStdOverflowError(const std::string& what_arg);
|
| 57 |
+
[[noreturn]] void ThrowStdOverflowError(const char* what_arg);
|
| 58 |
+
[[noreturn]] void ThrowStdUnderflowError(const std::string& what_arg);
|
| 59 |
+
[[noreturn]] void ThrowStdUnderflowError(const char* what_arg);
|
| 60 |
+
|
| 61 |
+
[[noreturn]] void ThrowStdBadFunctionCall();
|
| 62 |
+
[[noreturn]] void ThrowStdBadAlloc();
|
| 63 |
+
|
| 64 |
+
// ThrowStdBadArrayNewLength() cannot be consistently supported because
|
| 65 |
+
// std::bad_array_new_length is missing in libstdc++ until 4.9.0.
|
| 66 |
+
// https://gcc.gnu.org/onlinedocs/gcc-4.8.3/libstdc++/api/a01379_source.html
|
| 67 |
+
// https://gcc.gnu.org/onlinedocs/gcc-4.9.0/libstdc++/api/a01327_source.html
|
| 68 |
+
// libcxx (as of 3.2) and msvc (as of 2015) both have it.
|
| 69 |
+
// [[noreturn]] void ThrowStdBadArrayNewLength();
|
| 70 |
+
|
| 71 |
+
} // namespace base_internal
|
| 72 |
+
ABSL_NAMESPACE_END
|
| 73 |
+
} // namespace absl
|
| 74 |
+
|
| 75 |
+
#endif // ABSL_BASE_INTERNAL_THROW_DELEGATE_H_
|
include/absl/base/internal/tracing.h
ADDED
|
@@ -0,0 +1,81 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
// Copyright 2024 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_BASE_INTERNAL_TRACING_H_
|
| 16 |
+
#define ABSL_BASE_INTERNAL_TRACING_H_
|
| 17 |
+
|
| 18 |
+
#include "absl/base/config.h"
|
| 19 |
+
|
| 20 |
+
namespace absl {
|
| 21 |
+
ABSL_NAMESPACE_BEGIN
|
| 22 |
+
namespace base_internal {
|
| 23 |
+
|
| 24 |
+
// Well known Abseil object types that have causality.
|
| 25 |
+
enum class ObjectKind { kUnknown, kBlockingCounter, kNotification };
|
| 26 |
+
|
| 27 |
+
// `TraceWait` and `TraceContinue` record the start and end of a potentially
|
| 28 |
+
// blocking wait operation on `object`. `object` typically represents a higher
|
| 29 |
+
// level synchronization object such as `absl::Notification`.
|
| 30 |
+
void TraceWait(const void* object, ObjectKind kind);
|
| 31 |
+
void TraceContinue(const void* object, ObjectKind kind);
|
| 32 |
+
|
| 33 |
+
// `TraceSignal` records a signal on `object`.
|
| 34 |
+
void TraceSignal(const void* object, ObjectKind kind);
|
| 35 |
+
|
| 36 |
+
// `TraceObserved` records the non-blocking observation of a signaled object.
|
| 37 |
+
void TraceObserved(const void* object, ObjectKind kind);
|
| 38 |
+
|
| 39 |
+
// ---------------------------------------------------------------------------
|
| 40 |
+
// Weak implementation detail:
|
| 41 |
+
//
|
| 42 |
+
// We define the weak API as extern "C": in some build configurations we pass
|
| 43 |
+
// `--detect-odr-violations` to the gold linker. This causes it to flag weak
|
| 44 |
+
// symbol overrides as ODR violations. Because ODR only applies to C++ and not
|
| 45 |
+
// C, `--detect-odr-violations` ignores symbols not mangled with C++ names.
|
| 46 |
+
// By changing our extension points to be extern "C", we dodge this check.
|
| 47 |
+
// ---------------------------------------------------------------------------
|
| 48 |
+
extern "C" {
|
| 49 |
+
|
| 50 |
+
void ABSL_INTERNAL_C_SYMBOL(AbslInternalTraceWait)(const void* object,
|
| 51 |
+
ObjectKind kind);
|
| 52 |
+
void ABSL_INTERNAL_C_SYMBOL(AbslInternalTraceContinue)(const void* object,
|
| 53 |
+
ObjectKind kind);
|
| 54 |
+
void ABSL_INTERNAL_C_SYMBOL(AbslInternalTraceSignal)(const void* object,
|
| 55 |
+
ObjectKind kind);
|
| 56 |
+
void ABSL_INTERNAL_C_SYMBOL(AbslInternalTraceObserved)(const void* object,
|
| 57 |
+
ObjectKind kind);
|
| 58 |
+
|
| 59 |
+
} // extern "C"
|
| 60 |
+
|
| 61 |
+
inline void TraceWait(const void* object, ObjectKind kind) {
|
| 62 |
+
ABSL_INTERNAL_C_SYMBOL(AbslInternalTraceWait)(object, kind);
|
| 63 |
+
}
|
| 64 |
+
|
| 65 |
+
inline void TraceContinue(const void* object, ObjectKind kind) {
|
| 66 |
+
ABSL_INTERNAL_C_SYMBOL(AbslInternalTraceContinue)(object, kind);
|
| 67 |
+
}
|
| 68 |
+
|
| 69 |
+
inline void TraceSignal(const void* object, ObjectKind kind) {
|
| 70 |
+
ABSL_INTERNAL_C_SYMBOL(AbslInternalTraceSignal)(object, kind);
|
| 71 |
+
}
|
| 72 |
+
|
| 73 |
+
inline void TraceObserved(const void* object, ObjectKind kind) {
|
| 74 |
+
ABSL_INTERNAL_C_SYMBOL(AbslInternalTraceObserved)(object, kind);
|
| 75 |
+
}
|
| 76 |
+
|
| 77 |
+
} // namespace base_internal
|
| 78 |
+
ABSL_NAMESPACE_END
|
| 79 |
+
} // namespace absl
|
| 80 |
+
|
| 81 |
+
#endif // ABSL_BASE_INTERNAL_TRACING_H_
|
include/absl/base/internal/tsan_mutex_interface.h
ADDED
|
@@ -0,0 +1,68 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
// Copyright 2017 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 |
+
// This file is intended solely for spinlock.h.
|
| 16 |
+
// It provides ThreadSanitizer annotations for custom mutexes.
|
| 17 |
+
// See <sanitizer/tsan_interface.h> for meaning of these annotations.
|
| 18 |
+
|
| 19 |
+
#ifndef ABSL_BASE_INTERNAL_TSAN_MUTEX_INTERFACE_H_
|
| 20 |
+
#define ABSL_BASE_INTERNAL_TSAN_MUTEX_INTERFACE_H_
|
| 21 |
+
|
| 22 |
+
#include "absl/base/config.h"
|
| 23 |
+
|
| 24 |
+
// ABSL_INTERNAL_HAVE_TSAN_INTERFACE
|
| 25 |
+
// Macro intended only for internal use.
|
| 26 |
+
//
|
| 27 |
+
// Checks whether LLVM Thread Sanitizer interfaces are available.
|
| 28 |
+
// First made available in LLVM 5.0 (Sep 2017).
|
| 29 |
+
#ifdef ABSL_INTERNAL_HAVE_TSAN_INTERFACE
|
| 30 |
+
#error "ABSL_INTERNAL_HAVE_TSAN_INTERFACE cannot be directly set."
|
| 31 |
+
#endif
|
| 32 |
+
|
| 33 |
+
#if defined(ABSL_HAVE_THREAD_SANITIZER) && defined(__has_include)
|
| 34 |
+
#if __has_include(<sanitizer/tsan_interface.h>)
|
| 35 |
+
#define ABSL_INTERNAL_HAVE_TSAN_INTERFACE 1
|
| 36 |
+
#endif
|
| 37 |
+
#endif
|
| 38 |
+
|
| 39 |
+
#ifdef ABSL_INTERNAL_HAVE_TSAN_INTERFACE
|
| 40 |
+
#include <sanitizer/tsan_interface.h>
|
| 41 |
+
|
| 42 |
+
#define ABSL_TSAN_MUTEX_CREATE __tsan_mutex_create
|
| 43 |
+
#define ABSL_TSAN_MUTEX_DESTROY __tsan_mutex_destroy
|
| 44 |
+
#define ABSL_TSAN_MUTEX_PRE_LOCK __tsan_mutex_pre_lock
|
| 45 |
+
#define ABSL_TSAN_MUTEX_POST_LOCK __tsan_mutex_post_lock
|
| 46 |
+
#define ABSL_TSAN_MUTEX_PRE_UNLOCK __tsan_mutex_pre_unlock
|
| 47 |
+
#define ABSL_TSAN_MUTEX_POST_UNLOCK __tsan_mutex_post_unlock
|
| 48 |
+
#define ABSL_TSAN_MUTEX_PRE_SIGNAL __tsan_mutex_pre_signal
|
| 49 |
+
#define ABSL_TSAN_MUTEX_POST_SIGNAL __tsan_mutex_post_signal
|
| 50 |
+
#define ABSL_TSAN_MUTEX_PRE_DIVERT __tsan_mutex_pre_divert
|
| 51 |
+
#define ABSL_TSAN_MUTEX_POST_DIVERT __tsan_mutex_post_divert
|
| 52 |
+
|
| 53 |
+
#else
|
| 54 |
+
|
| 55 |
+
#define ABSL_TSAN_MUTEX_CREATE(...)
|
| 56 |
+
#define ABSL_TSAN_MUTEX_DESTROY(...)
|
| 57 |
+
#define ABSL_TSAN_MUTEX_PRE_LOCK(...)
|
| 58 |
+
#define ABSL_TSAN_MUTEX_POST_LOCK(...)
|
| 59 |
+
#define ABSL_TSAN_MUTEX_PRE_UNLOCK(...)
|
| 60 |
+
#define ABSL_TSAN_MUTEX_POST_UNLOCK(...)
|
| 61 |
+
#define ABSL_TSAN_MUTEX_PRE_SIGNAL(...)
|
| 62 |
+
#define ABSL_TSAN_MUTEX_POST_SIGNAL(...)
|
| 63 |
+
#define ABSL_TSAN_MUTEX_PRE_DIVERT(...)
|
| 64 |
+
#define ABSL_TSAN_MUTEX_POST_DIVERT(...)
|
| 65 |
+
|
| 66 |
+
#endif
|
| 67 |
+
|
| 68 |
+
#endif // ABSL_BASE_INTERNAL_TSAN_MUTEX_INTERFACE_H_
|
include/absl/base/internal/unaligned_access.h
ADDED
|
@@ -0,0 +1,89 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
//
|
| 2 |
+
// Copyright 2017 The Abseil Authors.
|
| 3 |
+
//
|
| 4 |
+
// Licensed under the Apache License, Version 2.0 (the "License");
|
| 5 |
+
// you may not use this file except in compliance with the License.
|
| 6 |
+
// You may obtain a copy of the License at
|
| 7 |
+
//
|
| 8 |
+
// https://www.apache.org/licenses/LICENSE-2.0
|
| 9 |
+
//
|
| 10 |
+
// Unless required by applicable law or agreed to in writing, software
|
| 11 |
+
// distributed under the License is distributed on an "AS IS" BASIS,
|
| 12 |
+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
| 13 |
+
// See the License for the specific language governing permissions and
|
| 14 |
+
// limitations under the License.
|
| 15 |
+
//
|
| 16 |
+
|
| 17 |
+
#ifndef ABSL_BASE_INTERNAL_UNALIGNED_ACCESS_H_
|
| 18 |
+
#define ABSL_BASE_INTERNAL_UNALIGNED_ACCESS_H_
|
| 19 |
+
|
| 20 |
+
#include <string.h>
|
| 21 |
+
|
| 22 |
+
#include <cstdint>
|
| 23 |
+
|
| 24 |
+
#include "absl/base/attributes.h"
|
| 25 |
+
#include "absl/base/config.h"
|
| 26 |
+
#include "absl/base/nullability.h"
|
| 27 |
+
|
| 28 |
+
// unaligned APIs
|
| 29 |
+
|
| 30 |
+
// Portable handling of unaligned loads, stores, and copies.
|
| 31 |
+
|
| 32 |
+
// The unaligned API is C++ only. The declarations use C++ features
|
| 33 |
+
// (namespaces, inline) which are absent or incompatible in C.
|
| 34 |
+
#if defined(__cplusplus)
|
| 35 |
+
namespace absl {
|
| 36 |
+
ABSL_NAMESPACE_BEGIN
|
| 37 |
+
namespace base_internal {
|
| 38 |
+
|
| 39 |
+
inline uint16_t UnalignedLoad16(absl::Nonnull<const void *> p) {
|
| 40 |
+
uint16_t t;
|
| 41 |
+
memcpy(&t, p, sizeof t);
|
| 42 |
+
return t;
|
| 43 |
+
}
|
| 44 |
+
|
| 45 |
+
inline uint32_t UnalignedLoad32(absl::Nonnull<const void *> p) {
|
| 46 |
+
uint32_t t;
|
| 47 |
+
memcpy(&t, p, sizeof t);
|
| 48 |
+
return t;
|
| 49 |
+
}
|
| 50 |
+
|
| 51 |
+
inline uint64_t UnalignedLoad64(absl::Nonnull<const void *> p) {
|
| 52 |
+
uint64_t t;
|
| 53 |
+
memcpy(&t, p, sizeof t);
|
| 54 |
+
return t;
|
| 55 |
+
}
|
| 56 |
+
|
| 57 |
+
inline void UnalignedStore16(absl::Nonnull<void *> p, uint16_t v) {
|
| 58 |
+
memcpy(p, &v, sizeof v);
|
| 59 |
+
}
|
| 60 |
+
|
| 61 |
+
inline void UnalignedStore32(absl::Nonnull<void *> p, uint32_t v) {
|
| 62 |
+
memcpy(p, &v, sizeof v);
|
| 63 |
+
}
|
| 64 |
+
|
| 65 |
+
inline void UnalignedStore64(absl::Nonnull<void *> p, uint64_t v) {
|
| 66 |
+
memcpy(p, &v, sizeof v);
|
| 67 |
+
}
|
| 68 |
+
|
| 69 |
+
} // namespace base_internal
|
| 70 |
+
ABSL_NAMESPACE_END
|
| 71 |
+
} // namespace absl
|
| 72 |
+
|
| 73 |
+
#define ABSL_INTERNAL_UNALIGNED_LOAD16(_p) \
|
| 74 |
+
(absl::base_internal::UnalignedLoad16(_p))
|
| 75 |
+
#define ABSL_INTERNAL_UNALIGNED_LOAD32(_p) \
|
| 76 |
+
(absl::base_internal::UnalignedLoad32(_p))
|
| 77 |
+
#define ABSL_INTERNAL_UNALIGNED_LOAD64(_p) \
|
| 78 |
+
(absl::base_internal::UnalignedLoad64(_p))
|
| 79 |
+
|
| 80 |
+
#define ABSL_INTERNAL_UNALIGNED_STORE16(_p, _val) \
|
| 81 |
+
(absl::base_internal::UnalignedStore16(_p, _val))
|
| 82 |
+
#define ABSL_INTERNAL_UNALIGNED_STORE32(_p, _val) \
|
| 83 |
+
(absl::base_internal::UnalignedStore32(_p, _val))
|
| 84 |
+
#define ABSL_INTERNAL_UNALIGNED_STORE64(_p, _val) \
|
| 85 |
+
(absl::base_internal::UnalignedStore64(_p, _val))
|
| 86 |
+
|
| 87 |
+
#endif // defined(__cplusplus), end of unaligned API
|
| 88 |
+
|
| 89 |
+
#endif // ABSL_BASE_INTERNAL_UNALIGNED_ACCESS_H_
|
include/absl/base/internal/unscaledcycleclock.h
ADDED
|
@@ -0,0 +1,108 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
// Copyright 2017 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 |
+
// UnscaledCycleClock
|
| 16 |
+
// An UnscaledCycleClock yields the value and frequency of a cycle counter
|
| 17 |
+
// that increments at a rate that is approximately constant.
|
| 18 |
+
// This class is for internal use only, you should consider using CycleClock
|
| 19 |
+
// instead.
|
| 20 |
+
//
|
| 21 |
+
// Notes:
|
| 22 |
+
// The cycle counter frequency is not necessarily the core clock frequency.
|
| 23 |
+
// That is, CycleCounter cycles are not necessarily "CPU cycles".
|
| 24 |
+
//
|
| 25 |
+
// An arbitrary offset may have been added to the counter at power on.
|
| 26 |
+
//
|
| 27 |
+
// On some platforms, the rate and offset of the counter may differ
|
| 28 |
+
// slightly when read from different CPUs of a multiprocessor. Usually,
|
| 29 |
+
// we try to ensure that the operating system adjusts values periodically
|
| 30 |
+
// so that values agree approximately. If you need stronger guarantees,
|
| 31 |
+
// consider using alternate interfaces.
|
| 32 |
+
//
|
| 33 |
+
// The CPU is not required to maintain the ordering of a cycle counter read
|
| 34 |
+
// with respect to surrounding instructions.
|
| 35 |
+
|
| 36 |
+
#ifndef ABSL_BASE_INTERNAL_UNSCALEDCYCLECLOCK_H_
|
| 37 |
+
#define ABSL_BASE_INTERNAL_UNSCALEDCYCLECLOCK_H_
|
| 38 |
+
|
| 39 |
+
#include <cstdint>
|
| 40 |
+
|
| 41 |
+
#if defined(__APPLE__)
|
| 42 |
+
#include <TargetConditionals.h>
|
| 43 |
+
#endif
|
| 44 |
+
|
| 45 |
+
#include "absl/base/config.h"
|
| 46 |
+
#include "absl/base/internal/unscaledcycleclock_config.h"
|
| 47 |
+
|
| 48 |
+
#if ABSL_USE_UNSCALED_CYCLECLOCK
|
| 49 |
+
|
| 50 |
+
namespace absl {
|
| 51 |
+
ABSL_NAMESPACE_BEGIN
|
| 52 |
+
namespace time_internal {
|
| 53 |
+
class UnscaledCycleClockWrapperForGetCurrentTime;
|
| 54 |
+
} // namespace time_internal
|
| 55 |
+
|
| 56 |
+
namespace base_internal {
|
| 57 |
+
class CycleClock;
|
| 58 |
+
class UnscaledCycleClockWrapperForInitializeFrequency;
|
| 59 |
+
|
| 60 |
+
class UnscaledCycleClock {
|
| 61 |
+
private:
|
| 62 |
+
UnscaledCycleClock() = delete;
|
| 63 |
+
|
| 64 |
+
// Return the value of a cycle counter that counts at a rate that is
|
| 65 |
+
// approximately constant.
|
| 66 |
+
static int64_t Now();
|
| 67 |
+
|
| 68 |
+
// Return the how much UnscaledCycleClock::Now() increases per second.
|
| 69 |
+
// This is not necessarily the core CPU clock frequency.
|
| 70 |
+
// It may be the nominal value report by the kernel, rather than a measured
|
| 71 |
+
// value.
|
| 72 |
+
static double Frequency();
|
| 73 |
+
|
| 74 |
+
// Allowed users
|
| 75 |
+
friend class base_internal::CycleClock;
|
| 76 |
+
friend class time_internal::UnscaledCycleClockWrapperForGetCurrentTime;
|
| 77 |
+
friend class base_internal::UnscaledCycleClockWrapperForInitializeFrequency;
|
| 78 |
+
};
|
| 79 |
+
|
| 80 |
+
#if defined(__x86_64__)
|
| 81 |
+
|
| 82 |
+
inline int64_t UnscaledCycleClock::Now() {
|
| 83 |
+
uint64_t low, high;
|
| 84 |
+
__asm__ volatile("rdtsc" : "=a"(low), "=d"(high));
|
| 85 |
+
return static_cast<int64_t>((high << 32) | low);
|
| 86 |
+
}
|
| 87 |
+
|
| 88 |
+
#elif defined(__aarch64__)
|
| 89 |
+
|
| 90 |
+
// System timer of ARMv8 runs at a different frequency than the CPU's.
|
| 91 |
+
// The frequency is fixed, typically in the range 1-50MHz. It can be
|
| 92 |
+
// read at CNTFRQ special register. We assume the OS has set up
|
| 93 |
+
// the virtual timer properly.
|
| 94 |
+
inline int64_t UnscaledCycleClock::Now() {
|
| 95 |
+
int64_t virtual_timer_value;
|
| 96 |
+
asm volatile("mrs %0, cntvct_el0" : "=r"(virtual_timer_value));
|
| 97 |
+
return virtual_timer_value;
|
| 98 |
+
}
|
| 99 |
+
|
| 100 |
+
#endif
|
| 101 |
+
|
| 102 |
+
} // namespace base_internal
|
| 103 |
+
ABSL_NAMESPACE_END
|
| 104 |
+
} // namespace absl
|
| 105 |
+
|
| 106 |
+
#endif // ABSL_USE_UNSCALED_CYCLECLOCK
|
| 107 |
+
|
| 108 |
+
#endif // ABSL_BASE_INTERNAL_UNSCALEDCYCLECLOCK_H_
|
include/absl/base/internal/unscaledcycleclock_config.h
ADDED
|
@@ -0,0 +1,62 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
// Copyright 2022 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_BASE_INTERNAL_UNSCALEDCYCLECLOCK_CONFIG_H_
|
| 16 |
+
#define ABSL_BASE_INTERNAL_UNSCALEDCYCLECLOCK_CONFIG_H_
|
| 17 |
+
|
| 18 |
+
#if defined(__APPLE__)
|
| 19 |
+
#include <TargetConditionals.h>
|
| 20 |
+
#endif
|
| 21 |
+
|
| 22 |
+
// The following platforms have an implementation of a hardware counter.
|
| 23 |
+
#if defined(__i386__) || defined(__x86_64__) || defined(__aarch64__) || \
|
| 24 |
+
defined(__powerpc__) || defined(__ppc__) || defined(_M_IX86) || \
|
| 25 |
+
(defined(_M_X64) && !defined(_M_ARM64EC))
|
| 26 |
+
#define ABSL_HAVE_UNSCALED_CYCLECLOCK_IMPLEMENTATION 1
|
| 27 |
+
#else
|
| 28 |
+
#define ABSL_HAVE_UNSCALED_CYCLECLOCK_IMPLEMENTATION 0
|
| 29 |
+
#endif
|
| 30 |
+
|
| 31 |
+
// The following platforms often disable access to the hardware
|
| 32 |
+
// counter (through a sandbox) even if the underlying hardware has a
|
| 33 |
+
// usable counter. The CycleTimer interface also requires a *scaled*
|
| 34 |
+
// CycleClock that runs at atleast 1 MHz. We've found some Android
|
| 35 |
+
// ARM64 devices where this is not the case, so we disable it by
|
| 36 |
+
// default on Android ARM64.
|
| 37 |
+
#if defined(__native_client__) || (defined(__APPLE__)) || \
|
| 38 |
+
(defined(__ANDROID__) && defined(__aarch64__))
|
| 39 |
+
#define ABSL_USE_UNSCALED_CYCLECLOCK_DEFAULT 0
|
| 40 |
+
#else
|
| 41 |
+
#define ABSL_USE_UNSCALED_CYCLECLOCK_DEFAULT 1
|
| 42 |
+
#endif
|
| 43 |
+
|
| 44 |
+
// UnscaledCycleClock is an optional internal feature.
|
| 45 |
+
// Use "#if ABSL_USE_UNSCALED_CYCLECLOCK" to test for its presence.
|
| 46 |
+
// Can be overridden at compile-time via -DABSL_USE_UNSCALED_CYCLECLOCK=0|1
|
| 47 |
+
#if !defined(ABSL_USE_UNSCALED_CYCLECLOCK)
|
| 48 |
+
#define ABSL_USE_UNSCALED_CYCLECLOCK \
|
| 49 |
+
(ABSL_HAVE_UNSCALED_CYCLECLOCK_IMPLEMENTATION && \
|
| 50 |
+
ABSL_USE_UNSCALED_CYCLECLOCK_DEFAULT)
|
| 51 |
+
#endif
|
| 52 |
+
|
| 53 |
+
#if ABSL_USE_UNSCALED_CYCLECLOCK
|
| 54 |
+
// This macro can be used to test if UnscaledCycleClock::Frequency()
|
| 55 |
+
// is NominalCPUFrequency() on a particular platform.
|
| 56 |
+
#if (defined(__i386__) || defined(__x86_64__) || defined(_M_IX86) || \
|
| 57 |
+
defined(_M_X64))
|
| 58 |
+
#define ABSL_INTERNAL_UNSCALED_CYCLECLOCK_FREQUENCY_IS_CPU_FREQUENCY
|
| 59 |
+
#endif
|
| 60 |
+
#endif
|
| 61 |
+
|
| 62 |
+
#endif // ABSL_BASE_INTERNAL_UNSCALEDCYCLECLO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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
// Copyright 2017 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_BASE_LOG_SEVERITY_H_
|
| 16 |
+
#define ABSL_BASE_LOG_SEVERITY_H_
|
| 17 |
+
|
| 18 |
+
#include <array>
|
| 19 |
+
#include <ostream>
|
| 20 |
+
|
| 21 |
+
#include "absl/base/attributes.h"
|
| 22 |
+
#include "absl/base/config.h"
|
| 23 |
+
|
| 24 |
+
namespace absl {
|
| 25 |
+
ABSL_NAMESPACE_BEGIN
|
| 26 |
+
|
| 27 |
+
// absl::LogSeverity
|
| 28 |
+
//
|
| 29 |
+
// Four severity levels are defined. Logging APIs should terminate the program
|
| 30 |
+
// when a message is logged at severity `kFatal`; the other levels have no
|
| 31 |
+
// special semantics.
|
| 32 |
+
//
|
| 33 |
+
// Values other than the four defined levels (e.g. produced by `static_cast`)
|
| 34 |
+
// are valid, but their semantics when passed to a function, macro, or flag
|
| 35 |
+
// depend on the function, macro, or flag. The usual behavior is to normalize
|
| 36 |
+
// such values to a defined severity level, however in some cases values other
|
| 37 |
+
// than the defined levels are useful for comparison.
|
| 38 |
+
//
|
| 39 |
+
// Example:
|
| 40 |
+
//
|
| 41 |
+
// // Effectively disables all logging:
|
| 42 |
+
// SetMinLogLevel(static_cast<absl::LogSeverity>(100));
|
| 43 |
+
//
|
| 44 |
+
// Abseil flags may be defined with type `LogSeverity`. Dependency layering
|
| 45 |
+
// constraints require that the `AbslParseFlag()` overload be declared and
|
| 46 |
+
// defined in the flags library itself rather than here. The `AbslUnparseFlag()`
|
| 47 |
+
// overload is defined there as well for consistency.
|
| 48 |
+
//
|
| 49 |
+
// absl::LogSeverity Flag String Representation
|
| 50 |
+
//
|
| 51 |
+
// An `absl::LogSeverity` has a string representation used for parsing
|
| 52 |
+
// command-line flags based on the enumerator name (e.g. `kFatal`) or
|
| 53 |
+
// its unprefixed name (without the `k`) in any case-insensitive form. (E.g.
|
| 54 |
+
// "FATAL", "fatal" or "Fatal" are all valid.) Unparsing such flags produces an
|
| 55 |
+
// unprefixed string representation in all caps (e.g. "FATAL") or an integer.
|
| 56 |
+
//
|
| 57 |
+
// Additionally, the parser accepts arbitrary integers (as if the type were
|
| 58 |
+
// `int`).
|
| 59 |
+
//
|
| 60 |
+
// Examples:
|
| 61 |
+
//
|
| 62 |
+
// --my_log_level=kInfo
|
| 63 |
+
// --my_log_level=INFO
|
| 64 |
+
// --my_log_level=info
|
| 65 |
+
// --my_log_level=0
|
| 66 |
+
//
|
| 67 |
+
// `DFATAL` and `kLogDebugFatal` are similarly accepted.
|
| 68 |
+
//
|
| 69 |
+
// Unparsing a flag produces the same result as `absl::LogSeverityName()` for
|
| 70 |
+
// the standard levels and a base-ten integer otherwise.
|
| 71 |
+
enum class LogSeverity : int {
|
| 72 |
+
kInfo = 0,
|
| 73 |
+
kWarning = 1,
|
| 74 |
+
kError = 2,
|
| 75 |
+
kFatal = 3,
|
| 76 |
+
};
|
| 77 |
+
|
| 78 |
+
// LogSeverities()
|
| 79 |
+
//
|
| 80 |
+
// Returns an iterable of all standard `absl::LogSeverity` values, ordered from
|
| 81 |
+
// least to most severe.
|
| 82 |
+
constexpr std::array<absl::LogSeverity, 4> LogSeverities() {
|
| 83 |
+
return {{absl::LogSeverity::kInfo, absl::LogSeverity::kWarning,
|
| 84 |
+
absl::LogSeverity::kError, absl::LogSeverity::kFatal}};
|
| 85 |
+
}
|
| 86 |
+
|
| 87 |
+
// `absl::kLogDebugFatal` equals `absl::LogSeverity::kFatal` in debug builds
|
| 88 |
+
// (i.e. when `NDEBUG` is not defined) and `absl::LogSeverity::kError`
|
| 89 |
+
// otherwise. Avoid ODR-using this variable as it has internal linkage and thus
|
| 90 |
+
// distinct storage in different TUs.
|
| 91 |
+
#ifdef NDEBUG
|
| 92 |
+
static constexpr absl::LogSeverity kLogDebugFatal = absl::LogSeverity::kError;
|
| 93 |
+
#else
|
| 94 |
+
static constexpr absl::LogSeverity kLogDebugFatal = absl::LogSeverity::kFatal;
|
| 95 |
+
#endif
|
| 96 |
+
|
| 97 |
+
// LogSeverityName()
|
| 98 |
+
//
|
| 99 |
+
// Returns the all-caps string representation (e.g. "INFO") of the specified
|
| 100 |
+
// severity level if it is one of the standard levels and "UNKNOWN" otherwise.
|
| 101 |
+
constexpr const char* LogSeverityName(absl::LogSeverity s) {
|
| 102 |
+
switch (s) {
|
| 103 |
+
case absl::LogSeverity::kInfo: return "INFO";
|
| 104 |
+
case absl::LogSeverity::kWarning: return "WARNING";
|
| 105 |
+
case absl::LogSeverity::kError: return "ERROR";
|
| 106 |
+
case absl::LogSeverity::kFatal: return "FATAL";
|
| 107 |
+
}
|
| 108 |
+
return "UNKNOWN";
|
| 109 |
+
}
|
| 110 |
+
|
| 111 |
+
// NormalizeLogSeverity()
|
| 112 |
+
//
|
| 113 |
+
// Values less than `kInfo` normalize to `kInfo`; values greater than `kFatal`
|
| 114 |
+
// normalize to `kError` (**NOT** `kFatal`).
|
| 115 |
+
constexpr absl::LogSeverity NormalizeLogSeverity(absl::LogSeverity s) {
|
| 116 |
+
absl::LogSeverity n = s;
|
| 117 |
+
if (n < absl::LogSeverity::kInfo) n = absl::LogSeverity::kInfo;
|
| 118 |
+
if (n > absl::LogSeverity::kFatal) n = absl::LogSeverity::kError;
|
| 119 |
+
return n;
|
| 120 |
+
}
|
| 121 |
+
constexpr absl::LogSeverity NormalizeLogSeverity(int s) {
|
| 122 |
+
return absl::NormalizeLogSeverity(static_cast<absl::LogSeverity>(s));
|
| 123 |
+
}
|
| 124 |
+
|
| 125 |
+
// operator<<
|
| 126 |
+
//
|
| 127 |
+
// The exact representation of a streamed `absl::LogSeverity` is deliberately
|
| 128 |
+
// unspecified; do not rely on it.
|
| 129 |
+
std::ostream& operator<<(std::ostream& os, absl::LogSeverity s);
|
| 130 |
+
|
| 131 |
+
// Enums representing a lower bound for LogSeverity. APIs that only operate on
|
| 132 |
+
// messages of at least a certain level (for example, `SetMinLogLevel()`) use
|
| 133 |
+
// this type to specify that level. absl::LogSeverityAtLeast::kInfinity is
|
| 134 |
+
// a level above all threshold levels and therefore no log message will
|
| 135 |
+
// ever meet this threshold.
|
| 136 |
+
enum class LogSeverityAtLeast : int {
|
| 137 |
+
kInfo = static_cast<int>(absl::LogSeverity::kInfo),
|
| 138 |
+
kWarning = static_cast<int>(absl::LogSeverity::kWarning),
|
| 139 |
+
kError = static_cast<int>(absl::LogSeverity::kError),
|
| 140 |
+
kFatal = static_cast<int>(absl::LogSeverity::kFatal),
|
| 141 |
+
kInfinity = 1000,
|
| 142 |
+
};
|
| 143 |
+
|
| 144 |
+
std::ostream& operator<<(std::ostream& os, absl::LogSeverityAtLeast s);
|
| 145 |
+
|
| 146 |
+
// Enums representing an upper bound for LogSeverity. APIs that only operate on
|
| 147 |
+
// messages of at most a certain level (for example, buffer all messages at or
|
| 148 |
+
// below a certain level) use this type to specify that level.
|
| 149 |
+
// absl::LogSeverityAtMost::kNegativeInfinity is a level below all threshold
|
| 150 |
+
// levels and therefore will exclude all log messages.
|
| 151 |
+
enum class LogSeverityAtMost : int {
|
| 152 |
+
kNegativeInfinity = -1000,
|
| 153 |
+
kInfo = static_cast<int>(absl::LogSeverity::kInfo),
|
| 154 |
+
kWarning = static_cast<int>(absl::LogSeverity::kWarning),
|
| 155 |
+
kError = static_cast<int>(absl::LogSeverity::kError),
|
| 156 |
+
kFatal = static_cast<int>(absl::LogSeverity::kFatal),
|
| 157 |
+
};
|
| 158 |
+
|
| 159 |
+
std::ostream& operator<<(std::ostream& os, absl::LogSeverityAtMost s);
|
| 160 |
+
|
| 161 |
+
#define COMPOP(op1, op2, T) \
|
| 162 |
+
constexpr bool operator op1(absl::T lhs, absl::LogSeverity rhs) { \
|
| 163 |
+
return static_cast<absl::LogSeverity>(lhs) op1 rhs; \
|
| 164 |
+
} \
|
| 165 |
+
constexpr bool operator op2(absl::LogSeverity lhs, absl::T rhs) { \
|
| 166 |
+
return lhs op2 static_cast<absl::LogSeverity>(rhs); \
|
| 167 |
+
}
|
| 168 |
+
|
| 169 |
+
// Comparisons between `LogSeverity` and `LogSeverityAtLeast`/
|
| 170 |
+
// `LogSeverityAtMost` are only supported in one direction.
|
| 171 |
+
// Valid checks are:
|
| 172 |
+
// LogSeverity >= LogSeverityAtLeast
|
| 173 |
+
// LogSeverity < LogSeverityAtLeast
|
| 174 |
+
// LogSeverity <= LogSeverityAtMost
|
| 175 |
+
// LogSeverity > LogSeverityAtMost
|
| 176 |
+
COMPOP(>, <, LogSeverityAtLeast)
|
| 177 |
+
COMPOP(<=, >=, LogSeverityAtLeast)
|
| 178 |
+
COMPOP(<, >, LogSeverityAtMost)
|
| 179 |
+
COMPOP(>=, <=, LogSeverityAtMost)
|
| 180 |
+
#undef COMPOP
|
| 181 |
+
|
| 182 |
+
ABSL_NAMESPACE_END
|
| 183 |
+
} // namespace absl
|
| 184 |
+
|
| 185 |
+
#endif // ABSL_BASE_LOG_SEVERITY_H_
|
include/absl/base/macros.h
ADDED
|
@@ -0,0 +1,220 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
//
|
| 2 |
+
// Copyright 2017 The Abseil Authors.
|
| 3 |
+
//
|
| 4 |
+
// Licensed under the Apache License, Version 2.0 (the "License");
|
| 5 |
+
// you may not use this file except in compliance with the License.
|
| 6 |
+
// You may obtain a copy of the License at
|
| 7 |
+
//
|
| 8 |
+
// https://www.apache.org/licenses/LICENSE-2.0
|
| 9 |
+
//
|
| 10 |
+
// Unless required by applicable law or agreed to in writing, software
|
| 11 |
+
// distributed under the License is distributed on an "AS IS" BASIS,
|
| 12 |
+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
| 13 |
+
// See the License for the specific language governing permissions and
|
| 14 |
+
// limitations under the License.
|
| 15 |
+
//
|
| 16 |
+
// -----------------------------------------------------------------------------
|
| 17 |
+
// File: macros.h
|
| 18 |
+
// -----------------------------------------------------------------------------
|
| 19 |
+
//
|
| 20 |
+
// This header file defines the set of language macros used within Abseil code.
|
| 21 |
+
// For the set of macros used to determine supported compilers and platforms,
|
| 22 |
+
// see absl/base/config.h instead.
|
| 23 |
+
//
|
| 24 |
+
// This code is compiled directly on many platforms, including client
|
| 25 |
+
// platforms like Windows, Mac, and embedded systems. Before making
|
| 26 |
+
// any changes here, make sure that you're not breaking any platforms.
|
| 27 |
+
|
| 28 |
+
#ifndef ABSL_BASE_MACROS_H_
|
| 29 |
+
#define ABSL_BASE_MACROS_H_
|
| 30 |
+
|
| 31 |
+
#include <cassert>
|
| 32 |
+
#include <cstddef>
|
| 33 |
+
|
| 34 |
+
#include "absl/base/attributes.h"
|
| 35 |
+
#include "absl/base/config.h"
|
| 36 |
+
#include "absl/base/optimization.h"
|
| 37 |
+
#include "absl/base/options.h"
|
| 38 |
+
#include "absl/base/port.h"
|
| 39 |
+
|
| 40 |
+
// ABSL_ARRAYSIZE()
|
| 41 |
+
//
|
| 42 |
+
// Returns the number of elements in an array as a compile-time constant, which
|
| 43 |
+
// can be used in defining new arrays. If you use this macro on a pointer by
|
| 44 |
+
// mistake, you will get a compile-time error.
|
| 45 |
+
#define ABSL_ARRAYSIZE(array) \
|
| 46 |
+
(sizeof(::absl::macros_internal::ArraySizeHelper(array)))
|
| 47 |
+
|
| 48 |
+
namespace absl {
|
| 49 |
+
ABSL_NAMESPACE_BEGIN
|
| 50 |
+
namespace macros_internal {
|
| 51 |
+
// Note: this internal template function declaration is used by ABSL_ARRAYSIZE.
|
| 52 |
+
// The function doesn't need a definition, as we only use its type.
|
| 53 |
+
template <typename T, size_t N>
|
| 54 |
+
auto ArraySizeHelper(const T (&array)[N]) -> char (&)[N];
|
| 55 |
+
} // namespace macros_internal
|
| 56 |
+
ABSL_NAMESPACE_END
|
| 57 |
+
} // namespace absl
|
| 58 |
+
|
| 59 |
+
// ABSL_BAD_CALL_IF()
|
| 60 |
+
//
|
| 61 |
+
// Used on a function overload to trap bad calls: any call that matches the
|
| 62 |
+
// overload will cause a compile-time error. This macro uses a clang-specific
|
| 63 |
+
// "enable_if" attribute, as described at
|
| 64 |
+
// https://clang.llvm.org/docs/AttributeReference.html#enable-if
|
| 65 |
+
//
|
| 66 |
+
// Overloads which use this macro should be bracketed by
|
| 67 |
+
// `#ifdef ABSL_BAD_CALL_IF`.
|
| 68 |
+
//
|
| 69 |
+
// Example:
|
| 70 |
+
//
|
| 71 |
+
// int isdigit(int c);
|
| 72 |
+
// #ifdef ABSL_BAD_CALL_IF
|
| 73 |
+
// int isdigit(int c)
|
| 74 |
+
// ABSL_BAD_CALL_IF(c <= -1 || c > 255,
|
| 75 |
+
// "'c' must have the value of an unsigned char or EOF");
|
| 76 |
+
// #endif // ABSL_BAD_CALL_IF
|
| 77 |
+
#if ABSL_HAVE_ATTRIBUTE(enable_if)
|
| 78 |
+
#define ABSL_BAD_CALL_IF(expr, msg) \
|
| 79 |
+
__attribute__((enable_if(expr, "Bad call trap"), unavailable(msg)))
|
| 80 |
+
#endif
|
| 81 |
+
|
| 82 |
+
// ABSL_ASSERT()
|
| 83 |
+
//
|
| 84 |
+
// In C++11, `assert` can't be used portably within constexpr functions.
|
| 85 |
+
// `assert` also generates spurious unused-symbol warnings.
|
| 86 |
+
// ABSL_ASSERT functions as a runtime assert but works in C++11 constexpr
|
| 87 |
+
// functions, and maintains references to symbols. Example:
|
| 88 |
+
//
|
| 89 |
+
// constexpr double Divide(double a, double b) {
|
| 90 |
+
// return ABSL_ASSERT(b != 0), a / b;
|
| 91 |
+
// }
|
| 92 |
+
//
|
| 93 |
+
// This macro is inspired by
|
| 94 |
+
// https://akrzemi1.wordpress.com/2017/05/18/asserts-in-constexpr-functions/
|
| 95 |
+
#if defined(NDEBUG)
|
| 96 |
+
#if ABSL_INTERNAL_CPLUSPLUS_LANG >= 202002L
|
| 97 |
+
// We use `decltype` here to avoid generating unnecessary code that the
|
| 98 |
+
// optimizer then has to optimize away.
|
| 99 |
+
// This not only improves compilation performance by reducing codegen bloat
|
| 100 |
+
// and optimization work, but also guarantees fast run-time performance without
|
| 101 |
+
// having to rely on the optimizer.
|
| 102 |
+
#define ABSL_ASSERT(expr) (decltype((expr) ? void() : void())())
|
| 103 |
+
#else
|
| 104 |
+
// Pre-C++20, lambdas can't be inside unevaluated operands, so we're forced to
|
| 105 |
+
// rely on the optimizer.
|
| 106 |
+
#define ABSL_ASSERT(expr) (false ? ((expr) ? void() : void()) : void())
|
| 107 |
+
#endif
|
| 108 |
+
#else
|
| 109 |
+
#define ABSL_ASSERT(expr) \
|
| 110 |
+
(ABSL_PREDICT_TRUE((expr)) ? static_cast<void>(0) \
|
| 111 |
+
: [] { assert(false && #expr); }()) // NOLINT
|
| 112 |
+
#endif
|
| 113 |
+
|
| 114 |
+
// `ABSL_INTERNAL_HARDENING_ABORT()` controls how `ABSL_HARDENING_ASSERT()`
|
| 115 |
+
// aborts the program in release mode (when NDEBUG is defined). The
|
| 116 |
+
// implementation should abort the program as quickly as possible and ideally it
|
| 117 |
+
// should not be possible to ignore the abort request.
|
| 118 |
+
#define ABSL_INTERNAL_HARDENING_ABORT() \
|
| 119 |
+
do { \
|
| 120 |
+
ABSL_INTERNAL_IMMEDIATE_ABORT_IMPL(); \
|
| 121 |
+
ABSL_INTERNAL_UNREACHABLE_IMPL(); \
|
| 122 |
+
} while (false)
|
| 123 |
+
|
| 124 |
+
// ABSL_HARDENING_ASSERT()
|
| 125 |
+
//
|
| 126 |
+
// `ABSL_HARDENING_ASSERT()` is like `ABSL_ASSERT()`, but used to implement
|
| 127 |
+
// runtime assertions that should be enabled in hardened builds even when
|
| 128 |
+
// `NDEBUG` is defined.
|
| 129 |
+
//
|
| 130 |
+
// When `NDEBUG` is not defined, `ABSL_HARDENING_ASSERT()` is identical to
|
| 131 |
+
// `ABSL_ASSERT()`.
|
| 132 |
+
//
|
| 133 |
+
// See `ABSL_OPTION_HARDENED` in `absl/base/options.h` for more information on
|
| 134 |
+
// hardened mode.
|
| 135 |
+
#if (ABSL_OPTION_HARDENED == 1 || ABSL_OPTION_HARDENED == 2) && defined(NDEBUG)
|
| 136 |
+
#define ABSL_HARDENING_ASSERT(expr) \
|
| 137 |
+
(ABSL_PREDICT_TRUE((expr)) ? static_cast<void>(0) \
|
| 138 |
+
: [] { ABSL_INTERNAL_HARDENING_ABORT(); }())
|
| 139 |
+
#else
|
| 140 |
+
#define ABSL_HARDENING_ASSERT(expr) ABSL_ASSERT(expr)
|
| 141 |
+
#endif
|
| 142 |
+
|
| 143 |
+
// ABSL_HARDENING_ASSERT_SLOW()
|
| 144 |
+
//
|
| 145 |
+
// `ABSL_HARDENING_ASSERT()` is like `ABSL_HARDENING_ASSERT()`,
|
| 146 |
+
// but specifically for assertions whose predicates are too slow
|
| 147 |
+
// to be enabled in many applications.
|
| 148 |
+
//
|
| 149 |
+
// When `NDEBUG` is not defined, `ABSL_HARDENING_ASSERT_SLOW()` is identical to
|
| 150 |
+
// `ABSL_ASSERT()`.
|
| 151 |
+
//
|
| 152 |
+
// See `ABSL_OPTION_HARDENED` in `absl/base/options.h` for more information on
|
| 153 |
+
// hardened mode.
|
| 154 |
+
#if ABSL_OPTION_HARDENED == 1 && defined(NDEBUG)
|
| 155 |
+
#define ABSL_HARDENING_ASSERT_SLOW(expr) \
|
| 156 |
+
(ABSL_PREDICT_TRUE((expr)) ? static_cast<void>(0) \
|
| 157 |
+
: [] { ABSL_INTERNAL_HARDENING_ABORT(); }())
|
| 158 |
+
#else
|
| 159 |
+
#define ABSL_HARDENING_ASSERT_SLOW(expr) ABSL_ASSERT(expr)
|
| 160 |
+
#endif
|
| 161 |
+
|
| 162 |
+
#ifdef ABSL_HAVE_EXCEPTIONS
|
| 163 |
+
#define ABSL_INTERNAL_TRY try
|
| 164 |
+
#define ABSL_INTERNAL_CATCH_ANY catch (...)
|
| 165 |
+
#define ABSL_INTERNAL_RETHROW do { throw; } while (false)
|
| 166 |
+
#else // ABSL_HAVE_EXCEPTIONS
|
| 167 |
+
#define ABSL_INTERNAL_TRY if (true)
|
| 168 |
+
#define ABSL_INTERNAL_CATCH_ANY else if (false)
|
| 169 |
+
#define ABSL_INTERNAL_RETHROW do {} while (false)
|
| 170 |
+
#endif // ABSL_HAVE_EXCEPTIONS
|
| 171 |
+
|
| 172 |
+
// ABSL_DEPRECATE_AND_INLINE()
|
| 173 |
+
//
|
| 174 |
+
// Marks a function or type alias as deprecated and tags it to be picked up for
|
| 175 |
+
// automated refactoring by go/cpp-inliner. It can added to inline function
|
| 176 |
+
// definitions or type aliases. It should only be used within a header file. It
|
| 177 |
+
// differs from `ABSL_DEPRECATED` in the following ways:
|
| 178 |
+
//
|
| 179 |
+
// 1. New uses of the function or type will be discouraged via Tricorder
|
| 180 |
+
// warnings.
|
| 181 |
+
// 2. If enabled via `METADATA`, automated changes will be sent out inlining the
|
| 182 |
+
// functions's body or replacing the type where it is used.
|
| 183 |
+
//
|
| 184 |
+
// For example:
|
| 185 |
+
//
|
| 186 |
+
// ABSL_DEPRECATE_AND_INLINE() inline int OldFunc(int x) {
|
| 187 |
+
// return NewFunc(x, 0);
|
| 188 |
+
// }
|
| 189 |
+
//
|
| 190 |
+
// will mark `OldFunc` as deprecated, and the go/cpp-inliner service will
|
| 191 |
+
// replace calls to `OldFunc(x)` with calls to `NewFunc(x, 0)`. Once all calls
|
| 192 |
+
// to `OldFunc` have been replaced, `OldFunc` can be deleted.
|
| 193 |
+
//
|
| 194 |
+
// See go/cpp-inliner for more information.
|
| 195 |
+
//
|
| 196 |
+
// Note: go/cpp-inliner is Google-internal service for automated refactoring.
|
| 197 |
+
// While open-source users do not have access to this service, the macro is
|
| 198 |
+
// provided for compatibility, and so that users receive deprecation warnings.
|
| 199 |
+
#if ABSL_HAVE_CPP_ATTRIBUTE(deprecated) && \
|
| 200 |
+
ABSL_HAVE_CPP_ATTRIBUTE(clang::annotate)
|
| 201 |
+
#define ABSL_DEPRECATE_AND_INLINE() [[deprecated, clang::annotate("inline-me")]]
|
| 202 |
+
#elif ABSL_HAVE_CPP_ATTRIBUTE(deprecated)
|
| 203 |
+
#define ABSL_DEPRECATE_AND_INLINE() [[deprecated]]
|
| 204 |
+
#else
|
| 205 |
+
#define ABSL_DEPRECATE_AND_INLINE()
|
| 206 |
+
#endif
|
| 207 |
+
|
| 208 |
+
// Requires the compiler to prove that the size of the given object is at least
|
| 209 |
+
// the expected amount.
|
| 210 |
+
#if ABSL_HAVE_ATTRIBUTE(diagnose_if) && ABSL_HAVE_BUILTIN(__builtin_object_size)
|
| 211 |
+
#define ABSL_INTERNAL_NEED_MIN_SIZE(Obj, N) \
|
| 212 |
+
__attribute__((diagnose_if(__builtin_object_size(Obj, 0) < N, \
|
| 213 |
+
"object size provably too small " \
|
| 214 |
+
"(this would corrupt memory)", \
|
| 215 |
+
"error")))
|
| 216 |
+
#else
|
| 217 |
+
#define ABSL_INTERNAL_NEED_MIN_SIZE(Obj, N)
|
| 218 |
+
#endif
|
| 219 |
+
|
| 220 |
+
#endif // ABSL_BASE_MACROS_H_
|