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/logging.hpp"
29 #include "rclcpp/macros.hpp"
30 #include "rclcpp/message_memory_strategy.hpp"
31 #include "rclcpp/visibility_control.hpp"
37 namespace message_pool_memory_strategy
50 typename std::enable_if<
51 rosidl_generator_traits::has_fixed_size<MessageT>::value
62 pool_mutex_ = std::make_shared<std::mutex>();
64 pool_ = std::shared_ptr<std::array<MessageT *, Size>>(
65 new std::array<MessageT *, Size>,
66 [](std::array<MessageT *, Size> * arr) {
67 for (
size_t i = 0; i < Size; ++i) {
73 free_list_ = std::make_shared<CircularArray<Size>>();
75 for (
size_t i = 0; i < Size; ++i) {
76 (*pool_)[i] =
static_cast<MessageT *
>(malloc(
sizeof(MessageT)));
77 free_list_->push_back(i);
89 std::lock_guard<std::mutex> lock(*pool_mutex_);
90 if (free_list_->size() == 0) {
91 throw std::runtime_error(
"No more free slots in the pool");
94 size_t current_index = free_list_->pop_front();
96 return std::shared_ptr<MessageT>(
97 new((*pool_)[current_index]) MessageT(),
98 [pool = this->pool_, pool_mutex = this->pool_mutex_,
99 free_list = this->free_list_](MessageT * p) {
100 std::lock_guard<std::mutex> lock(*pool_mutex);
101 for (
size_t i = 0; i < Size; ++i) {
102 if ((*pool)[i] == p) {
104 free_list->push_back(i);
127 void push_back(
const size_t v)
130 throw std::runtime_error(
"Tried to push too many items into the array");
132 array_[(front_ + size_) % N] = v;
139 throw std::runtime_error(
"Tried to pop item from empty array");
142 size_t val = array_[front_];
144 front_ = (front_ + 1) % N;
158 std::array<size_t, N> array_;
165 std::shared_ptr<std::mutex> pool_mutex_;
166 std::shared_ptr<std::array<MessageT *, Size>> pool_;
167 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(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.