Skip to content

Commit 85c5e3e

Browse files
committed
Fix task allocation
1 parent b9519b0 commit 85c5e3e

5 files changed

Lines changed: 116 additions & 98 deletions

File tree

‎include/beman/execution/detail/find_allocator.hpp‎

Lines changed: 0 additions & 47 deletions
This file was deleted.

‎include/beman/execution/detail/task.hpp‎

Lines changed: 96 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
#define INCLUDED_BEMAN_EXECUTION_DETAIL_TASK
66

77
#include <cassert>
8+
#include <cstdint>
9+
#include <cstring>
810
#include <beman/execution/detail/common.hpp>
911
#ifdef BEMAN_HAS_IMPORT_STD
1012
import std;
@@ -26,7 +28,6 @@ import beman.execution.detail.completion_signatures;
2628
import beman.execution.detail.decayed_same_as;
2729
import beman.execution.detail.env;
2830
import beman.execution.detail.env_of_t;
29-
import beman.execution.detail.find_allocator;
3031
import beman.execution.detail.forwarding_query;
3132
import beman.execution.detail.get_allocator;
3233
import beman.execution.detail.get_env;
@@ -62,7 +63,6 @@ import beman.execution.detail.unreachable;
6263
#include <beman/execution/detail/decayed_same_as.hpp>
6364
#include <beman/execution/detail/env.hpp>
6465
#include <beman/execution/detail/env_of_t.hpp>
65-
#include <beman/execution/detail/find_allocator.hpp>
6666
#include <beman/execution/detail/forwarding_query.hpp>
6767
#include <beman/execution/detail/get_allocator.hpp>
6868
#include <beman/execution/detail/get_env.hpp>
@@ -127,54 +127,112 @@ class handle {
127127
auto get_env() const noexcept { return ::beman::execution::get_env(*this->h); }
128128
};
129129

