Memory resource managing a pool of same size blocks, using an existing arena. More...
#include <cmsis-plus/memory/block-pool.h>
Public Member Functions | |
Public Member Functions | |
void * | allocate (std::size_t bytes, std::size_t alignment=max_align) |
Allocate a memory block. | |
void | deallocate (void *addr, std::size_t bytes, std::size_t alignment=max_align) noexcept |
Deallocate the previously allocated memory block. | |
bool | is_equal (memory_resource const &other) const noexcept |
Compare for equality with another memory_resource . | |
void | reset (void) noexcept |
Reset the memory manager to the initial state. | |
bool | coalesce (void) noexcept |
Coalesce free blocks. | |
std::size_t | max_size (void) const noexcept |
Get the largest value that can be passed to allocate() . | |
out_of_memory_handler_t | out_of_memory_handler (out_of_memory_handler_t handler) |
Set the out of memory handler. | |
out_of_memory_handler_t | out_of_memory_handler (void) |
Get the out of memory handler. | |
std::size_t | total_bytes (void) |
Get the total size of managed memory. | |
std::size_t | allocated_bytes (void) |
Get the current size of all allocated chunks. | |
std::size_t | max_allocated_bytes (void) |
Get the maximum allocated size. | |
std::size_t | free_bytes (void) |
Get the current size of all free chunks. | |
std::size_t | allocated_chunks (void) |
Get the current number of allocated chunks. | |
std::size_t | free_chunks (void) |
Get the current number of free chunks. | |
std::size_t | allocations (void) |
Get the number of allocations. | |
std::size_t | deallocations (void) |
Get the number of deallocations. | |
void | trace_print_statistics (void) |
Print a long message with usage statistics. | |
Public Member Functions | |
const char * | name (void) const |
Get object name. | |
Static Public Attributes | |
static constexpr std::size_t | max_align = alignof(std::max_align_t) |
The largest alignment for the platform. Also default when supplied alignment is not supported. | |
Protected Member Functions | |
Private Member Functions | |
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. | |
void | internal_reset_ (void) noexcept |
Internal function to reset the memory resource object. | |
virtual void * | do_allocate (std::size_t bytes, std::size_t alignment) override |
Implementation of the memory allocator. | |
virtual void | do_deallocate (void *addr, std::size_t bytes, std::size_t alignment) noexcept override |
Implementation of the memory deallocator. | |
virtual std::size_t | do_max_size (void) const noexcept override |
Implementation of the function to get max size. | |
virtual void | do_reset (void) noexcept override |
Implementation of the function to reset the memory manager. | |
Private Member Functions | |
virtual bool | do_is_equal (memory_resource const &other) const noexcept |
Implementation of the equality comparator. | |
virtual bool | do_coalesce (void) noexcept |
Implementation of the function to coalesce free blocks. | |
void | internal_increase_allocated_statistics (std::size_t bytes) noexcept |
Update statistics after allocation. | |
void | internal_decrease_allocated_statistics (std::size_t bytes) noexcept |
Update statistics after deallocation. | |
Constructors & Destructor | |
block_pool (std::size_t blocks, std::size_t block_size_bytes, void *addr, std::size_t bytes) | |
Construct a memory resource object instance. | |
block_pool (const char *name, std::size_t blocks, std::size_t block_size_bytes, void *addr, std::size_t bytes) | |
Construct a named memory resource object instance. | |
virtual | ~block_pool () override |
Destruct the memory resource object instance. | |
block_pool (const char *name) | |
Default constructor. Construct a memory resource object instance. | |
Memory resource managing a pool of same size blocks, using an existing arena.
This class is a deterministic, non-fragmenting memory manager, that allocates identical size blocks from a pool.
This memory manager is ideal for allocation of system objects.
The only drawback is that the maximum number of objects must be known before the first allocations, but usually this is not a problem.
Definition at line 56 of file block-pool.h.
|
inline |
Construct a memory resource object instance.
[in] | blocks | The maximum number of items in the pool. |
[in] | block_size_bytes | The size of an item, in bytes. |
[in] | addr | Begin of allocator arena. |
[in] | bytes | Size of allocator arena, in bytes. |
Definition at line 470 of file block-pool.h.
|
inline |
Construct a named memory resource object instance.
name | Pointer to name. | |
[in] | blocks | The maximum number of items in the pool. |
[in] | block_size_bytes | The size of an item, in bytes. |
[in] | addr | Begin of allocator arena. |
[in] | bytes | Size of allocator arena, in bytes. |
Definition at line 478 of file block-pool.h.
|
inlineprotected |
Default constructor. Construct a memory resource object instance.
Definition at line 463 of file block-pool.h.
|
overridevirtual |
Destruct the memory resource object instance.
Definition at line 35 of file block-pool.cpp.
|
inlineinherited |
Allocate a memory block.
bytes | Number of bytes to allocate. |
alignment | Alignment constraint (power of 2). |
nullptr
.Allocate storage with a size of at least bytes bytes. The returned storage is aligned to the specified alignment if such alignment is supported, and to alignof(std::max_align_t)
otherwise.
If the storage of the requested size and alignment cannot be obtained:
nullptr
;Equivalent to return do_allocate(bytes, alignment);
.
bad_alloc()
exception.Definition at line 1289 of file os-memory.h.
|
inlineinherited |
Get the current size of all allocated chunks.
Definition at line 1401 of file os-memory.h.
|
inlineinherited |
Get the current number of allocated chunks.
Definition at line 1419 of file os-memory.h.
|
inlineinherited |
Get the number of allocations.
Definition at line 1431 of file os-memory.h.
|
inlinenoexceptinherited |
Coalesce free blocks.
true | if the operation freed more memory. |
false | if the operation was ineffective. |
In case the memory manager does not coalesce during deallocation, traverse the list of free blocks and coalesce.
Return true
if the operation was successful and at least one larger block resulted.
Definition at line 1364 of file os-memory.h.
|
inlinenoexceptinherited |
Deallocate the previously allocated memory block.
addr | Address of the block to free. |
bytes | Number of bytes to deallocate (may be 0 if unknown). |
alignment | Alignment constraint (power of 2). |
Deallocate the storage pointed to by addr. The address shall have been returned by a prior call to allocate()
on a memory_resource that compares equal to *this
, and the storage it points to shall not yet have been deallocated.
Equivalent to return do_deallocate(p, bytes, alignment);
.
Definition at line 1311 of file os-memory.h.
|
inlineinherited |
Get the number of deallocations.
Definition at line 1437 of file os-memory.h.
|
overrideprotectedvirtual |
Implementation of the memory allocator.
[in] | bytes | Number of bytes to allocate. |
[in] | alignment | Alignment constraint (power of 2). |
nullptr
. Implements os::rtos::memory::memory_resource.
Definition at line 48 of file block-pool.cpp.
|
protectedvirtualnoexceptinherited |
Implementation of the function to coalesce free blocks.
true | if the operation resulted in larger blocks. |
false | if the operation was ineffective. |
The default implementation of this virtual function returns false, meaning the operation was ineffective.
Override this function to perform the action.
Definition at line 464 of file os-memory.cpp.
|
overrideprotectedvirtualnoexcept |
Implementation of the memory deallocator.
[in] | addr | Address of a previously allocated block to free. |
[in] | bytes | Number of bytes to deallocate (may be 0 if unknown). |
[in] | alignment | Alignment constraint (power of 2). |
Implements os::rtos::memory::memory_resource.
Definition at line 82 of file block-pool.cpp.
|
protectedvirtualnoexceptinherited |
Implementation of the equality comparator.
other | Reference to another memory_resource . |
true | The memory_resource objects are equal. |
false | The memory_resource objects are not equal. |
Compares *this
for equality with other
. Two memory_resources compare equal if and only if memory allocated from one memory_resource can be deallocated from the other and vice versa.
The most-derived type of other may not match the most derived type of *this
. A derived class implementation therefore must typically check whether the most derived types of *this
and other match using dynamic_cast, and immediately return false if the cast fails.
Definition at line 416 of file os-memory.cpp.
|
overrideprotectedvirtualnoexcept |
Implementation of the function to get max size.
Reimplemented from os::rtos::memory::memory_resource.
Definition at line 125 of file block-pool.cpp.
|
overrideprotectedvirtualnoexcept |
Implementation of the function to reset the memory manager.
Reimplemented from os::rtos::memory::memory_resource.
Definition at line 131 of file block-pool.cpp.
|
inlineinherited |
Get the current size of all free chunks.
Definition at line 1413 of file os-memory.h.
|
inlineinherited |
Get the current number of free chunks.
Definition at line 1425 of file os-memory.h.
|
protectednoexcept |
Internal function to construct the memory resource object instance.
[in] | blocks | The maximum number of items in the pool. |
[in] | block_size_bytes | The size of an item, in bytes. |
[in] | addr | Begin of allocator arena. |
[in] | bytes | Size of allocator arena, in bytes. |
Definition at line 140 of file block-pool.cpp.
|
protectednoexceptinherited |
Update statistics after deallocation.
[in] | bytes | Number of deallocated bytes. |
Definition at line 486 of file os-memory.cpp.
|
protectednoexceptinherited |
Update statistics after allocation.
[in] | bytes | Number of allocated bytes. |
Definition at line 470 of file os-memory.cpp.
|
protectednoexcept |
Internal function to reset the memory resource object.
Definition at line 175 of file block-pool.cpp.
|
inlinenoexceptinherited |
Compare for equality with another memory_resource
.
other | Reference to another memory_resource . |
true | The memory_resource objects are equal. |
false | The memory_resource objects are not equal. |
Compare *this
for equality with other. Two memory_resources
compare equal if and only if memory allocated from one memory_resource
can be deallocated from the other and vice versa.
Definition at line 1330 of file os-memory.h.
|
inlineinherited |
Get the maximum allocated size.
Definition at line 1407 of file os-memory.h.
|
inlinenoexceptinherited |
Get the largest value that can be passed to allocate()
.
Definition at line 1339 of file os-memory.h.
|
inlineinherited |
Get object name.
All objects return a non-null string; anonymous objects return "-"
.
Definition at line 759 of file os-decls.h.
|
inlineinherited |
Set the out of memory handler.
handler | Pointer to new handler. |
Definition at line 1374 of file os-memory.h.
|
inlineinherited |
Get the out of memory handler.
Definition at line 1389 of file os-memory.h.
|
inlinenoexceptinherited |
Reset the memory manager to the initial state.
Definition at line 1348 of file os-memory.h.
|
inlineinherited |
Get the total size of managed memory.
Definition at line 1395 of file os-memory.h.
|
inlineinherited |
Print a long message with usage statistics.
Definition at line 1443 of file os-memory.h.
|
staticconstexprinherited |
The largest alignment for the platform. Also default when supplied alignment is not supported.
Definition at line 168 of file os-memory.h.