| 1 | #ifndef BOOST_FIBERS_STACK_ALLOCATOR_WRAPPER_H |
|---|---|
| 2 | #define BOOST_FIBERS_STACK_ALLOCATOR_WRAPPER_H |
| 3 | |
| 4 | #include <memory> |
| 5 | |
| 6 | #include <boost/fiber/detail/config.hpp> |
| 7 | #include <boost/context/stack_context.hpp> |
| 8 | #include <boost/fiber/fixedsize_stack.hpp> |
| 9 | #include <boost/fiber/segmented_stack.hpp> |
| 10 | |
| 11 | namespace boost { |
| 12 | namespace fibers { |
| 13 | namespace detail { |
| 14 | class BOOST_FIBERS_DECL polymorphic_stack_allocator_base { |
| 15 | public: |
| 16 | polymorphic_stack_allocator_base() = default; |
| 17 | |
| 18 | virtual ~polymorphic_stack_allocator_base() = default; |
| 19 | |
| 20 | polymorphic_stack_allocator_base(const polymorphic_stack_allocator_base&) = delete; |
| 21 | polymorphic_stack_allocator_base& operator=(const polymorphic_stack_allocator_base&) = delete; |
| 22 | |
| 23 | polymorphic_stack_allocator_base(polymorphic_stack_allocator_base&&) = delete; |
| 24 | polymorphic_stack_allocator_base& operator=(polymorphic_stack_allocator_base&&) = delete; |
| 25 | |
| 26 | virtual boost::context::stack_context allocate() = 0; |
| 27 | |
| 28 | virtual void deallocate(boost::context::stack_context& sctx) = 0; |
| 29 | }; |
| 30 | |
| 31 | template< typename StackAllocator > |
| 32 | class BOOST_FIBERS_DECL polymorphic_stack_allocator_impl final : public polymorphic_stack_allocator_base { |
| 33 | public: |
| 34 | template<typename ... Args > |
| 35 | polymorphic_stack_allocator_impl( Args && ... args ) |
| 36 | :_allocator(std::forward< Args >( args) ... ) |
| 37 | {} |
| 38 | |
| 39 | ~polymorphic_stack_allocator_impl() = default; |
| 40 | |
| 41 | boost::context::stack_context allocate() override |
| 42 | { |
| 43 | return _allocator.allocate(); |
| 44 | } |
| 45 | |
| 46 | void deallocate(boost::context::stack_context& sctx) override |
| 47 | { |
| 48 | _allocator.deallocate(sctx); |
| 49 | } |
| 50 | |
| 51 | private: |
| 52 | StackAllocator _allocator; |
| 53 | }; |
| 54 | } |
| 55 | |
| 56 | class BOOST_FIBERS_DECL stack_allocator_wrapper final { |
| 57 | public: |
| 58 | stack_allocator_wrapper(std::unique_ptr<detail::polymorphic_stack_allocator_base> allocator) |
| 59 | :_allocator(std::move(allocator)) |
| 60 | {} |
| 61 | |
| 62 | ~stack_allocator_wrapper() = default; |
| 63 | |
| 64 | stack_allocator_wrapper(const stack_allocator_wrapper&) = delete; |
| 65 | stack_allocator_wrapper& operator=(const stack_allocator_wrapper&) = delete; |
| 66 | |
| 67 | stack_allocator_wrapper(stack_allocator_wrapper&&) = default; |
| 68 | stack_allocator_wrapper& operator=(stack_allocator_wrapper&&) = default; |
| 69 | |
| 70 | boost::context::stack_context allocate() |
| 71 | { |
| 72 | return _allocator->allocate(); |
| 73 | } |
| 74 | |
| 75 | void deallocate(boost::context::stack_context& sctx) |
| 76 | { |
| 77 | _allocator->deallocate(sctx); |
| 78 | } |
| 79 | |
| 80 | private: |
| 81 | std::unique_ptr<detail::polymorphic_stack_allocator_base> _allocator; |
| 82 | }; |
| 83 | |
| 84 | template <typename StackAllocator, typename ... Args> |
| 85 | BOOST_FIBERS_DECL stack_allocator_wrapper make_stack_allocator_wrapper(Args && ... args) |
| 86 | { |
| 87 | return stack_allocator_wrapper( |
| 88 | std::unique_ptr<detail::polymorphic_stack_allocator_base>( |
| 89 | new detail::polymorphic_stack_allocator_impl<StackAllocator>(std::forward< Args >( args) ... ))); |
| 90 | } |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | #endif |
| 95 |
