diff --git a/include/etl/ireference_counted_message_pool.h b/include/etl/ireference_counted_message_pool.h index fe9c5500..a875875b 100644 --- a/include/etl/ireference_counted_message_pool.h +++ b/include/etl/ireference_counted_message_pool.h @@ -35,7 +35,7 @@ SOFTWARE. namespace etl { - class ipool_message; + class ireference_counted_message; //*************************************************************************** /// Interface for a reference counted message pool. @@ -45,7 +45,7 @@ namespace etl public: virtual ~ireference_counted_message_pool() {} - virtual void release(const etl::ipool_message& msg) = 0; + virtual void release(const etl::ireference_counted_message& msg) = 0; }; } diff --git a/include/etl/reference_counted_message.h b/include/etl/reference_counted_message.h index 68dca06f..14f2d0c8 100644 --- a/include/etl/reference_counted_message.h +++ b/include/etl/reference_counted_message.h @@ -53,15 +53,8 @@ namespace etl virtual void release() = 0; ///< Release back to the owner. }; - //*************************************************************************** - // Reference counted message type. - //*************************************************************************** - class ipool_message : public etl::ireference_counted_message - { - }; - template - class pool_message : public etl::ipool_message + class reference_counted_message : public etl::ireference_counted_message { public: @@ -74,7 +67,7 @@ namespace etl /// Constructor /// \param msg The message to count. //*************************************************************************** - pool_message(const TMessage& msg_, etl::ireference_counted_message_pool& owner_) + reference_counted_message(const TMessage& msg_, etl::ireference_counted_message_pool& owner_) : rc_object(msg_) , owner(owner_) { @@ -122,86 +115,13 @@ namespace etl etl::ireference_counted_message_pool& owner; ///< The pool that owns this object. }; - //*************************************************************************** - /// Persistent message type. - /// The message type will always have a reference count of 1. - /// \tparam TMessage The message type stored. - //*************************************************************************** - class inon_pool_message : public etl::ireference_counted_message - { - }; - - template - class non_pool_message : public etl::inon_pool_message - { - public: - - ETL_STATIC_ASSERT((etl::is_base_of::value), "Not a message type"); - - typedef TMessage message_type; - - //*************************************************************************** - /// Constructor - /// \param msg The message to count. - //*************************************************************************** - explicit non_pool_message(const TMessage& msg_) - : rc_object(msg_) - { - } - - //*************************************************************************** - /// Get a const reference to the message. - /// \return A const reference to the message. - //*************************************************************************** - ETL_NODISCARD virtual const TMessage& get_message() const ETL_OVERRIDE - { - return rc_object.get_object(); - } - - //*************************************************************************** - /// Get a reference to the reference counter. - /// \return A reference to the reference counter. - //*************************************************************************** - ETL_NODISCARD virtual etl::ireference_counter& get_reference_counter() ETL_OVERRIDE - { - return rc_object.get_reference_counter(); - } - - //*************************************************************************** - /// Get a const reference to the reference counter. - /// \return A const reference to the reference counter. - //*************************************************************************** - ETL_NODISCARD virtual const etl::ireference_counter& get_reference_counter() const ETL_OVERRIDE - { - return rc_object.get_reference_counter(); - } - - //*************************************************************************** - /// Release back to the owner pool. - /// \return A reference to the owner pool. - //*************************************************************************** - virtual void release() ETL_OVERRIDE - { - // Do nothing. - } - - private: - - // This class must not be default contructed, copy constructed or assigned. - non_pool_message() ETL_DELETE; - non_pool_message(const non_pool_message&) ETL_DELETE; - non_pool_message& operator =(const non_pool_message&) ETL_DELETE; - - etl::persistent_object rc_object; ///< The reference counted object. - }; - #if ETL_CPP11_SUPPORTED && ETL_HAS_ATOMIC //*************************************************************************** /// Class for creating reference counted objects using an atomic counter. /// \tparam TObject The type to be reference counted. //*************************************************************************** template - using atomic_counted_message = etl::pool_message; + using atomic_counted_message = etl::reference_counted_message; #endif } diff --git a/include/etl/reference_counted_message_pool.h b/include/etl/reference_counted_message_pool.h index 23f12923..3614c7f6 100644 --- a/include/etl/reference_counted_message_pool.h +++ b/include/etl/reference_counted_message_pool.h @@ -109,11 +109,11 @@ namespace etl /// Allocate a reference counted message from the pool. //************************************************************************* template - etl::ipool_message* allocate(const TMessage& message) + etl::ireference_counted_message* allocate(const TMessage& message) { ETL_STATIC_ASSERT((etl::is_base_of::value), "Not a message type"); - typedef etl::pool_message rcm_t; + typedef etl::reference_counted_message rcm_t; typedef rcm_t* prcm_t; prcm_t p = ETL_NULLPTR; @@ -133,9 +133,9 @@ namespace etl //************************************************************************* /// Destruct a message and send it back to the pool. //************************************************************************* - void release(const etl::ipool_message& rcmessage) + void release(const etl::ireference_counted_message& rcmessage) { - rcmessage.~ipool_message(); + rcmessage.~ireference_counted_message(); bool released = memory_block_allocator.release(&rcmessage); ETL_ASSERT(released, ETL_ERROR(etl::reference_counted_message_pool_release_failure)); @@ -149,13 +149,13 @@ namespace etl private: // Size of the first pool message type. - static constexpr size_t size1 = sizeof(etl::pool_message); + static constexpr size_t size1 = sizeof(etl::reference_counted_message); // Maximum size of the the rest of the pool message types. static constexpr size_t size2 = max_pool_message_size::size; // Size of the first pool message type. - static constexpr size_t alignment1 = etl::alignment_of>::value; + static constexpr size_t alignment1 = etl::alignment_of>::value; // Maximum size of the the rest of the pool message types. static constexpr size_t alignment2 = max_pool_message_size::alignment; @@ -178,34 +178,43 @@ namespace etl ETL_STATIC_ASSERT((etl::is_base_of::value), "TMessage not derived from etl::imessage"); // The size of this pool message type. - static constexpr size_t size = sizeof(etl::pool_message); + static constexpr size_t size = sizeof(etl::reference_counted_message); // The maximum alignment. - static constexpr size_t alignment = etl::alignment_of>::value; + static constexpr size_t alignment = etl::alignment_of>::value; }; #else template struct max_pool_message_size { - static const size_t size = etl::largest, - etl::pool_message, - etl::pool_message, - etl::pool_message, - etl::pool_message, - etl::pool_message, - etl::pool_message, - etl::pool_message >::size; + ETL_STATIC_ASSERT((etl::is_base_of::value), "TMessage1 not derived from etl::imessage"); + ETL_STATIC_ASSERT((etl::is_base_of::value), "TMessage2 not derived from etl::imessage"); + ETL_STATIC_ASSERT((etl::is_base_of::value), "TMessage3 not derived from etl::imessage"); + ETL_STATIC_ASSERT((etl::is_base_of::value), "TMessage4 not derived from etl::imessage"); + ETL_STATIC_ASSERT((etl::is_base_of::value), "TMessage5 not derived from etl::imessage"); + ETL_STATIC_ASSERT((etl::is_base_of::value), "TMessage6 not derived from etl::imessage"); + ETL_STATIC_ASSERT((etl::is_base_of::value), "TMessage7 not derived from etl::imessage"); + ETL_STATIC_ASSERT((etl::is_base_of::value), "TMessage8 not derived from etl::imessage"); + + static const size_t size = etl::largest, + etl::reference_counted_message, + etl::reference_counted_message, + etl::reference_counted_message, + etl::reference_counted_message, + etl::reference_counted_message, + etl::reference_counted_message, + etl::reference_counted_message >::size; - static const size_t alignment = etl::largest, - etl::pool_message, - etl::pool_message, - etl::pool_message, - etl::pool_message, - etl::pool_message, - etl::pool_message, - etl::pool_message >::alignment; + static const size_t alignment = etl::largest, + etl::reference_counted_message, + etl::reference_counted_message, + etl::reference_counted_message, + etl::reference_counted_message, + etl::reference_counted_message, + etl::reference_counted_message, + etl::reference_counted_message >::alignment; }; #endif diff --git a/include/etl/reference_counted_object.h b/include/etl/reference_counted_object.h index 103306ba..e5bade9a 100644 --- a/include/etl/reference_counted_object.h +++ b/include/etl/reference_counted_object.h @@ -105,45 +105,6 @@ namespace etl TCounter reference_count; // The reference count object. }; - //*************************************************************************** - /// A null reference counter. - /// Always returns the reference count as 1, for persistent objects. - //*************************************************************************** - class null_reference_counter : public ireference_counter - { - public: - - //*************************************************************************** - /// Set the reference count. - //*************************************************************************** - virtual void set_reference_count(int32_t value) ETL_OVERRIDE - { - } - - //*************************************************************************** - /// Increment the reference count. - //*************************************************************************** - virtual void increment_reference_count() ETL_OVERRIDE - { - } - - //*************************************************************************** - /// Decrement the reference count. - //*************************************************************************** - ETL_NODISCARD virtual int32_t decrement_reference_count() ETL_OVERRIDE - { - return int32_t(1); - } - - //*************************************************************************** - /// Get the current reference count. - //*************************************************************************** - ETL_NODISCARD virtual int32_t get_reference_count() const ETL_OVERRIDE - { - return int32_t(1); - } - }; - //*************************************************************************** /// Base for all reference counted objects. //*************************************************************************** @@ -212,61 +173,6 @@ namespace etl etl::reference_counter reference_counter; ///< The reference counter. }; - //*************************************************************************** - /// Persistent message type. - /// The message type will always have a reference count of 1. - /// \tparam TObject The type stored in the object. - //*************************************************************************** - template - class persistent_object : public etl::ireference_counted_object - { - public: - - typedef TObject value_type; - - //*************************************************************************** - /// Constructor. - //*************************************************************************** - persistent_object(const TObject& object_) - : object(object_) - { - } - - //*************************************************************************** - /// Get a const reference to the counted object. - //*************************************************************************** - ETL_NODISCARD const value_type& get_object() const - { - return object; - } - - //*************************************************************************** - /// Get a reference to the reference counter. - //*************************************************************************** - ETL_NODISCARD virtual ireference_counter& get_reference_counter() ETL_OVERRIDE - { - return reference_counter; - } - - //*************************************************************************** - /// Get a const reference to the reference counter. - //*************************************************************************** - ETL_NODISCARD virtual const ireference_counter& get_reference_counter() const ETL_OVERRIDE - { - return reference_counter; - } - - private: - - // This class must not be default contructed, copy constructed or assigned. - persistent_object() ETL_DELETE; - persistent_object(const persistent_object&) ETL_DELETE; - persistent_object& operator =(const persistent_object&) ETL_DELETE; - - TObject object; ///< The object being reference counted. - etl::null_reference_counter reference_counter; ///< The reference counter. - }; - #if ETL_CPP11_SUPPORTED && ETL_HAS_ATOMIC //*************************************************************************** /// Class for creating reference counted objects using an atomic counter. diff --git a/include/etl/shared_message.h b/include/etl/shared_message.h index 704ea78c..95d45cff 100644 --- a/include/etl/shared_message.h +++ b/include/etl/shared_message.h @@ -68,20 +68,11 @@ namespace etl //************************************************************************* /// Constructor - /// \param np_message A reference to a message not controlled by a pool. //************************************************************************* - explicit shared_message(etl::inon_pool_message& np_message) - : p_rcmessage(&np_message) - { - } - - //************************************************************************* - /// Constructor - /// \param message A reference to the message allocated from the pool. - //************************************************************************* - explicit shared_message(etl::ipool_message& rcmessage_) - : p_rcmessage(&rcmessage_) + shared_message(etl::ireference_counted_message& rcm) { + p_rcmessage = &rcm; + p_rcmessage->get_reference_counter().set_reference_count(1U); } diff --git a/test/test_shared_message.cpp b/test/test_shared_message.cpp index be4b223a..5ca4bb04 100644 --- a/test/test_shared_message.cpp +++ b/test/test_shared_message.cpp @@ -159,6 +159,26 @@ namespace etl::atomic_counted_message_pool message_pool(memory_allocator); + //************************************************************************* + class Message2Allocator : public etl::ireference_counted_message_pool + { + public: + + static etl::reference_counted_message& Get() + { + static Message2Allocator allocator; + static Message2 message2; + static etl::reference_counted_message rcm2(message2, allocator); + + return rcm2; + } + + void release(const etl::ireference_counted_message& msg) override + { + // Do nothing. + } + }; + //************************************************************************* TEST(test_send_to_routers) { @@ -168,11 +188,9 @@ namespace router1.clear(); router2.clear(); - etl::non_pool_message npm((Message2())); // npm is not owned by any pool. Extra parentheses to fix 'vexing parse'. - etl::shared_message sm1(message_pool, Message1(1)); // sm1 holds a Message1 that is owned by message_pool. etl::shared_message sm2(message_pool, Message2()); // sm2 holds a Message2 that is owned by message_pool. - etl::shared_message sm3(npm); // sm3 holds a Message2 that is not owned by a message pool. + etl::shared_message sm3(Message2Allocator::Get()); // sm3 holds a Message2 that is owned by a statically allocated message pool. etl::shared_message sm4(sm1); // sm4 is a copy of sm1. bus.receive(sm1);