From 56766bc7964585b088add31c4c96b5b892225cb8 Mon Sep 17 00:00:00 2001 From: Mark de Wever Date: Sat, 6 Jul 2024 14:58:20 +0200 Subject: [PATCH 1/2] [libc++][vector] Tests shrink_to_fit requirement. vector's shrink_to_fit implementation is using the "swap-to-free-container-resources-trick" which only shrinks when the input vector is empty. Since the request to shrink_to_fit is non-binding, this is a valid implementation. It is not a high-quality implementation. Since vector is not a very popular container the implementation has not been changed and only a test to validate the non-growing property has been added. This was discovered while investigating https://github.com/llvm/llvm-project/issues/95161 --- .../vector.bool/shrink_to_fit.pass.cpp | 39 +++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/libcxx/test/std/containers/sequences/vector.bool/shrink_to_fit.pass.cpp b/libcxx/test/std/containers/sequences/vector.bool/shrink_to_fit.pass.cpp index b39245cab7bf4..18661ad825c6b 100644 --- a/libcxx/test/std/containers/sequences/vector.bool/shrink_to_fit.pass.cpp +++ b/libcxx/test/std/containers/sequences/vector.bool/shrink_to_fit.pass.cpp @@ -39,9 +39,48 @@ TEST_CONSTEXPR_CXX20 bool tests() return true; } +#if TEST_STD_VER >= 23 +std::size_t min_bytes = 1000; + +template +struct increasing_allocator { + using value_type = T; + increasing_allocator() = default; + template + increasing_allocator(const increasing_allocator&) noexcept {} + std::allocation_result allocate_at_least(std::size_t n) { + std::size_t allocation_amount = n * sizeof(T); + if (allocation_amount < min_bytes) + allocation_amount = min_bytes; + min_bytes += 1000; + return {static_cast(::operator new(allocation_amount)), allocation_amount / sizeof(T)}; + } + T* allocate(std::size_t n) { return allocate_at_least(n).ptr; } + void deallocate(T* p, std::size_t) noexcept { ::operator delete(static_cast(p)); } +}; + +template +bool operator==(increasing_allocator, increasing_allocator) { + return true; +} + +// https://github.com/llvm/llvm-project/issues/95161 +void test_increasing_allocator() { + std::vector> v; + v.push_back(1); + std::size_t capacity = v.capacity(); + v.shrink_to_fit(); + assert(v.capacity() <= capacity); + assert(v.size() == 1); +} +#endif // TEST_STD_VER >= 23 + int main(int, char**) { tests(); +#if TEST_STD_VER >= 23 + test_increasing_allocator(); +#endif // TEST_STD_VER >= 23 #if TEST_STD_VER > 17 static_assert(tests()); #endif From f698e05362fa813a0070562622e57de06c9cc037 Mon Sep 17 00:00:00 2001 From: Mark de Wever Date: Wed, 17 Jul 2024 08:00:31 +0200 Subject: [PATCH 2/2] Address review comments. --- .../vector.bool/shrink_to_fit.pass.cpp | 40 ++++++++++--------- 1 file changed, 22 insertions(+), 18 deletions(-) diff --git a/libcxx/test/std/containers/sequences/vector.bool/shrink_to_fit.pass.cpp b/libcxx/test/std/containers/sequences/vector.bool/shrink_to_fit.pass.cpp index 18661ad825c6b..f8bcee31964bb 100644 --- a/libcxx/test/std/containers/sequences/vector.bool/shrink_to_fit.pass.cpp +++ b/libcxx/test/std/containers/sequences/vector.bool/shrink_to_fit.pass.cpp @@ -40,23 +40,23 @@ TEST_CONSTEXPR_CXX20 bool tests() } #if TEST_STD_VER >= 23 -std::size_t min_bytes = 1000; - template struct increasing_allocator { - using value_type = T; - increasing_allocator() = default; + using value_type = T; + std::size_t min_elements = 1000; + increasing_allocator() = default; + template - increasing_allocator(const increasing_allocator&) noexcept {} - std::allocation_result allocate_at_least(std::size_t n) { - std::size_t allocation_amount = n * sizeof(T); - if (allocation_amount < min_bytes) - allocation_amount = min_bytes; - min_bytes += 1000; - return {static_cast(::operator new(allocation_amount)), allocation_amount / sizeof(T)}; + constexpr increasing_allocator(const increasing_allocator& other) noexcept : min_elements(other.min_elements) {} + + constexpr std::allocation_result allocate_at_least(std::size_t n) { + if (n < min_elements) + n = min_elements; + min_elements += 1000; + return std::allocator{}.allocate_at_least(n); } - T* allocate(std::size_t n) { return allocate_at_least(n).ptr; } - void deallocate(T* p, std::size_t) noexcept { ::operator delete(static_cast(p)); } + constexpr T* allocate(std::size_t n) { return allocate_at_least(n).ptr; } + constexpr void deallocate(T* p, std::size_t n) noexcept { std::allocator{}.deallocate(p, n); } }; template @@ -65,24 +65,28 @@ bool operator==(increasing_allocator, increasing_allocator) { } // https://github.com/llvm/llvm-project/issues/95161 -void test_increasing_allocator() { +constexpr bool test_increasing_allocator() { std::vector> v; v.push_back(1); std::size_t capacity = v.capacity(); v.shrink_to_fit(); assert(v.capacity() <= capacity); assert(v.size() == 1); + + return true; } #endif // TEST_STD_VER >= 23 int main(int, char**) { - tests(); -#if TEST_STD_VER >= 23 - test_increasing_allocator(); -#endif // TEST_STD_VER >= 23 + tests(); #if TEST_STD_VER > 17 static_assert(tests()); #endif +#if TEST_STD_VER >= 23 + test_increasing_allocator(); + static_assert(test_increasing_allocator()); +#endif // TEST_STD_VER >= 23 + return 0; }