stack_allocator_wrapper.hpp 2.8 KB

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