memory.hpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. #ifndef BOOST_STATECHART_DETAIL_MEMORY_HPP_INCLUDED
  2. #define BOOST_STATECHART_DETAIL_MEMORY_HPP_INCLUDED
  3. //////////////////////////////////////////////////////////////////////////////
  4. // Copyright 2005-2006 Andreas Huber Doenni
  5. // Distributed under the Boost Software License, Version 1.0. (See accompany-
  6. // ing file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  7. //////////////////////////////////////////////////////////////////////////////
  8. #include <boost/statechart/detail/avoid_unused_warning.hpp>
  9. #include <boost/assert.hpp>
  10. #include <boost/detail/allocator_utilities.hpp>
  11. #include <cstddef> // std::size_t
  12. #include <memory> // std::allocator_traits
  13. namespace boost
  14. {
  15. namespace statechart
  16. {
  17. #ifdef BOOST_NO_CXX11_ALLOCATOR
  18. typedef void none;
  19. #else
  20. // The specialization std::allocator<void> doesn't satisfy C++17's
  21. // allocator completeness requirements. Therefore it is deprecated
  22. // and should no longer be used. Supply a replacement type for all
  23. // the allocator default template arguments in the library.
  24. struct none {};
  25. #endif
  26. namespace detail
  27. {
  28. // defect: 'allocate' and 'deallocate' cannot handle stateful allocators!
  29. template< class MostDerived, class Allocator >
  30. void * allocate( std::size_t size )
  31. {
  32. avoid_unused_warning( size );
  33. // The assert below fails when memory is allocated for an event<>,
  34. // simple_state<> or state<> subtype object, *and* the first template
  35. // parameter passed to one of these templates is not equal to the most-
  36. // derived object being constructed.
  37. // The following examples apply to all these subtypes:
  38. // // Example 1
  39. // struct A {};
  40. // struct B : sc::simple_state< A, /* ... */ >
  41. // // Above, the first template parameter must be equal to the most-
  42. // // derived type
  43. //
  44. // // Example 2
  45. // struct A : sc::event< A >
  46. // struct B : A { /* ... */ };
  47. // void f() { delete new B(); }
  48. // // Above the most-derived type being constructed is B, but A was passed
  49. // // as the most-derived type to event<>.
  50. BOOST_ASSERT( size == sizeof( MostDerived ) );
  51. typedef typename boost::detail::allocator::rebind_to<
  52. Allocator, MostDerived
  53. >::type md_allocator;
  54. md_allocator alloc;
  55. #ifdef BOOST_NO_CXX11_ALLOCATOR
  56. return alloc.allocate( 1, static_cast< MostDerived * >( 0 ) );
  57. #else
  58. typedef std::allocator_traits<md_allocator> md_traits;
  59. return md_traits::allocate( alloc, 1, static_cast< MostDerived * >( 0 ) );
  60. #endif
  61. }
  62. template< class MostDerived, class Allocator >
  63. void deallocate( void * pObject )
  64. {
  65. typedef typename boost::detail::allocator::rebind_to<
  66. Allocator, MostDerived
  67. >::type md_allocator;
  68. md_allocator alloc;
  69. #ifdef BOOST_NO_CXX11_ALLOCATOR
  70. alloc.deallocate( static_cast< MostDerived * >( pObject ), 1 );
  71. #else
  72. typedef std::allocator_traits<md_allocator> md_traits;
  73. md_traits::deallocate( alloc, static_cast< MostDerived * >( pObject ), 1 );
  74. #endif
  75. }
  76. } // namespace detail
  77. } // namespace statechart
  78. } // namespace boost
  79. #endif