28#ifndef CMSIS_PLUS_RTOS_OS_MEMPOOL_H_
29#define CMSIS_PLUS_RTOS_OS_MEMPOOL_H_
33#if defined(__cplusplus)
44#pragma GCC diagnostic push
47#pragma clang diagnostic ignored "-Wc++98-compat"
48#pragma clang diagnostic ignored "-Wdocumentation-unknown-command"
60#pragma GCC diagnostic push
61#pragma GCC diagnostic ignored "-Wpadded"
175 template<
typename T, std::
size_t blocks, std::
size_t block_size_
bytes>
179 T
pool[(blocks * block_size_bytes +
sizeof(T) - 1) /
sizeof(T)];
189 constexpr std::size_t
191 std::size_t block_size_bytes)
195 * ((block_size_bytes + (
sizeof(T) - 1)) & ~(
sizeof(T) - 1)));
218 memory_pool (std::size_t blocks, std::size_t block_size_bytes,
231 std::size_t block_size_bytes,
const attributes& attr =
428 internal_construct_ (std::size_t blocks, std::size_t block_size_bytes,
430 std::size_t pool_size_bytes);
440 internal_init_ (
void);
449 internal_try_first_ (
void);
470#if !defined(OS_USE_RTOS_PORT_MEMORY_POOL)
478 clock* clock_ =
nullptr;
484 void* pool_addr_ =
nullptr;
489 void* allocated_pool_addr_ =
nullptr;
494 const void* allocator_ =
nullptr;
496#if defined(OS_USE_RTOS_PORT_MEMORY_POOL)
497 friend class port::memory_pool;
498 os_mempool_port_data_t port_;
505 std::size_t pool_size_bytes_ = 0;
509 std::size_t allocated_pool_size_elements_ = 0;
529 void*
volatile first_ =
nullptr;
548 template<
typename Allocator = memory::allocator<
void*>>
586 std::size_t block_size_bytes,
627 template<
typename T,
typename Allocator = memory::allocator<
void*>>
754 template<
typename T, std::
size_t N>
884#pragma GCC diagnostic pop
928 return block_size_bytes_;
946 return (
count () == 0);
994 template<
typename Allocator>
997 std::size_t blocks, std::size_t block_size_bytes,
1000 {
nullptr, blocks, block_size_bytes, attr, allocator }
1030 template<
typename Allocator>
1032 const char* name, std::size_t blocks, std::size_t block_size_bytes,
1037#if defined(OS_TRACE_RTOS_MEMPOOL)
1038 trace::printf (
"%s() @%p %s %d %d\n", __func__,
this, this->name (),
1039 blocks, block_size_bytes);
1041 if (attr.mp_pool_address !=
nullptr)
1044 internal_construct_ (blocks, block_size_bytes, attr,
nullptr, 0);
1048 allocator_ = &allocator;
1052 allocated_pool_size_elements_ = (compute_allocated_size_bytes<
1053 typename allocator_type::value_type> (blocks, block_size_bytes)
1054 +
sizeof(
typename allocator_type::value_type) - 1)
1055 /
sizeof(
typename allocator_type::value_type);
1057 allocated_pool_addr_ =
1058 const_cast<allocator_type&
> (allocator).allocate (
1059 allocated_pool_size_elements_);
1061 internal_construct_ (
1065 allocated_pool_addr_,
1066 allocated_pool_size_elements_
1067 *
sizeof(
typename allocator_type::value_type));
1087 template<
typename Allocator>
1090#if defined(OS_TRACE_RTOS_MEMPOOL)
1093 typedef typename std::allocator_traits<allocator_type>::pointer pointer;
1095 if (allocated_pool_addr_ !=
nullptr)
1097 static_cast<allocator_type*
> (
const_cast<void*
> (allocator_))->deallocate (
1098 static_cast<pointer
> (allocated_pool_addr_),
1099 allocated_pool_size_elements_);
1101 allocated_pool_addr_ =
nullptr;
1135 template<
typename T,
typename Allocator>
1174 template<
typename T,
typename Allocator>
1177 const char* name, std::size_t blocks,
1203 template<
typename T,
typename Allocator>
1216 template<
typename T,
typename Allocator>
1230 template<
typename T,
typename Allocator>
1244 template<
typename T,
typename Allocator>
1259 template<
typename T,
typename Allocator>
1294 template<
typename T, std::
size_t N>
1298 internal_construct_ (blocks,
sizeof(T), attr, &arena_,
sizeof(arena_));
1327 template<
typename T, std::
size_t N>
1333 internal_construct_ (blocks,
sizeof(T), attr, &arena_,
sizeof(arena_));
1349 template<
typename T, std::
size_t N>
1362 template<
typename T, std::
size_t N>
1376 template<
typename T, std::
size_t N>
1390 template<
typename T, std::
size_t N>
1404 template<
typename T, std::
size_t N>
1414#pragma GCC diagnostic pop
Base class for attributes.
Base class for named system objects.
const char * name(void) const
Get object name.
Priority ordered list of threads.
Standard allocator based on the RTOS system default memory manager.
Storage for a memory pool.
T pool[(blocks *block_size_bytes+sizeof(T) - 1)/sizeof(T)]
void * mp_pool_address
Address of the user defined storage for the memory pool.
~attributes()=default
Destruct the memory pool attributes object instance.
std::size_t mp_pool_size_bytes
Size of the user defined storage for the memory pool.
attributes(const attributes &)=default
attributes(attributes &&)=default
constexpr attributes()
Construct a memory pool attributes object instance.
attributes & operator=(const attributes &)=default
Template of a synchronised memory pool with allocator.
virtual ~memory_pool_allocated() override
Destruct the memory pool object instance.
memory_pool_allocated(const char *name, std::size_t blocks, std::size_t block_size_bytes, const attributes &attr=initializer, const allocator_type &allocator=allocator_type())
Construct a named memory pool object instance.
Allocator allocator_type
Standard allocator type definition.
memory_pool_allocated(std::size_t blocks, std::size_t block_size_bytes, const attributes &attr=initializer, const allocator_type &allocator=allocator_type())
Construct a memory pool object instance.
Template of a synchronised memory pool with block type and local storage.
arena< void *, blocks, sizeof(T)> arena_
Local storage for the pool.
result_t free(value_type *block)
Free the memory block.
memory_pool_inclusive(const attributes &attr=initializer)
Construct a memory pool object instance.
T value_type
Local type of message.
value_type * timed_alloc(clock::duration_t timeout)
Allocate a memory block with timeout.
virtual ~memory_pool_inclusive() override
Destruct the memory pool object instance.
static const std::size_t blocks
Local constant based on template definition.
value_type * try_alloc(void)
Allocate a memory block.
value_type * alloc(void)
Allocate a memory block.
Template of a synchronised memory pool with block type and allocator.
result_t free(value_type *block)
Free the memory block.
T value_type
Standard allocator type definition.
value_type * try_alloc(void)
Allocate a memory block.
value_type * alloc(void)
Allocate a memory block.
Allocator allocator_type
Standard allocator type definition.
virtual ~memory_pool_typed() override
Destruct the memory pool object instance.
memory_pool_typed(std::size_t blocks, const memory_pool::attributes &attr=memory_pool::initializer, const allocator_type &allocator=allocator_type())
Construct a memory pool object instance.
value_type * timed_alloc(clock::duration_t timeout)
Allocate a memory block with timeout.
Synchronised memory pool, using the default RTOS allocator.
void * timed_alloc(clock::duration_t timeout)
Allocate a memory block with timeout.
void * try_alloc(void)
Try to allocate a memory block.
std::size_t block_size(void) const
Get block size.
void * alloc(void)
Allocate a memory block.
bool empty(void) const
Check if the memory pool is empty.
std::size_t capacity(void) const
Get memory pool capacity.
std::size_t count(void) const
Get blocks count.
result_t free(void *block)
Free the memory block.
void * pool(void)
Get the pool storage address.
result_t reset(void)
Reset the memory pool.
constexpr std::size_t compute_allocated_size_bytes(std::size_t blocks, std::size_t block_size_bytes)
Calculator for pool storage requirements.
virtual ~memory_pool()
Destruct the memory pool object instance.
bool operator==(const memory_pool &rhs) const
Compare memory pools.
bool full(void) const
Check if the memory pool is full.
int printf(const char *format,...)
Write a formatted string to the trace device.
port::clock::duration_t duration_t
Type of variables holding clock durations.
static constexpr memory_pool::size_t max_size
Maximum pool size.
uint16_t size_t
Type of memory pool size storage.
static const attributes initializer
Default memory pool initialiser.
uint32_t result_t
Type of values returned by RTOS functions.