[libcxx-commits] [libcxx] [libc++] Fix shrink_to_fit to swap buffer only when capacity is strictly smaller (PR #127321)
Peng Liu via libcxx-commits
libcxx-commits at lists.llvm.org
Tue Feb 18 06:07:47 PST 2025
================
@@ -49,4 +50,89 @@ TEST_CONSTEXPR_CXX20 bool operator==(increasing_allocator<T>, increasing_allocat
return true;
}
+template <std::size_t MinAllocSize, typename T>
+class min_size_allocator {
+public:
+ using value_type = T;
+ min_size_allocator() = default;
+
+ template <typename U>
+ TEST_CONSTEXPR_CXX20 min_size_allocator(const min_size_allocator<MinAllocSize, U>&) TEST_NOEXCEPT {}
+
+#if TEST_STD_VER >= 23
+ TEST_CONSTEXPR_CXX23 std::allocation_result<T*> allocate_at_least(std::size_t n) {
+ if (n < MinAllocSize)
+ n = MinAllocSize;
+ return std::allocator<T>{}.allocate_at_least(n);
+ }
+#endif // TEST_STD_VER >= 23
+
+ TEST_NODISCARD TEST_CONSTEXPR_CXX20 T* allocate(std::size_t n) {
+ if (n < MinAllocSize)
+ n = MinAllocSize;
+ return std::allocator<T>().allocate(n);
+ }
+
+ TEST_CONSTEXPR_CXX20 void deallocate(T* p, std::size_t n) TEST_NOEXCEPT { std::allocator<T>().deallocate(p, n); }
+
+ template <typename U>
+ struct rebind {
+ using other = min_size_allocator<MinAllocSize, U>;
+ };
+};
+
+template <std::size_t MinAllocSize, typename T, typename U>
+TEST_CONSTEXPR_CXX20 bool
+operator==(const min_size_allocator<MinAllocSize, T>&, const min_size_allocator<MinAllocSize, U>&) {
+ return true;
+}
+
+template <std::size_t MinAllocSize, typename T, typename U>
+TEST_CONSTEXPR_CXX20 bool
+operator!=(const min_size_allocator<MinAllocSize, T>&, const min_size_allocator<MinAllocSize, U>&) {
+ return false;
+}
+
+template <typename T>
+class pow2_allocator {
+public:
+ using value_type = T;
+ pow2_allocator() = default;
+
+ template <typename U>
+ TEST_CONSTEXPR_CXX20 pow2_allocator(const pow2_allocator<U>&) TEST_NOEXCEPT {}
+
+#if TEST_STD_VER >= 23
+ TEST_CONSTEXPR_CXX23 std::allocation_result<T*> allocate_at_least(std::size_t n) {
+ return std::allocator<T>{}.allocate_at_least(next_power_of_two(n));
+ }
+#endif // TEST_STD_VER >= 23
+
+ TEST_NODISCARD TEST_CONSTEXPR_CXX20 T* allocate(std::size_t n) {
+ return std::allocator<T>().allocate(next_power_of_two(n));
+ }
+
+ TEST_CONSTEXPR_CXX20 void deallocate(T* p, std::size_t n) TEST_NOEXCEPT { std::allocator<T>().deallocate(p, n); }
+
+private:
+ TEST_CONSTEXPR_CXX20 std::size_t next_power_of_two(std::size_t n) const {
+ if ((n & (n - 1)) == 0)
+ return n;
+ for (std::size_t shift = 1; shift < std::numeric_limits<std::size_t>::digits; shift <<= 1) {
+ n |= n >> shift;
+ }
+ return n + 1;
+ }
+};
+
+template <typename T, typename U>
+TEST_CONSTEXPR_CXX20 bool operator==(const pow2_allocator<T>&, const pow2_allocator<U>&) {
+ return true;
+}
+
+template <typename T, typename U>
+TEST_CONSTEXPR_CXX20 bool operator!=(const pow2_allocator<T>&, const pow2_allocator<U>&) {
+ return false;
+}
----------------
winner245 wrote:
This allocator and the `min_size_allocator` are two instances of forcing equal allocation sizes so that we can trigger the QoI matter as described in the PR. I just wanted to make sure we have a slightly more intensive test coverage for `shrink_to_fit` given that its implementation can be very tricky.
https://github.com/llvm/llvm-project/pull/127321
More information about the libcxx-commits
mailing list