ROS 2 rclcpp + rcl - rolling  rolling-a919a6e5
ROS 2 C++ Client Library with ROS Client Library
memory_strategy.cpp
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 #include "rclcpp/memory_strategy.hpp"
16 #include <memory>
17 
19 
20 rclcpp::SubscriptionBase::SharedPtr
21 MemoryStrategy::get_subscription_by_handle(
22  const std::shared_ptr<const rcl_subscription_t> & subscriber_handle,
23  const WeakCallbackGroupsToNodesMap & weak_groups_to_nodes)
24 {
25  for (const auto & pair : weak_groups_to_nodes) {
26  auto group = pair.first.lock();
27  if (!group) {
28  continue;
29  }
30  auto match_subscription = group->find_subscription_ptrs_if(
31  [&subscriber_handle](const rclcpp::SubscriptionBase::SharedPtr & subscription) -> bool {
32  return subscription->get_subscription_handle() == subscriber_handle;
33  });
34  if (match_subscription) {
35  return match_subscription;
36  }
37  }
38  return nullptr;
39 }
40 
41 rclcpp::ServiceBase::SharedPtr
42 MemoryStrategy::get_service_by_handle(
43  const std::shared_ptr<const rcl_service_t> & service_handle,
44  const WeakCallbackGroupsToNodesMap & weak_groups_to_nodes)
45 {
46  for (const auto & pair : weak_groups_to_nodes) {
47  auto group = pair.first.lock();
48  if (!group) {
49  continue;
50  }
51  auto service_ref = group->find_service_ptrs_if(
52  [&service_handle](const rclcpp::ServiceBase::SharedPtr & service) -> bool {
53  return service->get_service_handle() == service_handle;
54  });
55  if (service_ref) {
56  return service_ref;
57  }
58  }
59  return nullptr;
60 }
61 
62 rclcpp::ClientBase::SharedPtr
63 MemoryStrategy::get_client_by_handle(
64  const std::shared_ptr<const rcl_client_t> & client_handle,
65  const WeakCallbackGroupsToNodesMap & weak_groups_to_nodes)
66 {
67  for (const auto & pair : weak_groups_to_nodes) {
68  auto group = pair.first.lock();
69  if (!group) {
70  continue;
71  }
72  auto client_ref = group->find_client_ptrs_if(
73  [&client_handle](const rclcpp::ClientBase::SharedPtr & client) -> bool {
74  return client->get_client_handle() == client_handle;
75  });
76  if (client_ref) {
77  return client_ref;
78  }
79  }
80  return nullptr;
81 }
82 
83 rclcpp::TimerBase::SharedPtr
84 MemoryStrategy::get_timer_by_handle(
85  const std::shared_ptr<const rcl_timer_t> & timer_handle,
86  const WeakCallbackGroupsToNodesMap & weak_groups_to_nodes)
87 {
88  for (const auto & pair : weak_groups_to_nodes) {
89  auto group = pair.first.lock();
90  if (!group) {
91  continue;
92  }
93  auto timer_ref = group->find_timer_ptrs_if(
94  [&timer_handle](const rclcpp::TimerBase::SharedPtr & timer) -> bool {
95  return timer->get_timer_handle() == timer_handle;
96  });
97  if (timer_ref) {
98  return timer_ref;
99  }
100  }
101  return nullptr;
102 }
103 
104 rclcpp::node_interfaces::NodeBaseInterface::SharedPtr
105 MemoryStrategy::get_node_by_group(
106  const rclcpp::CallbackGroup::SharedPtr & group,
107  const WeakCallbackGroupsToNodesMap & weak_groups_to_nodes)
108 {
109  if (!group) {
110  return nullptr;
111  }
112 
113  rclcpp::CallbackGroup::WeakPtr weak_group_ptr(group);
114  const auto finder = weak_groups_to_nodes.find(weak_group_ptr);
115  if (finder != weak_groups_to_nodes.end()) {
116  rclcpp::node_interfaces::NodeBaseInterface::SharedPtr node_ptr = finder->second.lock();
117  return node_ptr;
118  }
119  return nullptr;
120 }
121 
122 rclcpp::CallbackGroup::SharedPtr
123 MemoryStrategy::get_group_by_subscription(
124  const rclcpp::SubscriptionBase::SharedPtr & subscription,
125  const WeakCallbackGroupsToNodesMap & weak_groups_to_nodes)
126 {
127  for (const auto & pair : weak_groups_to_nodes) {
128  auto group = pair.first.lock();
129  auto node = pair.second.lock();
130  if (!group || !node) {
131  continue;
132  }
133  auto match_sub = group->find_subscription_ptrs_if(
134  [&subscription](const rclcpp::SubscriptionBase::SharedPtr & sub) -> bool {
135  return sub == subscription;
136  });
137  if (match_sub) {
138  return group;
139  }
140  }
141  return nullptr;
142 }
143 
144 rclcpp::CallbackGroup::SharedPtr
145 MemoryStrategy::get_group_by_service(
146  const rclcpp::ServiceBase::SharedPtr & service,
147  const WeakCallbackGroupsToNodesMap & weak_groups_to_nodes)
148 {
149  for (const auto & pair : weak_groups_to_nodes) {
150  auto group = pair.first.lock();
151  auto node = pair.second.lock();
152  if (!group || !node) {
153  continue;
154  }
155  auto service_ref = group->find_service_ptrs_if(
156  [&service](const rclcpp::ServiceBase::SharedPtr & serv) -> bool {
157  return serv == service;
158  });
159  if (service_ref) {
160  return group;
161  }
162  }
163  return nullptr;
164 }
165 
166 rclcpp::CallbackGroup::SharedPtr
167 MemoryStrategy::get_group_by_client(
168  const rclcpp::ClientBase::SharedPtr & client,
169  const WeakCallbackGroupsToNodesMap & weak_groups_to_nodes)
170 {
171  for (const auto & pair : weak_groups_to_nodes) {
172  auto group = pair.first.lock();
173  auto node = pair.second.lock();
174  if (!group || !node) {
175  continue;
176  }
177  auto client_ref = group->find_client_ptrs_if(
178  [&client](const rclcpp::ClientBase::SharedPtr & cli) -> bool {
179  return cli == client;
180  });
181  if (client_ref) {
182  return group;
183  }
184  }
185  return nullptr;
186 }
187 
188 rclcpp::CallbackGroup::SharedPtr
189 MemoryStrategy::get_group_by_timer(
190  const rclcpp::TimerBase::SharedPtr & timer,
191  const WeakCallbackGroupsToNodesMap & weak_groups_to_nodes)
192 {
193  for (const auto & pair : weak_groups_to_nodes) {
194  auto group = pair.first.lock();
195  auto node = pair.second.lock();
196  if (!group || !node) {
197  continue;
198  }
199  auto timer_ref = group->find_timer_ptrs_if(
200  [&timer](const rclcpp::TimerBase::SharedPtr & time) -> bool {
201  return time == timer;
202  });
203  if (timer_ref) {
204  return group;
205  }
206  }
207  return nullptr;
208 }
209 
210 rclcpp::CallbackGroup::SharedPtr
211 MemoryStrategy::get_group_by_waitable(
212  const rclcpp::Waitable::SharedPtr & waitable,
213  const WeakCallbackGroupsToNodesMap & weak_groups_to_nodes)
214 {
215  for (const auto & pair : weak_groups_to_nodes) {
216  auto group = pair.first.lock();
217  auto node = pair.second.lock();
218  if (!group || !node) {
219  continue;
220  }
221  auto waitable_ref = group->find_waitable_ptrs_if(
222  [&waitable](const rclcpp::Waitable::SharedPtr & group_waitable) -> bool {
223  return group_waitable == waitable;
224  });
225  if (waitable_ref) {
226  return group;
227  }
228  }
229  return nullptr;
230 }
Delegate for handling memory allocations while the Executor is executing.