/src/abseil-cpp/absl/container/internal/container_memory.h
Line | Count | Source (jump to first uncovered line) |
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_CONTAINER_INTERNAL_CONTAINER_MEMORY_H_ |
16 | | #define ABSL_CONTAINER_INTERNAL_CONTAINER_MEMORY_H_ |
17 | | |
18 | | #include <cassert> |
19 | | #include <cstddef> |
20 | | #include <cstdint> |
21 | | #include <cstring> |
22 | | #include <memory> |
23 | | #include <new> |
24 | | #include <tuple> |
25 | | #include <type_traits> |
26 | | #include <utility> |
27 | | |
28 | | #include "absl/base/config.h" |
29 | | #include "absl/memory/memory.h" |
30 | | #include "absl/meta/type_traits.h" |
31 | | #include "absl/utility/utility.h" |
32 | | |
33 | | #ifdef ABSL_HAVE_ADDRESS_SANITIZER |
34 | | #include <sanitizer/asan_interface.h> |
35 | | #endif |
36 | | |
37 | | #ifdef ABSL_HAVE_MEMORY_SANITIZER |
38 | | #include <sanitizer/msan_interface.h> |
39 | | #endif |
40 | | |
41 | | namespace absl { |
42 | | ABSL_NAMESPACE_BEGIN |
43 | | namespace container_internal { |
44 | | |
45 | | template <size_t Alignment> |
46 | | struct alignas(Alignment) AlignedType {}; |
47 | | |
48 | | // Allocates at least n bytes aligned to the specified alignment. |
49 | | // Alignment must be a power of 2. It must be positive. |
50 | | // |
51 | | // Note that many allocators don't honor alignment requirements above certain |
52 | | // threshold (usually either alignof(std::max_align_t) or alignof(void*)). |
53 | | // Allocate() doesn't apply alignment corrections. If the underlying allocator |
54 | | // returns insufficiently alignment pointer, that's what you are going to get. |
55 | | template <size_t Alignment, class Alloc> |
56 | 8 | void* Allocate(Alloc* alloc, size_t n) { |
57 | 8 | static_assert(Alignment > 0, ""); |
58 | 8 | assert(n && "n must be positive"); |
59 | 8 | using M = AlignedType<Alignment>; |
60 | 8 | using A = typename absl::allocator_traits<Alloc>::template rebind_alloc<M>; |
61 | 8 | using AT = typename absl::allocator_traits<Alloc>::template rebind_traits<M>; |
62 | | // On macOS, "mem_alloc" is a #define with one argument defined in |
63 | | // rpc/types.h, so we can't name the variable "mem_alloc" and initialize it |
64 | | // with the "foo(bar)" syntax. |
65 | 8 | A my_mem_alloc(*alloc); |
66 | 8 | void* p = AT::allocate(my_mem_alloc, (n + sizeof(M) - 1) / sizeof(M)); |
67 | 8 | assert(reinterpret_cast<uintptr_t>(p) % Alignment == 0 && |
68 | 8 | "allocator does not respect alignment"); |
69 | 8 | return p; |
70 | 8 | } |
71 | | |
72 | | // Returns true if the destruction of the value with given Allocator will be |
73 | | // trivial. |
74 | | template <class Allocator, class ValueType> |
75 | 0 | constexpr auto IsDestructionTrivial() { |
76 | 0 | constexpr bool result = |
77 | 0 | std::is_trivially_destructible<ValueType>::value && |
78 | 0 | std::is_same<typename absl::allocator_traits< |
79 | 0 | Allocator>::template rebind_alloc<char>, |
80 | 0 | std::allocator<char>>::value; |
81 | 0 | return std::integral_constant<bool, result>(); |
82 | 0 | } Unexecuted instantiation: auto absl::container_internal::IsDestructionTrivial<std::__1::allocator<std::__1::pair<std::__1::basic_string_view<char, std::__1::char_traits<char> > const, absl::CommandLineFlag*> >, std::__1::pair<std::__1::basic_string_view<char, std::__1::char_traits<char> > const, absl::CommandLineFlag*> >() Unexecuted instantiation: auto absl::container_internal::IsDestructionTrivial<std::__1::allocator<char>, std::__1::pair<std::__1::basic_string_view<char, std::__1::char_traits<char> > const, absl::CommandLineFlag*> >() |
83 | | |
84 | | // The pointer must have been previously obtained by calling |
85 | | // Allocate<Alignment>(alloc, n). |
86 | | template <size_t Alignment, class Alloc> |
87 | 6 | void Deallocate(Alloc* alloc, void* p, size_t n) { |
88 | 6 | static_assert(Alignment > 0, ""); |
89 | 6 | assert(n && "n must be positive"); |
90 | 6 | using M = AlignedType<Alignment>; |
91 | 6 | using A = typename absl::allocator_traits<Alloc>::template rebind_alloc<M>; |
92 | 6 | using AT = typename absl::allocator_traits<Alloc>::template rebind_traits<M>; |
93 | | // On macOS, "mem_alloc" is a #define with one argument defined in |
94 | | // rpc/types.h, so we can't name the variable "mem_alloc" and initialize it |
95 | | // with the "foo(bar)" syntax. |
96 | 6 | A my_mem_alloc(*alloc); |
97 | 6 | AT::deallocate(my_mem_alloc, static_cast<M*>(p), |
98 | 6 | (n + sizeof(M) - 1) / sizeof(M)); |
99 | 6 | } Unexecuted instantiation: void absl::container_internal::Deallocate<8ul, std::__1::allocator<std::__1::pair<std::__1::basic_string_view<char, std::__1::char_traits<char> > const, absl::CommandLineFlag*> > >(std::__1::allocator<std::__1::pair<std::__1::basic_string_view<char, std::__1::char_traits<char> > const, absl::CommandLineFlag*> >*, void*, unsigned long) void absl::container_internal::Deallocate<8ul, std::__1::allocator<char> >(std::__1::allocator<char>*, void*, unsigned long) Line | Count | Source | 87 | 6 | void Deallocate(Alloc* alloc, void* p, size_t n) { | 88 | 6 | static_assert(Alignment > 0, ""); | 89 | 6 | assert(n && "n must be positive"); | 90 | 6 | using M = AlignedType<Alignment>; | 91 | 6 | using A = typename absl::allocator_traits<Alloc>::template rebind_alloc<M>; | 92 | 6 | using AT = typename absl::allocator_traits<Alloc>::template rebind_traits<M>; | 93 | | // On macOS, "mem_alloc" is a #define with one argument defined in | 94 | | // rpc/types.h, so we can't name the variable "mem_alloc" and initialize it | 95 | | // with the "foo(bar)" syntax. | 96 | 6 | A my_mem_alloc(*alloc); | 97 | 6 | AT::deallocate(my_mem_alloc, static_cast<M*>(p), | 98 | 6 | (n + sizeof(M) - 1) / sizeof(M)); | 99 | 6 | } |
|
100 | | |
101 | | namespace memory_internal { |
102 | | |
103 | | // Constructs T into uninitialized storage pointed by `ptr` using the args |
104 | | // specified in the tuple. |
105 | | template <class Alloc, class T, class Tuple, size_t... I> |
106 | | void ConstructFromTupleImpl(Alloc* alloc, T* ptr, Tuple&& t, |
107 | | absl::index_sequence<I...>) { |
108 | | absl::allocator_traits<Alloc>::construct( |
109 | | *alloc, ptr, std::get<I>(std::forward<Tuple>(t))...); |
110 | | } |
111 | | |
112 | | template <class T, class F> |
113 | | struct WithConstructedImplF { |
114 | | template <class... Args> |
115 | | decltype(std::declval<F>()(std::declval<T>())) operator()( |
116 | | Args&&... args) const { |
117 | | return std::forward<F>(f)(T(std::forward<Args>(args)...)); |
118 | | } |
119 | | F&& f; |
120 | | }; |
121 | | |
122 | | template <class T, class Tuple, size_t... Is, class F> |
123 | | decltype(std::declval<F>()(std::declval<T>())) WithConstructedImpl( |
124 | | Tuple&& t, absl::index_sequence<Is...>, F&& f) { |
125 | | return WithConstructedImplF<T, F>{std::forward<F>(f)}( |
126 | | std::get<Is>(std::forward<Tuple>(t))...); |
127 | | } |
128 | | |
129 | | template <class T, size_t... Is> |
130 | | auto TupleRefImpl(T&& t, absl::index_sequence<Is...>) |
131 | | -> decltype(std::forward_as_tuple(std::get<Is>(std::forward<T>(t))...)) { |
132 | | return std::forward_as_tuple(std::get<Is>(std::forward<T>(t))...); |
133 | | } |
134 | | |
135 | | // Returns a tuple of references to the elements of the input tuple. T must be a |
136 | | // tuple. |
137 | | template <class T> |
138 | | auto TupleRef(T&& t) -> decltype(TupleRefImpl( |
139 | | std::forward<T>(t), |
140 | | absl::make_index_sequence< |
141 | | std::tuple_size<typename std::decay<T>::type>::value>())) { |
142 | | return TupleRefImpl( |
143 | | std::forward<T>(t), |
144 | | absl::make_index_sequence< |
145 | | std::tuple_size<typename std::decay<T>::type>::value>()); |
146 | | } |
147 | | |
148 | | template <class F, class K, class V> |
149 | | decltype(std::declval<F>()(std::declval<const K&>(), std::piecewise_construct, |
150 | | std::declval<std::tuple<K>>(), std::declval<V>())) |
151 | 51 | DecomposePairImpl(F&& f, std::pair<std::tuple<K>, V> p) { |
152 | 51 | const auto& key = std::get<0>(p.first); |
153 | 51 | return std::forward<F>(f)(key, std::piecewise_construct, std::move(p.first), |
154 | 51 | std::move(p.second)); |
155 | 51 | } _ZN4absl18container_internal15memory_internal17DecomposePairImplINS0_12raw_hash_setINS0_17FlatHashMapPolicyINSt3__117basic_string_viewIcNS5_11char_traitsIcEEEEPNS_15CommandLineFlagEEENS0_10StringHashENS0_8StringEqENS5_9allocatorINS5_4pairIKS9_SB_EEEEE12EqualElementIS9_EERSH_NS5_5tupleIJRKSB_EEEEEDTclclsr3stdE7declvalIT_EEclsr3stdE7declvalIRKT0_EEL_ZNS5_19piecewise_constructEEclsr3stdE7declvalINSO_IJST_EEEEEclsr3stdE7declvalIT1_EEEEOSS_NSG_ISW_SX_EE Line | Count | Source | 151 | 19 | DecomposePairImpl(F&& f, std::pair<std::tuple<K>, V> p) { | 152 | 19 | const auto& key = std::get<0>(p.first); | 153 | 19 | return std::forward<F>(f)(key, std::piecewise_construct, std::move(p.first), | 154 | 19 | std::move(p.second)); | 155 | 19 | } |
_ZN4absl18container_internal15memory_internal17DecomposePairImplINS0_12raw_hash_setINS0_17FlatHashMapPolicyINSt3__117basic_string_viewIcNS5_11char_traitsIcEEEEPNS_15CommandLineFlagEEENS0_10StringHashENS0_8StringEqENS5_9allocatorINS5_4pairIKS9_SB_EEEEE19EmplaceDecomposableEOSH_NS5_5tupleIJOSB_EEEEEDTclclsr3stdE7declvalIT_EEclsr3stdE7declvalIRKT0_EEL_ZNS5_19piecewise_constructEEclsr3stdE7declvalINSN_IJSR_EEEEEclsr3stdE7declvalIT1_EEEEOSQ_NSG_ISU_SV_EE Line | Count | Source | 151 | 16 | DecomposePairImpl(F&& f, std::pair<std::tuple<K>, V> p) { | 152 | 16 | const auto& key = std::get<0>(p.first); | 153 | 16 | return std::forward<F>(f)(key, std::piecewise_construct, std::move(p.first), | 154 | 16 | std::move(p.second)); | 155 | 16 | } |
Unexecuted instantiation: _ZN4absl18container_internal15memory_internal17DecomposePairImplINS0_12raw_hash_setINS0_17FlatHashMapPolicyINSt3__117basic_string_viewIcNS5_11char_traitsIcEEEEPNS_15CommandLineFlagEEENS0_10StringHashENS0_8StringEqENS5_9allocatorINS5_4pairIKS9_SB_EEEEE11HashElementERSH_NS5_5tupleIJRKSB_EEEEEDTclclsr3stdE7declvalIT_EEclsr3stdE7declvalIRKT0_EEL_ZNS5_19piecewise_constructEEclsr3stdE7declvalINSN_IJSS_EEEEEclsr3stdE7declvalIT1_EEEEOSR_NSG_ISV_SW_EE _ZN4absl18container_internal15memory_internal17DecomposePairImplINS0_12raw_hash_setINS0_17FlatHashMapPolicyINSt3__117basic_string_viewIcNS5_11char_traitsIcEEEEPNS_15CommandLineFlagEEENS0_10StringHashENS0_8StringEqENS5_9allocatorINS5_4pairIKS9_SB_EEEEE11FindElementERSH_NS5_5tupleIJRKSB_EEEEEDTclclsr3stdE7declvalIT_EEclsr3stdE7declvalIRKT0_EEL_ZNS5_19piecewise_constructEEclsr3stdE7declvalINSN_IJSS_EEEEEclsr3stdE7declvalIT1_EEEEOSR_NSG_ISV_SW_EE Line | Count | Source | 151 | 16 | DecomposePairImpl(F&& f, std::pair<std::tuple<K>, V> p) { | 152 | 16 | const auto& key = std::get<0>(p.first); | 153 | 16 | return std::forward<F>(f)(key, std::piecewise_construct, std::move(p.first), | 154 | 16 | std::move(p.second)); | 155 | 16 | } |
Unexecuted instantiation: _ZN4absl18container_internal15memory_internal17DecomposePairImplINS0_12raw_hash_setINS0_17FlatHashMapPolicyINSt3__117basic_string_viewIcNS5_11char_traitsIcEEEEPNS_15CommandLineFlagEEENS0_10StringHashENS0_8StringEqENS5_9allocatorINS5_4pairIKS9_SB_EEEEE19EmplaceDecomposableEOS9_NS5_5tupleIJOSB_EEEEEDTclclsr3stdE7declvalIT_EEclsr3stdE7declvalIRKT0_EEL_ZNS5_19piecewise_constructEEclsr3stdE7declvalINSN_IJSR_EEEEEclsr3stdE7declvalIT1_EEEEOSQ_NSG_ISU_SV_EE Unexecuted instantiation: _ZN4absl18container_internal15memory_internal17DecomposePairImplINS0_18hash_policy_traitsINS0_17FlatHashMapPolicyINSt3__117basic_string_viewIcNS5_11char_traitsIcEEEEPNS_15CommandLineFlagEEEvE11HashElementINS0_10StringHashEEERKS9_NS5_5tupleIJRKSB_EEEEEDTclclsr3stdE7declvalIT_EEclsr3stdE7declvalIRKT0_EEL_ZNS5_19piecewise_constructEEclsr3stdE7declvalINSJ_IJSO_EEEEEclsr3stdE7declvalIT1_EEEEOSN_NS5_4pairISR_SS_EE |
156 | | |
157 | | } // namespace memory_internal |
158 | | |
159 | | // Constructs T into uninitialized storage pointed by `ptr` using the args |
160 | | // specified in the tuple. |
161 | | template <class Alloc, class T, class Tuple> |
162 | | void ConstructFromTuple(Alloc* alloc, T* ptr, Tuple&& t) { |
163 | | memory_internal::ConstructFromTupleImpl( |
164 | | alloc, ptr, std::forward<Tuple>(t), |
165 | | absl::make_index_sequence< |
166 | | std::tuple_size<typename std::decay<Tuple>::type>::value>()); |
167 | | } |
168 | | |
169 | | // Constructs T using the args specified in the tuple and calls F with the |
170 | | // constructed value. |
171 | | template <class T, class Tuple, class F> |
172 | | decltype(std::declval<F>()(std::declval<T>())) WithConstructed(Tuple&& t, |
173 | | F&& f) { |
174 | | return memory_internal::WithConstructedImpl<T>( |
175 | | std::forward<Tuple>(t), |
176 | | absl::make_index_sequence< |
177 | | std::tuple_size<typename std::decay<Tuple>::type>::value>(), |
178 | | std::forward<F>(f)); |
179 | | } |
180 | | |
181 | | // Given arguments of an std::pair's constructor, PairArgs() returns a pair of |
182 | | // tuples with references to the passed arguments. The tuples contain |
183 | | // constructor arguments for the first and the second elements of the pair. |
184 | | // |
185 | | // The following two snippets are equivalent. |
186 | | // |
187 | | // 1. std::pair<F, S> p(args...); |
188 | | // |
189 | | // 2. auto a = PairArgs(args...); |
190 | | // std::pair<F, S> p(std::piecewise_construct, |
191 | | // std::move(a.first), std::move(a.second)); |
192 | 0 | inline std::pair<std::tuple<>, std::tuple<>> PairArgs() { return {}; } |
193 | | template <class F, class S> |
194 | 51 | std::pair<std::tuple<F&&>, std::tuple<S&&>> PairArgs(F&& f, S&& s) { |
195 | 51 | return {std::piecewise_construct, std::forward_as_tuple(std::forward<F>(f)), |
196 | 51 | std::forward_as_tuple(std::forward<S>(s))}; |
197 | 51 | } std::__1::pair<std::__1::tuple<std::__1::basic_string_view<char, std::__1::char_traits<char> > const&>, std::__1::tuple<absl::CommandLineFlag* const&> > absl::container_internal::PairArgs<std::__1::basic_string_view<char, std::__1::char_traits<char> > const&, absl::CommandLineFlag* const&>(std::__1::basic_string_view<char, std::__1::char_traits<char> > const&, absl::CommandLineFlag* const&) Line | Count | Source | 194 | 35 | std::pair<std::tuple<F&&>, std::tuple<S&&>> PairArgs(F&& f, S&& s) { | 195 | 35 | return {std::piecewise_construct, std::forward_as_tuple(std::forward<F>(f)), | 196 | 35 | std::forward_as_tuple(std::forward<S>(s))}; | 197 | 35 | } |
std::__1::pair<std::__1::tuple<std::__1::basic_string_view<char, std::__1::char_traits<char> > const&&>, std::__1::tuple<absl::CommandLineFlag*&&> > absl::container_internal::PairArgs<std::__1::basic_string_view<char, std::__1::char_traits<char> > const, absl::CommandLineFlag*>(std::__1::basic_string_view<char, std::__1::char_traits<char> > const&&, absl::CommandLineFlag*&&) Line | Count | Source | 194 | 16 | std::pair<std::tuple<F&&>, std::tuple<S&&>> PairArgs(F&& f, S&& s) { | 195 | 16 | return {std::piecewise_construct, std::forward_as_tuple(std::forward<F>(f)), | 196 | 16 | std::forward_as_tuple(std::forward<S>(s))}; | 197 | 16 | } |
Unexecuted instantiation: std::__1::pair<std::__1::tuple<std::__1::basic_string_view<char, std::__1::char_traits<char> >&&>, std::__1::tuple<absl::CommandLineFlag*&&> > absl::container_internal::PairArgs<std::__1::basic_string_view<char, std::__1::char_traits<char> >, absl::CommandLineFlag*>(std::__1::basic_string_view<char, std::__1::char_traits<char> >&&, absl::CommandLineFlag*&&) |
198 | | template <class F, class S> |
199 | | std::pair<std::tuple<const F&>, std::tuple<const S&>> PairArgs( |
200 | 35 | const std::pair<F, S>& p) { |
201 | 35 | return PairArgs(p.first, p.second); |
202 | 35 | } |
203 | | template <class F, class S> |
204 | 16 | std::pair<std::tuple<F&&>, std::tuple<S&&>> PairArgs(std::pair<F, S>&& p) { |
205 | 16 | return PairArgs(std::forward<F>(p.first), std::forward<S>(p.second)); |
206 | 16 | } std::__1::pair<std::__1::tuple<std::__1::basic_string_view<char, std::__1::char_traits<char> > const&&>, std::__1::tuple<absl::CommandLineFlag*&&> > absl::container_internal::PairArgs<std::__1::basic_string_view<char, std::__1::char_traits<char> > const, absl::CommandLineFlag*>(std::__1::pair<std::__1::basic_string_view<char, std::__1::char_traits<char> > const, absl::CommandLineFlag*>&&) Line | Count | Source | 204 | 16 | std::pair<std::tuple<F&&>, std::tuple<S&&>> PairArgs(std::pair<F, S>&& p) { | 205 | 16 | return PairArgs(std::forward<F>(p.first), std::forward<S>(p.second)); | 206 | 16 | } |
Unexecuted instantiation: std::__1::pair<std::__1::tuple<std::__1::basic_string_view<char, std::__1::char_traits<char> >&&>, std::__1::tuple<absl::CommandLineFlag*&&> > absl::container_internal::PairArgs<std::__1::basic_string_view<char, std::__1::char_traits<char> >, absl::CommandLineFlag*>(std::__1::pair<std::__1::basic_string_view<char, std::__1::char_traits<char> >, absl::CommandLineFlag*>&&) |
207 | | template <class F, class S> |
208 | | auto PairArgs(std::piecewise_construct_t, F&& f, S&& s) |
209 | | -> decltype(std::make_pair(memory_internal::TupleRef(std::forward<F>(f)), |
210 | | memory_internal::TupleRef(std::forward<S>(s)))) { |
211 | | return std::make_pair(memory_internal::TupleRef(std::forward<F>(f)), |
212 | | memory_internal::TupleRef(std::forward<S>(s))); |
213 | | } |
214 | | |
215 | | // A helper function for implementing apply() in map policies. |
216 | | template <class F, class... Args> |
217 | | auto DecomposePair(F&& f, Args&&... args) |
218 | | -> decltype(memory_internal::DecomposePairImpl( |
219 | 51 | std::forward<F>(f), PairArgs(std::forward<Args>(args)...))) { |
220 | 51 | return memory_internal::DecomposePairImpl( |
221 | 51 | std::forward<F>(f), PairArgs(std::forward<Args>(args)...)); |
222 | 51 | } _ZN4absl18container_internal13DecomposePairINS0_12raw_hash_setINS0_17FlatHashMapPolicyINSt3__117basic_string_viewIcNS4_11char_traitsIcEEEEPNS_15CommandLineFlagEEENS0_10StringHashENS0_8StringEqENS4_9allocatorINS4_4pairIKS8_SA_EEEEE12EqualElementIS8_EEJRSH_EEEDTclsr15memory_internalE17DecomposePairImplclsr3stdE7forwardIT_Efp_Ecl8PairArgsspclsr3stdE7forwardIT0_Efp0_EEEEOSN_DpOSO_ Line | Count | Source | 219 | 19 | std::forward<F>(f), PairArgs(std::forward<Args>(args)...))) { | 220 | 19 | return memory_internal::DecomposePairImpl( | 221 | 19 | std::forward<F>(f), PairArgs(std::forward<Args>(args)...)); | 222 | 19 | } |
_ZN4absl18container_internal13DecomposePairINS0_12raw_hash_setINS0_17FlatHashMapPolicyINSt3__117basic_string_viewIcNS4_11char_traitsIcEEEEPNS_15CommandLineFlagEEENS0_10StringHashENS0_8StringEqENS4_9allocatorINS4_4pairIKS8_SA_EEEEE19EmplaceDecomposableEJSH_EEEDTclsr15memory_internalE17DecomposePairImplclsr3stdE7forwardIT_Efp_Ecl8PairArgsspclsr3stdE7forwardIT0_Efp0_EEEEOSL_DpOSM_ Line | Count | Source | 219 | 16 | std::forward<F>(f), PairArgs(std::forward<Args>(args)...))) { | 220 | 16 | return memory_internal::DecomposePairImpl( | 221 | 16 | std::forward<F>(f), PairArgs(std::forward<Args>(args)...)); | 222 | 16 | } |
Unexecuted instantiation: _ZN4absl18container_internal13DecomposePairINS0_12raw_hash_setINS0_17FlatHashMapPolicyINSt3__117basic_string_viewIcNS4_11char_traitsIcEEEEPNS_15CommandLineFlagEEENS0_10StringHashENS0_8StringEqENS4_9allocatorINS4_4pairIKS8_SA_EEEEE11HashElementEJRSH_EEEDTclsr15memory_internalE17DecomposePairImplclsr3stdE7forwardIT_Efp_Ecl8PairArgsspclsr3stdE7forwardIT0_Efp0_EEEEOSM_DpOSN_ _ZN4absl18container_internal13DecomposePairINS0_12raw_hash_setINS0_17FlatHashMapPolicyINSt3__117basic_string_viewIcNS4_11char_traitsIcEEEEPNS_15CommandLineFlagEEENS0_10StringHashENS0_8StringEqENS4_9allocatorINS4_4pairIKS8_SA_EEEEE11FindElementEJRSH_EEEDTclsr15memory_internalE17DecomposePairImplclsr3stdE7forwardIT_Efp_Ecl8PairArgsspclsr3stdE7forwardIT0_Efp0_EEEEOSM_DpOSN_ Line | Count | Source | 219 | 16 | std::forward<F>(f), PairArgs(std::forward<Args>(args)...))) { | 220 | 16 | return memory_internal::DecomposePairImpl( | 221 | 16 | std::forward<F>(f), PairArgs(std::forward<Args>(args)...)); | 222 | 16 | } |
Unexecuted instantiation: _ZN4absl18container_internal13DecomposePairINS0_12raw_hash_setINS0_17FlatHashMapPolicyINSt3__117basic_string_viewIcNS4_11char_traitsIcEEEEPNS_15CommandLineFlagEEENS0_10StringHashENS0_8StringEqENS4_9allocatorINS4_4pairIKS8_SA_EEEEE19EmplaceDecomposableEJNSF_IS8_SA_EEEEEDTclsr15memory_internalE17DecomposePairImplclsr3stdE7forwardIT_Efp_Ecl8PairArgsspclsr3stdE7forwardIT0_Efp0_EEEEOSM_DpOSN_ Unexecuted instantiation: _ZN4absl18container_internal13DecomposePairINS0_12raw_hash_setINS0_17FlatHashMapPolicyINSt3__117basic_string_viewIcNS4_11char_traitsIcEEEEPNS_15CommandLineFlagEEENS0_10StringHashENS0_8StringEqENS4_9allocatorINS4_4pairIKS8_SA_EEEEE12EqualElementIS8_EEJRKSH_EEEDTclsr15memory_internalE17DecomposePairImplclsr3stdE7forwardIT_Efp_Ecl8PairArgsspclsr3stdE7forwardIT0_Efp0_EEEEOSO_DpOSP_ Unexecuted instantiation: _ZN4absl18container_internal13DecomposePairINS0_12raw_hash_setINS0_17FlatHashMapPolicyINSt3__117basic_string_viewIcNS4_11char_traitsIcEEEEPNS_15CommandLineFlagEEENS0_10StringHashENS0_8StringEqENS4_9allocatorINS4_4pairIKS8_SA_EEEEE11HashElementEJRKSH_EEEDTclsr15memory_internalE17DecomposePairImplclsr3stdE7forwardIT_Efp_Ecl8PairArgsspclsr3stdE7forwardIT0_Efp0_EEEEOSN_DpOSO_ Unexecuted instantiation: _ZN4absl18container_internal13DecomposePairINS0_18hash_policy_traitsINS0_17FlatHashMapPolicyINSt3__117basic_string_viewIcNS4_11char_traitsIcEEEEPNS_15CommandLineFlagEEEvE11HashElementINS0_10StringHashEEEJRNS4_4pairIKS8_SA_EEEEEDTclsr15memory_internalE17DecomposePairImplclsr3stdE7forwardIT_Efp_Ecl8PairArgsspclsr3stdE7forwardIT0_Efp0_EEEEOSK_DpOSL_ |
223 | | |
224 | | // A helper function for implementing apply() in set policies. |
225 | | template <class F, class Arg> |
226 | | decltype(std::declval<F>()(std::declval<const Arg&>(), std::declval<Arg>())) |
227 | | DecomposeValue(F&& f, Arg&& arg) { |
228 | | const auto& key = arg; |
229 | | return std::forward<F>(f)(key, std::forward<Arg>(arg)); |
230 | | } |
231 | | |
232 | | // Helper functions for asan and msan. |
233 | 30 | inline void SanitizerPoisonMemoryRegion(const void* m, size_t s) { |
234 | | #ifdef ABSL_HAVE_ADDRESS_SANITIZER |
235 | | ASAN_POISON_MEMORY_REGION(m, s); |
236 | | #endif |
237 | | #ifdef ABSL_HAVE_MEMORY_SANITIZER |
238 | | __msan_poison(m, s); |
239 | | #endif |
240 | 30 | (void)m; |
241 | 30 | (void)s; |
242 | 30 | } |
243 | | |
244 | 28 | inline void SanitizerUnpoisonMemoryRegion(const void* m, size_t s) { |
245 | | #ifdef ABSL_HAVE_ADDRESS_SANITIZER |
246 | | ASAN_UNPOISON_MEMORY_REGION(m, s); |
247 | | #endif |
248 | | #ifdef ABSL_HAVE_MEMORY_SANITIZER |
249 | | __msan_unpoison(m, s); |
250 | | #endif |
251 | 28 | (void)m; |
252 | 28 | (void)s; |
253 | 28 | } |
254 | | |
255 | | template <typename T> |
256 | | inline void SanitizerPoisonObject(const T* object) { |
257 | | SanitizerPoisonMemoryRegion(object, sizeof(T)); |
258 | | } |
259 | | |
260 | | template <typename T> |
261 | | inline void SanitizerUnpoisonObject(const T* object) { |
262 | | SanitizerUnpoisonMemoryRegion(object, sizeof(T)); |
263 | | } |
264 | | |
265 | | namespace memory_internal { |
266 | | |
267 | | // If Pair is a standard-layout type, OffsetOf<Pair>::kFirst and |
268 | | // OffsetOf<Pair>::kSecond are equivalent to offsetof(Pair, first) and |
269 | | // offsetof(Pair, second) respectively. Otherwise they are -1. |
270 | | // |
271 | | // The purpose of OffsetOf is to avoid calling offsetof() on non-standard-layout |
272 | | // type, which is non-portable. |
273 | | template <class Pair, class = std::true_type> |
274 | | struct OffsetOf { |
275 | | static constexpr size_t kFirst = static_cast<size_t>(-1); |
276 | | static constexpr size_t kSecond = static_cast<size_t>(-1); |
277 | | }; |
278 | | |
279 | | template <class Pair> |
280 | | struct OffsetOf<Pair, typename std::is_standard_layout<Pair>::type> { |
281 | | static constexpr size_t kFirst = offsetof(Pair, first); |
282 | | static constexpr size_t kSecond = offsetof(Pair, second); |
283 | | }; |
284 | | |
285 | | template <class K, class V> |
286 | | struct IsLayoutCompatible { |
287 | | private: |
288 | | struct Pair { |
289 | | K first; |
290 | | V second; |
291 | | }; |
292 | | |
293 | | // Is P layout-compatible with Pair? |
294 | | template <class P> |
295 | 0 | static constexpr bool LayoutCompatible() { |
296 | 0 | return std::is_standard_layout<P>() && sizeof(P) == sizeof(Pair) && |
297 | 0 | alignof(P) == alignof(Pair) && |
298 | 0 | memory_internal::OffsetOf<P>::kFirst == |
299 | 0 | memory_internal::OffsetOf<Pair>::kFirst && |
300 | 0 | memory_internal::OffsetOf<P>::kSecond == |
301 | 0 | memory_internal::OffsetOf<Pair>::kSecond; |
302 | 0 | } Unexecuted instantiation: bool absl::container_internal::memory_internal::IsLayoutCompatible<std::__1::basic_string_view<char, std::__1::char_traits<char> >, absl::CommandLineFlag*>::LayoutCompatible<std::__1::pair<std::__1::basic_string_view<char, std::__1::char_traits<char> >, absl::CommandLineFlag*> >() Unexecuted instantiation: bool absl::container_internal::memory_internal::IsLayoutCompatible<std::__1::basic_string_view<char, std::__1::char_traits<char> >, absl::CommandLineFlag*>::LayoutCompatible<std::__1::pair<std::__1::basic_string_view<char, std::__1::char_traits<char> > const, absl::CommandLineFlag*> >() |
303 | | |
304 | | public: |
305 | | // Whether pair<const K, V> and pair<K, V> are layout-compatible. If they are, |
306 | | // then it is safe to store them in a union and read from either. |
307 | | static constexpr bool value = std::is_standard_layout<K>() && |
308 | | std::is_standard_layout<Pair>() && |
309 | | memory_internal::OffsetOf<Pair>::kFirst == 0 && |
310 | | LayoutCompatible<std::pair<K, V>>() && |
311 | | LayoutCompatible<std::pair<const K, V>>(); |
312 | | }; |
313 | | |
314 | | } // namespace memory_internal |
315 | | |
316 | | // The internal storage type for key-value containers like flat_hash_map. |
317 | | // |
318 | | // It is convenient for the value_type of a flat_hash_map<K, V> to be |
319 | | // pair<const K, V>; the "const K" prevents accidental modification of the key |
320 | | // when dealing with the reference returned from find() and similar methods. |
321 | | // However, this creates other problems; we want to be able to emplace(K, V) |
322 | | // efficiently with move operations, and similarly be able to move a |
323 | | // pair<K, V> in insert(). |
324 | | // |
325 | | // The solution is this union, which aliases the const and non-const versions |
326 | | // of the pair. This also allows flat_hash_map<const K, V> to work, even though |
327 | | // that has the same efficiency issues with move in emplace() and insert() - |
328 | | // but people do it anyway. |
329 | | // |
330 | | // If kMutableKeys is false, only the value member can be accessed. |
331 | | // |
332 | | // If kMutableKeys is true, key can be accessed through all slots while value |
333 | | // and mutable_value must be accessed only via INITIALIZED slots. Slots are |
334 | | // created and destroyed via mutable_value so that the key can be moved later. |
335 | | // |
336 | | // Accessing one of the union fields while the other is active is safe as |
337 | | // long as they are layout-compatible, which is guaranteed by the definition of |
338 | | // kMutableKeys. For C++11, the relevant section of the standard is |
339 | | // https://timsong-cpp.github.io/cppwp/n3337/class.mem#19 (9.2.19) |
340 | | template <class K, class V> |
341 | | union map_slot_type { |
342 | 16 | map_slot_type() {} |
343 | | ~map_slot_type() = delete; |
344 | | using value_type = std::pair<const K, V>; |
345 | | using mutable_value_type = |
346 | | std::pair<absl::remove_const_t<K>, absl::remove_const_t<V>>; |
347 | | |
348 | | value_type value; |
349 | | mutable_value_type mutable_value; |
350 | | absl::remove_const_t<K> key; |
351 | | }; |
352 | | |
353 | | template <class K, class V> |
354 | | struct map_slot_policy { |
355 | | using slot_type = map_slot_type<K, V>; |
356 | | using value_type = std::pair<const K, V>; |
357 | | using mutable_value_type = |
358 | | std::pair<absl::remove_const_t<K>, absl::remove_const_t<V>>; |
359 | | |
360 | | private: |
361 | 16 | static void emplace(slot_type* slot) { |
362 | | // The construction of union doesn't do anything at runtime but it allows us |
363 | | // to access its members without violating aliasing rules. |
364 | 16 | new (slot) slot_type; |
365 | 16 | } |
366 | | // If pair<const K, V> and pair<K, V> are layout-compatible, we can accept one |
367 | | // or the other via slot_type. We are also free to access the key via |
368 | | // slot_type::key in this case. |
369 | | using kMutableKeys = memory_internal::IsLayoutCompatible<K, V>; |
370 | | |
371 | | public: |
372 | | static value_type& element(slot_type* slot) { return slot->value; } |
373 | | static const value_type& element(const slot_type* slot) { |
374 | | return slot->value; |
375 | | } |
376 | | |
377 | | // When C++17 is available, we can use std::launder to provide mutable |
378 | | // access to the key for use in node handle. |
379 | | #if defined(__cpp_lib_launder) && __cpp_lib_launder >= 201606 |
380 | | static K& mutable_key(slot_type* slot) { |
381 | | // Still check for kMutableKeys so that we can avoid calling std::launder |
382 | | // unless necessary because it can interfere with optimizations. |
383 | | return kMutableKeys::value ? slot->key |
384 | | : *std::launder(const_cast<K*>( |
385 | | std::addressof(slot->value.first))); |
386 | | } |
387 | | #else // !(defined(__cpp_lib_launder) && __cpp_lib_launder >= 201606) |
388 | | static const K& mutable_key(slot_type* slot) { return key(slot); } |
389 | | #endif |
390 | | |
391 | | static const K& key(const slot_type* slot) { |
392 | | return kMutableKeys::value ? slot->key : slot->value.first; |
393 | | } |
394 | | |
395 | | template <class Allocator, class... Args> |
396 | 16 | static void construct(Allocator* alloc, slot_type* slot, Args&&... args) { |
397 | 16 | emplace(slot); |
398 | 16 | if (kMutableKeys::value) { |
399 | 16 | absl::allocator_traits<Allocator>::construct(*alloc, &slot->mutable_value, |
400 | 16 | std::forward<Args>(args)...); |
401 | 16 | } else { |
402 | 0 | absl::allocator_traits<Allocator>::construct(*alloc, &slot->value, |
403 | 0 | std::forward<Args>(args)...); |
404 | 0 | } |
405 | 16 | } void absl::container_internal::map_slot_policy<std::__1::basic_string_view<char, std::__1::char_traits<char> >, absl::CommandLineFlag*>::construct<std::__1::allocator<std::__1::pair<std::__1::basic_string_view<char, std::__1::char_traits<char> > const, absl::CommandLineFlag*> >, std::__1::piecewise_construct_t const&, std::__1::tuple<std::__1::basic_string_view<char, std::__1::char_traits<char> > const&&>, std::__1::tuple<absl::CommandLineFlag*&&> >(std::__1::allocator<std::__1::pair<std::__1::basic_string_view<char, std::__1::char_traits<char> > const, absl::CommandLineFlag*> >*, absl::container_internal::map_slot_type<std::__1::basic_string_view<char, std::__1::char_traits<char> >, absl::CommandLineFlag*>*, std::__1::piecewise_construct_t const&, std::__1::tuple<std::__1::basic_string_view<char, std::__1::char_traits<char> > const&&>&&, std::__1::tuple<absl::CommandLineFlag*&&>&&) Line | Count | Source | 396 | 16 | static void construct(Allocator* alloc, slot_type* slot, Args&&... args) { | 397 | 16 | emplace(slot); | 398 | 16 | if (kMutableKeys::value) { | 399 | 16 | absl::allocator_traits<Allocator>::construct(*alloc, &slot->mutable_value, | 400 | 16 | std::forward<Args>(args)...); | 401 | 16 | } else { | 402 | 0 | absl::allocator_traits<Allocator>::construct(*alloc, &slot->value, | 403 | 0 | std::forward<Args>(args)...); | 404 | 0 | } | 405 | 16 | } |
Unexecuted instantiation: void absl::container_internal::map_slot_policy<std::__1::basic_string_view<char, std::__1::char_traits<char> >, absl::CommandLineFlag*>::construct<std::__1::allocator<std::__1::pair<std::__1::basic_string_view<char, std::__1::char_traits<char> > const, absl::CommandLineFlag*> >, std::__1::piecewise_construct_t const&, std::__1::tuple<std::__1::basic_string_view<char, std::__1::char_traits<char> >&&>, std::__1::tuple<absl::CommandLineFlag*&&> >(std::__1::allocator<std::__1::pair<std::__1::basic_string_view<char, std::__1::char_traits<char> > const, absl::CommandLineFlag*> >*, absl::container_internal::map_slot_type<std::__1::basic_string_view<char, std::__1::char_traits<char> >, absl::CommandLineFlag*>*, std::__1::piecewise_construct_t const&, std::__1::tuple<std::__1::basic_string_view<char, std::__1::char_traits<char> >&&>&&, std::__1::tuple<absl::CommandLineFlag*&&>&&) |
406 | | |
407 | | // Construct this slot by moving from another slot. |
408 | | template <class Allocator> |
409 | | static void construct(Allocator* alloc, slot_type* slot, slot_type* other) { |
410 | | emplace(slot); |
411 | | if (kMutableKeys::value) { |
412 | | absl::allocator_traits<Allocator>::construct( |
413 | | *alloc, &slot->mutable_value, std::move(other->mutable_value)); |
414 | | } else { |
415 | | absl::allocator_traits<Allocator>::construct(*alloc, &slot->value, |
416 | | std::move(other->value)); |
417 | | } |
418 | | } |
419 | | |
420 | | // Construct this slot by copying from another slot. |
421 | | template <class Allocator> |
422 | | static void construct(Allocator* alloc, slot_type* slot, |
423 | | const slot_type* other) { |
424 | | emplace(slot); |
425 | | absl::allocator_traits<Allocator>::construct(*alloc, &slot->value, |
426 | | other->value); |
427 | | } |
428 | | |
429 | | template <class Allocator> |
430 | 0 | static auto destroy(Allocator* alloc, slot_type* slot) { |
431 | 0 | if (kMutableKeys::value) { |
432 | 0 | absl::allocator_traits<Allocator>::destroy(*alloc, &slot->mutable_value); |
433 | 0 | } else { |
434 | 0 | absl::allocator_traits<Allocator>::destroy(*alloc, &slot->value); |
435 | 0 | } |
436 | 0 | return IsDestructionTrivial<Allocator, value_type>(); |
437 | 0 | } Unexecuted instantiation: auto absl::container_internal::map_slot_policy<std::__1::basic_string_view<char, std::__1::char_traits<char> >, absl::CommandLineFlag*>::destroy<std::__1::allocator<std::__1::pair<std::__1::basic_string_view<char, std::__1::char_traits<char> > const, absl::CommandLineFlag*> > >(std::__1::allocator<std::__1::pair<std::__1::basic_string_view<char, std::__1::char_traits<char> > const, absl::CommandLineFlag*> >*, absl::container_internal::map_slot_type<std::__1::basic_string_view<char, std::__1::char_traits<char> >, absl::CommandLineFlag*>*) Unexecuted instantiation: auto absl::container_internal::map_slot_policy<std::__1::basic_string_view<char, std::__1::char_traits<char> >, absl::CommandLineFlag*>::destroy<std::__1::allocator<char> >(std::__1::allocator<char>*, absl::container_internal::map_slot_type<std::__1::basic_string_view<char, std::__1::char_traits<char> >, absl::CommandLineFlag*>*) |
438 | | |
439 | | template <class Allocator> |
440 | | static auto transfer(Allocator* alloc, slot_type* new_slot, |
441 | 0 | slot_type* old_slot) { |
442 | 0 | auto is_relocatable = |
443 | 0 | typename absl::is_trivially_relocatable<value_type>::type(); |
444 | |
|
445 | 0 | emplace(new_slot); |
446 | 0 | #if defined(__cpp_lib_launder) && __cpp_lib_launder >= 201606 |
447 | 0 | if (is_relocatable) { |
448 | | // TODO(b/247130232,b/251814870): remove casts after fixing warnings. |
449 | 0 | std::memcpy(static_cast<void*>(std::launder(&new_slot->value)), |
450 | 0 | static_cast<const void*>(&old_slot->value), |
451 | 0 | sizeof(value_type)); |
452 | 0 | return is_relocatable; |
453 | 0 | } |
454 | 0 | #endif |
455 | | |
456 | 0 | if (kMutableKeys::value) { |
457 | 0 | absl::allocator_traits<Allocator>::construct( |
458 | 0 | *alloc, &new_slot->mutable_value, std::move(old_slot->mutable_value)); |
459 | 0 | } else { |
460 | 0 | absl::allocator_traits<Allocator>::construct(*alloc, &new_slot->value, |
461 | 0 | std::move(old_slot->value)); |
462 | 0 | } |
463 | 0 | destroy(alloc, old_slot); |
464 | 0 | return is_relocatable; |
465 | 0 | } Unexecuted instantiation: auto absl::container_internal::map_slot_policy<std::__1::basic_string_view<char, std::__1::char_traits<char> >, absl::CommandLineFlag*>::transfer<std::__1::allocator<std::__1::pair<std::__1::basic_string_view<char, std::__1::char_traits<char> > const, absl::CommandLineFlag*> > >(std::__1::allocator<std::__1::pair<std::__1::basic_string_view<char, std::__1::char_traits<char> > const, absl::CommandLineFlag*> >*, absl::container_internal::map_slot_type<std::__1::basic_string_view<char, std::__1::char_traits<char> >, absl::CommandLineFlag*>*, absl::container_internal::map_slot_type<std::__1::basic_string_view<char, std::__1::char_traits<char> >, absl::CommandLineFlag*>*) Unexecuted instantiation: auto absl::container_internal::map_slot_policy<std::__1::basic_string_view<char, std::__1::char_traits<char> >, absl::CommandLineFlag*>::transfer<std::__1::allocator<char> >(std::__1::allocator<char>*, absl::container_internal::map_slot_type<std::__1::basic_string_view<char, std::__1::char_traits<char> >, absl::CommandLineFlag*>*, absl::container_internal::map_slot_type<std::__1::basic_string_view<char, std::__1::char_traits<char> >, absl::CommandLineFlag*>*) |
466 | | }; |
467 | | |
468 | | // Type erased function for computing hash of the slot. |
469 | | using HashSlotFn = size_t (*)(const void* hash_fn, void* slot); |
470 | | |
471 | | // Type erased function to apply `Fn` to data inside of the `slot`. |
472 | | // The data is expected to have type `T`. |
473 | | template <class Fn, class T> |
474 | 0 | size_t TypeErasedApplyToSlotFn(const void* fn, void* slot) { |
475 | 0 | const auto* f = static_cast<const Fn*>(fn); |
476 | 0 | return (*f)(*static_cast<const T*>(slot)); |
477 | 0 | } |
478 | | |
479 | | // Type erased function to apply `Fn` to data inside of the `*slot_ptr`. |
480 | | // The data is expected to have type `T`. |
481 | | template <class Fn, class T> |
482 | | size_t TypeErasedDerefAndApplyToSlotFn(const void* fn, void* slot_ptr) { |
483 | | const auto* f = static_cast<const Fn*>(fn); |
484 | | const T* slot = *static_cast<const T**>(slot_ptr); |
485 | | return (*f)(*slot); |
486 | | } |
487 | | |
488 | | } // namespace container_internal |
489 | | ABSL_NAMESPACE_END |
490 | | } // namespace absl |
491 | | |
492 | | #endif // ABSL_CONTAINER_INTERNAL_CONTAINER_MEMORY_H_ |