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
1012import std;
@@ -26,7 +28,6 @@ import beman.execution.detail.completion_signatures;
2628import beman.execution.detail.decayed_same_as;
2729import beman.execution.detail.env;
2830import beman.execution.detail.env_of_t ;
29- import beman.execution.detail.find_allocator;
3031import beman.execution.detail.forwarding_query;
3132import beman.execution.detail.get_allocator;
3233import 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
180238template <typename >
0 commit comments