create_control_block.ipp 2.0 KB

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