13#ifndef CMSIS_PLUS_MEMORY_BLOCK_POOL_H_
14#define CMSIS_PLUS_MEMORY_BLOCK_POOL_H_
18#if defined(__cplusplus)
26#pragma GCC diagnostic push
28#pragma clang diagnostic ignored "-Wc++98-compat"
72 block_pool (std::size_t blocks, std::size_t block_size_bytes,
void* addr,
84 std::size_t block_size_bytes,
void* addr, std::size_t bytes);
140 void* addr, std::size_t bytes)
noexcept;
159 do_allocate (std::size_t bytes, std::size_t alignment)
override;
170 do_deallocate (
void* addr, std::size_t bytes, std::size_t alignment)
206 void* pool_addr_ =
nullptr;
211 void*
volatile first_ =
nullptr;
216 std::size_t blocks_ = 0;
221 std::size_t block_size_bytes_ = 0;
226 volatile std::size_t count_ = 0;
248 template<
typename T, std::
size_t N>
258 static_assert(
sizeof(
value_type) >=
sizeof(
void*),
259 "Template type T must be large enough to store a pointer.");
345 template<
typename T,
typename A = os::rtos::memory::allocator<T>>
355 static_assert(
sizeof(
value_type) >=
sizeof(
void*),
356 "Template type T must be large enough to store a pointer.");
370 static_assert(
sizeof(
value_type) ==
sizeof(
typename allocator_traits::value_type),
371 "The allocator must be parametrised with a type of same size.");
464 rtos::memory::memory_resource
471 void* addr, std::size_t bytes) :
473 {
nullptr, blocks, block_size_bytes, addr, bytes }
479 std::size_t block_size_bytes,
void* addr,
481 rtos::memory::memory_resource
485 block_size_bytes, addr, bytes,
this, this->name ());
487 internal_construct_ (blocks, block_size_bytes, addr, bytes);
492 template<
typename T, std::
size_t N>
499 template<
typename T, std::
size_t N>
506 trace::printf (
"%s() @%p %s\n", __func__,
this, this->name ());
508 internal_construct_ (blocks,
sizeof(value_type), &arena_[0],
512 template<
typename T, std::
size_t N>
515 trace::printf (
"%s() @%p %s\n", __func__,
this, this->name ());
520 template<
typename T,
typename A>
528 template<
typename T,
typename A>
530 const char* name, std::size_t blocks,
const allocator_type& allocator) :
534 trace::printf (
"%s(%u,%p) @%p %s\n", __func__, blocks, &allocator,
this,
539 static_cast<allocator_type*
> (&
const_cast<allocator_type&
> (allocator));
541 void* addr = allocator_->allocate (blocks);
547 internal_construct_ (blocks,
sizeof(value_type), addr,
548 blocks *
sizeof(value_type));
551 template<
typename T,
typename A>
554 trace::printf (
"%s() @%p %s\n", __func__,
this, this->name ());
557 if (allocator_ !=
nullptr)
559 allocator_->deallocate (
560 static_cast<typename allocator_traits::pointer
> (pool_addr_),
564 allocator_ =
nullptr;
573#pragma GCC diagnostic pop
Memory resource managing a dynamically allocated pool. of same size blocks of type T.
A allocator_type
Standard allocator type definition.
T value_type
Standard allocator type definition.
virtual ~block_pool_typed_allocated() override
Destruct the memory resource object instance.
block_pool_typed_allocated(std::size_t blocks, const allocator_type &allocator=allocator_type())
Construct a memory resource object instance.
std::allocator_traits< A > allocator_traits
Standard allocator traits definition.
Memory resource managing an internal pool. of same size blocks of type T.
block_pool_typed_inclusive(void)
Construct a memory resource object instance.
T value_type
Standard allocator type definition.
static const std::size_t blocks
Local constant based on template definition.
virtual ~block_pool_typed_inclusive() override
Destruct the memory resource object instance.
Memory resource managing a pool of same size blocks, using an existing arena.
block_pool(std::size_t blocks, std::size_t block_size_bytes, void *addr, std::size_t bytes)
Construct a memory resource object instance.
virtual void * do_allocate(std::size_t bytes, std::size_t alignment) override
Implementation of the memory allocator.
virtual std::size_t do_max_size(void) const noexcept override
Implementation of the function to get max size.
void internal_construct_(std::size_t blocks, std::size_t block_size_bytes, void *addr, std::size_t bytes) noexcept
Internal function to construct the memory resource object instance.
virtual void do_deallocate(void *addr, std::size_t bytes, std::size_t alignment) noexcept override
Implementation of the memory deallocator.
void internal_reset_(void) noexcept
Internal function to reset the memory resource object.
virtual ~block_pool() override
Destruct the memory resource object instance.
virtual void do_reset(void) noexcept override
Implementation of the function to reset the memory manager.
const char * name(void) const
Get object name.
Memory resource manager (abstract class).
int printf(const char *format,...)
Write a formatted string to the trace device.
void __throw_bad_alloc(void)
Single file µOS++ RTOS definitions.