Added Mempool

This commit is contained in:
CarlWachter 2023-09-22 17:53:04 +02:00
parent 327005a4b6
commit f813e7b6ea
2 changed files with 103 additions and 0 deletions

View File

@ -0,0 +1,63 @@
/**
* @file
* @brief RTOS MemPool implementation.
*/
#ifndef STA_RTOS_MEMPOOL_HPP
#define STA_RTOS_MEMPOOL_HPP
#include <cmsis_os2.h>
#include <cstdint>
namespace sta
{
/**
* @brief Interface object for using CMSIS RTOS2 MemPool.
*
* @tparam Message type
*
* @ingroup STA_RTOS_API
*/
template <typename T>
class RtosMemPool
{
public:
using Message = T; /**< Queue message type */
public:
// Default Constructor
RtosMemPool();
/**
* @brief Create a memory pool.
*
* @param block_count names the amount of blocks.
* @param block_size names the size of each block.
*/
RtosMemPool(uint32_t block_count, uint32_t block_size);
/**
* @brief Allocate a new Memory Block.
*
* @param timeout names the timeout in milliseconds.
*/
void * alloc(uint32_t timeout);
/**
* @brief Free a Memory Block.
*
* @param block names the block to free.
*/
osStatus_t free(void *block);
private:
osMemoryPoolId_t handle_; /**< CMSIS RTOS2 queue handle */
};
} // namespace sta
#include <sta/rtos/mempool.tpp>
#endif // STA_RTOS_MEMPOOL_HPP

View File

@ -0,0 +1,40 @@
#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 */)
{
return osMemoryPoolAlloc(handle_, timeout);
}
template <typename T>
osStatus_t RtosMemPool<T>::free(void * block)
{
return osMemoryPoolFree(handle_, block);
}
} // namespace sta
#endif // STA_RTOS_MEMPOOL_TPP