diff --git a/example/BitMachine/BitMachine.cpp b/example/BitMachine/BitMachine.cpp index 73b783d..658ebea 100644 --- a/example/BitMachine/BitMachine.cpp +++ b/example/BitMachine/BitMachine.cpp @@ -267,6 +267,18 @@ struct BitState : sc::simple_state< BitState< stateNo >, BitMachine >, IDisplay #endif { + #ifdef CUSTOMIZE_MEMORY_MANAGEMENT + static void * operator new( size_t size ) + { + return UniqueObject< BitState< stateNo > >::operator new( size ); + } + + static void operator delete( void * p, size_t size ) + { + UniqueObject< BitState< stateNo > >::operator delete( p, size ); + } + #endif + typedef typename FlipTransitionList< stateNo >::type reactions; virtual void Display() const diff --git a/include/boost/statechart/detail/memory.hpp b/include/boost/statechart/detail/memory.hpp new file mode 100644 index 0000000..3cbbb54 --- /dev/null +++ b/include/boost/statechart/detail/memory.hpp @@ -0,0 +1,72 @@ +#ifndef BOOST_STATECHART_DETAIL_MEMORY_HPP_INCLUDED +#define BOOST_STATECHART_DETAIL_MEMORY_HPP_INCLUDED +////////////////////////////////////////////////////////////////////////////// +// (c) Copyright Andreas Huber Doenni 2005 +// Distributed under the Boost Software License, Version 1.0. (See accompany- +// ing file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) +////////////////////////////////////////////////////////////////////////////// + + + +#include + +#include +#include + +#include // std::size_t + + + +namespace boost +{ +namespace statechart +{ +namespace detail +{ + + + +template< class MostDerived, class Allocator > +void * allocate( std::size_t size ) +{ + avoid_unused_warning( size ); + // The assert below fails when memory is allocated for an event<>, + // simple_state<> or state<> subtype object, *and* the first template + // parameter passed to one of these templates is not equal to the most- + // derived object being constructed. + // The following examples apply to all these subtypes: + // // Example 1 + // struct A {}; + // struct B : sc::simple_state< A, /* ... */ > + // // Above, the first template parameter must be equal to the most- + // // derived type + // + // // Example 2 + // struct A : sc::event< A > + // struct B : A { /* ... */ }; + // void f() { delete new B(); } + // // Above the most-derived type being constructed is B, but A was passed + // // as the most-derived type to event<>. + BOOST_ASSERT( size == sizeof( MostDerived ) ); + return typename boost::detail::allocator::rebind_to< + Allocator, MostDerived + >::type().allocate( 1, static_cast< MostDerived * >( 0 ) ); +} + +template< class MostDerived, class Allocator > +void deallocate( void * pObject ) +{ + return typename boost::detail::allocator::rebind_to< + Allocator, MostDerived + >::type().deallocate( static_cast< MostDerived * >( pObject ), 1 ); +} + + + +} // namespace detail +} // namespace statechart +} // namespace boost + + + +#endif diff --git a/include/boost/statechart/simple_state.hpp b/include/boost/statechart/simple_state.hpp index 7282515..0a45614 100644 --- a/include/boost/statechart/simple_state.hpp +++ b/include/boost/statechart/simple_state.hpp @@ -13,6 +13,7 @@ #include #include #include +#include #include #include @@ -48,7 +49,9 @@ #include #include #include -#include // boost::polymorphic_downcast +#include // boost::polymorphic_downcast + +#include // std::size_t @@ -429,6 +432,18 @@ class simple_state : public detail::simple_state_base_type< MostDerived, typedef mpl::bool_< false > history_destination; + void * operator new( std::size_t size ) + { + return detail::allocate< MostDerived, + typename outermost_context_type::allocator_type >( size ); + } + + void operator delete( void * pState ) + { + detail::deallocate< MostDerived, + typename outermost_context_type::allocator_type >( pState ); + } + outermost_context_base_type & outermost_context_base() { // This assert fails when an attempt is made to access the state machine