Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:52:31

0001 #ifndef BOOST_STATECHART_DETAIL_MEMORY_HPP_INCLUDED
0002 #define BOOST_STATECHART_DETAIL_MEMORY_HPP_INCLUDED
0003 //////////////////////////////////////////////////////////////////////////////
0004 // Copyright 2005-2006 Andreas Huber Doenni
0005 // Distributed under the Boost Software License, Version 1.0. (See accompany-
0006 // ing file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
0007 //////////////////////////////////////////////////////////////////////////////
0008 
0009 
0010 
0011 #include <boost/statechart/detail/avoid_unused_warning.hpp>
0012 
0013 #include <boost/assert.hpp>
0014 #include <boost/detail/allocator_utilities.hpp>
0015 
0016 #include <cstddef> // std::size_t
0017 #include <memory>  // std::allocator_traits
0018 
0019 
0020 namespace boost
0021 {
0022 namespace statechart
0023 {
0024 
0025 #ifdef BOOST_NO_CXX11_ALLOCATOR
0026 typedef void none;
0027 #else
0028 // The specialization std::allocator<void> doesn't satisfy C++17's
0029 // allocator completeness requirements. Therefore it is deprecated
0030 // and should no longer be used. Supply a replacement type for all
0031 // the allocator default template arguments in the library.
0032 struct none {};
0033 #endif
0034 
0035 namespace detail
0036 {
0037 
0038 
0039 
0040 // defect: 'allocate' and 'deallocate' cannot handle stateful allocators!
0041 
0042 template< class MostDerived, class Allocator >
0043 void * allocate( std::size_t size )
0044 {
0045   avoid_unused_warning( size );
0046   // The assert below fails when memory is allocated for an event<>,
0047   // simple_state<> or state<> subtype object, *and* the first template
0048   // parameter passed to one of these templates is not equal to the most-
0049   // derived object being constructed.
0050   // The following examples apply to all these subtypes:
0051   // // Example 1
0052   // struct A {};
0053   // struct B : sc::simple_state< A, /* ... */ >
0054   // // Above, the first template parameter must be equal to the most-
0055   // // derived type
0056   // 
0057   // // Example 2
0058   // struct A : sc::event< A >
0059   // struct B : A { /* ... */ };
0060   // void f() { delete new B(); }
0061   // // Above the most-derived type being constructed is B, but A was passed
0062   // // as the most-derived type to event<>.
0063   BOOST_ASSERT( size == sizeof( MostDerived ) );
0064   typedef typename boost::detail::allocator::rebind_to<
0065     Allocator, MostDerived
0066   >::type md_allocator;
0067   md_allocator alloc;
0068 #ifdef BOOST_NO_CXX11_ALLOCATOR
0069   return alloc.allocate( 1, static_cast< MostDerived * >( 0 ) );
0070 #else
0071   typedef std::allocator_traits<md_allocator> md_traits;
0072   return md_traits::allocate( alloc, 1, static_cast< MostDerived * >( 0 ) );
0073 #endif
0074 }
0075 
0076 template< class MostDerived, class Allocator >
0077 void deallocate( void * pObject )
0078 {
0079   typedef typename boost::detail::allocator::rebind_to<
0080     Allocator, MostDerived
0081   >::type md_allocator;
0082   md_allocator alloc;
0083 #ifdef BOOST_NO_CXX11_ALLOCATOR
0084   alloc.deallocate( static_cast< MostDerived * >( pObject ), 1 );
0085 #else
0086   typedef std::allocator_traits<md_allocator> md_traits;
0087   md_traits::deallocate( alloc, static_cast< MostDerived * >( pObject ), 1 );
0088 #endif
0089 }
0090 
0091 
0092 
0093 } // namespace detail
0094 } // namespace statechart
0095 } // namespace boost
0096 
0097 
0098 
0099 #endif