28 #ifndef CMSIS_PLUS_RTOS_OS_MEMPOOL_H_ 29 #define CMSIS_PLUS_RTOS_OS_MEMPOOL_H_ 33 #if defined(__cplusplus) 49 #pragma GCC diagnostic push 50 #pragma GCC diagnostic ignored "-Wpadded" 71 using size_t = uint16_t;
164 template<
typename T, std::
size_t blocks, std::
size_t block_size_bytes>
168 T
pool[(blocks * block_size_bytes +
sizeof(T) - 1) /
sizeof(T)];
178 constexpr std::size_t
180 std::size_t block_size_bytes)
184 * ((block_size_bytes + (
sizeof(T) - 1)) & ~(
sizeof(T) - 1)));
207 memory_pool (std::size_t blocks, std::size_t block_size_bytes,
220 std::size_t block_size_bytes,
const attributes& attr =
417 internal_construct_ (std::size_t blocks, std::size_t block_size_bytes,
419 std::size_t pool_size_bytes);
429 internal_init_ (
void);
438 internal_try_first_ (
void);
459 #if !defined(OS_USE_RTOS_PORT_MEMORY_POOL) 467 clock* clock_ =
nullptr;
473 void* pool_addr_ =
nullptr;
478 void* allocated_pool_addr_ =
nullptr;
483 const void* allocator_ =
nullptr;
485 #if defined(OS_USE_RTOS_PORT_MEMORY_POOL) 486 friend class port::memory_pool;
487 os_mempool_port_data_t port_;
494 std::size_t pool_size_bytes_ = 0;
498 std::size_t allocated_pool_size_elements_ = 0;
518 void*
volatile first_ =
nullptr;
537 template<
typename Allocator = memory::allocator<
void*>>
575 std::size_t block_size_bytes,
616 template<
typename T,
typename Allocator = memory::allocator<
void*>>
743 template<
typename T, std::
size_t N>
756 static const std::size_t blocks = N;
873 #pragma GCC diagnostic pop 921 return block_size_bytes_;
943 return (
count () == 0);
995 template<
typename Allocator>
998 std::size_t blocks, std::size_t block_size_bytes,
1001 {
nullptr, blocks, block_size_bytes, attr, allocator }
1031 template<
typename Allocator>
1033 const char*
name, std::size_t blocks, std::size_t block_size_bytes,
1038 #if defined(OS_TRACE_RTOS_MEMPOOL) 1040 blocks, block_size_bytes);
1042 if (attr.mp_pool_address !=
nullptr)
1045 internal_construct_ (blocks, block_size_bytes, attr,
nullptr, 0);
1058 allocated_pool_addr_ =
1060 allocated_pool_size_elements_);
1062 internal_construct_ (
1066 allocated_pool_addr_,
1067 allocated_pool_size_elements_
1088 template<
typename Allocator>
1091 #if defined(OS_TRACE_RTOS_MEMPOOL) 1094 typedef typename std::allocator_traits<allocator_type>::pointer pointer;
1096 if (allocated_pool_addr_ !=
nullptr)
1099 static_cast<pointer> (allocated_pool_addr_),
1100 allocated_pool_size_elements_);
1102 allocated_pool_addr_ =
nullptr;
1136 template<
typename T,
typename Allocator>
1175 template<
typename T,
typename Allocator>
1178 const char*
name, std::size_t blocks,
1204 template<
typename T,
typename Allocator>
1217 template<
typename T,
typename Allocator>
1231 template<
typename T,
typename Allocator>
1245 template<
typename T,
typename Allocator>
1260 template<
typename T,
typename Allocator>
1295 template<
typename T, std::
size_t N>
1299 internal_construct_ (blocks,
sizeof(T), attr, &arena_,
sizeof(arena_));
1328 template<
typename T, std::
size_t N>
1334 internal_construct_ (blocks,
sizeof(T), attr, &
arena_,
sizeof(
arena_));
1350 template<
typename T, std::
size_t N>
1363 template<
typename T, std::
size_t N>
1377 template<
typename T, std::
size_t N>
1391 template<
typename T, std::
size_t N>
1405 template<
typename T, std::
size_t N>
value_type * alloc(void)
Allocate a memory block.
virtual ~memory_pool_inclusive()
Destruct the memory pool object instance.
value_type * try_alloc(void)
Allocate a memory block.
constexpr std::size_t compute_allocated_size_bytes(std::size_t blocks, std::size_t block_size_bytes)
Calculator for pool storage requirements.
result_t free(value_type *block)
Free the memory block.
std::size_t capacity(void) const
Get memory pool capacity.
std::size_t mp_pool_size_bytes
Size of the user defined storage for the memory pool.
void deallocate(value_type *addr, std::size_t elements) noexcept
Deallocate the number of memory blocks of type value_type.
virtual ~memory_pool()
Destruct the memory pool object instance.
Priority ordered list of threads.
void * timed_alloc(clock::duration_t timeout)
Allocate a memory block with timeout.
attributes & operator=(const attributes &)=default
bool empty(void) const
Check if the memory pool is empty.
Base class for attributes.
arena< void *, blocks, sizeof(T)> arena_
Local storage for the pool.
std::size_t block_size(void) const
Get block size.
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.
virtual ~memory_pool_typed()
Destruct the memory pool object instance.
virtual ~memory_pool_allocated()
Destruct the memory pool object instance.
static const attributes initializer
Default memory pool initialiser.
void * pool(void)
Get the pool storage address.
Storage for a memory pool.
const char * name(void) const
Get object name.
Base class for named system objects.
T value_type
Standard allocator type definition.
void * alloc(void)
Allocate a memory block.
Template of a synchronised memory pool with block type and allocator.
port::clock::duration_t duration_t
Type of variables holding clock durations.
result_t reset(void)
Reset the memory pool.
result_t free(value_type *block)
Free the memory block.
Synchronised memory pool, using the default RTOS allocator.
int printf(const char *format,...)
Write a formatted string to the trace device.
static constexpr memory_pool::size_t max_size
Maximum pool size.
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 allocator.
value_type * timed_alloc(clock::duration_t timeout)
Allocate a memory block with timeout.
T value_type
Type of elements to be allocated.
memory_pool(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.
value_type * alloc(void)
Allocate a memory block.
T value_type
Local type of message.
void * try_alloc(void)
Try to allocate a memory block.
std::size_t count(void) const
Get blocks count.
value_type * timed_alloc(clock::duration_t timeout)
Allocate a memory block with timeout.
memory_pool_inclusive(const attributes &attr=initializer)
Construct a memory pool object instance.
void * mp_pool_address
Address of the user defined storage for the memory pool.
bool full(void) const
Check if the memory pool is full.
value_type * try_alloc(void)
Allocate a memory block.
result_t free(void *block)
Free the memory block.
uint32_t result_t
Type of values returned by RTOS functions.
bool operator==(const memory_pool &rhs) const
Compare memory pools.
uint16_t size_t
Type of memory pool size storage.
allocator_stateless_default_resource< T > allocator
Type of allocator used by the system objects. Must be stateless.
Template of a synchronised memory pool with block type and local storage.
Standard allocator based on the RTOS system default memory manager.
~attributes()=default
Destruct the memory pool attributes object instance.
constexpr attributes()
Construct a memory pool attributes object instance.