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 #include <memory> // std::allocator_traits
18
19
20 namespace boost
21 {
22 namespace statechart
23 {
24
25 #ifdef BOOST_NO_CXX11_ALLOCATOR
26 typedef void none;
27 #else
28 // The specialization std::allocator<void> doesn't satisfy C++17's
29 // allocator completeness requirements. Therefore it is deprecated
30 // and should no longer be used. Supply a replacement type for all
31 // the allocator default template arguments in the library.
32 struct none {};
33 #endif
34
35 namespace detail
36 {
37
38
39
40 // defect: 'allocate' and 'deallocate' cannot handle stateful allocators!
41
42 template< class MostDerived, class Allocator >
allocate(std::size_t size)43 void * allocate( std::size_t size )
44 {
45 avoid_unused_warning( size );
46 // The assert below fails when memory is allocated for an event<>,
47 // simple_state<> or state<> subtype object, *and* the first template
48 // parameter passed to one of these templates is not equal to the most-
49 // derived object being constructed.
50 // The following examples apply to all these subtypes:
51 // // Example 1
52 // struct A {};
53 // struct B : sc::simple_state< A, /* ... */ >
54 // // Above, the first template parameter must be equal to the most-
55 // // derived type
56 //
57 // // Example 2
58 // struct A : sc::event< A >
59 // struct B : A { /* ... */ };
60 // void f() { delete new B(); }
61 // // Above the most-derived type being constructed is B, but A was passed
62 // // as the most-derived type to event<>.
63 BOOST_ASSERT( size == sizeof( MostDerived ) );
64 typedef typename boost::detail::allocator::rebind_to<
65 Allocator, MostDerived
66 >::type md_allocator;
67 md_allocator alloc;
68 #ifdef BOOST_NO_CXX11_ALLOCATOR
69 return alloc.allocate( 1, static_cast< MostDerived * >( 0 ) );
70 #else
71 typedef std::allocator_traits<md_allocator> md_traits;
72 return md_traits::allocate( alloc, 1, static_cast< MostDerived * >( 0 ) );
73 #endif
74 }
75
76 template< class MostDerived, class Allocator >
deallocate(void * pObject)77 void deallocate( void * pObject )
78 {
79 typedef typename boost::detail::allocator::rebind_to<
80 Allocator, MostDerived
81 >::type md_allocator;
82 md_allocator alloc;
83 #ifdef BOOST_NO_CXX11_ALLOCATOR
84 alloc.deallocate( static_cast< MostDerived * >( pObject ), 1 );
85 #else
86 typedef std::allocator_traits<md_allocator> md_traits;
87 md_traits::deallocate( alloc, static_cast< MostDerived * >( pObject ), 1 );
88 #endif
89 }
90
91
92
93 } // namespace detail
94 } // namespace statechart
95 } // namespace boost
96
97
98
99 #endif
100