Coverage Report

Created: 2026-10-28 06:49

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