12#ifndef CMSIS_PLUS_RTOS_OS_MEMPOOL_H_
13#define CMSIS_PLUS_RTOS_OS_MEMPOOL_H_
17#if defined(__cplusplus)
21#if defined(OS_USE_OS_APP_CONFIG_H)
22#include <cmsis-plus/os-app-config.h>
32#pragma GCC diagnostic push
34#pragma clang diagnostic ignored "-Wc++98-compat"
35#pragma clang diagnostic ignored "-Wdocumentation-unknown-command"
47#pragma GCC diagnostic push
49#pragma clang diagnostic ignored "-Wpadded"
50#elif defined(__GNUC__)
51#pragma GCC diagnostic ignored "-Wpadded"
60#pragma GCC diagnostic push
62#elif defined(__GNUC__)
63#pragma GCC diagnostic ignored "-Wsuggest-final-methods"
64#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)];
186 template <
typename T>
187 constexpr std::size_t
189 std::size_t block_size_bytes)
193 * ((block_size_bytes + (
sizeof (T) - 1)) & ~(
sizeof (T) - 1)));
217 memory_pool (std::size_t blocks, std::size_t block_size_bytes,
230 std::size_t block_size_bytes,
424 internal_construct_ (std::size_t blocks, std::size_t block_size_bytes,
426 std::size_t pool_size_bytes);
436 internal_init_ (
void);
445 internal_try_first_ (
void);
465#if !defined(OS_USE_RTOS_PORT_MEMORY_POOL)
473 clock* clock_ =
nullptr;
479 void* pool_addr_ =
nullptr;
484 void* allocated_pool_addr_ =
nullptr;
489 const void* allocator_ =
nullptr;
491#if defined(OS_USE_RTOS_PORT_MEMORY_POOL)
492 friend class port::memory_pool;
493 os_mempool_port_data_t port_;
500 std::size_t pool_size_bytes_ = 0;
504 std::size_t allocated_pool_size_elements_ = 0;
524 void*
volatile first_ =
nullptr;
534#pragma GCC diagnostic pop
543 template <
typename Allocator = memory::allocator<
void*>>
580 std::size_t block_size_bytes,
621#pragma GCC diagnostic push
622#if defined(__clang__)
623#elif defined(__GNUC__)
624#pragma GCC diagnostic ignored "-Wsuggest-final-methods"
625#pragma GCC diagnostic ignored "-Wsuggest-final-types"
627 template <
typename T,
typename Allocator = memory::allocator<
void*>>
744#pragma GCC diagnostic pop
754 template <
typename T, std::
size_t N>
883#pragma GCC diagnostic pop
925 return block_size_bytes_;
943 return (
count () == 0);
991 template <
typename Allocator>
993 std::size_t blocks, std::size_t block_size_bytes,
1025 template <
typename Allocator>
1027 const char* name, std::size_t blocks, std::size_t block_size_bytes,
1031#if defined(OS_TRACE_RTOS_MEMPOOL)
1032 trace::printf (
"%s() @%p %s %d %d\n", __func__,
this, this->name (),
1033 blocks, block_size_bytes);
1038 internal_construct_ (blocks, block_size_bytes, attr,
nullptr, 0);
1042 allocator_ = &allocator;
1046 allocated_pool_size_elements_
1048 typename allocator_type::value_type> (blocks,
1050 +
sizeof (
typename allocator_type::value_type) - 1)
1051 /
sizeof (
typename allocator_type::value_type);
1053 allocated_pool_addr_
1055 allocated_pool_size_elements_);
1057 internal_construct_ (
1058 blocks, block_size_bytes, attr, allocated_pool_addr_,
1059 allocated_pool_size_elements_
1060 *
sizeof (
typename allocator_type::value_type));
1080 template <
typename Allocator>
1083#if defined(OS_TRACE_RTOS_MEMPOOL)
1086 typedef typename std::allocator_traits<allocator_type>::pointer pointer;
1088 if (allocated_pool_addr_ !=
nullptr)
1091 ->deallocate (
static_cast<pointer
> (allocated_pool_addr_),
1092 allocated_pool_size_elements_);
1094 allocated_pool_addr_ =
nullptr;
1128 template <
typename T,
typename Allocator>
1165 template <
typename T,
typename Allocator>
1167 const char* name, std::size_t blocks,
1192#pragma GCC diagnostic push
1193#if defined(__clang__)
1194#elif defined(__GNUC__)
1195#pragma GCC diagnostic ignored "-Wsuggest-final-methods"
1197 template <
typename T,
typename Allocator>
1201#pragma GCC diagnostic pop
1210 template <
typename T,
typename Allocator>
1225 template <
typename T,
typename Allocator>
1240 template <
typename T,
typename Allocator>
1255 template <
typename T,
typename Allocator>
1290 template <
typename T, std::
size_t N>
1293 internal_construct_ (blocks,
sizeof (T), attr, &arena_,
sizeof (arena_));
1322 template <
typename T, std::
size_t N>
1343 template <
typename T, std::
size_t N>
1355 template <
typename T, std::
size_t N>
1369 template <
typename T, std::
size_t N>
1383 template <
typename T, std::
size_t N>
1397 template <
typename T, std::
size_t N>
1407#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.
result_t free(value_type *block)
Free the memory block.
memory_pool_inclusive(const attributes &attr=initializer)
Construct a memory pool object instance.
arena< void *, blocks, sizeof(T)> arena_
Local storage for the pool.
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.