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_groups_or_nodes =
false;
156 for (
const auto & pair : weak_groups_to_nodes) {
157 auto group = pair.first.lock();
158 auto node = pair.second.lock();
159 if (group ==
nullptr || node ==
nullptr) {
160 has_invalid_weak_groups_or_nodes =
true;
163 if (!group || !group->can_be_taken_from().load()) {
166 group->find_subscription_ptrs_if(
167 [
this](
const rclcpp::SubscriptionBase::SharedPtr & subscription) {
168 subscription_handles_.push_back(subscription->get_subscription_handle());
171 group->find_service_ptrs_if(
172 [
this](
const rclcpp::ServiceBase::SharedPtr & service) {
173 service_handles_.push_back(service->get_service_handle());
176 group->find_client_ptrs_if(
177 [
this](
const rclcpp::ClientBase::SharedPtr & client) {
178 client_handles_.push_back(client->get_client_handle());
181 group->find_timer_ptrs_if(
182 [
this](
const rclcpp::TimerBase::SharedPtr & timer) {
183 timer_handles_.push_back(timer->get_timer_handle());
186 group->find_waitable_ptrs_if(
187 [
this](
const rclcpp::Waitable::SharedPtr & waitable) {
188 waitable_handles_.push_back(waitable);
193 return has_invalid_weak_groups_or_nodes;
198 if (
nullptr == waitable) {
201 waitable_handles_.push_back(waitable);
206 for (
auto subscription : subscription_handles_) {
210 "Couldn't add subscription to wait set: %s", rcl_get_error_string().str);
215 for (
auto client : client_handles_) {
219 "Couldn't add client to wait set: %s", rcl_get_error_string().str);
224 for (
auto service : service_handles_) {
228 "Couldn't add service to wait set: %s", rcl_get_error_string().str);
233 for (
auto timer : timer_handles_) {
237 "Couldn't add timer to wait set: %s", rcl_get_error_string().str);
242 for (
auto guard_condition : guard_conditions_) {
246 "Couldn't add guard_condition to wait set: %s",
247 rcl_get_error_string().str);
252 for (
auto waitable : waitable_handles_) {
253 if (!waitable->add_to_wait_set(wait_set)) {
256 "Couldn't add waitable to wait set: %s", rcl_get_error_string().str);
268 auto it = subscription_handles_.begin();
269 while (it != subscription_handles_.end()) {
277 it = subscription_handles_.erase(it);
280 if (!group->can_be_taken_from().load()) {
290 subscription_handles_.erase(it);
294 it = subscription_handles_.erase(it);
303 auto it = service_handles_.begin();
304 while (it != service_handles_.end()) {
312 it = service_handles_.erase(it);
315 if (!group->can_be_taken_from().load()) {
325 service_handles_.erase(it);
329 it = service_handles_.erase(it);
338 auto it = client_handles_.begin();
339 while (it != client_handles_.end()) {
347 it = client_handles_.erase(it);
350 if (!group->can_be_taken_from().load()) {
360 client_handles_.erase(it);
364 it = client_handles_.erase(it);
373 auto it = timer_handles_.begin();
374 while (it != timer_handles_.end()) {
382 it = timer_handles_.erase(it);
385 if (!group->can_be_taken_from().load()) {
392 any_exec.
timer = timer;
395 timer_handles_.erase(it);
399 it = timer_handles_.erase(it);
408 auto it = waitable_handles_.begin();
409 while (it != waitable_handles_.end()) {
417 it = waitable_handles_.erase(it);
420 if (!group->can_be_taken_from().load()) {
430 waitable_handles_.erase(it);
434 it = waitable_handles_.erase(it);
440 return rclcpp::allocator::get_rcl_allocator<void *, VoidAlloc>(*allocator_.
get());
445 size_t number_of_subscriptions = subscription_handles_.size();
446 for (
auto waitable : waitable_handles_) {
447 number_of_subscriptions += waitable->get_number_of_ready_subscriptions();
449 return number_of_subscriptions;
454 size_t number_of_services = service_handles_.size();
455 for (
auto waitable : waitable_handles_) {
456 number_of_services += waitable->get_number_of_ready_services();
458 return number_of_services;
463 size_t number_of_events = 0;
464 for (
auto waitable : waitable_handles_) {
465 number_of_events += waitable->get_number_of_ready_events();
467 return number_of_events;
472 size_t number_of_clients = client_handles_.size();
473 for (
auto waitable : waitable_handles_) {
474 number_of_clients += waitable->get_number_of_ready_clients();
476 return number_of_clients;
482 for (
auto waitable : waitable_handles_) {
490 size_t number_of_timers = timer_handles_.size();
491 for (
auto waitable : waitable_handles_) {
492 number_of_timers += waitable->get_number_of_ready_timers();
494 return number_of_timers;
499 return waitable_handles_.size();
507 VectorRebind<const rcl_guard_condition_t *> guard_conditions_;
509 VectorRebind<std::shared_ptr<const rcl_subscription_t>> subscription_handles_;
510 VectorRebind<std::shared_ptr<const rcl_service_t>> service_handles_;
511 VectorRebind<std::shared_ptr<const rcl_client_t>> client_handles_;
512 VectorRebind<std::shared_ptr<const rcl_timer_t>> timer_handles_;
513 VectorRebind<std::shared_ptr<Waitable>> waitable_handles_;
522 #endif // RCLCPP__STRATEGIES__ALLOCATOR_MEMORY_STRATEGY_HPP_