From d7ffcc681f266895df0e227d0df9eb3edfb5ac53 Mon Sep 17 00:00:00 2001 From: Jackie Kay Date: Wed, 14 Oct 2015 14:50:58 -0700 Subject: [PATCH 1/2] reduce redundant memory allocations in default memory strategy --- rclcpp/include/rclcpp/memory_strategy.hpp | 67 +++++++++++++++++++++-- 1 file changed, 63 insertions(+), 4 deletions(-) diff --git a/rclcpp/include/rclcpp/memory_strategy.hpp b/rclcpp/include/rclcpp/memory_strategy.hpp index 7b60f86..ddb2f1e 100644 --- a/rclcpp/include/rclcpp/memory_strategy.hpp +++ b/rclcpp/include/rclcpp/memory_strategy.hpp @@ -53,8 +53,31 @@ public: */ virtual void ** borrow_handles(HandleType type, size_t number_of_handles) { - (void)type; - return static_cast(alloc(sizeof(void *) * number_of_handles)); + switch (type) { + case HandleType::subscription_handle: + if (subscription_handles.size() < number_of_handles) { + subscription_handles.resize(number_of_handles, 0); + } + return static_cast(subscription_handles.data()); + case HandleType::service_handle: + if (service_handles.size() < number_of_handles) { + service_handles.resize(number_of_handles, 0); + } + return static_cast(service_handles.data()); + case HandleType::client_handle: + if (client_handles.size() < number_of_handles) { + client_handles.resize(number_of_handles, 0); + } + return static_cast(client_handles.data()); + case HandleType::guard_condition_handle: + if (number_of_handles > 2) { + throw std::runtime_error("Too many guard condition handles requested!"); + } + return guard_cond_handles.data(); + default: + throw std::runtime_error("Unknown HandleType " + std::to_string(static_cast(type)) + + ", could not borrow handle memory."); + } } /// Return the memory borrowed in borrow_handles. @@ -65,8 +88,39 @@ public: */ virtual void return_handles(HandleType type, void ** handles) { - (void)type; - this->free(handles); + switch (type) { + case HandleType::subscription_handle: + if (handles != subscription_handles.data()) { + throw std::runtime_error( + "tried to return memory that isn't handled by this MemoryStrategy"); + } + memset(handles, 0, subscription_handles.size()); + break; + case HandleType::service_handle: + if (handles != service_handles.data()) { + throw std::runtime_error( + "tried to return memory that isn't handled by this MemoryStrategy"); + } + memset(handles, 0, service_handles.size()); + break; + case HandleType::client_handle: + if (handles != client_handles.data()) { + throw std::runtime_error( + "tried to return memory that isn't handled by this MemoryStrategy"); + } + memset(handles, 0, client_handles.size()); + break; + case HandleType::guard_condition_handle: + if (handles != guard_cond_handles.data()) { + throw std::runtime_error( + "tried to return memory that isn't handled by this MemoryStrategy"); + } + guard_cond_handles.fill(0); + break; + default: + throw std::runtime_error("Unknown HandleType " + std::to_string(static_cast(type)) + + ", could not borrow handle memory."); + } } /// Provide a newly initialized AnyExecutable object. @@ -101,6 +155,11 @@ public: std::vector subs; std::vector services; std::vector clients; + + std::vector subscription_handles; + std::vector service_handles; + std::vector client_handles; + std::array guard_cond_handles; }; From 4fa974d96f94effe3702007f1fb30c4bcf8e080a Mon Sep 17 00:00:00 2001 From: Jackie Kay Date: Thu, 15 Oct 2015 09:29:57 -0700 Subject: [PATCH 2/2] Change doc block --- rclcpp/include/rclcpp/memory_strategy.hpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/rclcpp/include/rclcpp/memory_strategy.hpp b/rclcpp/include/rclcpp/memory_strategy.hpp index ddb2f1e..eed0e0e 100644 --- a/rclcpp/include/rclcpp/memory_strategy.hpp +++ b/rclcpp/include/rclcpp/memory_strategy.hpp @@ -46,7 +46,8 @@ public: /// Borrow memory for storing data for subscriptions, services, clients, or guard conditions. /** - * The default implementation ignores the handle type and dynamically allocates the memory. + * The default implementation stores std::vectors for each handle type and resizes the vectors + * as necessary based on the requested number of handles. * \param[in] The type of entity that this function is requesting for. * \param[in] The number of handles to borrow. * \return Pointer to the allocated handles.