diff --git a/rclcpp/include/rclcpp/any_executable.hpp b/rclcpp/include/rclcpp/any_executable.hpp index 05c0b15..b791da3 100644 --- a/rclcpp/include/rclcpp/any_executable.hpp +++ b/rclcpp/include/rclcpp/any_executable.hpp @@ -23,23 +23,23 @@ namespace rclcpp { namespace executor { - struct AnyExecutable - { +struct AnyExecutable +{ - AnyExecutable() - : subscription(0), timer(0), callback_group(0), node(0) - {} - // Either the subscription or the timer will be set, but not both - rclcpp::subscription::SubscriptionBase::SharedPtr subscription; - rclcpp::timer::TimerBase::SharedPtr timer; - rclcpp::service::ServiceBase::SharedPtr service; - rclcpp::client::ClientBase::SharedPtr client; - // These are used to keep the scope on the containing items - rclcpp::callback_group::CallbackGroup::SharedPtr callback_group; - rclcpp::node::Node::SharedPtr node; - }; + AnyExecutable() + : subscription(0), timer(0), callback_group(0), node(0) + {} + // Either the subscription or the timer will be set, but not both + rclcpp::subscription::SubscriptionBase::SharedPtr subscription; + rclcpp::timer::TimerBase::SharedPtr timer; + rclcpp::service::ServiceBase::SharedPtr service; + rclcpp::client::ClientBase::SharedPtr client; + // These are used to keep the scope on the containing items + rclcpp::callback_group::CallbackGroup::SharedPtr callback_group; + rclcpp::node::Node::SharedPtr node; +}; - typedef std::shared_ptr AnyExecutableSharedPtr; +typedef std::shared_ptr AnyExecutableSharedPtr; } } diff --git a/rclcpp/include/rclcpp/executor.hpp b/rclcpp/include/rclcpp/executor.hpp index b966b9c..1467d26 100644 --- a/rclcpp/include/rclcpp/executor.hpp +++ b/rclcpp/include/rclcpp/executor.hpp @@ -43,7 +43,7 @@ public: RCLCPP_MAKE_SHARED_DEFINITIONS(Executor); Executor(memory_strategy::MemoryStrategySharedPtr ms = - memory_strategy::create_default_strategy()) + memory_strategy::create_default_strategy()) : interrupt_guard_condition_(rmw_create_guard_condition()), _memory_strategy(ms) { @@ -120,15 +120,13 @@ public: void set_memory_strategy(memory_strategy::MemoryStrategySharedPtr memory_strategy) { - if (memory_strategy == nullptr) - { + if (memory_strategy == nullptr) { throw std::runtime_error("Received NULL memory strategy in executor."); } _memory_strategy = memory_strategy; } protected: - void execute_any_executable(AnyExecutableSharedPtr & any_exec) { diff --git a/rclcpp/include/rclcpp/memory_strategy.hpp b/rclcpp/include/rclcpp/memory_strategy.hpp index f5e7b77..14296ee 100644 --- a/rclcpp/include/rclcpp/memory_strategy.hpp +++ b/rclcpp/include/rclcpp/memory_strategy.hpp @@ -20,12 +20,12 @@ namespace rclcpp { // TODO move HandleType somewhere where it makes sense -enum class HandleType{subscriber_handle, service_handle, client_handle, guard_condition_handle}; +enum class HandleType {subscriber_handle, service_handle, client_handle, guard_condition_handle}; namespace executor { - class Executor; -}; +class Executor; +} namespace memory_strategy { @@ -36,13 +36,12 @@ class MemoryStrategy friend class executor::Executor; public: - - virtual void** borrow_handles(HandleType type, size_t number_of_handles) + virtual void ** borrow_handles(HandleType type, size_t number_of_handles) { - return static_cast(rcl_malloc(sizeof(void *) * number_of_handles)); + return static_cast(rcl_malloc(sizeof(void *) * number_of_handles)); } - virtual void return_handles(HandleType type, void** handles) + virtual void return_handles(HandleType type, void ** handles) { this->rcl_free(handles); } @@ -52,20 +51,18 @@ public: return executor::AnyExecutableSharedPtr(new executor::AnyExecutable); } - virtual void *rcl_malloc(size_t size) + virtual void * rcl_malloc(size_t size) { return std::malloc(size); } - virtual void rcl_free(void *ptr) + virtual void rcl_free(void * ptr) { return std::free(ptr); } protected: - private: - }; typedef std::shared_ptr MemoryStrategySharedPtr; diff --git a/rclcpp/include/rclcpp/strategies/static_memory_strategy.hpp b/rclcpp/include/rclcpp/strategies/static_memory_strategy.hpp index 1178be4..a6cb72b 100644 --- a/rclcpp/include/rclcpp/strategies/static_memory_strategy.hpp +++ b/rclcpp/include/rclcpp/strategies/static_memory_strategy.hpp @@ -1,4 +1,3 @@ - // Copyright 2015 Open Source Robotics Foundation, Inc. // // Licensed under the Apache License, Version 2.0 (the "License"); @@ -31,7 +30,6 @@ class StaticMemoryStrategy : public memory_strategy::MemoryStrategy { public: - StaticMemoryStrategy() { memset(_memory_pool, 0, _pool_size); @@ -42,21 +40,18 @@ public: _pool_seq = 0; _exec_seq = 0; - for (size_t i = 0; i < _pool_size; ++i) - { + for (size_t i = 0; i < _pool_size; ++i) { _memory_map[_memory_pool[i]] = 0; } - for (size_t i = 0; i < _max_executables; ++i) - { + for (size_t i = 0; i < _max_executables; ++i) { _executable_pool[i] = std::make_shared(executor::AnyExecutable()); } } - void** borrow_handles(HandleType type, size_t number_of_handles) + void ** borrow_handles(HandleType type, size_t number_of_handles) { - switch(type) - { + switch (type) { case HandleType::subscriber_handle: return _subscriber_pool; case HandleType::service_handle: @@ -71,10 +66,9 @@ public: throw std::runtime_error("Unrecognized enum, could not borrow handle memory."); } - void return_handles(HandleType type, void** handles) + void return_handles(HandleType type, void ** handles) { - switch(type) - { + switch (type) { case HandleType::subscriber_handle: memset(_subscriber_pool, 0, _max_subscribers); break; @@ -95,8 +89,7 @@ public: executor::AnyExecutableSharedPtr instantiate_next_executable() { - if (_exec_seq >= _max_executables) - { + if (_exec_seq >= _max_executables) { // wrap around _exec_seq = 0; } @@ -106,18 +99,16 @@ public: return _executable_pool[prev_exec_seq]; } - void *rcl_malloc(size_t size) + void * rcl_malloc(size_t size) { // Extremely naive static allocation strategy // Keep track of block size at a given pointer - if (_pool_seq + size > _pool_size) - { + if (_pool_seq + size > _pool_size) { // Start at 0 _pool_seq = 0; } - void *ptr = _memory_pool[_pool_seq]; - if (_memory_map.count(ptr) == 0) - { + void * ptr = _memory_pool[_pool_seq]; + if (_memory_map.count(ptr) == 0) { // We expect to have the state for all blocks pre-mapped into _memory_map throw std::runtime_error("Unexpected pointer in rcl_malloc."); } @@ -127,10 +118,9 @@ public: return _memory_pool[prev_pool_seq]; } - void rcl_free(void *ptr) + void rcl_free(void * ptr) { - if (_memory_map.count(ptr) == 0) - { + if (_memory_map.count(ptr) == 0) { // We expect to have the state for all blocks pre-mapped into _memory_map throw std::runtime_error("Unexpected pointer in rcl_free."); } @@ -139,8 +129,6 @@ public: } protected: - - private: static const size_t _pool_size = 1024; static const size_t _max_subscribers = 10; @@ -149,17 +137,17 @@ private: static const size_t _max_guard_conditions = 50; static const size_t _max_executables = 1; - void *_memory_pool[_pool_size]; - void *_subscriber_pool[_max_subscribers]; - void *_service_pool[_max_services]; - void *_client_pool[_max_clients]; - void *_guard_condition_pool[_max_guard_conditions]; + void * _memory_pool[_pool_size]; + void * _subscriber_pool[_max_subscribers]; + void * _service_pool[_max_services]; + void * _client_pool[_max_clients]; + void * _guard_condition_pool[_max_guard_conditions]; executor::AnyExecutableSharedPtr _executable_pool[_max_executables]; size_t _pool_seq; size_t _exec_seq; - std::map _memory_map; + std::map _memory_map; }; }