15 #ifndef RCLCPP__STRATEGIES__MESSAGE_POOL_MEMORY_STRATEGY_HPP_
16 #define RCLCPP__STRATEGIES__MESSAGE_POOL_MEMORY_STRATEGY_HPP_
20 #include "rosidl_runtime_cpp/traits.hpp"
22 #include "rclcpp/macros.hpp"
23 #include "rclcpp/message_memory_strategy.hpp"
24 #include "rclcpp/visibility_control.hpp"
30 namespace message_pool_memory_strategy
43 typename std::enable_if<
44 rosidl_generator_traits::has_fixed_size<MessageT>::value
55 : next_array_index_(0)
57 for (
size_t i = 0; i < Size; ++i) {
58 pool_[i].msg_ptr_ = std::make_shared<MessageT>();
59 pool_[i].used =
false;
71 size_t current_index = next_array_index_;
72 next_array_index_ = (next_array_index_ + 1) % Size;
73 if (pool_[current_index].used) {
74 throw std::runtime_error(
"Tried to access message that was still in use! Abort.");
76 pool_[current_index].msg_ptr_->~MessageT();
77 new (pool_[current_index].msg_ptr_.get())MessageT;
79 pool_[current_index].used =
true;
80 return pool_[current_index].msg_ptr_;
90 for (
size_t i = 0; i < Size; ++i) {
91 if (pool_[i].msg_ptr_ == msg) {
92 pool_[i].used =
false;
96 throw std::runtime_error(
"Unrecognized message ptr in return_message.");
102 std::shared_ptr<MessageT> msg_ptr_;
106 std::array<PoolMember, Size> pool_;
107 size_t next_array_index_;
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.