Loading...
Searching...
No Matches
reference_counted_message_pool.h
Go to the documentation of this file.
/******************************************************************************
The MIT License(MIT)
Embedded Template Library.
https://github.com/ETLCPP/etl
https://www.etlcpp.com
Copyright(c) 2020 John Wellbelove
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files(the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and / or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions :
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
******************************************************************************/
#ifndef ETL_REFERENCE_COUNTED_MESSAGE_POOL_INCLUDED
#define ETL_REFERENCE_COUNTED_MESSAGE_POOL_INCLUDED
#include "platform.h"
#include "atomic.h"
#include "error_handler.h"
#include "imemory_block_allocator.h"
#include "ireference_counted_message_pool.h"
#include "largest.h"
#include "memory.h"
#include "message.h"
#include "reference_counted_message.h"
#include "static_assert.h"
#include "utility.h"
namespace etl
{
//***************************************************************************
//***************************************************************************
class reference_counted_message_pool_exception : public etl::exception
{
public:
reference_counted_message_pool_exception(string_type reason_, string_type file_name_, numeric_type line_number_)
: exception(reason_, file_name_, line_number_)
{
}
};
//***************************************************************************
//***************************************************************************
class reference_counted_message_pool_allocation_failure : public etl::reference_counted_message_pool_exception
{
public:
reference_counted_message_pool_allocation_failure(string_type file_name_, numeric_type line_number_)
: reference_counted_message_pool_exception(
ETL_ERROR_TEXT("reference_counted_message_pool:allocation failure", ETL_REFERENCE_COUNTER_MESSAGE_POOL_FILE_ID"A"), file_name_, line_number_)
{
}
};
//***************************************************************************
//***************************************************************************
class reference_counted_message_pool_release_failure : public etl::reference_counted_message_pool_exception
{
public:
reference_counted_message_pool_release_failure(string_type file_name_, numeric_type line_number_)
: reference_counted_message_pool_exception(
ETL_ERROR_TEXT("reference_counted_message_pool:release failure", ETL_REFERENCE_COUNTER_MESSAGE_POOL_FILE_ID"B"), file_name_, line_number_)
{
}
};
//***************************************************************************
//***************************************************************************
template <typename TCounter>
class reference_counted_message_pool : public etl::ireference_counted_message_pool
{
public:
//*************************************************************************
//*************************************************************************
reference_counted_message_pool(etl::imemory_block_allocator& memory_block_allocator_)
: memory_block_allocator(memory_block_allocator_)
{
}
#if ETL_USING_CPP11
//*************************************************************************
//*************************************************************************
template <typename TMessage, typename... TArgs>
etl::reference_counted_message<TMessage, TCounter>* allocate(TArgs&&... args)
{
ETL_STATIC_ASSERT((etl::is_base_of<etl::imessage, TMessage>::value), "Not a message type");
typedef etl::reference_counted_message<TMessage, TCounter> rcm_t;
typedef rcm_t* prcm_t;
prcm_t p = ETL_NULLPTR;
lock();
p = static_cast<prcm_t>(memory_block_allocator.allocate(sizeof(rcm_t), etl::alignment_of<rcm_t>::value));
unlock();
if (p != ETL_NULLPTR)
{
::new (p) rcm_t(*this, etl::forward<TArgs>(args)...);
}
ETL_ASSERT((p != ETL_NULLPTR), ETL_ERROR(etl::reference_counted_message_pool_allocation_failure));
return p;
}
#endif
//*************************************************************************
//*************************************************************************
template <typename TMessage>
etl::reference_counted_message<TMessage, TCounter>* allocate(const TMessage& message)
{
ETL_STATIC_ASSERT((etl::is_base_of<etl::imessage, TMessage>::value), "Not a message type");
typedef etl::reference_counted_message<TMessage, TCounter> rcm_t;
typedef rcm_t* prcm_t;
prcm_t p = ETL_NULLPTR;
lock();
p = static_cast<prcm_t>(memory_block_allocator.allocate(sizeof(rcm_t), etl::alignment_of<rcm_t>::value));
unlock();
if (p != ETL_NULLPTR)
{
::new (p) rcm_t(message, *this);
}
ETL_ASSERT((p != ETL_NULLPTR), ETL_ERROR(etl::reference_counted_message_pool_allocation_failure));
return p;
}
//*************************************************************************
//*************************************************************************
template <typename TMessage>
etl::reference_counted_message<TMessage, TCounter>* allocate()
{
ETL_STATIC_ASSERT((etl::is_base_of<etl::imessage, TMessage>::value), "Not a message type");
typedef etl::reference_counted_message<TMessage, TCounter> rcm_t;
typedef rcm_t* prcm_t;
prcm_t p = ETL_NULLPTR;
lock();
p = static_cast<prcm_t>(memory_block_allocator.allocate(sizeof(rcm_t), etl::alignment_of<rcm_t>::value));
unlock();
if (p != ETL_NULLPTR)
{
::new (p) rcm_t(*this);
}
ETL_ASSERT((p != ETL_NULLPTR), ETL_ERROR(etl::reference_counted_message_pool_allocation_failure));
return p;
}
//*************************************************************************
//*************************************************************************
void release(const etl::ireference_counted_message& rcmessage)
{
bool released = false;
lock();
if (memory_block_allocator.is_owner_of(&rcmessage))
{
rcmessage.~ireference_counted_message();
released = memory_block_allocator.release(&rcmessage);
}
unlock();
ETL_ASSERT(released, ETL_ERROR(etl::reference_counted_message_pool_release_failure));
}
#if ETL_USING_CPP11
//*****************************************************
template <typename TMessage1, typename... TMessages>
struct pool_message_parameters
{
private:
// Size of the first pool message type.
static constexpr size_t size1 = sizeof(etl::reference_counted_message<TMessage1, TCounter>);
// Maximum size of the rest of the pool message types.
static constexpr size_t size2 = pool_message_parameters<TMessages...>::max_size;
// Size of the first pool message type.
static constexpr size_t alignment1 = etl::alignment_of< etl::reference_counted_message<TMessage1, TCounter>>::value;
// Maximum size of the rest of the pool message types.
static constexpr size_t alignment2 = pool_message_parameters<TMessages...>::max_alignment;
public:
// The maximum size.
static constexpr size_t max_size = (size1 < size2) ? size2 : size1;
// The maximum alignment.
static constexpr size_t max_alignment = (alignment1 < alignment2) ? alignment2 : alignment1;
};
//*****************************************************
template <typename TMessage1>
struct pool_message_parameters<TMessage1>
{
public:
ETL_STATIC_ASSERT((etl::is_base_of<etl::imessage, TMessage1>::value), "TMessage not derived from etl::imessage");
// The size of this pool message type.
static constexpr size_t max_size = sizeof(etl::reference_counted_message<TMessage1, TCounter>);
// The maximum alignment.
static constexpr size_t max_alignment = etl::alignment_of< etl::reference_counted_message<TMessage1, TCounter>>::value;
};
#else
template <typename TMessage1, typename TMessage2 = TMessage1, typename TMessage3 = TMessage1, typename TMessage4 = TMessage1,
typename TMessage5 = TMessage1, typename TMessage6 = TMessage1, typename TMessage7 = TMessage1, typename TMessage8 = TMessage1>
struct pool_message_parameters
{
ETL_STATIC_ASSERT((etl::is_base_of<etl::imessage, TMessage1>::value), "TMessage1 not derived from etl::imessage");
ETL_STATIC_ASSERT((etl::is_base_of<etl::imessage, TMessage1>::value), "TMessage2 not derived from etl::imessage");
ETL_STATIC_ASSERT((etl::is_base_of<etl::imessage, TMessage1>::value), "TMessage3 not derived from etl::imessage");
ETL_STATIC_ASSERT((etl::is_base_of<etl::imessage, TMessage1>::value), "TMessage4 not derived from etl::imessage");
ETL_STATIC_ASSERT((etl::is_base_of<etl::imessage, TMessage1>::value), "TMessage5 not derived from etl::imessage");
ETL_STATIC_ASSERT((etl::is_base_of<etl::imessage, TMessage1>::value), "TMessage6 not derived from etl::imessage");
ETL_STATIC_ASSERT((etl::is_base_of<etl::imessage, TMessage1>::value), "TMessage7 not derived from etl::imessage");
ETL_STATIC_ASSERT((etl::is_base_of<etl::imessage, TMessage1>::value), "TMessage8 not derived from etl::imessage");
static ETL_CONSTANT size_t max_size =
etl::largest< etl::reference_counted_message<TMessage1, TCounter>, etl::reference_counted_message<TMessage2, TCounter>,
etl::reference_counted_message<TMessage3, TCounter>, etl::reference_counted_message<TMessage4, TCounter>,
etl::reference_counted_message<TMessage5, TCounter>, etl::reference_counted_message<TMessage6, TCounter>,
etl::reference_counted_message<TMessage7, TCounter>, etl::reference_counted_message<TMessage8, TCounter> >::size;
static ETL_CONSTANT size_t max_alignment =
etl::largest< etl::reference_counted_message<TMessage1, TCounter>, etl::reference_counted_message<TMessage2, TCounter>,
etl::reference_counted_message<TMessage3, TCounter>, etl::reference_counted_message<TMessage4, TCounter>,
etl::reference_counted_message<TMessage5, TCounter>, etl::reference_counted_message<TMessage6, TCounter>,
etl::reference_counted_message<TMessage7, TCounter>, etl::reference_counted_message<TMessage8, TCounter> >::alignment;
};
#endif
private:
imemory_block_allocator& memory_block_allocator;
// Should not be copied.
reference_counted_message_pool(const reference_counted_message_pool&) ETL_DELETE;
reference_counted_message_pool& operator=(const reference_counted_message_pool&) ETL_DELETE;
};
#if ETL_USING_CPP11
template <typename TCounter>
template <typename TMessage1, typename... TMessages>
constexpr size_t reference_counted_message_pool< TCounter>::pool_message_parameters< TMessage1, TMessages...>::max_size;
template <typename TCounter>
template <typename TMessage1, typename... TMessages>
constexpr size_t reference_counted_message_pool< TCounter>::pool_message_parameters< TMessage1, TMessages...>::max_alignment;
#else
template <typename TCounter>
template <typename TMessage1, typename TMessage2, typename TMessage3, typename TMessage4, typename TMessage5, typename TMessage6,
typename TMessage7, typename TMessage8>
const size_t reference_counted_message_pool<TCounter>::pool_message_parameters< TMessage1, TMessage2, TMessage3, TMessage4, TMessage5, TMessage6,
TMessage7, TMessage8>::max_size;
template <typename TCounter>
template <typename TMessage1, typename TMessage2, typename TMessage3, typename TMessage4, typename TMessage5, typename TMessage6,
typename TMessage7, typename TMessage8>
const size_t reference_counted_message_pool<TCounter>::pool_message_parameters< TMessage1, TMessage2, TMessage3, TMessage4, TMessage5, TMessage6,
TMessage7, TMessage8>::max_alignment;
#endif
#if ETL_USING_CPP11 && ETL_HAS_ATOMIC
using atomic_counted_message_pool = reference_counted_message_pool<etl::atomic_int>;
#endif
} // namespace etl
#endif
etl::imemory_block_allocator
The interface for a memory block pool.
Definition imemory_block_allocator.h:44
etl::imemory_block_allocator::allocate
void * allocate(size_t required_size, size_t required_alignment)
Definition imemory_block_allocator.h:57
etl::ireference_counted_message_pool
Interface for a reference counted message pool.
Definition ireference_counted_message_pool.h:44
etl::ireference_counted_message_pool::unlock
virtual void unlock()
Definition ireference_counted_message_pool.h:65
etl::ireference_counted_message_pool::lock
virtual void lock()
Definition ireference_counted_message_pool.h:56
etl::ireference_counted_message
Definition reference_counted_message.h:48
etl::message
Definition message.h:92
etl::reference_counted_message_pool_allocation_failure
Exception if the allocation failed.
Definition reference_counted_message_pool.h:65
etl::reference_counted_message_pool_exception
Exception type for etl::reference_counted_message_pool.
Definition reference_counted_message_pool.h:52
etl::reference_counted_message_pool_release_failure
Exception if the release failed.
Definition reference_counted_message_pool.h:79
etl::reference_counted_message_pool
A pool for allocating reference counted messages.
Definition reference_counted_message_pool.h:94
etl::reference_counted_message_pool::release
void release(const etl::ireference_counted_message &rcmessage)
Destruct a message and send it back to the allocator.
Definition reference_counted_message_pool.h:191
etl::reference_counted_message_pool::reference_counted_message_pool
reference_counted_message_pool(etl::imemory_block_allocator &memory_block_allocator_)
Constructor.
Definition reference_counted_message_pool.h:100
etl::reference_counted_message_pool::allocate
etl::reference_counted_message< TMessage, TCounter > * allocate(const TMessage &message)
Allocate a reference counted message from the pool.
Definition reference_counted_message_pool.h:138
etl::reference_counted_message_pool::allocate
etl::reference_counted_message< TMessage, TCounter > * allocate()
Allocate a reference counted message from the pool.
Definition reference_counted_message_pool.h:165
etl::reference_counted_message
Definition reference_counted_message.h:69
error_handler.h
ETL_ASSERT
#define ETL_ASSERT(b, e)
Definition error_handler.h:511
etl::exception::exception
ETL_EXCEPTION_CONSTEXPR exception(string_type reason_, string_type, numeric_type)
Constructor.
Definition exception.h:81
etl::exception
Definition exception.h:59
etl::largest
Definition largest.h:45
imemory_block_allocator.h
ireference_counted_message_pool.h
largest.h
memory.h
etl
Definition absolute.h:40
etl::size
ETL_CONSTEXPR TContainer::size_type size(const TContainer &container)
Definition iterator.h:1434
platform.h
etl::reference_counted_message_pool::pool_message_parameters
Definition reference_counted_message_pool.h:253
utility.h