Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 0 additions & 47 deletions include/beman/execution/detail/find_allocator.hpp

This file was deleted.

138 changes: 98 additions & 40 deletions include/beman/execution/detail/task.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
#define INCLUDED_BEMAN_EXECUTION_DETAIL_TASK

#include <cassert>
#include <cstdint>
#include <cstring>
#include <beman/execution/detail/common.hpp>
#ifdef BEMAN_HAS_IMPORT_STD
import std;
Expand All @@ -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;
Expand Down Expand Up @@ -62,7 +63,6 @@ import beman.execution.detail.unreachable;
#include <beman/execution/detail/decayed_same_as.hpp>
#include <beman/execution/detail/env.hpp>
#include <beman/execution/detail/env_of_t.hpp>
#include <beman/execution/detail/find_allocator.hpp>
#include <beman/execution/detail/forwarding_query.hpp>
#include <beman/execution/detail/get_allocator.hpp>
#include <beman/execution/detail/get_env.hpp>
Expand Down Expand Up @@ -127,54 +127,112 @@ class handle {
auto get_env() const noexcept { return ::beman::execution::get_env(*this->h); }
};

template <typename Allocator>
struct allocator_support {
using allocator_traits = ::std::allocator_traits<Allocator>;
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<Allocator*>(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 <typename... Args>
auto operator new(::std::size_t size, const Args&... args) -> void* {
if constexpr (::std::same_as<Allocator, ::std::allocator<::std::byte>>) {
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<void*>(ceiling_align(least, alignment));
};

using dealloc_fn_t = void (*)(default_align_t*, ::std::size_t) noexcept;

template <typename Alloc>
static auto allocate(Alloc alloc, ::std::size_t n) -> void* {
using alloc_t = typename ::std::allocator_traits<Alloc>::template rebind_alloc<default_align_t>;
using alloc_traits = ::std::allocator_traits<alloc_t>;
alloc_t allocator_(::std::move(alloc));
if constexpr (::std::default_initializable<Alloc> 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<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<alloc_t*>(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<alloc_t*>(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, ::std::allocator<::std::byte>>) {
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<default_align_t*>(ptr), n);
}
};

template <typename Alloc>
struct allocator_support {
auto operator new(::std::size_t n) -> void* {
static_assert(::std::default_initializable<Alloc>);
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 <typename OtherAlloc, typename... Args>
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<OtherAlloc>);
return co_memory::allocate(other_alloc, n);
}

template <typename This, typename OtherAlloc, typename... Args>
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<OtherAlloc>);
return co_memory::allocate(other_alloc, n);
}

auto operator delete(void* ptr, ::std::size_t n) noexcept -> void { co_memory::deallocate(ptr, n); }
};

template <typename>
Expand Down Expand Up @@ -632,10 +690,10 @@ struct promise_env {

template <typename Q, typename... Args>
requires(::beman::execution::forwarding_query(Q())) && requires(const Promise* p, Q q, Args&&... args) {
q(p->get_environment(), ::std::forward<Args>(args)...);
p->get_environment().query(q, ::std::forward<Args>(args)...);
}
auto query(Q q, Args&&... args) const noexcept {
return q(this->promise->get_environment(), ::std::forward<Args>(args)...);
return this->promise->get_environment().query(q, ::std::forward<Args>(args)...);
}

const Promise* promise;
Expand Down
2 changes: 0 additions & 2 deletions src/beman/execution/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
11 changes: 0 additions & 11 deletions src/beman/execution/find_allocator.cppm

This file was deleted.

20 changes: 20 additions & 0 deletions tests/beman/execution/exec-task.test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<std::byte>)
auto foreign_allocator_task(std::allocator_arg_t, std::pmr::polymorphic_allocator<>) -> test_std::task<int> {
co_return 67;
}

auto scheduler_task() -> test_std::task<int> {
const auto scheduler = co_await test_std::read_env(test_std::get_scheduler);
static_assert(std::same_as<std::remove_cvref_t<decltype(scheduler)>, test_std::task_scheduler>);
Expand Down Expand Up @@ -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());
Expand All @@ -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();
}
Loading