memory.hpp 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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. namespace boost
  13. {
  14. namespace statechart
  15. {
  16. namespace detail
  17. {
  18. template< class MostDerived, class Allocator >
  19. void * allocate( std::size_t size )
  20. {
  21. avoid_unused_warning( size );
  22. // The assert below fails when memory is allocated for an event<>,
  23. // simple_state<> or state<> subtype object, *and* the first template
  24. // parameter passed to one of these templates is not equal to the most-
  25. // derived object being constructed.
  26. // The following examples apply to all these subtypes:
  27. // // Example 1
  28. // struct A {};
  29. // struct B : sc::simple_state< A, /* ... */ >
  30. // // Above, the first template parameter must be equal to the most-
  31. // // derived type
  32. //
  33. // // Example 2
  34. // struct A : sc::event< A >
  35. // struct B : A { /* ... */ };
  36. // void f() { delete new B(); }
  37. // // Above the most-derived type being constructed is B, but A was passed
  38. // // as the most-derived type to event<>.
  39. BOOST_ASSERT( size == sizeof( MostDerived ) );
  40. return typename boost::detail::allocator::rebind_to<
  41. Allocator, MostDerived
  42. >::type().allocate( 1, static_cast< MostDerived * >( 0 ) );
  43. }
  44. template< class MostDerived, class Allocator >
  45. void deallocate( void * pObject )
  46. {
  47. return typename boost::detail::allocator::rebind_to<
  48. Allocator, MostDerived
  49. >::type().deallocate( static_cast< MostDerived * >( pObject ), 1 );
  50. }
  51. } // namespace detail
  52. } // namespace statechart
  53. } // namespace boost
  54. #endif