10#ifndef STK_MEMORY_BLOCKPOOL_H_
11#define STK_MEMORY_BLOCKPOOL_H_
21#ifndef STK_MEMORY_BLOCKPOOL_ALIGN_DEFAULT
22 #define STK_MEMORY_BLOCKPOOL_ALIGN_DEFAULT stk::memory::MemoryAllocator::MIN_ALIGNMENT
32#ifndef STK_MEMORY_BLOCKPOOL_BITMAP
33 #define STK_MEMORY_BLOCKPOOL_BITMAP (0)
163 explicit BlockMemoryPool(
size_t capacity,
size_t raw_block_size, uint8_t *storage,
164 size_t storage_size,
const char *name =
nullptr,
181#if STK_MEMORY_HAS_HEAP
182 explicit BlockMemoryPool(
size_t capacity,
size_t raw_block_size,
const char *name =
nullptr,
205 return Max(alignment, ((raw_size + (alignment - 1U)) & ~(alignment - 1U)));
308#if STK_MEMORY_PLACEMENT_NEW
316 template <
typename T,
typename... TArgs> T *
TimedNewT(
Timeout timeout_ticks, TArgs &&...args);
321 template <
typename T,
typename... TArgs> T *
NewT(TArgs &&...args);
325 template <
typename T,
typename... TArgs> T *
TryNewT(TArgs &&...args);
462 return ((alignment >=
sizeof(
MemoryBlock)) && ((alignment & (alignment - 1U)) == 0U));
467 static bool ArgsValid(
size_t capacity,
size_t raw_block_size,
size_t alignment,
size_t block_size)
469 return ((capacity > 0U) && (capacity <=
CAPACITY_MAX) && (raw_block_size > 0U) &&
476 void Setup(
size_t raw_block_size,
size_t alignment);
498#if STK_MEMORY_BLOCKPOOL_BITMAP
499 bool BitmapTest(
size_t index)
const {
return ((
m_bitmap[index >> 3U] &
static_cast<uint8_t
>(1U << (index & 7U))) != 0U); }
500 void BitmapSet(
size_t index) {
m_bitmap[index >> 3U] =
static_cast<uint8_t
>(
m_bitmap[index >> 3U] |
static_cast<uint8_t
>(1U << (index & 7U))); }
501 void BitmapClear(
size_t index) {
m_bitmap[index >> 3U] =
static_cast<uint8_t
>(
m_bitmap[index >> 3U] &
static_cast<uint8_t
>(~(1U << (index & 7U)))); }
523 size_t storage_size,
const char *name,
size_t alignment)
539#if STK_SYNC_DEBUG_NAMES
545 Setup(raw_block_size, alignment);
548#if STK_MEMORY_HAS_HEAP
564#if STK_SYNC_DEBUG_NAMES
574 const size_t max_size =
static_cast<size_t>(~static_cast<size_t>(0U));
580 const size_t total = ((capacity *
m_block_size) + bitmap);
591 Setup(raw_block_size, alignment);
600#if STK_MEMORY_HAS_HEAP
621 if (block ==
nullptr)
649 const Ticks deadline = (timed_wait ? (
GetTicks() +
static_cast<Ticks>(timeout_ticks)) : 0);
655 Timeout remaining = timeout_ticks;
672 remaining =
static_cast<Timeout>(left);
677 result =
m_cv.WaitEx(cs_, remaining);
712 void *block =
nullptr;
733 result =
static_cast<T *
>(
TimedAlloc(timeout_ticks));
765#if STK_MEMORY_PLACEMENT_NEW
767template <
typename T,
typename... TArgs>
774 auto const obj =
new (
static_cast<void *
>(ptr)) T(
static_cast<TArgs &&
>(args)...);
781template <
typename T,
typename... TArgs>
787template <
typename T,
typename... TArgs>
800 const bool freed =
Free(ptr);
805template <
typename T,
typename... TArgs>
811template <
typename T,
typename... TArgs>
825 bool success =
false;
827 if ((ptr !=
nullptr) && (
m_blocks !=
nullptr))
834 if ((pt < lo) || (pt >= hi))
839 else if ((
static_cast<size_t>(pt - lo) %
m_block_size) != 0U)
855 #if STK_MEMORY_BLOCKPOOL_BITMAP
857 const size_t index = (
static_cast<size_t>(pt - lo) /
m_block_size);
859 if (!BitmapTest(index))
868 #elif STK_MEMORY_POOL_CHECKS
873 if (node ==
reinterpret_cast<const MemoryBlock *
>(ptr))
884 #if STK_MEMORY_POOL_CHECKS
890 auto *
const blk =
reinterpret_cast<MemoryBlock *
>(ptr);
942 const size_t shift = (((addr + (alignment - 1U)) & ~(alignment - 1U)) - addr);
982#if STK_MEMORY_BLOCKPOOL_BITMAP
1011#if STK_MEMORY_BLOCKPOOL_BITMAP
1015#if STK_MEMORY_POOL_CHECKS
1031template <
size_t TBlockSize,
size_t TCapacity,
size_t TAlign>
1051template <
size_t TBlockSize,
size_t TCapacity,
size_t TAlign = BlockMemoryPool::ALIGN_DEFAULT>
1058 "alignment must be a power of two not less than sizeof(void *)");
1066#if STK_MEMORY_PLACEMENT_NEW
1069template <
typename T,
size_t N>
Memory allocator front-end with checked API and its weak backend hooks: stk::memory::MemoryAllocator.
#define STK_MEMORY_BLOCKPOOL_ALIGN_DEFAULT
Default block alignment (bytes, power of two, not less than sizeof(void *)) used when a pool is const...
#define STK_MEMORY_BLOCKPOOL_BITMAP
When defined as 1, BlockMemoryPool keeps one "allocated" bit per block (capacity/8 bytes,...
#define STK_UNUSED(X)
Explicitly marks a variable as unused to suppress compiler warnings.
#define __stk_forceinline
Forces compiler to always inline the decorated function, regardless of optimisation level.
#define STK_ASSERT(e)
Runtime assertion. Halts execution if the expression e evaluates to false.
#define STK_VIRT_DTOR
Expands to nothing if STK_STRICT_COMPLIANCY=0 (non-virtual destructors, smaller objects),...
Implementation of synchronization primitive: stk::sync::ConditionVariable.
Namespace of STK package.
uintptr_t Word
Native processor word type.
static constexpr Timeout NO_WAIT
Timeout value: return immediately if the synchronization object is not yet signaled (non-blocking pol...
EWaitResult
Wait result (see IKernelService::Wait).
@ WAIT_RESULT_FAIL
IKernelService::Wait returned with error without waiting.
@ WAIT_RESULT_TIMEOUT
The wake was caused by a timeout expiry.
@ WAIT_RESULT_SIGNAL
The wake was caused by a signal.
int64_t Ticks
Ticks value.
int32_t Timeout
Timeout time (ticks).
constexpr T Max(T a, T b) noexcept
Compile-time maximum of two values.
void STK_MEMSET(void *const dest, uint8_t value, size_t size)
Implementation of STK_MEMSET.
static Ticks GetTicks()
Get number of ticks elapsed since kernel start.
static constexpr Timeout WAIT_INFINITE
Timeout value: block indefinitely until the synchronization object is signaled.
static T * WordToPtr(Word value) noexcept
Cast a CPU register-width integer back to a pointer.
static Word PtrToWord(T *const ptr) noexcept
Cast a pointer to a CPU register-width integer.
bool IsInsideISR()
Check whether the CPU is currently executing inside a hardware interrupt service routine (ISR).
StaticBlockPool< sizeof(T), N,((alignof(T) > BlockMemoryPool::ALIGN_DEFAULT) ? alignof(T) :BlockMemoryPool::ALIGN_DEFAULT)> StaticBlockPoolFor
Pool of N blocks sized and aligned for objects of type T.
static void * AllocateAligned(size_t size, size_t alignment, size_t offset=0U)
Allocate memory with an alignment stricter than MIN_ALIGNMENT and an optional offset.
static void FreeAligned(void *ptr)
Free memory obtained with AllocateAligned().
Snapshot of the memory allocator's statistics.
Minimal move-only owning pointer for an object that lives in a memory pool.
uint8_t * m_bitmap
"allocated" bitmap (STK_MEMORY_BLOCKPOOL_BITMAP), otherwise unused
void DeleteT(T *ptr)
Destroy an object created with the NewT() family and return its block to the pool.
size_t GetUsedCount() const
Get the number of currently allocated (outstanding) blocks.
EWaitResult TimedAllocEx(void **block, Timeout timeout_ticks=WAIT_INFINITE)
Allocate one block, preserving the full wait outcome.
static const size_t ALIGN_DEFAULT
Default block alignment in bytes (see STK_MEMORY_BLOCKPOOL_ALIGN_DEFAULT).
static constexpr size_t BitmapBytes(size_t capacity)
Number of bytes of the "allocated" bitmap for capacity blocks.
static const uint8_t FILL_ALLOC
payload fill on allocation (STK_MEMORY_POOL_CHECKS)
size_t GetFreeCount() const
Get the number of free (available) blocks.
uint8_t * m_storage
raw storage as supplied or allocated (before alignment)
void * PopFreeList()
Pop the head block from the free-list, increment m_used_count, and return it.
size_t m_storage_size
size of the raw storage in bytes
T * TryNewT(TArgs &&...args)
Non-blocking TimedNewT(). ISR-safe if the constructor of T is.
static const size_t CAPACITY_MAX
Max capacity supported (number of blocks).
T * NewT(TArgs &&...args)
Allocate one block and construct a T in it, blocking indefinitely.
size_t m_capacity
total number of blocks (0 if invalid)
static bool ArgsValid(size_t capacity, size_t raw_block_size, size_t alignment, size_t block_size)
Check constructor arguments (block size overflow included).
size_t GetCapacity() const
Get the total block capacity of the pool.
STK_NONCOPYABLE_CLASS(BlockMemoryPool)
static __stk_forceinline void Fill(void *ptr, size_t size, uint8_t value)
Fill size bytes at ptr with value (used by diagnostics only).
T * TryAllocT()
Non-blocking typed allocation attempt.
size_t GetBlockAlignment() const
Get the alignment (bytes) of every block.
bool IsFull() const
Check whether all blocks are currently allocated (pool exhausted).
size_t GetAllocFailCount() const
Get the number of allocation requests that failed because the pool was exhausted (timeouts and NO_WAI...
size_t m_block_align
alignment of every block in bytes
bool IsStorageValid() const
Verify that the backing storage is valid and the pool is ready for use.
static const uint8_t FILL_FREE
payload fill on free (STK_MEMORY_POOL_CHECKS)
uint8_t * m_blocks
aligned start of block 0, nullptr if the pool is invalid
MemoryAllocator::Stats m_stats
live statistics in bytes (blocks * block size): usage, watermark, counters
MemoryAllocator::Stats GetStats() const
Get a consistent statistics snapshot in the same format as MemoryAllocator::GetStats().
bool Free(void *ptr)
Return a previously allocated block to the pool.
static bool IsValidAlignment(size_t alignment)
Check an alignment value: a power of two, large enough for the free-list link.
size_t GetPeakUsedCount() const
Get the high watermark: the maximum number of blocks that were allocated at the same time.
STK_VIRT_DTOR ~BlockMemoryPool()
Destructor.
void Setup(size_t raw_block_size, size_t alignment)
Align storage, trim the capacity to what fits and build the free-list.
void BuildFreeList()
Initialise the intrusive free-list across m_blocks.
void * TryAlloc()
Non-blocking allocation attempt.
MemoryBlock * m_free_list
head of the intrusive free-list (nullptr when pool is empty)
void * TimedAlloc(Timeout timeout_ticks=WAIT_INFINITE)
Allocate one block, blocking until one becomes available or the timeout expires.
sync::ConditionVariable m_cv
signalled by Free() to wake one task blocked in TimedAlloc()
BlockMemoryPool(size_t capacity, size_t raw_block_size, uint8_t *storage, size_t storage_size, const char *name=nullptr, size_t alignment=ALIGN_DEFAULT)
Construct a pool backed by caller-supplied (external) storage.
PoolPtr< T, BlockMemoryPool > MakeT(TArgs &&...args)
Blocking allocate-and-construct returning an owning PoolPtr.
size_t GetBlockSize() const
Get the aligned block size used internally by the allocator.
void * Alloc()
Allocate one block, blocking indefinitely until one is available.
PoolPtr< T, BlockMemoryPool > TryMakeT(TArgs &&...args)
Non-blocking allocate-and-construct returning an owning PoolPtr.
T * TimedAllocT(Timeout timeout_ticks=WAIT_INFINITE)
Allocate one typed block, blocking until one becomes available or the timeout expires.
bool IsEmpty() const
Check whether all blocks are free (no outstanding allocations).
static constexpr size_t RequiredStorageSize(size_t capacity, size_t raw_block_size, size_t alignment=ALIGN_DEFAULT)
Compute the size of the storage buffer required for an external-storage pool.
size_t m_used_count
number of blocks currently allocated (outstanding)
static constexpr size_t AlignBlockSize(size_t raw_size, size_t alignment=ALIGN_DEFAULT)
Round a raw block size up to the nearest multiple of alignment.
T * AllocT()
Allocate one typed block, blocking indefinitely until one is available.
size_t m_block_size
aligned block size in bytes (>= m_block_align)
bool m_storage_owned
true -> storage is heap-allocated; free in destructor
T * TimedNewT(Timeout timeout_ticks, TArgs &&...args)
Allocate one block and construct a T in it (placement new), waiting up to timeout_ticks.
Intrusive free-list node overlaid on the first word of every free block.
MemoryBlock * next
next free block in the list, or nullptr if this is the last one
Storage holder. Kept as a separate (first) base class so that the buffer exists before BlockMemoryPoo...
uint8_t m_buf[BlockMemoryPool::RequiredStorageSize(TCapacity, TBlockSize, TAlign)]
Fixed-size block pool that owns its storage: no heap, no separately declared (and possibly mis-sized ...
StaticBlockPool(const char *name=nullptr)
STK_STATIC_ASSERT_DESC((TCapacity > 0U) &&(TCapacity<=BlockMemoryPool::CAPACITY_MAX), "invalid capacity")
STK_STATIC_ASSERT_DESC(TBlockSize > 0U, "block size must be greater than zero")
STK_STATIC_ASSERT_DESC((TAlign >=sizeof(void *)) &&((TAlign &(TAlign - 1U))==0U), "alignment must be a power of two not less than sizeof(void *)")
RAII instance that enters the critical section on construction and exits it on destruction.
void SetTraceName(const char *name)
Set name.
RAII-style low-level synchronization primitive for atomic code execution. Used as building brick for ...
Condition Variable primitive for signaling between tasks based on specific predicates.