From 5be22f98733c674d532598454ae729253bc53e82 Mon Sep 17 00:00:00 2001 From: Evan Brown Date: Mon, 17 Jul 2023 14:06:54 -0700 Subject: Move growth_left to the backing array. PiperOrigin-RevId: 548794485 Change-Id: Ie82d5f8ad752518ef05b38144ca1e32b21c9def8 --- absl/container/internal/raw_hash_set_test.cc | 48 +++++++++++++++++++++++++--- 1 file changed, 44 insertions(+), 4 deletions(-) (limited to 'absl/container/internal/raw_hash_set_test.cc') diff --git a/absl/container/internal/raw_hash_set_test.cc b/absl/container/internal/raw_hash_set_test.cc index ce1070ba..6cbf6dea 100644 --- a/absl/container/internal/raw_hash_set_test.cc +++ b/absl/container/internal/raw_hash_set_test.cc @@ -17,6 +17,7 @@ #include #include #include +#include #include #include #include @@ -436,6 +437,41 @@ struct CustomAllocIntTable using Base::Base; }; +// Tries to allocate memory at the minimum alignment even when the default +// allocator uses a higher alignment. +template +struct MinimumAlignmentAlloc : std::allocator { + MinimumAlignmentAlloc() = default; + + template + explicit MinimumAlignmentAlloc(const MinimumAlignmentAlloc& /*other*/) {} + + template + struct rebind { + using other = MinimumAlignmentAlloc; + }; + + T* allocate(size_t n) { + T* ptr = std::allocator::allocate(n + 1); + char* cptr = reinterpret_cast(ptr); + cptr += alignof(T); + return reinterpret_cast(cptr); + } + + void deallocate(T* ptr, size_t n) { + char* cptr = reinterpret_cast(ptr); + cptr -= alignof(T); + std::allocator::deallocate(reinterpret_cast(cptr), n + 1); + } +}; + +struct MinimumAlignmentUint8Table + : raw_hash_set, + std::equal_to, MinimumAlignmentAlloc> { + using Base = typename MinimumAlignmentUint8Table::raw_hash_set; + using Base::Base; +}; + struct BadFastHash { template size_t operator()(const T&) const { @@ -460,14 +496,12 @@ TEST(Table, EmptyFunctorOptimization) { void* slots; size_t size; size_t capacity; - size_t growth_left; }; struct MockTableInfozDisabled { void* ctrl; void* slots; size_t size; size_t capacity; - size_t growth_left; }; struct StatelessHash { size_t operator()(absl::string_view) const { return 0; } @@ -2247,11 +2281,17 @@ TEST(Sanitizer, PoisoningOnErase) { } #endif // ABSL_HAVE_ADDRESS_SANITIZER -TEST(Table, AlignOne) { +template +class AlignOneTest : public ::testing::Test {}; +using AlignOneTestTypes = + ::testing::Types; +TYPED_TEST_SUITE(AlignOneTest, AlignOneTestTypes); + +TYPED_TEST(AlignOneTest, AlignOne) { // We previously had a bug in which we were copying a control byte over the // first slot when alignof(value_type) is 1. We test repeated // insertions/erases and verify that the behavior is correct. - Uint8Table t; + TypeParam t; std::unordered_set verifier; // NOLINT // Do repeated insertions/erases from the table. -- cgit v1.2.3