15 #ifndef RCLCPP__STRATEGIES__ALLOCATOR_MEMORY_STRATEGY_HPP_ 16 #define RCLCPP__STRATEGIES__ALLOCATOR_MEMORY_STRATEGY_HPP_ 34 namespace memory_strategies
36 namespace allocator_memory_strategy
45 template<
typename Alloc = std::allocator<
void>>
56 allocator_ = std::make_shared<VoidAlloc>(*allocator.get());
61 allocator_ = std::make_shared<VoidAlloc>();
66 for (
const auto & existing_guard_condition : guard_conditions_) {
67 if (existing_guard_condition == guard_condition) {
71 guard_conditions_.push_back(guard_condition);
76 for (
auto it = guard_conditions_.begin(); it != guard_conditions_.end(); ++it) {
77 if (*it == guard_condition) {
78 guard_conditions_.erase(it);
86 subscription_handles_.clear();
87 service_handles_.clear();
88 client_handles_.clear();
89 timer_handles_.clear();
90 waitable_handles_.clear();
101 for (
size_t i = 0; i < subscription_handles_.size(); ++i) {
103 subscription_handles_[i].reset();
106 for (
size_t i = 0; i < service_handles_.size(); ++i) {
108 service_handles_[i].reset();
111 for (
size_t i = 0; i < client_handles_.size(); ++i) {
113 client_handles_[i].reset();
116 for (
size_t i = 0; i < timer_handles_.size(); ++i) {
117 if (!wait_set->
timers[i]) {
118 timer_handles_[i].reset();
121 for (
size_t i = 0; i < waitable_handles_.size(); ++i) {
122 if (!waitable_handles_[i]->is_ready(wait_set)) {
123 waitable_handles_[i].reset();
127 subscription_handles_.erase(
128 std::remove(subscription_handles_.begin(), subscription_handles_.end(),
nullptr),
129 subscription_handles_.end()
132 service_handles_.erase(
133 std::remove(service_handles_.begin(), service_handles_.end(),
nullptr),
134 service_handles_.end()
137 client_handles_.erase(
138 std::remove(client_handles_.begin(), client_handles_.end(),
nullptr),
139 client_handles_.end()
142 timer_handles_.erase(
143 std::remove(timer_handles_.begin(), timer_handles_.end(),
nullptr),
147 waitable_handles_.erase(
148 std::remove(waitable_handles_.begin(), waitable_handles_.end(),
nullptr),
149 waitable_handles_.end()
155 bool has_invalid_weak_nodes =
false;
156 for (
auto & weak_node : weak_nodes) {
157 auto node = weak_node.lock();
159 has_invalid_weak_nodes =
true;
162 for (
auto & weak_group : node->get_callback_groups()) {
163 auto group = weak_group.lock();
164 if (!group || !group->can_be_taken_from().load()) {
167 for (
auto & weak_subscription : group->get_subscription_ptrs()) {
168 auto subscription = weak_subscription.lock();
170 subscription_handles_.push_back(subscription->get_subscription_handle());
171 if (subscription->get_intra_process_subscription_handle()) {
172 subscription_handles_.push_back(
173 subscription->get_intra_process_subscription_handle());
177 for (
auto & weak_service : group->get_service_ptrs()) {
178 auto service = weak_service.lock();
180 service_handles_.push_back(service->get_service_handle());
183 for (
auto & weak_client : group->get_client_ptrs()) {
184 auto client = weak_client.lock();
186 client_handles_.push_back(client->get_client_handle());
189 for (
auto & weak_timer : group->get_timer_ptrs()) {
190 auto timer = weak_timer.lock();
192 timer_handles_.push_back(timer->get_timer_handle());
195 for (
auto & weak_waitable : group->get_waitable_ptrs()) {
196 auto waitable = weak_waitable.lock();
198 waitable_handles_.push_back(waitable);
203 return has_invalid_weak_nodes;
208 for (
auto subscription : subscription_handles_) {
217 for (
auto client : client_handles_) {
226 for (
auto service : service_handles_) {
235 for (
auto timer : timer_handles_) {
244 for (
auto guard_condition : guard_conditions_) {
248 "Couldn't add guard_condition to wait set: %s",
254 for (
auto waitable : waitable_handles_) {
255 if (!waitable->add_to_wait_set(wait_set)) {
270 auto it = subscription_handles_.begin();
271 while (it != subscription_handles_.end()) {
275 bool is_intra_process =
false;
276 if (subscription->get_intra_process_subscription_handle()) {
277 is_intra_process = subscription->get_intra_process_subscription_handle() == *it;
284 it = subscription_handles_.erase(it);
287 if (!group->can_be_taken_from().load()) {
294 if (is_intra_process) {
301 subscription_handles_.erase(it);
305 it = subscription_handles_.erase(it);
314 auto it = service_handles_.begin();
315 while (it != service_handles_.end()) {
323 it = service_handles_.erase(it);
326 if (!group->can_be_taken_from().load()) {
336 service_handles_.erase(it);
340 it = service_handles_.erase(it);
347 auto it = client_handles_.begin();
348 while (it != client_handles_.end()) {
356 it = client_handles_.erase(it);
359 if (!group->can_be_taken_from().load()) {
369 client_handles_.erase(it);
373 it = client_handles_.erase(it);
380 auto it = waitable_handles_.begin();
381 while (it != waitable_handles_.end()) {
389 it = waitable_handles_.erase(it);
392 if (!group->can_be_taken_from().load()) {
402 waitable_handles_.erase(it);
406 it = waitable_handles_.erase(it);
412 return rclcpp::allocator::get_rcl_allocator<void *, VoidAlloc>(*allocator_.
get());
417 size_t number_of_subscriptions = subscription_handles_.size();
418 for (
auto waitable : waitable_handles_) {
419 number_of_subscriptions += waitable->get_number_of_ready_subscriptions();
421 return number_of_subscriptions;
426 size_t number_of_services = service_handles_.size();
427 for (
auto waitable : waitable_handles_) {
428 number_of_services += waitable->get_number_of_ready_services();
430 return number_of_services;
435 size_t number_of_events = 0;
436 for (
auto waitable : waitable_handles_) {
437 number_of_events += waitable->get_number_of_ready_events();
439 return number_of_events;
444 size_t number_of_clients = client_handles_.size();
445 for (
auto waitable : waitable_handles_) {
446 number_of_clients += waitable->get_number_of_ready_clients();
448 return number_of_clients;
454 for (
auto waitable : waitable_handles_) {
455 number_of_guard_conditions += waitable->get_number_of_ready_guard_conditions();
462 size_t number_of_timers = timer_handles_.size();
463 for (
auto waitable : waitable_handles_) {
464 number_of_timers += waitable->get_number_of_ready_timers();
466 return number_of_timers;
471 return waitable_handles_.size();
494 #endif // RCLCPP__STRATEGIES__ALLOCATOR_MEMORY_STRATEGY_HPP_ #define rcl_get_error_string
const rcl_service_t ** services
rclcpp::ServiceBase::SharedPtr service
Definition: any_executable.hpp:47
rclcpp::node_interfaces::NodeBaseInterface::SharedPtr node_base
Definition: any_executable.hpp:52
Delegate for handling memory allocations while the Executor is executing.
Definition: memory_strategy.hpp:41
rclcpp::Waitable::SharedPtr waitable
Definition: any_executable.hpp:49
size_t number_of_guard_conditions() const
Definition: allocator_memory_strategy.hpp:451
rclcpp::SubscriptionBase::SharedPtr subscription_intra_process
Definition: any_executable.hpp:45
Definition: any_executable.hpp:35
virtual rcl_allocator_t get_allocator()
Definition: allocator_memory_strategy.hpp:410
This header provides the get_node_topics_interface() template function.
Definition: allocator_common.hpp:24
rclcpp::ClientBase::SharedPtr client
Definition: any_executable.hpp:48
size_t number_of_ready_subscriptions() const
Definition: allocator_memory_strategy.hpp:415
size_t number_of_ready_services() const
Definition: allocator_memory_strategy.hpp:424
const rcl_client_t ** clients
AllocatorMemoryStrategy()
Definition: allocator_memory_strategy.hpp:59
bool collect_entities(const WeakNodeList &weak_nodes)
Definition: allocator_memory_strategy.hpp:153
typename VoidAllocTraits::allocator_type VoidAlloc
Definition: allocator_memory_strategy.hpp:52
const rcl_timer_t ** timers
virtual void get_next_service(executor::AnyExecutable &any_exec, const WeakNodeList &weak_nodes)
Definition: allocator_memory_strategy.hpp:310
rclcpp::SubscriptionBase::SharedPtr subscription
Definition: any_executable.hpp:44
typename std::allocator_traits< Alloc >::template rebind_traits< T > AllocRebind
Definition: allocator_common.hpp:30
virtual void get_next_waitable(executor::AnyExecutable &any_exec, const WeakNodeList &weak_nodes)
Definition: allocator_memory_strategy.hpp:378
static rclcpp::node_interfaces::NodeBaseInterface::SharedPtr get_node_by_group(rclcpp::callback_group::CallbackGroup::SharedPtr group, const WeakNodeList &weak_nodes)
#define RCLCPP_SMART_PTR_DEFINITIONS(...)
Definition: macros.hpp:36
virtual void get_next_client(executor::AnyExecutable &any_exec, const WeakNodeList &weak_nodes)
Definition: allocator_memory_strategy.hpp:345
virtual void get_next_subscription(executor::AnyExecutable &any_exec, const WeakNodeList &weak_nodes)
Definition: allocator_memory_strategy.hpp:266
void add_guard_condition(const rcl_guard_condition_t *guard_condition)
Definition: allocator_memory_strategy.hpp:64
static rclcpp::callback_group::CallbackGroup::SharedPtr get_group_by_waitable(rclcpp::Waitable::SharedPtr waitable, const WeakNodeList &weak_nodes)
rcl_ret_t rcl_wait_set_add_timer(rcl_wait_set_t *wait_set, const rcl_timer_t *timer, size_t *index)
static rclcpp::callback_group::CallbackGroup::SharedPtr get_group_by_client(rclcpp::ClientBase::SharedPtr client, const WeakNodeList &weak_nodes)
size_t number_of_ready_clients() const
Definition: allocator_memory_strategy.hpp:442
typename allocator::AllocRebind< void *, Alloc > VoidAllocTraits
Definition: allocator_memory_strategy.hpp:51
rcl_ret_t rcl_wait_set_add_service(rcl_wait_set_t *wait_set, const rcl_service_t *service, size_t *index)
#define RCUTILS_LOG_ERROR_NAMED(name,...)
rclcpp::callback_group::CallbackGroup::SharedPtr callback_group
Definition: any_executable.hpp:51
virtual void remove_null_handles(rcl_wait_set_t *wait_set)
Definition: allocator_memory_strategy.hpp:93
static rclcpp::callback_group::CallbackGroup::SharedPtr get_group_by_service(rclcpp::ServiceBase::SharedPtr service, const WeakNodeList &weak_nodes)
void remove_guard_condition(const rcl_guard_condition_t *guard_condition)
Definition: allocator_memory_strategy.hpp:74
void clear_handles()
Definition: allocator_memory_strategy.hpp:84
static rclcpp::callback_group::CallbackGroup::SharedPtr get_group_by_subscription(rclcpp::SubscriptionBase::SharedPtr subscription, const WeakNodeList &weak_nodes)
static rclcpp::SubscriptionBase::SharedPtr get_subscription_by_handle(std::shared_ptr< const rcl_subscription_t > subscriber_handle, const WeakNodeList &weak_nodes)
rcl_ret_t rcl_wait_set_add_subscription(rcl_wait_set_t *wait_set, const rcl_subscription_t *subscription, size_t *index)
size_t number_of_waitables() const
Definition: allocator_memory_strategy.hpp:469
rcl_ret_t rcl_wait_set_add_client(rcl_wait_set_t *wait_set, const rcl_client_t *client, size_t *index)
static rclcpp::ServiceBase::SharedPtr get_service_by_handle(std::shared_ptr< const rcl_service_t > service_handle, const WeakNodeList &weak_nodes)
static rclcpp::ClientBase::SharedPtr get_client_by_handle(std::shared_ptr< const rcl_client_t > client_handle, const WeakNodeList &weak_nodes)
size_t number_of_ready_timers() const
Definition: allocator_memory_strategy.hpp:460
size_t number_of_ready_events() const
Definition: allocator_memory_strategy.hpp:433
bool add_handles_to_wait_set(rcl_wait_set_t *wait_set)
Definition: allocator_memory_strategy.hpp:206
Delegate for handling memory allocations while the Executor is executing.
Definition: allocator_memory_strategy.hpp:46
const rcl_subscription_t ** subscriptions
rcl_ret_t rcl_wait_set_add_guard_condition(rcl_wait_set_t *wait_set, const rcl_guard_condition_t *guard_condition, size_t *index)