2 // Copyright Oliver Kowalke 2014.
3 // Distributed under the Boost Software License, Version 1.0.
4 // (See accompanying file LICENSE_1_0.txt or copy at
5 // http://www.boost.org/LICENSE_1_0.txt)
7 #ifndef BOOST_COROUTINES2_DETAIL_CREATE_CONTROLBLOCK_IPP
8 #define BOOST_COROUTINES2_DETAIL_CREATE_CONTROLBLOCK_IPP
14 #include <boost/assert.hpp>
15 #include <boost/config.hpp>
17 #include <boost/context/preallocated.hpp>
18 #include <boost/context/stack_context.hpp>
20 #include <boost/coroutine2/detail/config.hpp>
22 #ifdef BOOST_HAS_ABI_HEADERS
23 # include BOOST_ABI_PREFIX
27 namespace coroutines2 {
30 template< typename ControlBlock, typename StackAllocator, typename Fn >
31 ControlBlock * create_control_block( StackAllocator && salloc, Fn && fn) {
32 auto sctx = salloc.allocate();
33 // reserve space for control structure
34 #if defined(BOOST_NO_CXX11_CONSTEXPR) || defined(BOOST_NO_CXX11_STD_ALIGN)
35 void * sp = static_cast< char * >( sctx.sp) - sizeof( ControlBlock);
36 const std::size_t size = sctx.size - sizeof( ControlBlock);
38 constexpr std::size_t func_alignment = 64; // alignof( ControlBlock);
39 constexpr std::size_t func_size = sizeof( ControlBlock);
40 // reserve space on stack
41 void * sp = static_cast< char * >( sctx.sp) - func_size - func_alignment;
43 std::size_t space = func_size + func_alignment;
44 sp = std::align( func_alignment, func_size, sp, space);
45 BOOST_ASSERT( nullptr != sp);
46 // calculate remaining size
47 const std::size_t size = sctx.size - ( static_cast< char * >( sctx.sp) - static_cast< char * >( sp) );
49 // placment new for control structure on coroutine stack
50 return new ( sp) ControlBlock{ context::preallocated( sp, size, sctx),
51 std::forward< StackAllocator >( salloc), std::forward< Fn >( fn) };
56 #ifdef BOOST_HAS_ABI_HEADERS
57 # include BOOST_ABI_SUFFIX
60 #endif // BOOST_COROUTINES2_DETAIL_CREATE_CONTROLBLOCK_IPP