[libcxx-commits] [libcxx] [libc++][pstl] Implementation of parallel std::destroy() and std::destroy_n() (PR #211888)

Michael G. Kazakov via libcxx-commits libcxx-commits at lists.llvm.org
Sat Jul 25 05:14:15 PDT 2026


https://github.com/mikekazakov updated https://github.com/llvm/llvm-project/pull/211888

>From 723b523e8e9f681c3271275bb5b1724d858fbb1a Mon Sep 17 00:00:00 2001
From: Michael Kazakov <mike.kazakov at gmail.com>
Date: Fri, 24 Jul 2026 19:58:22 +0100
Subject: [PATCH 1/4] Added an implementation of destroy() and destroy_n()

---
 libcxx/include/CMakeLists.txt                 |   1 +
 libcxx/include/__memory/pstl.h                |  64 ++++++++
 libcxx/include/__pstl/backend_fwd.h           |  12 ++
 libcxx/include/__pstl/backends/default.h      |  29 ++++
 libcxx/include/memory                         |   1 +
 .../pstl.iterator-requirements.verify.cpp     |   6 +
 .../test/libcxx/transitive_includes/cxx26.csv |   7 +
 .../pstl.exception_handling.pass.cpp          |   9 ++
 .../specialized.destroy/pstl.destroy.pass.cpp | 149 ++++++++++++++++++
 .../pstl.destroy_n.pass.cpp                   | 149 ++++++++++++++++++
 10 files changed, 427 insertions(+)
 create mode 100644 libcxx/include/__memory/pstl.h
 create mode 100644 libcxx/test/std/utilities/memory/specialized.algorithms/specialized.destroy/pstl.destroy.pass.cpp
 create mode 100644 libcxx/test/std/utilities/memory/specialized.algorithms/specialized.destroy/pstl.destroy_n.pass.cpp

diff --git a/libcxx/include/CMakeLists.txt b/libcxx/include/CMakeLists.txt
index b40f586161e62..d87a708998798 100644
--- a/libcxx/include/CMakeLists.txt
+++ b/libcxx/include/CMakeLists.txt
@@ -608,6 +608,7 @@ set(files
   __memory/noexcept_move_assign_container.h
   __memory/out_ptr.h
   __memory/pointer_traits.h
+  __memory/pstl.h  
   __memory/ranges_construct_at.h
   __memory/ranges_destroy.h
   __memory/ranges_uninitialized_algorithms.h
diff --git a/libcxx/include/__memory/pstl.h b/libcxx/include/__memory/pstl.h
new file mode 100644
index 0000000000000..a4fc727f3bf72
--- /dev/null
+++ b/libcxx/include/__memory/pstl.h
@@ -0,0 +1,64 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef _LIBCPP___MEMORY_PSTL_H
+#define _LIBCPP___MEMORY_PSTL_H
+
+#include <__config>
+
+#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
+#  pragma GCC system_header
+#endif
+
+_LIBCPP_PUSH_MACROS
+#include <__undef_macros>
+
+#if _LIBCPP_HAS_EXPERIMENTAL_PSTL && _LIBCPP_STD_VER >= 17
+
+#  include <__iterator/cpp17_iterator_concepts.h>
+#  include <__iterator/iterator_traits.h>
+#  include <__pstl/backend.h>
+#  include <__pstl/dispatch.h>
+#  include <__pstl/handle_exception.h>
+#  include <__type_traits/enable_if.h>
+#  include <__type_traits/is_execution_policy.h>
+#  include <__type_traits/remove_cvref.h>
+#  include <__utility/forward.h>
+#  include <__utility/move.h>
+
+_LIBCPP_BEGIN_NAMESPACE_STD
+
+template <class _ExecutionPolicy,
+          class _ForwardIterator,
+          class _RawPolicy                                    = __remove_cvref_t<_ExecutionPolicy>,
+          enable_if_t<is_execution_policy_v<_RawPolicy>, int> = 0>
+_LIBCPP_HIDE_FROM_ABI void destroy(_ExecutionPolicy&& __policy, _ForwardIterator __first, _ForwardIterator __last) {
+  _LIBCPP_REQUIRE_CPP17_FORWARD_ITERATOR(_ForwardIterator, "destroy requires ForwardIterators");
+  using _Implementation = __pstl::__dispatch<__pstl::__destroy, __pstl::__current_configuration, _RawPolicy>;
+  __pstl::__handle_exception<_Implementation>(
+      std::forward<_ExecutionPolicy>(__policy), std::move(__first), std::move(__last));
+}
+
+template <class _ExecutionPolicy,
+          class _ForwardIterator,
+          class _Size,
+          class _RawPolicy                                    = __remove_cvref_t<_ExecutionPolicy>,
+          enable_if_t<is_execution_policy_v<_RawPolicy>, int> = 0>
+_LIBCPP_HIDE_FROM_ABI void destroy_n(_ExecutionPolicy&& __policy, _ForwardIterator __first, _Size __n) {
+  _LIBCPP_REQUIRE_CPP17_FORWARD_ITERATOR(_ForwardIterator, "destroy_n requires ForwardIterators");
+  using _Implementation = __pstl::__dispatch<__pstl::__destroy_n, __pstl::__current_configuration, _RawPolicy>;
+  __pstl::__handle_exception<_Implementation>(std::forward<_ExecutionPolicy>(__policy), std::move(__first), __n);
+}
+
+_LIBCPP_END_NAMESPACE_STD
+
+#endif // _LIBCPP_HAS_EXPERIMENTAL_PSTL && _LIBCPP_STD_VER >= 17
+
+_LIBCPP_POP_MACROS
+
+#endif // _LIBCPP___MEMORY_PSTL_H
diff --git a/libcxx/include/__pstl/backend_fwd.h b/libcxx/include/__pstl/backend_fwd.h
index 2ba5b4434fc33..3372d7c5b3a9b 100644
--- a/libcxx/include/__pstl/backend_fwd.h
+++ b/libcxx/include/__pstl/backend_fwd.h
@@ -324,6 +324,18 @@ struct __adjacent_difference;
 // operator()(_Policy&& __policy, _ForwardIterator1 __first1, _ForwardIterator1 __last1,
 //                                _ForwardIterator2 __first2, _BinaryOperation &&__op) const noexcept;
 
+template <class _Backend, class _ExecutionPolicy>
+struct __destroy;
+// template <class _Policy, class _ForwardIterator>
+// optional<__empty>
+// operator()(_Policy&& __policy, _ForwardIterator __first, _ForwardIterator __last) const noexcept;
+
+template <class _Backend, class _ExecutionPolicy>
+struct __destroy_n;
+// template <class _Policy, class _ForwardIterator, class Size>
+// optional<__empty>
+// operator()(_Policy&& __policy, _ForwardIterator __first, Size __n) const noexcept;
+
 } // namespace __pstl
 _LIBCPP_END_NAMESPACE_STD
 
diff --git a/libcxx/include/__pstl/backends/default.h b/libcxx/include/__pstl/backends/default.h
index 28bc75be147aa..affcea6a678e1 100644
--- a/libcxx/include/__pstl/backends/default.h
+++ b/libcxx/include/__pstl/backends/default.h
@@ -24,6 +24,8 @@
 #include <__iterator/iterator_traits.h>
 #include <__iterator/next.h>
 #include <__iterator/reverse_iterator.h>
+#include <__memory/addressof.h>
+#include <__memory/construct_at.h>
 #include <__pstl/backend_fwd.h>
 #include <__pstl/dispatch.h>
 #include <__type_traits/desugars_to.h>
@@ -65,6 +67,8 @@ namespace __pstl {
 //
 // for_each family
 // ---------------
+// - destroy
+// - destroy_n
 // - for_each_n
 // - fill
 // - fill_n
@@ -215,6 +219,31 @@ struct __find_first_of<__default_backend_tag, _ExecutionPolicy> {
 //////////////////////////////////////////////////////////////
 // for_each family
 //////////////////////////////////////////////////////////////
+
+template <class _ExecutionPolicy>
+struct __destroy<__default_backend_tag, _ExecutionPolicy> {
+  template <class _Policy, class _ForwardIterator>
+  optional<__empty> operator()(_Policy&& __policy, _ForwardIterator __first, _ForwardIterator __last) const noexcept {
+    using _ForEach = __dispatch<__for_each, __current_configuration, _ExecutionPolicy>;
+    using _Ref     = __iterator_reference<_ForwardIterator>;
+    return _ForEach()(__policy, std::move(__first), std::move(__last), [&](_Ref __element) {
+      std::destroy_at(std::addressof(__element));
+    });
+  }
+};
+
+template <class _ExecutionPolicy>
+struct __destroy_n<__default_backend_tag, _ExecutionPolicy> {
+  template <class _Policy, class _ForwardIterator, class Size>
+  optional<__empty> operator()(_Policy&& __policy, _ForwardIterator __first, Size __n) const noexcept {
+    using _ForEachN = __dispatch<__for_each_n, __current_configuration, _ExecutionPolicy>;
+    using _Ref      = __iterator_reference<_ForwardIterator>;
+    return _ForEachN()(__policy, std::move(__first), __n, [&](_Ref __element) {
+      std::destroy_at(std::addressof(__element));
+    });
+  }
+};
+
 template <class _ExecutionPolicy>
 struct __for_each_n<__default_backend_tag, _ExecutionPolicy> {
   template <class _Policy, class _ForwardIterator, class _Size, class _Function>
diff --git a/libcxx/include/memory b/libcxx/include/memory
index d5d48ed9a9e6d..29666746f13ec 100644
--- a/libcxx/include/memory
+++ b/libcxx/include/memory
@@ -963,6 +963,7 @@ template<class Pointer = void, class Smart, class... Args>
 #  if _LIBCPP_STD_VER >= 17
 #    include <__memory/construct_at.h>
 #    include <__memory/destroy.h>
+#    include <__memory/pstl.h>
 #  endif
 
 #  if _LIBCPP_STD_VER >= 20
diff --git a/libcxx/test/libcxx/algorithms/pstl.iterator-requirements.verify.cpp b/libcxx/test/libcxx/algorithms/pstl.iterator-requirements.verify.cpp
index 402d8fbf9131a..baedbf4addb21 100644
--- a/libcxx/test/libcxx/algorithms/pstl.iterator-requirements.verify.cpp
+++ b/libcxx/test/libcxx/algorithms/pstl.iterator-requirements.verify.cpp
@@ -28,6 +28,7 @@
 #include <cstddef>
 #include <execution>
 #include <numeric>
+#include <memory>
 
 #include "test_iterators.h"
 
@@ -70,6 +71,11 @@ void f(non_forward_iterator non_fwd,
     (void)std::count_if(pol, non_fwd, non_fwd, pred); // expected-error@*:* {{static assertion failed: count_if}}
   }
 
+  {
+    (void)std::destroy(pol, non_fwd, non_fwd); // expected-error@*:* {{static assertion failed: destroy}}
+    (void)std::destroy_n(pol, non_fwd, n);     // expected-error@*:* {{static assertion failed: destroy_n}}
+  }
+
   {
     (void)std::equal(pol, non_fwd, non_fwd, it);       // expected-error@*:* {{static assertion failed: equal}}
     (void)std::equal(pol, it, it, non_fwd);            // expected-error@*:* {{static assertion failed: equal}}
diff --git a/libcxx/test/libcxx/transitive_includes/cxx26.csv b/libcxx/test/libcxx/transitive_includes/cxx26.csv
index f712f36caa9fc..e8e7e6680af0b 100644
--- a/libcxx/test/libcxx/transitive_includes/cxx26.csv
+++ b/libcxx/test/libcxx/transitive_includes/cxx26.csv
@@ -625,11 +625,18 @@ mdspan limits
 mdspan span
 mdspan stdexcept
 mdspan version
+memory cctype
+memory climits
 memory compare
 memory cstdint
 memory cstring
+memory ctime
+memory cwchar
+memory cwctype
 memory initializer_list
 memory limits
+memory optional
+memory ratio
 memory tuple
 memory typeinfo
 memory version
diff --git a/libcxx/test/std/algorithms/pstl.exception_handling.pass.cpp b/libcxx/test/std/algorithms/pstl.exception_handling.pass.cpp
index e00046fdb1dd6..bbe27b6ef42a6 100644
--- a/libcxx/test/std/algorithms/pstl.exception_handling.pass.cpp
+++ b/libcxx/test/std/algorithms/pstl.exception_handling.pass.cpp
@@ -21,6 +21,7 @@
 
 #include <algorithm>
 #include <numeric>
+#include <memory>
 
 #include "check_assertion.h"
 #include "test_execution_policies.h"
@@ -127,6 +128,14 @@ int main(int, char**) {
         assert_non_throwing([=, &policy] { (void)std::count_if(policy, std::move(first1), std::move(last1), pred); });
       }
 
+      {
+        // destroy(first, last)
+        assert_non_throwing([=, &policy] { (void)std::destroy(policy, std::move(first1), std::move(last1)); });
+
+        // destroy_n(first, n)
+        assert_non_throwing([=, &policy] { (void)std::destroy_n(policy, std::move(first1), n); });
+      }
+
       {
         auto binary_pred = maybe_throw(tokens[5], [](int x, int y) -> bool { return x == y; });
 
diff --git a/libcxx/test/std/utilities/memory/specialized.algorithms/specialized.destroy/pstl.destroy.pass.cpp b/libcxx/test/std/utilities/memory/specialized.algorithms/specialized.destroy/pstl.destroy.pass.cpp
new file mode 100644
index 0000000000000..a2ffccefb8608
--- /dev/null
+++ b/libcxx/test/std/utilities/memory/specialized.algorithms/specialized.destroy/pstl.destroy.pass.cpp
@@ -0,0 +1,149 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+// REQUIRES: std-at-least-c++17
+
+// UNSUPPORTED: libcpp-has-no-incomplete-pstl
+
+// template <class ExecutionPolicy,
+//           class ForwardIterator>
+//   void destroy(ExecutionPolicy&& exec, ForwardIterator first, ForwardIterator last);
+
+#include <atomic>
+#include <algorithm>
+#include <cassert>
+#include <functional>
+#include <iterator>
+#include <limits>
+#include <memory>
+#include <numeric>
+
+#include "test_execution_policies.h"
+#include "test_iterators.h"
+#include "test_macros.h"
+#include "type_algorithms.h"
+#include "runway_sample.h"
+
+EXECUTION_POLICY_SFINAE_TEST(destroy);
+
+static_assert(sfinae_test_destroy<int, int*, int*>);
+static_assert(!sfinae_test_destroy<std::execution::parallel_policy, int*, int*>);
+
+struct Counted {
+  std::atomic_int* counter_;
+  Counted(std::atomic_int* counter) : counter_(counter) { counter_->fetch_add(1); }
+  Counted(Counted const& other) : counter_(other.counter_) { counter_->fetch_add(1); }
+  ~Counted() { counter_->fetch_sub(1); }
+  friend void operator&(Counted) = delete;
+};
+
+template <class Iter>
+struct TestCounted {
+  template <class ExecutionPolicy>
+  void operator()(ExecutionPolicy&& policy) {
+    {
+      // Test destroy() with a range of Counted objects.
+      // Ranges vary in size from 0 to 1073.
+      // Each object has its own counter.
+      std::atomic_int counters[1073];
+      std::fill_n(std::begin(counters), std::size(counters), 0);
+
+      using Alloc = std::allocator<Counted>;
+      Alloc alloc;
+      Counted* pool = std::allocator_traits<Alloc>::allocate(alloc, std::size(counters));
+
+      runway_sample(std::size(counters) + 1, [&](size_t size) {
+        for (std::size_t i = 0; i < size; ++i) {
+          std::allocator_traits<Alloc>::construct(alloc, std::addressof(pool[i]), &counters[i]);
+        }
+        assert(std::all_of(std::begin(counters), std::begin(counters) + size, [](auto& x) { return x == 1; }));
+
+        std::destroy(policy, Iter(pool), Iter(pool + size));
+        ASSERT_SAME_TYPE(decltype(std::destroy(policy, Iter(pool), Iter(pool + size))), void);
+        assert(std::all_of(std::begin(counters), std::begin(counters) + size, [](auto& x) { return x == 0; }));
+      });
+
+      std::allocator_traits<Alloc>::deallocate(alloc, pool, std::size(counters));
+    }
+  }
+};
+
+#if TEST_STD_VER > 17
+template <class Iter>
+struct TestArrayCounted3 {
+  template <class ExecutionPolicy>
+  void operator()(ExecutionPolicy&& policy) {
+    {
+      // Test destroy() with a range of 5 Counted[3] objects.
+      // A shared counter is used for all objects.
+      using Array             = Counted[3];
+      using Alloc             = std::allocator<Array>;
+      std::atomic_int counter = 0;
+      Alloc alloc;
+      Array* pool = std::allocator_traits<Alloc>::allocate(alloc, 5);
+
+      for (Array* p = pool; p != pool + 5; ++p) {
+        Array& arr = *p;
+        for (int i = 0; i != 3; ++i) {
+          std::allocator_traits<Alloc>::construct(alloc, std::addressof(arr[i]), &counter);
+        }
+      }
+      assert(counter == 5 * 3);
+
+      std::destroy(policy, Iter(pool), Iter(pool + 5));
+      ASSERT_SAME_TYPE(decltype(std::destroy(policy, Iter(pool), Iter(pool + 5))), void);
+      assert(counter == 0);
+
+      std::allocator_traits<Alloc>::deallocate(alloc, pool, 5);
+    }
+  }
+};
+
+template <class Iter>
+struct TestArrayCounted32 {
+  template <class ExecutionPolicy>
+  void operator()(ExecutionPolicy&& policy) {
+    {
+      // Test destroy() with a range of 5 Counted[3][2] objects.
+      // A shared counter is used for all objects.
+      using Array             = Counted[3][2];
+      using Alloc             = std::allocator<Array>;
+      std::atomic_int counter = 0;
+      Alloc alloc;
+      Array* pool = std::allocator_traits<Alloc>::allocate(alloc, 5);
+
+      for (Array* p = pool; p != pool + 5; ++p) {
+        Array& arr = *p;
+        for (int i = 0; i != 3; ++i) {
+          for (int j = 0; j != 2; ++j) {
+            std::allocator_traits<Alloc>::construct(alloc, std::addressof(arr[i][j]), &counter);
+          }
+        }
+      }
+      assert(counter == 5 * 3 * 2);
+
+      std::destroy(policy, Iter(pool), Iter(pool + 5));
+      ASSERT_SAME_TYPE(decltype(std::destroy(policy, Iter(pool), Iter(pool + 5))), void);
+      assert(counter == 0);
+
+      std::allocator_traits<Alloc>::deallocate(alloc, pool, 5);
+    }
+  }
+};
+#endif // TEST_STD_VER > 17
+
+int main(int, char**) {
+  types::for_each(types::forward_iterator_list<Counted*>{}, TestIteratorWithPolicies<TestCounted>{});
+#if TEST_STD_VER > 17
+  using CountedArray3 = Counted[3];
+  types::for_each(types::forward_iterator_list<CountedArray3*>{}, TestIteratorWithPolicies<TestArrayCounted3>{});
+  using CountedArray32 = Counted[3][2];
+  types::for_each(types::forward_iterator_list<CountedArray32*>{}, TestIteratorWithPolicies<TestArrayCounted32>{});
+#endif // TEST_STD_VER > 17
+  return 0;
+}
diff --git a/libcxx/test/std/utilities/memory/specialized.algorithms/specialized.destroy/pstl.destroy_n.pass.cpp b/libcxx/test/std/utilities/memory/specialized.algorithms/specialized.destroy/pstl.destroy_n.pass.cpp
new file mode 100644
index 0000000000000..e48e3010373df
--- /dev/null
+++ b/libcxx/test/std/utilities/memory/specialized.algorithms/specialized.destroy/pstl.destroy_n.pass.cpp
@@ -0,0 +1,149 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+// REQUIRES: std-at-least-c++17
+
+// UNSUPPORTED: libcpp-has-no-incomplete-pstl
+
+// template <class ExecutionPolicy,
+//           class ForwardIterator,
+//           class Size>
+//   void destroy_n(ExecutionPolicy&& exec, ForwardIterator first, Size n);
+
+#include <atomic>
+#include <algorithm>
+#include <cassert>
+#include <functional>
+#include <iterator>
+#include <limits>
+#include <memory>
+#include <numeric>
+
+#include "test_execution_policies.h"
+#include "test_iterators.h"
+#include "test_macros.h"
+#include "type_algorithms.h"
+#include "runway_sample.h"
+
+EXECUTION_POLICY_SFINAE_TEST(destroy_n);
+
+static_assert(sfinae_test_destroy_n<int, int*, int>);
+static_assert(!sfinae_test_destroy_n<std::execution::parallel_policy, int*, int>);
+
+struct Counted {
+  std::atomic_int* counter_;
+  Counted(std::atomic_int* counter) : counter_(counter) { counter_->fetch_add(1); }
+  Counted(Counted const& other) : counter_(other.counter_) { counter_->fetch_add(1); }
+  ~Counted() { counter_->fetch_sub(1); }
+  friend void operator&(Counted) = delete;
+};
+
+template <class Iter>
+struct TestCounted {
+  template <class ExecutionPolicy>
+  void operator()(ExecutionPolicy&& policy) {
+    {
+      // Test destroy_n() with a range of Counted objects.
+      // Ranges vary in size from 0 to 1073.
+      // Each object has its own counter.
+      std::atomic_int counters[1073];
+      std::fill_n(std::begin(counters), std::size(counters), 0);
+
+      using Alloc = std::allocator<Counted>;
+      Alloc alloc;
+      Counted* pool = std::allocator_traits<Alloc>::allocate(alloc, std::size(counters));
+
+      runway_sample(std::size(counters) + 1, [&](size_t size) {
+        for (std::size_t i = 0; i < size; ++i) {
+          std::allocator_traits<Alloc>::construct(alloc, std::addressof(pool[i]), &counters[i]);
+        }
+        assert(std::all_of(std::begin(counters), std::begin(counters) + size, [](auto& x) { return x == 1; }));
+
+        std::destroy_n(policy, Iter(pool), size);
+        ASSERT_SAME_TYPE(decltype(std::destroy_n(policy, Iter(pool), size)), void);
+        assert(std::all_of(std::begin(counters), std::begin(counters) + size, [](auto& x) { return x == 0; }));
+      });
+
+      std::allocator_traits<Alloc>::deallocate(alloc, pool, std::size(counters));
+    }
+  }
+};
+
+#if TEST_STD_VER > 17
+template <class Iter>
+struct TestArrayCounted3 {
+  template <class ExecutionPolicy>
+  void operator()(ExecutionPolicy&& policy) {
+    {
+      // Test destroy_n() with a range of 5 Counted[3] objects.
+      // A shared counter is used for all objects.
+      using Array             = Counted[3];
+      using Alloc             = std::allocator<Array>;
+      std::atomic_int counter = 0;
+      Alloc alloc;
+      Array* pool = std::allocator_traits<Alloc>::allocate(alloc, 5);
+
+      for (Array* p = pool; p != pool + 5; ++p) {
+        Array& arr = *p;
+        for (int i = 0; i != 3; ++i) {
+          std::allocator_traits<Alloc>::construct(alloc, std::addressof(arr[i]), &counter);
+        }
+      }
+      assert(counter == 5 * 3);
+
+      std::destroy_n(policy, Iter(pool), 5);
+      ASSERT_SAME_TYPE(decltype(std::destroy_n(policy, Iter(pool), 5)), void);
+      assert(counter == 0);
+
+      std::allocator_traits<Alloc>::deallocate(alloc, pool, 5);
+    }
+  }
+};
+
+template <class Iter>
+struct TestArrayCounted32 {
+  template <class ExecutionPolicy>
+  void operator()(ExecutionPolicy&& policy) {
+    {
+      // Test destroy_n() with a range of 5 Counted[3][2] objects.
+      // A shared counter is used for all objects.
+      using Array             = Counted[3][2];
+      using Alloc             = std::allocator<Array>;
+      std::atomic_int counter = 0;
+      Alloc alloc;
+      Array* pool = std::allocator_traits<Alloc>::allocate(alloc, 5);
+
+      for (Array* p = pool; p != pool + 5; ++p) {
+        Array& arr = *p;
+        for (int i = 0; i != 3; ++i) {
+          for (int j = 0; j != 2; ++j) {
+            std::allocator_traits<Alloc>::construct(alloc, std::addressof(arr[i][j]), &counter);
+          }
+        }
+      }
+      assert(counter == 5 * 3 * 2);
+      std::destroy_n(policy, Iter(pool), 5);
+      ASSERT_SAME_TYPE(decltype(std::destroy_n(policy, Iter(pool), 5)), void);
+      assert(counter == 0);
+
+      std::allocator_traits<Alloc>::deallocate(alloc, pool, 5);
+    }
+  }
+};
+#endif // TEST_STD_VER > 17
+
+int main(int, char**) {
+  types::for_each(types::forward_iterator_list<Counted*>{}, TestIteratorWithPolicies<TestCounted>{});
+#if TEST_STD_VER > 17
+  using CountedArray3 = Counted[3];
+  types::for_each(types::forward_iterator_list<CountedArray3*>{}, TestIteratorWithPolicies<TestArrayCounted3>{});
+  using CountedArray32 = Counted[3][2];
+  types::for_each(types::forward_iterator_list<CountedArray32*>{}, TestIteratorWithPolicies<TestArrayCounted32>{});
+#endif // TEST_STD_VER > 17
+  return 0;
+}

>From 057406a814308225558a511309a6f71fe733634d Mon Sep 17 00:00:00 2001
From: Michael Kazakov <mike.kazakov at gmail.com>
Date: Fri, 24 Jul 2026 20:13:21 +0100
Subject: [PATCH 2/4] Added pstl.h to the modulemap

---
 libcxx/include/module.modulemap.in | 1 +
 1 file changed, 1 insertion(+)

diff --git a/libcxx/include/module.modulemap.in b/libcxx/include/module.modulemap.in
index 39b4e0bb986c6..f0fcf72f178e5 100644
--- a/libcxx/include/module.modulemap.in
+++ b/libcxx/include/module.modulemap.in
@@ -1682,6 +1682,7 @@ module std {
     module noexcept_move_assign_container     { header "__memory/noexcept_move_assign_container.h" }
     module out_ptr                            { header "__memory/out_ptr.h" }
     module pointer_traits                     { header "__memory/pointer_traits.h" }
+    module pstl                               { header "__memory/pstl.h" }    
     module ranges_construct_at                { header "__memory/ranges_construct_at.h" }
     module ranges_destroy                     { header "__memory/ranges_destroy.h" }
     module ranges_uninitialized_algorithms {

>From 7833b7f53352b74379d9f49ed2de3e2f7821aa68 Mon Sep 17 00:00:00 2001
From: Michael Kazakov <mike.kazakov at gmail.com>
Date: Fri, 24 Jul 2026 20:37:17 +0100
Subject: [PATCH 3/4] Added a missing underscore

---
 libcxx/include/__pstl/backend_fwd.h      | 4 ++--
 libcxx/include/__pstl/backends/default.h | 4 ++--
 2 files changed, 4 insertions(+), 4 deletions(-)

diff --git a/libcxx/include/__pstl/backend_fwd.h b/libcxx/include/__pstl/backend_fwd.h
index 3372d7c5b3a9b..6cb6b2b36bc9f 100644
--- a/libcxx/include/__pstl/backend_fwd.h
+++ b/libcxx/include/__pstl/backend_fwd.h
@@ -332,9 +332,9 @@ struct __destroy;
 
 template <class _Backend, class _ExecutionPolicy>
 struct __destroy_n;
-// template <class _Policy, class _ForwardIterator, class Size>
+// template <class _Policy, class _ForwardIterator, class _Size>
 // optional<__empty>
-// operator()(_Policy&& __policy, _ForwardIterator __first, Size __n) const noexcept;
+// operator()(_Policy&& __policy, _ForwardIterator __first, _Size __n) const noexcept;
 
 } // namespace __pstl
 _LIBCPP_END_NAMESPACE_STD
diff --git a/libcxx/include/__pstl/backends/default.h b/libcxx/include/__pstl/backends/default.h
index affcea6a678e1..01f641c63d99c 100644
--- a/libcxx/include/__pstl/backends/default.h
+++ b/libcxx/include/__pstl/backends/default.h
@@ -234,8 +234,8 @@ struct __destroy<__default_backend_tag, _ExecutionPolicy> {
 
 template <class _ExecutionPolicy>
 struct __destroy_n<__default_backend_tag, _ExecutionPolicy> {
-  template <class _Policy, class _ForwardIterator, class Size>
-  optional<__empty> operator()(_Policy&& __policy, _ForwardIterator __first, Size __n) const noexcept {
+  template <class _Policy, class _ForwardIterator, class _Size>
+  optional<__empty> operator()(_Policy&& __policy, _ForwardIterator __first, _Size __n) const noexcept {
     using _ForEachN = __dispatch<__for_each_n, __current_configuration, _ExecutionPolicy>;
     using _Ref      = __iterator_reference<_ForwardIterator>;
     return _ForEachN()(__policy, std::move(__first), __n, [&](_Ref __element) {

>From 458d0e5321ecf96d050e32c28971df5a3449a6e1 Mon Sep 17 00:00:00 2001
From: Michael Kazakov <mike.kazakov at gmail.com>
Date: Sat, 25 Jul 2026 13:13:59 +0100
Subject: [PATCH 4/4] transitive_includes updates

---
 libcxx/test/libcxx/transitive_includes/cxx17.csv | 16 ++++++++++++++++
 libcxx/test/libcxx/transitive_includes/cxx20.csv |  6 ++++++
 libcxx/test/libcxx/transitive_includes/cxx23.csv |  7 +++++++
 3 files changed, 29 insertions(+)

diff --git a/libcxx/test/libcxx/transitive_includes/cxx17.csv b/libcxx/test/libcxx/transitive_includes/cxx17.csv
index a93fb1e26196b..b1319b212d73d 100644
--- a/libcxx/test/libcxx/transitive_includes/cxx17.csv
+++ b/libcxx/test/libcxx/transitive_includes/cxx17.csv
@@ -143,6 +143,7 @@ barrier iterator
 barrier limits
 barrier memory
 barrier new
+barrier optional
 barrier ratio
 barrier stdexcept
 barrier tuple
@@ -1389,7 +1390,9 @@ memory initializer_list
 memory iosfwd
 memory iterator
 memory limits
+memory memory
 memory new
+memory optional
 memory ratio
 memory stdexcept
 memory tuple
@@ -1550,6 +1553,7 @@ optional iterator
 optional limits
 optional memory
 optional new
+optional optional
 optional ratio
 optional stdexcept
 optional tuple
@@ -1855,6 +1859,7 @@ scoped_allocator iterator
 scoped_allocator limits
 scoped_allocator memory
 scoped_allocator new
+scoped_allocator optional
 scoped_allocator ratio
 scoped_allocator stdexcept
 scoped_allocator tuple
@@ -2120,8 +2125,19 @@ stdexcept new
 stdexcept type_traits
 stdexcept typeinfo
 stdexcept version
+stop_token atomic
+stop_token climits
+stop_token cmath
+stop_token compare
 stop_token cstddef
+stop_token cstdint
+stop_token cstdlib
+stop_token cstring
+stop_token ctime
 stop_token iosfwd
+stop_token limits
+stop_token ratio
+stop_token type_traits
 stop_token version
 streambuf algorithm
 streambuf atomic
diff --git a/libcxx/test/libcxx/transitive_includes/cxx20.csv b/libcxx/test/libcxx/transitive_includes/cxx20.csv
index ded0dd1a51c16..e1c6d88a9f8f9 100644
--- a/libcxx/test/libcxx/transitive_includes/cxx20.csv
+++ b/libcxx/test/libcxx/transitive_includes/cxx20.csv
@@ -50,6 +50,7 @@ any iterator
 any limits
 any memory
 any new
+any optional
 any ratio
 any stdexcept
 any tuple
@@ -123,6 +124,7 @@ barrier iterator
 barrier limits
 barrier memory
 barrier new
+barrier optional
 barrier ratio
 barrier stdexcept
 barrier tuple
@@ -1378,7 +1380,9 @@ memory initializer_list
 memory iosfwd
 memory iterator
 memory limits
+memory memory
 memory new
+memory optional
 memory ratio
 memory stdexcept
 memory tuple
@@ -1539,6 +1543,7 @@ optional iterator
 optional limits
 optional memory
 optional new
+optional optional
 optional ratio
 optional stdexcept
 optional tuple
@@ -1869,6 +1874,7 @@ scoped_allocator iterator
 scoped_allocator limits
 scoped_allocator memory
 scoped_allocator new
+scoped_allocator optional
 scoped_allocator ratio
 scoped_allocator stdexcept
 scoped_allocator tuple
diff --git a/libcxx/test/libcxx/transitive_includes/cxx23.csv b/libcxx/test/libcxx/transitive_includes/cxx23.csv
index 53ae6c9b4e591..4fa3fab31f46e 100644
--- a/libcxx/test/libcxx/transitive_includes/cxx23.csv
+++ b/libcxx/test/libcxx/transitive_includes/cxx23.csv
@@ -673,11 +673,18 @@ mdspan limits
 mdspan span
 mdspan stdexcept
 mdspan version
+memory cctype
+memory climits
 memory compare
 memory cstdint
 memory cstring
+memory ctime
+memory cwchar
+memory cwctype
 memory initializer_list
 memory limits
+memory optional
+memory ratio
 memory tuple
 memory typeinfo
 memory version



More information about the libcxx-commits mailing list