rclcpp  master
C++ ROS Client Library API
message_pool_memory_strategy.hpp
Go to the documentation of this file.
1 // Copyright 2015 Open Source Robotics Foundation, Inc.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #ifndef RCLCPP__STRATEGIES__MESSAGE_POOL_MEMORY_STRATEGY_HPP_
16 #define RCLCPP__STRATEGIES__MESSAGE_POOL_MEMORY_STRATEGY_HPP_
17 
18 #include <memory>
19 
20 #include "rclcpp/macros.hpp"
23 
24 namespace rclcpp
25 {
26 namespace strategies
27 {
28 namespace message_pool_memory_strategy
29 {
30 
32 
38 template<
39  typename MessageT,
40  size_t Size,
41  typename std::enable_if<
42  rosidl_generator_traits::has_fixed_size<MessageT>::value
43  >::type * = nullptr
44 >
47 {
48 public:
50 
51 
54  {
55  for (size_t i = 0; i < Size; ++i) {
56  pool_[i].msg_ptr_ = std::make_shared<MessageT>();
57  pool_[i].used = false;
58  }
59  }
60 
62 
68  {
69  size_t current_index = next_array_index_;
70  next_array_index_ = (next_array_index_ + 1) % Size;
71  if (pool_[current_index].used) {
72  throw std::runtime_error("Tried to access message that was still in use! Abort.");
73  }
74  pool_[current_index].msg_ptr_->~MessageT();
75  new (pool_[current_index].msg_ptr_.get())MessageT;
76 
77  pool_[current_index].used = true;
78  return pool_[current_index].msg_ptr_;
79  }
80 
82 
87  {
88  for (size_t i = 0; i < Size; ++i) {
89  if (pool_[i].msg_ptr_ == msg) {
90  pool_[i].used = false;
91  return;
92  }
93  }
94  throw std::runtime_error("Unrecognized message ptr in return_message.");
95  }
96 
97 protected:
98  struct PoolMember
99  {
101  bool used;
102  };
103 
106 };
107 
108 } // namespace message_pool_memory_strategy
109 } // namespace strategies
110 } // namespace rclcpp
111 
112 #endif // RCLCPP__STRATEGIES__MESSAGE_POOL_MEMORY_STRATEGY_HPP_
Default allocation strategy for messages received by subscriptions.
Definition: message_memory_strategy.hpp:40
std::shared_ptr< MessageT > msg_ptr_
Definition: message_pool_memory_strategy.hpp:100
Definition: allocator_common.hpp:24
std::array< PoolMember, Size > pool_
Definition: message_pool_memory_strategy.hpp:104
void return_message(std::shared_ptr< MessageT > &msg)
Return a message to the message pool.
Definition: message_pool_memory_strategy.hpp:86
#define RCLCPP_SMART_PTR_DEFINITIONS(...)
Definition: macros.hpp:36
Completely static memory allocation strategy for messages.
Definition: message_pool_memory_strategy.hpp:45
size_t next_array_index_
Definition: message_pool_memory_strategy.hpp:105
std::shared_ptr< MessageT > borrow_message()
Borrow a message from the message pool.
Definition: message_pool_memory_strategy.hpp:67