rclcpp  master
C++ ROS Client Library API
message_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__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 
24 #include "rclcpp/exceptions.hpp"
25 #include "rclcpp/macros.hpp"
27 
28 #include "rmw/serialized_message.h"
29 
30 namespace rclcpp
31 {
32 namespace message_memory_strategy
33 {
34 
36 
37 template<typename MessageT, typename Alloc = std::allocator<void>>
39 {
40 public:
42 
43  using MessageAllocTraits = allocator::AllocRebind<MessageT, Alloc>;
44  using MessageAlloc = typename MessageAllocTraits::allocator_type;
45  using MessageDeleter = allocator::Deleter<MessageAlloc, MessageT>;
46 
48  using SerializedMessageAlloc = typename SerializedMessageAllocTraits::allocator_type;
51 
52  using BufferAllocTraits = allocator::AllocRebind<char, Alloc>;
53  using BufferAlloc = typename BufferAllocTraits::allocator_type;
54  using BufferDeleter = allocator::Deleter<BufferAlloc, char>;
55 
57  {
58  message_allocator_ = std::make_shared<MessageAlloc>();
59  serialized_message_allocator_ = std::make_shared<SerializedMessageAlloc>();
60  buffer_allocator_ = std::make_shared<BufferAlloc>();
61  rcutils_allocator_ = allocator::get_rcl_allocator<char, BufferAlloc>(*buffer_allocator_.get());
62  }
63 
65  {
66  message_allocator_ = std::make_shared<MessageAlloc>(*allocator.get());
67  serialized_message_allocator_ = std::make_shared<SerializedMessageAlloc>(*allocator.get());
68  buffer_allocator_ = std::make_shared<BufferAlloc>(*allocator.get());
69  rcutils_allocator_ = allocator::get_rcl_allocator<char, BufferAlloc>(*buffer_allocator_.get());
70  }
71 
73  static SharedPtr create_default()
74  {
75  return std::make_shared<MessageMemoryStrategy<MessageT, Alloc>>(std::make_shared<Alloc>());
76  }
77 
79 
81  {
82  return std::allocate_shared<MessageT, MessageAlloc>(*message_allocator_.get());
83  }
84 
86  {
87  auto msg = new rcl_serialized_message_t;
89  auto ret = rmw_serialized_message_init(msg, capacity, &rcutils_allocator_);
90  if (ret != RCL_RET_OK) {
92  }
93 
94  auto serialized_msg = std::shared_ptr<rcl_serialized_message_t>(msg,
95  [](rmw_serialized_message_t * msg) {
96  auto ret = rmw_serialized_message_fini(msg);
97  delete msg;
98  if (ret != RCL_RET_OK) {
99  rclcpp::exceptions::throw_from_rcl_error(ret, "leaking memory");
100  }
101  });
102 
103  return serialized_msg;
104  }
105 
107  {
109  }
110 
111  virtual void set_default_buffer_capacity(size_t capacity)
112  {
113  default_buffer_capacity_ = capacity;
114  }
115 
117 
119  {
120  msg.reset();
121  }
122 
124  {
125  serialized_msg.reset();
126  }
127 
130 
133 
137 
139 };
140 
141 } // namespace message_memory_strategy
142 } // namespace rclcpp
143 
144 #endif // RCLCPP__MESSAGE_MEMORY_STRATEGY_HPP_
static SharedPtr create_default()
Default factory method.
Definition: message_memory_strategy.hpp:73
virtual void return_serialized_message(std::shared_ptr< rcl_serialized_message_t > &serialized_msg)
Definition: message_memory_strategy.hpp:123
size_t default_buffer_capacity_
Definition: message_memory_strategy.hpp:136
rmw_ret_t rmw_serialized_message_fini(rmw_serialized_message_t *msg)
allocator::AllocRebind< char, std::allocator< void > > BufferAllocTraits
Definition: message_memory_strategy.hpp:52
Definition: allocator_common.hpp:24
virtual void return_message(std::shared_ptr< MessageT > &msg)
Release ownership of the message, which will deallocate it if it has no more owners.
Definition: message_memory_strategy.hpp:118
std::shared_ptr< MessageAlloc > message_allocator_
Definition: message_memory_strategy.hpp:128
#define RCL_RET_OK
void throw_from_rcl_error(rcl_ret_t ret, const std::string &prefix="", const rcl_error_state_t *error_state=nullptr, void(*reset_error)()=rcl_reset_error)
Throw a C++ std::exception which was created based on an rcl error.
std::shared_ptr< BufferAlloc > buffer_allocator_
Definition: message_memory_strategy.hpp:134
std::shared_ptr< SerializedMessageAlloc > serialized_message_allocator_
Definition: message_memory_strategy.hpp:131
typename std::allocator_traits< Alloc >::template rebind_traits< T > AllocRebind
Definition: allocator_common.hpp:30
#define RCLCPP_SMART_PTR_DEFINITIONS(...)
Definition: macros.hpp:36
virtual std::shared_ptr< rcl_serialized_message_t > borrow_serialized_message()
Definition: message_memory_strategy.hpp:106
rmw_ret_t rmw_serialized_message_init(rmw_serialized_message_t *msg, size_t buffer_capacity, const rcutils_allocator_t *allocator)
typename BufferAllocTraits::allocator_type BufferAlloc
Definition: message_memory_strategy.hpp:53
allocator::AllocRebind< rcl_serialized_message_t, std::allocator< void > > SerializedMessageAllocTraits
Definition: message_memory_strategy.hpp:47
allocator::Deleter< SerializedMessageAlloc, rcl_serialized_message_t > SerializedMessageDeleter
Definition: message_memory_strategy.hpp:50
T reset(T... args)
allocator::AllocRebind< MessageT, std::allocator< void > > MessageAllocTraits
Definition: message_memory_strategy.hpp:43
rcutils_allocator_t rcutils_allocator_
Definition: message_memory_strategy.hpp:138
Default allocation strategy for messages received by subscriptions.
Definition: message_memory_strategy.hpp:38
SerializedMessageDeleter serialized_message_deleter_
Definition: message_memory_strategy.hpp:132
MessageMemoryStrategy(std::shared_ptr< Alloc > allocator)
Definition: message_memory_strategy.hpp:64
T get(T... args)
allocator::Deleter< MessageAlloc, MessageT > MessageDeleter
Definition: message_memory_strategy.hpp:45
BufferDeleter buffer_deleter_
Definition: message_memory_strategy.hpp:135
typename MessageAllocTraits::allocator_type MessageAlloc
Definition: message_memory_strategy.hpp:44
typename SerializedMessageAllocTraits::allocator_type SerializedMessageAlloc
Definition: message_memory_strategy.hpp:48
virtual std::shared_ptr< rcl_serialized_message_t > borrow_serialized_message(size_t capacity)
Definition: message_memory_strategy.hpp:85
typename std::conditional< std::is_same< typename std::allocator_traits< Alloc >::template rebind_alloc< T >, typename std::allocator< void >::template rebind< T >::other >::value, std::default_delete< T >, AllocatorDeleter< Alloc > >::type Deleter
Definition: allocator_deleter.hpp:101
virtual std::shared_ptr< MessageT > borrow_message()
By default, dynamically allocate a new message.
Definition: message_memory_strategy.hpp:80
MessageDeleter message_deleter_
Definition: message_memory_strategy.hpp:129
allocator::Deleter< BufferAlloc, char > BufferDeleter
Definition: message_memory_strategy.hpp:54
rmw_serialized_message_t rmw_get_zero_initialized_serialized_message(void)
virtual void set_default_buffer_capacity(size_t capacity)
Definition: message_memory_strategy.hpp:111