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