5 #ifndef UAVCAN_HELPERS_HEAP_BASED_POOL_ALLOCATOR_HPP_INCLUDED 6 #define UAVCAN_HELPERS_HEAP_BASED_POOL_ALLOCATOR_HPP_INCLUDED 71 uint16_t block_capacity_hard_limit = 0) :
72 capacity_soft_limit_(block_capacity_soft_limit),
73 capacity_hard_limit_((block_capacity_hard_limit > 0) ? block_capacity_hard_limit :
76 num_reserved_blocks_(0),
77 num_allocated_blocks_(0),
89 if (num_allocated_blocks_ > 0)
91 UAVCAN_TRACE(
"HeapBasedPoolAllocator",
"%u BLOCKS LEAKED", num_allocated_blocks_);
102 if (size > BlockSize)
108 RaiiSynchronizer lock;
111 Node*
const p = reserve_;
115 reserve_ = reserve_->next;
116 num_allocated_blocks_++;
120 if (num_reserved_blocks_ >= capacity_hard_limit_)
127 void*
const m = std::malloc(
sizeof(
Node));
130 RaiiSynchronizer lock;
133 num_reserved_blocks_++;
134 num_allocated_blocks_++;
147 RaiiSynchronizer lock;
150 Node*
const node =
static_cast<Node*
>(
const_cast<void*
>(ptr));
151 node->next = reserve_;
154 num_allocated_blocks_--;
178 RaiiSynchronizer lock;
184 reserve_ = reserve_->next;
185 num_reserved_blocks_--;
202 RaiiSynchronizer lock;
204 return num_allocated_blocks_;
212 RaiiSynchronizer lock;
214 return num_reserved_blocks_;
struct UAVCAN_EXPORT NumericTraits
const uint16_t capacity_soft_limit_
uint16_t num_allocated_blocks_
const uint16_t capacity_hard_limit_
uint16_t getBlockCapacityHardLimit() const
#define UAVCAN_TRACE(...)
virtual void deallocate(const void *ptr)
uint16_t num_reserved_blocks_
virtual void * allocate(std::size_t size)
UAVCAN_EXPORT const T & max(const T &a, const T &b)
virtual uint16_t getBlockCapacity() const
HeapBasedPoolAllocator(uint16_t block_capacity_soft_limit, uint16_t block_capacity_hard_limit=0)
UAVCAN_EXPORT const T & min(const T &a, const T &b)
uint16_t getNumReservedBlocks() const
uint16_t getNumAllocatedBlocks() const
~HeapBasedPoolAllocator()