15 #ifndef RCLCPP__STRATEGIES__MESSAGE_POOL_MEMORY_STRATEGY_HPP_
16 #define RCLCPP__STRATEGIES__MESSAGE_POOL_MEMORY_STRATEGY_HPP_
23 #include <type_traits>
25 #include "rosidl_runtime_cpp/traits.hpp"
27 #include "rclcpp/logger.hpp"
28 #include "rclcpp/macros.hpp"
29 #include "rclcpp/message_memory_strategy.hpp"
30 #include "rclcpp/visibility_control.hpp"
36 namespace message_pool_memory_strategy
49 typename std::enable_if<
50 rosidl_generator_traits::has_fixed_size<MessageT>::value
61 pool_mutex_ = std::make_shared<std::mutex>();
63 pool_ = std::shared_ptr<std::array<MessageT *, Size>>(
64 new std::array<MessageT *, Size>,
65 [](std::array<MessageT *, Size> * arr) {
66 for (
size_t i = 0; i < Size; ++i) {
72 free_list_ = std::make_shared<CircularArray<Size>>();
74 for (
size_t i = 0; i < Size; ++i) {
75 (*pool_)[i] =
static_cast<MessageT *
>(malloc(
sizeof(MessageT)));
76 free_list_->push_back(i);
88 std::lock_guard<std::mutex> lock(*pool_mutex_);
89 if (free_list_->size() == 0) {
90 throw std::runtime_error(
"No more free slots in the pool");
93 size_t current_index = free_list_->pop_front();
95 return std::shared_ptr<MessageT>(
96 new((*pool_)[current_index]) MessageT(),
97 [pool = this->pool_, pool_mutex = this->pool_mutex_,
98 free_list = this->free_list_](MessageT * p) {
99 std::lock_guard<std::mutex> lock(*pool_mutex);
100 for (
size_t i = 0; i < Size; ++i) {
101 if ((*pool)[i] == p) {
103 free_list->push_back(i);
126 void push_back(
const size_t v)
129 throw std::runtime_error(
"Tried to push too many items into the array");
131 array_[(front_ + size_) % N] = v;
138 throw std::runtime_error(
"Tried to pop item from empty array");
141 size_t val = array_[front_];
143 front_ = (front_ + 1) % N;
157 std::array<size_t, N> array_;
164 std::shared_ptr<std::mutex> pool_mutex_;
165 std::shared_ptr<std::array<MessageT *, Size>> pool_;
166 std::shared_ptr<CircularArray<Size>> free_list_;
Default allocation strategy for messages received by subscriptions.
Completely static memory allocation strategy for messages.
std::shared_ptr< MessageT > borrow_message()
Borrow a message from the message pool.
void return_message([[maybe_unused]] std::shared_ptr< MessageT > &msg)
Return a message to the message pool.
Versions of rosidl_typesupport_cpp::get_message_type_support_handle that handle adapted types.