ROS 2 rclcpp + rcl - humble  humble
ROS 2 C++ Client Library with ROS Client Library
message_memory_strategy.hpp
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__MESSAGE_MEMORY_STRATEGY_HPP_
16 #define RCLCPP__MESSAGE_MEMORY_STRATEGY_HPP_
17 
18 #include <memory>
19 #include <stdexcept>
20 
21 #include "rcl/types.h"
22 
23 #include "rclcpp/allocator/allocator_common.hpp"
24 #include "rclcpp/exceptions.hpp"
25 #include "rclcpp/macros.hpp"
26 #include "rclcpp/serialized_message.hpp"
27 #include "rclcpp/visibility_control.hpp"
28 
29 #include "rcutils/logging_macros.h"
30 
31 #include "rmw/serialized_message.h"
32 
33 namespace rclcpp
34 {
35 namespace message_memory_strategy
36 {
37 
39 
40 template<typename MessageT, typename Alloc = std::allocator<void>>
42 {
43 public:
44  RCLCPP_SMART_PTR_DEFINITIONS(MessageMemoryStrategy)
45 
46  using MessageAllocTraits = allocator::AllocRebind<MessageT, Alloc>;
47  using MessageAlloc = typename MessageAllocTraits::allocator_type;
48  using MessageDeleter = allocator::Deleter<MessageAlloc, MessageT>;
49 
50  using SerializedMessageAllocTraits = allocator::AllocRebind<rclcpp::SerializedMessage, Alloc>;
51  using SerializedMessageAlloc = typename SerializedMessageAllocTraits::allocator_type;
52  using SerializedMessageDeleter =
53  allocator::Deleter<SerializedMessageAlloc, rclcpp::SerializedMessage>;
54 
55  using BufferAllocTraits = allocator::AllocRebind<char, Alloc>;
56  using BufferAlloc = typename BufferAllocTraits::allocator_type;
57  using BufferDeleter = allocator::Deleter<BufferAlloc, char>;
58 
60  {
61  message_allocator_ = std::make_shared<MessageAlloc>();
62  serialized_message_allocator_ = std::make_shared<SerializedMessageAlloc>();
63  buffer_allocator_ = std::make_shared<BufferAlloc>();
64  rcutils_allocator_ = allocator::get_rcl_allocator<char, BufferAlloc>(*buffer_allocator_.get());
65  }
66 
67  explicit MessageMemoryStrategy(std::shared_ptr<Alloc> allocator)
68  {
69  message_allocator_ = std::make_shared<MessageAlloc>(*allocator.get());
70  serialized_message_allocator_ = std::make_shared<SerializedMessageAlloc>(*allocator.get());
71  buffer_allocator_ = std::make_shared<BufferAlloc>(*allocator.get());
72  rcutils_allocator_ = allocator::get_rcl_allocator<char, BufferAlloc>(*buffer_allocator_.get());
73  }
74 
75  virtual ~MessageMemoryStrategy() = default;
76 
79  {
80  return std::make_shared<MessageMemoryStrategy<MessageT, Alloc>>(std::make_shared<Alloc>());
81  }
82 
84 
85  virtual std::shared_ptr<MessageT> borrow_message()
86  {
87  return std::allocate_shared<MessageT, MessageAlloc>(*message_allocator_.get());
88  }
89 
90  virtual std::shared_ptr<rclcpp::SerializedMessage> borrow_serialized_message(size_t capacity)
91  {
92  return std::make_shared<rclcpp::SerializedMessage>(capacity);
93  }
94 
95  virtual std::shared_ptr<rclcpp::SerializedMessage> borrow_serialized_message()
96  {
97  return borrow_serialized_message(default_buffer_capacity_);
98  }
99 
100  virtual void set_default_buffer_capacity(size_t capacity)
101  {
102  default_buffer_capacity_ = capacity;
103  }
104 
106 
107  virtual void return_message(std::shared_ptr<MessageT> & msg)
108  {
109  msg.reset();
110  }
111 
112  virtual void return_serialized_message(
113  std::shared_ptr<rclcpp::SerializedMessage> & serialized_msg)
114  {
115  serialized_msg.reset();
116  }
117 
118  std::shared_ptr<MessageAlloc> message_allocator_;
119  MessageDeleter message_deleter_;
120 
121  std::shared_ptr<SerializedMessageAlloc> serialized_message_allocator_;
122  SerializedMessageDeleter serialized_message_deleter_;
123 
124  std::shared_ptr<BufferAlloc> buffer_allocator_;
125  BufferDeleter buffer_deleter_;
126  size_t default_buffer_capacity_ = 0;
127 
128  rcutils_allocator_t rcutils_allocator_;
129 };
130 
131 } // namespace message_memory_strategy
132 } // namespace rclcpp
133 
134 #endif // RCLCPP__MESSAGE_MEMORY_STRATEGY_HPP_
Default allocation strategy for messages received by subscriptions.
static SharedPtr create_default()
Default factory method.
virtual void return_message(std::shared_ptr< MessageT > &msg)
Release ownership of the message, which will deallocate it if it has no more owners.
virtual std::shared_ptr< MessageT > borrow_message()
By default, dynamically allocate a new message.
Versions of rosidl_typesupport_cpp::get_message_type_support_handle that handle adapted types.
@ SharedPtr
Set the data type used in the intra-process buffer as std::shared_ptr<MessageT>