13#ifndef CMSIS_PLUS_RTOS_OS_MEMPOOL_H_
14#define CMSIS_PLUS_RTOS_OS_MEMPOOL_H_
18#if defined(__cplusplus)
22#if defined(OS_USE_OS_APP_CONFIG_H)
23#include <cmsis-plus/os-app-config.h>
33#pragma GCC diagnostic push
35#pragma clang diagnostic ignored "-Wc++98-compat"
36#pragma clang diagnostic ignored "-Wdocumentation-unknown-command"
48#pragma GCC diagnostic push
50#pragma clang diagnostic ignored "-Wpadded"
51#elif defined(__GNUC__)
52#pragma GCC diagnostic ignored "-Wpadded"
61#pragma GCC diagnostic push
63#elif defined(__GNUC__)
64#pragma GCC diagnostic ignored "-Wsuggest-final-methods"
65#pragma GCC diagnostic ignored "-Wsuggest-final-types"
173 template<
typename T, std::
size_t blocks, std::
size_t block_size_
bytes>
177 T
pool[(blocks * block_size_bytes +
sizeof(T) - 1) /
sizeof(T)];
187 constexpr std::size_t
189 std::size_t block_size_bytes)
193 * ((block_size_bytes + (
sizeof(T) - 1)) & ~(
sizeof(T) - 1)));
216 memory_pool (std::size_t blocks, std::size_t block_size_bytes,
229 std::size_t block_size_bytes,
const attributes& attr =
426 internal_construct_ (std::size_t blocks, std::size_t block_size_bytes,
428 std::size_t pool_size_bytes);
438 internal_init_ (
void);
447 internal_try_first_ (
void);
468#if !defined(OS_USE_RTOS_PORT_MEMORY_POOL)
476 clock* clock_ =
nullptr;
482 void* pool_addr_ =
nullptr;
487 void* allocated_pool_addr_ =
nullptr;
492 const void* allocator_ =
nullptr;
494#if defined(OS_USE_RTOS_PORT_MEMORY_POOL)
495 friend class port::memory_pool;
496 os_mempool_port_data_t port_;
503 std::size_t pool_size_bytes_ = 0;
507 std::size_t allocated_pool_size_elements_ = 0;
527 void*
volatile first_ =
nullptr;
538#pragma GCC diagnostic pop
547 template<
typename Allocator = memory::allocator<
void*>>
585 std::size_t block_size_bytes,
626#pragma GCC diagnostic push
627#if defined(__clang__)
628#elif defined(__GNUC__)
629#pragma GCC diagnostic ignored "-Wsuggest-final-methods"
630#pragma GCC diagnostic ignored "-Wsuggest-final-types"
632 template<
typename T,
typename Allocator = memory::allocator<
void*>>
750#pragma GCC diagnostic pop
760 template<
typename T, std::
size_t N>
890#pragma GCC diagnostic pop
933 return block_size_bytes_;
951 return (
count () == 0);
999 template<
typename Allocator>
1002 std::size_t blocks, std::size_t block_size_bytes,
1005 {
nullptr, blocks, block_size_bytes, attr, allocator }
1034 template<
typename Allocator>
1036 const char* name, std::size_t blocks, std::size_t block_size_bytes,
1041#if defined(OS_TRACE_RTOS_MEMPOOL)
1042 trace::printf (
"%s() @%p %s %d %d\n", __func__,
this, this->name (),
1043 blocks, block_size_bytes);
1045 if (attr.mp_pool_address !=
nullptr)
1048 internal_construct_ (blocks, block_size_bytes, attr,
nullptr, 0);
1052 allocator_ = &allocator;
1056 allocated_pool_size_elements_ = (compute_allocated_size_bytes<
1057 typename allocator_type::value_type> (blocks, block_size_bytes)
1058 +
sizeof(
typename allocator_type::value_type) - 1)
1059 /
sizeof(
typename allocator_type::value_type);
1061 allocated_pool_addr_ =
1062 const_cast<allocator_type&
> (allocator).allocate (
1063 allocated_pool_size_elements_);
1065 internal_construct_ (
1069 allocated_pool_addr_,
1070 allocated_pool_size_elements_
1071 *
sizeof(
typename allocator_type::value_type));
1091 template<
typename Allocator>
1094#if defined(OS_TRACE_RTOS_MEMPOOL)
1097 typedef typename std::allocator_traits<allocator_type>::pointer pointer;
1099 if (allocated_pool_addr_ !=
nullptr)
1101 static_cast<allocator_type*
> (
const_cast<void*
> (allocator_))->deallocate (
1102 static_cast<pointer
> (allocated_pool_addr_),
1103 allocated_pool_size_elements_);
1105 allocated_pool_addr_ =
nullptr;
1139 template<
typename T,
typename Allocator>
1177 template<
typename T,
typename Allocator>
1180 const char* name, std::size_t blocks,
1205#pragma GCC diagnostic push
1206#if defined(__clang__)
1207#elif defined(__GNUC__)
1208#pragma GCC diagnostic ignored "-Wsuggest-final-methods"
1210 template<
typename T,
typename Allocator>
1214#pragma GCC diagnostic pop
1223 template<
typename T,
typename Allocator>
1237 template<
typename T,
typename Allocator>
1251 template<
typename T,
typename Allocator>
1266 template<
typename T,
typename Allocator>
1301 template<
typename T, std::
size_t N>
1305 internal_construct_ (blocks,
sizeof(T), attr, &arena_,
sizeof(arena_));
1334 template<
typename T, std::
size_t N>
1340 internal_construct_ (blocks,
sizeof(T), attr, &arena_,
sizeof(arena_));
1356 template<
typename T, std::
size_t N>
1368 template<
typename T, std::
size_t N>
1382 template<
typename T, std::
size_t N>
1396 template<
typename T, std::
size_t N>
1410 template<
typename T, std::
size_t N>
1420#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.