rtos2-utils/include/sta/rtos/mempool.tpp
2023-09-23 11:20:53 +02:00

44 lines
1.0 KiB
C++

#ifndef STA_RTOS_MEMPOOL_TPP
#define STA_RTOS_MEMPOOL_TPP
#ifndef STA_RTOS_MEMPOOL_HPP
# error "Internal header. Use <sta/rtos/mempool.hpp> instead."
#endif // !STA_RTOS_MEMPOOL_HPP
#include <sta/debug/assert.hpp>
namespace sta
{
template <typename T>
RtosMemPool<T>::RtosMemPool()
{
}
template <typename T>
RtosMemPool<T>::RtosMemPool(uint32_t block_count, uint32_t block_size)
{
osMemoryPoolAttr_t mp_attr = { .name = "MemoryPool" };
handle_ = osMemoryPoolNew(block_size, block_count, &mp_attr);
STA_ASSERT_MSG(handle_ != nullptr, "Failed to Create RtosMemPool");
}
template <typename T>
void * RtosMemPool<T>::alloc(uint32_t timeout /* = osWaitForever */)
{
void * addr = osMemoryPoolAlloc(handle_, timeout);
STA_ASSERT_MSG(addr != nullptr, "Failed to Alloc RtosMemPool Block");
return addr;
}
template <typename T>
void RtosMemPool<T>::free(void * block)
{
STA_ASSERT_MSG(osMemoryPoolFree(handle_, block) == osOK,
"Failed to Free RtosMemPool Block");
}
} // namespace sta
#endif // STA_RTOS_MEMPOOL_TPP