[libcxx-commits] [libcxx] [libc++] Fix shrink_to_fit to swap buffer only when capacity is strictly smaller (PR #127321)

Nikolas Klauser via libcxx-commits libcxx-commits at lists.llvm.org
Tue Feb 18 05:51:13 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
----------------
philnik777 wrote:

Yes, `allocator_traits` prefers `allocate_at_least` if it exists. If we remove it here, it doesn't exist, so `allocator_traits` calls the `allocate` below. Am I missing something?

https://github.com/llvm/llvm-project/pull/127321


More information about the libcxx-commits mailing list