Classes | Public Member Functions | Private Attributes | List of all members
uavcan::HeapBasedPoolAllocator< BlockSize, RaiiSynchronizer > Class Template Reference

#include <heap_based_pool_allocator.hpp>

Inheritance diagram for uavcan::HeapBasedPoolAllocator< BlockSize, RaiiSynchronizer >:
Inheritance graph
[legend]

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 ()
 
- Public Member Functions inherited from uavcan::IPoolAllocator
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_
 
Nodereserve_
 

Additional Inherited Members

- Private Member Functions inherited from uavcan::Noncopyable
 Noncopyable ()
 
 ~Noncopyable ()
 

Detailed Description

template<std::size_t BlockSize, typename RaiiSynchronizer = char>
class uavcan::HeapBasedPoolAllocator< BlockSize, RaiiSynchronizer >

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.

Constructor & Destructor Documentation

◆ HeapBasedPoolAllocator()

template<std::size_t BlockSize, typename RaiiSynchronizer = char>
uavcan::HeapBasedPoolAllocator< BlockSize, RaiiSynchronizer >::HeapBasedPoolAllocator ( uint16_t  block_capacity_soft_limit,
uint16_t  block_capacity_hard_limit = 0 
)
inline

The allocator initializes with empty reserve, so first allocations will be served from heap.

Parameters
block_capacity_soft_limitBlock capacity that will be reported via getBlockCapacity().
block_capacity_hard_limitReal 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.

◆ ~HeapBasedPoolAllocator()

template<std::size_t BlockSize, typename RaiiSynchronizer = char>
uavcan::HeapBasedPoolAllocator< BlockSize, RaiiSynchronizer >::~HeapBasedPoolAllocator ( )
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.

Member Function Documentation

◆ allocate()

template<std::size_t BlockSize, typename RaiiSynchronizer = char>
virtual void* uavcan::HeapBasedPoolAllocator< BlockSize, RaiiSynchronizer >::allocate ( std::size_t  size)
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.

◆ deallocate()

template<std::size_t BlockSize, typename RaiiSynchronizer = char>
virtual void uavcan::HeapBasedPoolAllocator< BlockSize, RaiiSynchronizer >::deallocate ( const void *  ptr)
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.

◆ getBlockCapacity()

template<std::size_t BlockSize, typename RaiiSynchronizer = char>
virtual uint16_t uavcan::HeapBasedPoolAllocator< BlockSize, RaiiSynchronizer >::getBlockCapacity ( ) const
inlinevirtual

The soft limit.

Implements uavcan::IPoolAllocator.

Definition at line 161 of file heap_based_pool_allocator.hpp.

◆ getBlockCapacityHardLimit()

template<std::size_t BlockSize, typename RaiiSynchronizer = char>
uint16_t uavcan::HeapBasedPoolAllocator< BlockSize, RaiiSynchronizer >::getBlockCapacityHardLimit ( ) const
inline

The hard limit.

Definition at line 166 of file heap_based_pool_allocator.hpp.

◆ getNumAllocatedBlocks()

template<std::size_t BlockSize, typename RaiiSynchronizer = char>
uint16_t uavcan::HeapBasedPoolAllocator< BlockSize, RaiiSynchronizer >::getNumAllocatedBlocks ( ) const
inline

Number of blocks that are currently in use by the application.

Definition at line 200 of file heap_based_pool_allocator.hpp.

◆ getNumReservedBlocks()

template<std::size_t BlockSize, typename RaiiSynchronizer = char>
uint16_t uavcan::HeapBasedPoolAllocator< BlockSize, RaiiSynchronizer >::getNumReservedBlocks ( ) const
inline

Number of blocks that are acquired from the heap.

Definition at line 210 of file heap_based_pool_allocator.hpp.

◆ shrink()

template<std::size_t BlockSize, typename RaiiSynchronizer = char>
void uavcan::HeapBasedPoolAllocator< BlockSize, RaiiSynchronizer >::shrink ( )
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.

Member Data Documentation

◆ capacity_hard_limit_

template<std::size_t BlockSize, typename RaiiSynchronizer = char>
const uint16_t uavcan::HeapBasedPoolAllocator< BlockSize, RaiiSynchronizer >::capacity_hard_limit_
private

Definition at line 53 of file heap_based_pool_allocator.hpp.

◆ capacity_soft_limit_

template<std::size_t BlockSize, typename RaiiSynchronizer = char>
const uint16_t uavcan::HeapBasedPoolAllocator< BlockSize, RaiiSynchronizer >::capacity_soft_limit_
private

Definition at line 52 of file heap_based_pool_allocator.hpp.

◆ num_allocated_blocks_

template<std::size_t BlockSize, typename RaiiSynchronizer = char>
uint16_t uavcan::HeapBasedPoolAllocator< BlockSize, RaiiSynchronizer >::num_allocated_blocks_
private

Definition at line 56 of file heap_based_pool_allocator.hpp.

◆ num_reserved_blocks_

template<std::size_t BlockSize, typename RaiiSynchronizer = char>
uint16_t uavcan::HeapBasedPoolAllocator< BlockSize, RaiiSynchronizer >::num_reserved_blocks_
private

Definition at line 55 of file heap_based_pool_allocator.hpp.

◆ reserve_

template<std::size_t BlockSize, typename RaiiSynchronizer = char>
Node* uavcan::HeapBasedPoolAllocator< BlockSize, RaiiSynchronizer >::reserve_
private

Definition at line 58 of file heap_based_pool_allocator.hpp.


The documentation for this class was generated from the following file:


uavcan_communicator
Author(s):
autogenerated on Fri Dec 13 2024 03:10:04