130-
template <typename Allocator>
131-
struct allocator_support {
132-
using allocator_traits = ::std::allocator_traits<Allocator>;
130+
struct co_memory {
131+
struct default_align_t {
132+
alignas(__STDCPP_DEFAULT_NEW_ALIGNMENT__)::std::byte _[__STDCPP_DEFAULT_NEW_ALIGNMENT__];
133+
};
133134

134-
static auto offset(::std::size_t size) noexcept -> ::std::size_t {
135-
return (size + alignof(Allocator) - 1u) & ~(alignof(Allocator) - 1u);
135+
static constexpr auto ceiling_division(::std::uintptr_t n, ::std::uintptr_t m) noexcept -> ::std::uintptr_t {
136+
assert(m != 0);
137+
return (n + m - 1) / m;
136138
}
137139

138-
static auto get_allocator(void* ptr, ::std::size_t size) noexcept -> Allocator* {
139-
ptr = static_cast<::std::byte*>(ptr) + offset(size);
140-
return ::std::launder(reinterpret_cast<Allocator*>(ptr));
140+
static constexpr auto ceiling_align(::std::uintptr_t n, ::std::uintptr_t m) noexcept -> ::std::uintptr_t {
141+
assert(m != 0);
142+
return ceiling_division(n, m) * m;
141143
}
142144

143-
template <typename... Args>
144-
auto operator new(::std::size_t size, const Args&... args) -> void* {
145-
if constexpr (::std::same_as<Allocator, ::std::allocator<::std::byte>>) {
146-
Allocator alloc{};
147-
return allocator_traits::allocate(alloc, size);
145+
static auto align_address(void* ptr, ::std::size_t alignment, ::std::size_t least_offset) noexcept -> void* {
146+
auto least = reinterpret_cast<::std::uintptr_t>(static_cast<::std::byte*>(ptr) + least_offset);
147+
return reinterpret_cast<void*>(ceiling_align(least, alignment));
148+
};
149+
150+
using dealloc_fn_t = void (*)(default_align_t*, ::std::size_t) noexcept;
151+
152+
template <typename Alloc>
153+
static auto allocate(Alloc alloc, ::std::size_t n) -> void* {
154+
using alloc_t = typename ::std::allocator_traits<Alloc>::template rebind_alloc<default_align_t>;
155+
using alloc_traits = ::std::allocator_traits<alloc_t>;
156+
alloc_t allocator_(::std::move(alloc));
157+
if constexpr (::std::default_initializable<Alloc> and
158+
alloc_traits::is_always_equal::value) { // for stateless-allocator
159+
/*
160+
* layout:
161+
* +-----------------+-----------------------+------------------+
162+
* | coroutine state | deallocating function | possible padding |
163+
* +-----------------+-----------------------+------------------+
164+
* | |
165+
* `n` bytes `sizeof(dealloc_fn_t)` bytes
166+
*/
167+
auto offset_of_dealloc_fn = n;
168+
n += sizeof(dealloc_fn_t);
169+
void* result = alloc_traits::allocate(allocator_, ceiling_division(n, sizeof(default_align_t)));
170+
dealloc_fn_t dealloc_fn = +[](default_align_t* ptr, ::std::size_t co_state_size) noexcept {
171+
auto length = co_state_size + sizeof(dealloc_fn_t);
172+
alloc_t alloc_(Alloc{});
173+
alloc_traits::deallocate(alloc_, ptr, ceiling_division(length, alignof(default_align_t)));
174+
};
175+
::std::memcpy(static_cast<::std::byte*>(result) + offset_of_dealloc_fn, &dealloc_fn, sizeof(dealloc_fn_t));
176+
return result;
148177
} else {
149-
Allocator alloc{::beman::execution::detail::find_allocator<Allocator>(args...)};
150-
void* ptr{allocator_traits::allocate(alloc, allocator_support::offset(size) + sizeof(Allocator))};
151-
try {
152-
new (allocator_support::get_allocator(ptr, size)) Allocator(alloc);
153-
} catch (...) {
154-
allocator_traits::deallocate(
155-
alloc, static_cast<::std::byte*>(ptr), allocator_support::offset(size) + sizeof(Allocator));
156-
throw;
157-
}
158-
return ptr;
178+
/*
179+
* layout:
180+
* +-----------------+-----------------------+------------------+-----------+------------------+
181+
* | coroutine state | deallocating function | possible padding | allocator | possible padding |
182+
* +-----------------+-----------------------+------------------+-----------+------------------+
183+
* | | |
184+
* `n` bytes `sizeof(dealloc_fn_t)` bytes `sizeof(alloc_t)` bytes
185+
*/
186+
static constexpr ::std::size_t max_alignment = (::std::max)(alignof(alloc_t), alignof(default_align_t));
187+
auto offset_of_dealloc_fn = n;
188+
n += sizeof(dealloc_fn_t);
189+
void* result = alloc_traits::allocate(
190+
allocator_, (n + sizeof(alloc_t) + max_alignment - 1) / alignof(default_align_t));
191+
dealloc_fn_t dealloc_fn = +[](default_align_t* ptr, ::std::size_t co_state_size) noexcept {
192+
auto length = co_state_size + sizeof(dealloc_fn_t);
193+
auto alloc_ptr_ = ::std::launder(static_cast<alloc_t*>(align_address(ptr, alignof(alloc_t), length)));
194+
alloc_t alloc_ = ::std::move(*alloc_ptr_);
195+
::std::destroy_at(alloc_ptr_);
196+
alloc_traits::deallocate(
197+
alloc_, ptr, (length + sizeof(alloc_t) + max_alignment - 1) / alignof(default_align_t));
198+
};
199+
::std::memcpy(static_cast<::std::byte*>(result) + offset_of_dealloc_fn, &dealloc_fn, sizeof(dealloc_fn_t));
200+
void(::std::construct_at(static_cast<alloc_t*>(align_address(result, alignof(alloc_t), n)),
201+
::std::move(allocator_)));
202+
return result;
159203
}
160204
}
161205

162-
auto operator delete(void* ptr, ::std::size_t size) noexcept -> void {
163-
if constexpr (::std::same_as<Allocator, ::std::allocator<::std::byte>>) {
164-
Allocator alloc{};
165-
allocator_traits::deallocate(alloc, static_cast<::std::byte*>(ptr), size);
166-
} else {
167-
Allocator* aptr{allocator_support::get_allocator(ptr, size)};
168-
Allocator alloc{*aptr};
169-
aptr->~Allocator();
170-
allocator_traits::deallocate(
171-
alloc, static_cast<::std::byte*>(ptr), allocator_support::offset(size) + sizeof(Allocator));
172-
}
206+
static auto deallocate(void* ptr, ::std::size_t n) noexcept -> void {
207+
dealloc_fn_t dealloc_fn = nullptr;
208+
::std::memcpy(&dealloc_fn, static_cast<::std::byte*>(ptr) + n, sizeof(dealloc_fn_t));
209+
dealloc_fn(static_cast<default_align_t*>(ptr), n);
210+
}
211+
};
212+
213+
template <typename Alloc>
214+
struct allocator_support {
215+
auto operator new(::std::size_t n) -> void* {
216+
static_assert(::std::default_initializable<Alloc>);
217+
return co_memory::allocate(Alloc(), n);
173218
}
174219

175-
auto operator delete(void* ptr, ::std::size_t size, const auto&...) noexcept -> void {
176-
allocator_support::operator delete(ptr, size);
220+
template <typename OtherAlloc, typename... Args>
221+
auto operator new(::std::size_t n, ::std::allocator_arg_t, const OtherAlloc& other_alloc, const Args&...)
222+
-> void* {
223+
static_assert(::beman::execution::detail::simple_allocator<OtherAlloc>);
224+
return co_memory::allocate(other_alloc, n);
177225
}
226+
227+
template <typename This, typename OtherAlloc, typename... Args>
228+
auto
229+
operator new(::std::size_t n, const This&, ::std::allocator_arg_t, const OtherAlloc& other_alloc, const Args&...)
230+
-> void* {
231+
static_assert(::beman::execution::detail::simple_allocator<OtherAlloc>);
232+
return co_memory::allocate(other_alloc, n);
233+
}
234+
235+
auto operator delete(void* ptr, ::std::size_t n) noexcept -> void { co_memory::deallocate(ptr, n); }
178236
};
179237

180238
template <typename>

‎src/beman/execution/CMakeLists.txt‎

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,6 @@ target_sources(
9191
${PROJECT_SOURCE_DIR}/include/beman/execution/detail/exit_scope_sender.hpp
9292
${PROJECT_SOURCE_DIR}/include/beman/execution/detail/exit_scope_sender_in.hpp
9393
${PROJECT_SOURCE_DIR}/include/beman/execution/detail/exit_scope_sender_of_t.hpp
94-
${PROJECT_SOURCE_DIR}/include/beman/execution/detail/find_allocator.hpp
9594
${PROJECT_SOURCE_DIR}/include/beman/execution/detail/forward_like.hpp
9695
${PROJECT_SOURCE_DIR}/include/beman/execution/detail/forwarding_query.hpp
9796
${PROJECT_SOURCE_DIR}/include/beman/execution/detail/fwd_env.hpp
@@ -327,7 +326,6 @@ if(BEMAN_USE_MODULES)
327326
exit_scope_sender.cppm
328327
exit_scope_sender_in.cppm
329328
exit_scope_sender_of_t.cppm
330-
find_allocator.cppm
331329
forward_like.cppm
332330
forwarding_query.cppm
333331
fwd_env.cppm

‎src/beman/execution/find_allocator.cppm‎

Lines changed: 0 additions & 11 deletions
This file was deleted.

‎tests/beman/execution/exec-task.test.cpp‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,11 @@ auto pmr_allocator_task(std::allocator_arg_t, std::pmr::polymorphic_allocator<>
9595
co_return 42;
9696
}
9797

98+
// the coroutine allocator type is unrelated to the task's allocator_type (std::allocator<std::byte>)
99+
auto foreign_allocator_task(std::allocator_arg_t, std::pmr::polymorphic_allocator<>) -> test_std::task<int> {
100+
co_return 67;
101+
}
102+
98103
auto scheduler_task() -> test_std::task<int> {
99104
const auto scheduler = co_await test_std::read_env(test_std::get_scheduler);
100105
static_assert(std::same_as<std::remove_cvref_t<decltype(scheduler)>, test_std::task_scheduler>);
@@ -214,6 +219,20 @@ auto test_task_uses_pmr_allocator_arg() -> void {
214219
ASSERT(resource.deallocations == resource.allocations);
215220
}
216221

222+
auto test_task_allocator_arg_unrelated_to_allocator_type() -> void {
223+
counting_memory_resource resource;
224+
std::pmr::polymorphic_allocator<> allocator{&resource};
225+
226+
auto task = foreign_allocator_task(std::allocator_arg, allocator);
227+
ASSERT(resource.allocations == 1uz);
228+
ASSERT(resource.deallocations == 0uz);
229+
230+
auto result = test_std::sync_wait(std::move(task));
231+
ASSERT(result.has_value());
232+
ASSERT(std::get<0>(*result) == 67);
233+
ASSERT(resource.deallocations == 1uz);
234+
}
235+
217236
auto test_task_propagates_stop_token() -> void {
218237
test_std::inplace_stop_source source;
219238
ASSERT(source.request_stop());
@@ -233,5 +252,6 @@ TEST(exec_task) {
233252
test_task_stopped_completion();
234253
test_task_errors();
235254
test_task_uses_pmr_allocator_arg();
255+
test_task_allocator_arg_unrelated_to_allocator_type();
236256
test_task_propagates_stop_token();
237257
}

0 commit comments

Comments
 (0)