diff --git a/include/sta/rtos/mempool.hpp b/include/sta/rtos/mempool.hpp new file mode 100644 index 0000000..e4656a0 --- /dev/null +++ b/include/sta/rtos/mempool.hpp @@ -0,0 +1,63 @@ +/** + * @file + * @brief RTOS MemPool implementation. + */ +#ifndef STA_RTOS_MEMPOOL_HPP +#define STA_RTOS_MEMPOOL_HPP + +#include + +#include + + +namespace sta +{ + /** + * @brief Interface object for using CMSIS RTOS2 MemPool. + * + * @tparam Message type + * + * @ingroup STA_RTOS_API + */ + template + 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 + + +#endif // STA_RTOS_MEMPOOL_HPP diff --git a/include/sta/rtos/mempool.tpp b/include/sta/rtos/mempool.tpp new file mode 100644 index 0000000..bd3ab29 --- /dev/null +++ b/include/sta/rtos/mempool.tpp @@ -0,0 +1,40 @@ +#ifndef STA_RTOS_MEMPOOL_TPP +#define STA_RTOS_MEMPOOL_TPP + +#ifndef STA_RTOS_MEMPOOL_HPP +# error "Internal header. Use instead." +#endif // !STA_RTOS_MEMPOOL_HPP + +#include + + +namespace sta +{ + template + RtosMemPool::RtosMemPool() + { + } + + template + RtosMemPool::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 + void * RtosMemPool::alloc(uint32_t timeout /* = osWaitForever */) + { + return osMemoryPoolAlloc(handle_, timeout); + } + + template + osStatus_t RtosMemPool::free(void * block) + { + return osMemoryPoolFree(handle_, block); + } +} // namespace sta + + +#endif // STA_RTOS_MEMPOOL_TPP