#include <heap_based_pool_allocator.hpp>
Classes | |
union | Node |
Public Member Functions | |
virtual void * | allocate (std::size_t size) |
virtual void | deallocate (const void *ptr) |
virtual uint16_t | getBlockCapacity () const |
uint16_t | getBlockCapacityHardLimit () const |
uint16_t | getNumAllocatedBlocks () const |
uint16_t | getNumReservedBlocks () const |
HeapBasedPoolAllocator (uint16_t block_capacity_soft_limit, uint16_t block_capacity_hard_limit=0) | |
void | shrink () |
~HeapBasedPoolAllocator () | |
![]() | |
virtual | ~IPoolAllocator () |
Private Attributes | |
const uint16_t | capacity_hard_limit_ |
const uint16_t | capacity_soft_limit_ |
uint16_t | num_allocated_blocks_ |
uint16_t | num_reserved_blocks_ |
Node * | reserve_ |
Additional Inherited Members | |
![]() | |
Noncopyable () | |
~Noncopyable () | |
A special-purpose implementation of a pool allocator that keeps the pool in the heap using malloc()/free(). The pool grows dynamically, ad-hoc, thus using as little memory as possible.
Allocated blocks will not be freed back automatically, but there are two ways to force their deallocation:
The pool can be limited in growth with hard and soft limits. The soft limit defines the value that will be reported via IPoolAllocator::getBlockCapacity(). The hard limit defines the maximum number of blocks that can be allocated from heap. Typically, the hard limit should be equal or greater than the soft limit.
The allocator can be made thread-safe (optional) by means of providing a RAII-lock type via the second template argument. The allocator uses the lock only to access the shared state, therefore critical sections are only a few cycles long, which implies that it should be acceptable to use hardware IRQ disabling instead of a mutex for performance reasons. For example, an IRQ-based RAII-lock type can be implemented as follows: struct RaiiSynchronizer { RaiiSynchronizer() { __disable_irq(); } ~RaiiSynchronizer() { __enable_irq(); } };
Definition at line 40 of file heap_based_pool_allocator.hpp.
|
inline |
The allocator initializes with empty reserve, so first allocations will be served from heap.
block_capacity_soft_limit | Block capacity that will be reported via getBlockCapacity(). |
block_capacity_hard_limit | Real block capacity limit; the number of allocated blocks will never exceed this value. Hard limit should be higher than soft limit. Default value is two times the soft limit. |
Definition at line 70 of file heap_based_pool_allocator.hpp.
|
inline |
The destructor de-allocates all blocks that are currently in the reserve. BLOCKS THAT ARE CURRENTLY HELD BY THE APPLICATION WILL LEAK.
Definition at line 85 of file heap_based_pool_allocator.hpp.
|
inlinevirtual |
Takes a block from the reserve, unless it's empty. In the latter case, allocates a new block in the heap.
Implements uavcan::IPoolAllocator.
Definition at line 100 of file heap_based_pool_allocator.hpp.
|
inlinevirtual |
Puts the block back to reserve. The block will not be free()d automatically; see shrink().
Implements uavcan::IPoolAllocator.
Definition at line 143 of file heap_based_pool_allocator.hpp.
|
inlinevirtual |
The soft limit.
Implements uavcan::IPoolAllocator.
Definition at line 161 of file heap_based_pool_allocator.hpp.
|
inline |
The hard limit.
Definition at line 166 of file heap_based_pool_allocator.hpp.
|
inline |
Number of blocks that are currently in use by the application.
Definition at line 200 of file heap_based_pool_allocator.hpp.
|
inline |
Number of blocks that are acquired from the heap.
Definition at line 210 of file heap_based_pool_allocator.hpp.
|
inline |
Frees all blocks that are not in use at the moment. Post-condition is getNumAllocatedBlocks() == getNumReservedBlocks().
Definition at line 172 of file heap_based_pool_allocator.hpp.
|
private |
Definition at line 53 of file heap_based_pool_allocator.hpp.
|
private |
Definition at line 52 of file heap_based_pool_allocator.hpp.
|
private |
Definition at line 56 of file heap_based_pool_allocator.hpp.
|
private |
Definition at line 55 of file heap_based_pool_allocator.hpp.
|
private |
Definition at line 58 of file heap_based_pool_allocator.hpp.