From 85c5e3ec8fff627bfb43e85c1aac7e77a3838322 Mon Sep 17 00:00:00 2001 From: Cra3z Date: Mon, 16 Nov 2026 16:21:07 +0800 Subject: [PATCH 1/2] Fix task allocation --- .../beman/execution/detail/find_allocator.hpp | 47 ------ include/beman/execution/detail/task.hpp | 134 +++++++++++++----- src/beman/execution/CMakeLists.txt | 2 - src/beman/execution/find_allocator.cppm | 11 -- tests/beman/execution/exec-task.test.cpp | 20 +++ 5 files changed, 116 insertions(+), 98 deletions(-) delete mode 100644 include/beman/execution/detail/find_allocator.hpp delete mode 100644 src/beman/execution/find_allocator.cppm diff --git a/include/beman/execution/detail/find_allocator.hpp b/include/beman/execution/detail/find_allocator.hpp deleted file mode 100644 index 51b61e18..00000000 --- a/include/beman/execution/detail/find_allocator.hpp +++ /dev/null @@ -1,47 +0,0 @@ -// include/beman/execution/detail/find_allocator.hpp -*-C++-*- -// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception - -#ifndef INCLUDED_BEMAN_EXECUTION_DETAIL_FIND_ALLOCATOR -#define INCLUDED_BEMAN_EXECUTION_DETAIL_FIND_ALLOCATOR - -#include -#ifdef BEMAN_HAS_IMPORT_STD -import std; -#else -#include -#include -#endif - -// ---------------------------------------------------------------------------- - -namespace beman::execution::detail { -template -Allocator find_allocator() noexcept { - return Allocator(); -} -template -Allocator find_allocator(const ::std::allocator_arg_t&) noexcept { - static_assert( - requires { - { Allocator() } -> ::std::same_as; - }, "There needs to be an allocator argument following std::allocator_arg"); - return Allocator(); -} - -template -Allocator find_allocator(const ::std::allocator_arg_t&, const Alloc& alloc, const A&...) noexcept { - static_assert( - requires(const Alloc& a) { Allocator(a); }, - "The allocator needs to be constructible from the argument following std::allocator_arg"); - return Allocator(alloc); -} -template -Allocator find_allocator(A0 const&, const A&... a) noexcept { - return ::beman::execution::detail::find_allocator(a...); -} - -} // namespace beman::execution::detail - -// ---------------------------------------------------------------------------- - -#endif diff --git a/include/beman/execution/detail/task.hpp b/include/beman/execution/detail/task.hpp index 816b4ad2..e01ee711 100644 --- a/include/beman/execution/detail/task.hpp +++ b/include/beman/execution/detail/task.hpp @@ -5,6 +5,8 @@ #define INCLUDED_BEMAN_EXECUTION_DETAIL_TASK #include +#include +#include #include #ifdef BEMAN_HAS_IMPORT_STD import std; @@ -26,7 +28,6 @@ import beman.execution.detail.completion_signatures; import beman.execution.detail.decayed_same_as; import beman.execution.detail.env; import beman.execution.detail.env_of_t; -import beman.execution.detail.find_allocator; import beman.execution.detail.forwarding_query; import beman.execution.detail.get_allocator; import beman.execution.detail.get_env; @@ -62,7 +63,6 @@ import beman.execution.detail.unreachable; #include #include #include -#include #include #include #include @@ -127,54 +127,112 @@ class handle { auto get_env() const noexcept { return ::beman::execution::get_env(*this->h); } }; -template -struct allocator_support { - using allocator_traits = ::std::allocator_traits; +struct co_memory { + struct default_align_t { + alignas(__STDCPP_DEFAULT_NEW_ALIGNMENT__)::std::byte _[__STDCPP_DEFAULT_NEW_ALIGNMENT__]; + }; - static auto offset(::std::size_t size) noexcept -> ::std::size_t { - return (size + alignof(Allocator) - 1u) & ~(alignof(Allocator) - 1u); + static constexpr auto ceiling_division(::std::uintptr_t n, ::std::uintptr_t m) noexcept -> ::std::uintptr_t { + assert(m != 0); + return (n + m - 1) / m; } - static auto get_allocator(void* ptr, ::std::size_t size) noexcept -> Allocator* { - ptr = static_cast<::std::byte*>(ptr) + offset(size); - return ::std::launder(reinterpret_cast(ptr)); + static constexpr auto ceiling_align(::std::uintptr_t n, ::std::uintptr_t m) noexcept -> ::std::uintptr_t { + assert(m != 0); + return ceiling_division(n, m) * m; } - template - auto operator new(::std::size_t size, const Args&... args) -> void* { - if constexpr (::std::same_as>) { - Allocator alloc{}; - return allocator_traits::allocate(alloc, size); + static auto align_address(void* ptr, ::std::size_t alignment, ::std::size_t least_offset) noexcept -> void* { + auto least = reinterpret_cast<::std::uintptr_t>(static_cast<::std::byte*>(ptr) + least_offset); + return reinterpret_cast(ceiling_align(least, alignment)); + }; + + using dealloc_fn_t = void (*)(default_align_t*, ::std::size_t) noexcept; + + template + static auto allocate(Alloc alloc, ::std::size_t n) -> void* { + using alloc_t = typename ::std::allocator_traits::template rebind_alloc; + using alloc_traits = ::std::allocator_traits; + alloc_t allocator_(::std::move(alloc)); + if constexpr (::std::default_initializable and + alloc_traits::is_always_equal::value) { // for stateless-allocator + /* + * layout: + * +-----------------+-----------------------+------------------+ + * | coroutine state | deallocating function | possible padding | + * +-----------------+-----------------------+------------------+ + * | | + * `n` bytes `sizeof(dealloc_fn_t)` bytes + */ + auto offset_of_dealloc_fn = n; + n += sizeof(dealloc_fn_t); + void* result = alloc_traits::allocate(allocator_, ceiling_division(n, sizeof(default_align_t))); + dealloc_fn_t dealloc_fn = +[](default_align_t* ptr, ::std::size_t co_state_size) noexcept { + auto length = co_state_size + sizeof(dealloc_fn_t); + alloc_t alloc_(Alloc{}); + alloc_traits::deallocate(alloc_, ptr, ceiling_division(length, alignof(default_align_t))); + }; + ::std::memcpy(static_cast<::std::byte*>(result) + offset_of_dealloc_fn, &dealloc_fn, sizeof(dealloc_fn_t)); + return result; } else { - Allocator alloc{::beman::execution::detail::find_allocator(args...)}; - void* ptr{allocator_traits::allocate(alloc, allocator_support::offset(size) + sizeof(Allocator))}; - try { - new (allocator_support::get_allocator(ptr, size)) Allocator(alloc); - } catch (...) { - allocator_traits::deallocate( - alloc, static_cast<::std::byte*>(ptr), allocator_support::offset(size) + sizeof(Allocator)); - throw; - } - return ptr; + /* + * layout: + * +-----------------+-----------------------+------------------+-----------+------------------+ + * | coroutine state | deallocating function | possible padding | allocator | possible padding | + * +-----------------+-----------------------+------------------+-----------+------------------+ + * | | | + * `n` bytes `sizeof(dealloc_fn_t)` bytes `sizeof(alloc_t)` bytes + */ + static constexpr ::std::size_t max_alignment = (::std::max)(alignof(alloc_t), alignof(default_align_t)); + auto offset_of_dealloc_fn = n; + n += sizeof(dealloc_fn_t); + void* result = alloc_traits::allocate( + allocator_, (n + sizeof(alloc_t) + max_alignment - 1) / alignof(default_align_t)); + dealloc_fn_t dealloc_fn = +[](default_align_t* ptr, ::std::size_t co_state_size) noexcept { + auto length = co_state_size + sizeof(dealloc_fn_t); + auto alloc_ptr_ = ::std::launder(static_cast(align_address(ptr, alignof(alloc_t), length))); + alloc_t alloc_ = ::std::move(*alloc_ptr_); + ::std::destroy_at(alloc_ptr_); + alloc_traits::deallocate( + alloc_, ptr, (length + sizeof(alloc_t) + max_alignment - 1) / alignof(default_align_t)); + }; + ::std::memcpy(static_cast<::std::byte*>(result) + offset_of_dealloc_fn, &dealloc_fn, sizeof(dealloc_fn_t)); + void(::std::construct_at(static_cast(align_address(result, alignof(alloc_t), n)), + ::std::move(allocator_))); + return result; } } - auto operator delete(void* ptr, ::std::size_t size) noexcept -> void { - if constexpr (::std::same_as>) { - Allocator alloc{}; - allocator_traits::deallocate(alloc, static_cast<::std::byte*>(ptr), size); - } else { - Allocator* aptr{allocator_support::get_allocator(ptr, size)}; - Allocator alloc{*aptr}; - aptr->~Allocator(); - allocator_traits::deallocate( - alloc, static_cast<::std::byte*>(ptr), allocator_support::offset(size) + sizeof(Allocator)); - } + static auto deallocate(void* ptr, ::std::size_t n) noexcept -> void { + dealloc_fn_t dealloc_fn = nullptr; + ::std::memcpy(&dealloc_fn, static_cast<::std::byte*>(ptr) + n, sizeof(dealloc_fn_t)); + dealloc_fn(static_cast(ptr), n); + } +}; + +template +struct allocator_support { + auto operator new(::std::size_t n) -> void* { + static_assert(::std::default_initializable); + return co_memory::allocate(Alloc(), n); } - auto operator delete(void* ptr, ::std::size_t size, const auto&...) noexcept -> void { - allocator_support::operator delete(ptr, size); + template + auto operator new(::std::size_t n, ::std::allocator_arg_t, const OtherAlloc& other_alloc, const Args&...) + -> void* { + static_assert(::beman::execution::detail::simple_allocator); + return co_memory::allocate(other_alloc, n); } + + template + auto + operator new(::std::size_t n, const This&, ::std::allocator_arg_t, const OtherAlloc& other_alloc, const Args&...) + -> void* { + static_assert(::beman::execution::detail::simple_allocator); + return co_memory::allocate(other_alloc, n); + } + + auto operator delete(void* ptr, ::std::size_t n) noexcept -> void { co_memory::deallocate(ptr, n); } }; template diff --git a/src/beman/execution/CMakeLists.txt b/src/beman/execution/CMakeLists.txt index de4f5e8e..c9cbb017 100644 --- a/src/beman/execution/CMakeLists.txt +++ b/src/beman/execution/CMakeLists.txt @@ -91,7 +91,6 @@ target_sources( ${PROJECT_SOURCE_DIR}/include/beman/execution/detail/exit_scope_sender.hpp ${PROJECT_SOURCE_DIR}/include/beman/execution/detail/exit_scope_sender_in.hpp ${PROJECT_SOURCE_DIR}/include/beman/execution/detail/exit_scope_sender_of_t.hpp - ${PROJECT_SOURCE_DIR}/include/beman/execution/detail/find_allocator.hpp ${PROJECT_SOURCE_DIR}/include/beman/execution/detail/forward_like.hpp ${PROJECT_SOURCE_DIR}/include/beman/execution/detail/forwarding_query.hpp ${PROJECT_SOURCE_DIR}/include/beman/execution/detail/fwd_env.hpp @@ -327,7 +326,6 @@ if(BEMAN_USE_MODULES) exit_scope_sender.cppm exit_scope_sender_in.cppm exit_scope_sender_of_t.cppm - find_allocator.cppm forward_like.cppm forwarding_query.cppm fwd_env.cppm diff --git a/src/beman/execution/find_allocator.cppm b/src/beman/execution/find_allocator.cppm deleted file mode 100644 index 65d418ea..00000000 --- a/src/beman/execution/find_allocator.cppm +++ /dev/null @@ -1,11 +0,0 @@ -module; -// src/beman/execution/find_allocator.cppm -*-C++-*- -// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception - -#include - -export module beman.execution.detail.find_allocator; - -namespace beman::execution::detail { -export using beman::execution::detail::find_allocator; -} // namespace beman::execution::detail diff --git a/tests/beman/execution/exec-task.test.cpp b/tests/beman/execution/exec-task.test.cpp index c598f991..b6fec466 100644 --- a/tests/beman/execution/exec-task.test.cpp +++ b/tests/beman/execution/exec-task.test.cpp @@ -95,6 +95,11 @@ auto pmr_allocator_task(std::allocator_arg_t, std::pmr::polymorphic_allocator<> co_return 42; } +// the coroutine allocator type is unrelated to the task's allocator_type (std::allocator) +auto foreign_allocator_task(std::allocator_arg_t, std::pmr::polymorphic_allocator<>) -> test_std::task { + co_return 67; +} + auto scheduler_task() -> test_std::task { const auto scheduler = co_await test_std::read_env(test_std::get_scheduler); static_assert(std::same_as, test_std::task_scheduler>); @@ -214,6 +219,20 @@ auto test_task_uses_pmr_allocator_arg() -> void { ASSERT(resource.deallocations == resource.allocations); } +auto test_task_allocator_arg_unrelated_to_allocator_type() -> void { + counting_memory_resource resource; + std::pmr::polymorphic_allocator<> allocator{&resource}; + + auto task = foreign_allocator_task(std::allocator_arg, allocator); + ASSERT(resource.allocations == 1uz); + ASSERT(resource.deallocations == 0uz); + + auto result = test_std::sync_wait(std::move(task)); + ASSERT(result.has_value()); + ASSERT(std::get<0>(*result) == 67); + ASSERT(resource.deallocations == 1uz); +} + auto test_task_propagates_stop_token() -> void { test_std::inplace_stop_source source; ASSERT(source.request_stop()); @@ -233,5 +252,6 @@ TEST(exec_task) { test_task_stopped_completion(); test_task_errors(); test_task_uses_pmr_allocator_arg(); + test_task_allocator_arg_unrelated_to_allocator_type(); test_task_propagates_stop_token(); } From c38561720ae2a615ad5b31bbee06bb99ec9da34e Mon Sep 17 00:00:00 2001 From: Cra3z Date: Mon, 16 Nov 2026 18:23:02 +0800 Subject: [PATCH 2/2] Fix task-promise query --- include/beman/execution/detail/task.hpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/include/beman/execution/detail/task.hpp b/include/beman/execution/detail/task.hpp index e01ee711..72c942e1 100644 --- a/include/beman/execution/detail/task.hpp +++ b/include/beman/execution/detail/task.hpp @@ -690,10 +690,10 @@ struct promise_env { template requires(::beman::execution::forwarding_query(Q())) && requires(const Promise* p, Q q, Args&&... args) { - q(p->get_environment(), ::std::forward(args)...); + p->get_environment().query(q, ::std::forward(args)...); } auto query(Q q, Args&&... args) const noexcept { - return q(this->promise->get_environment(), ::std::forward(args)...); + return this->promise->get_environment().query(q, ::std::forward(args)...); } const Promise* promise;