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 
9 
10 
11 #include <boost/statechart/detail/avoid_unused_warning.hpp>
12 
13 #include <boost/assert.hpp>
14 #include <boost/detail/allocator_utilities.hpp>
15 
16 #include <cstddef> // std::size_t
17 
18 
19 
20 namespace boost
21 {
22 namespace statechart
23 {
24 namespace detail
25 {
26 
27 
28 
29 template< class MostDerived, class Allocator >
allocate(std::size_t size)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   return typename boost::detail::allocator::rebind_to<
52     Allocator, MostDerived
53   >::type().allocate( 1, static_cast< MostDerived * >( 0 ) );
54 }
55 
56 template< class MostDerived, class Allocator >
deallocate(void * pObject)57 void deallocate( void * pObject )
58 {
59   return typename boost::detail::allocator::rebind_to<
60     Allocator, MostDerived
61   >::type().deallocate( static_cast< MostDerived * >( pObject ), 1 );
62 }
63 
64 
65 
66 } // namespace detail
67 } // namespace statechart
68 } // namespace boost
69 
70 
71 
72 #endif
73