-
Notifications
You must be signed in to change notification settings - Fork 435
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #64 from ros2/msg_memory_strategy
Create MessageMemoryStrategy for subscribers
- Loading branch information
Showing
5 changed files
with
164 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
// Copyright 2015 Open Source Robotics Foundation, Inc. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
#ifndef RCLCPP_RCLCPP_MSG_MEMORY_STRATEGY_HPP_ | ||
#define RCLCPP_RCLCPP_MSG_MEMORY_STRATEGY_HPP_ | ||
|
||
#include <memory> | ||
|
||
#include <rclcpp/macros.hpp> | ||
|
||
namespace rclcpp | ||
{ | ||
namespace message_memory_strategy | ||
{ | ||
|
||
template<typename MessageT> | ||
class MessageMemoryStrategy | ||
{ | ||
|
||
public: | ||
RCLCPP_MAKE_SHARED_DEFINITIONS(MessageMemoryStrategy); | ||
|
||
static SharedPtr create_default() | ||
{ | ||
return SharedPtr(new MessageMemoryStrategy<MessageT>); | ||
} | ||
|
||
virtual std::shared_ptr<MessageT> borrow_message() | ||
{ | ||
return std::shared_ptr<MessageT>(new MessageT); | ||
} | ||
|
||
virtual void return_message(std::shared_ptr<MessageT> & msg) | ||
{ | ||
msg.reset(); | ||
} | ||
}; | ||
|
||
} /* message_memory_strategy */ | ||
} /* rclcpp */ | ||
|
||
#endif /* RCLCPP_RCLCPP_MSG_MEMORY_STRATEGY_HPP_ */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
85 changes: 85 additions & 0 deletions
85
rclcpp/include/rclcpp/strategies/message_pool_memory_strategy.hpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
// Copyright 2015 Open Source Robotics Foundation, Inc. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
#ifndef RCLCPP_RCLCPP_MSG_POOL_MEMORY_STRATEGY_HPP_ | ||
#define RCLCPP_RCLCPP_MSG_POOL_MEMORY_STRATEGY_HPP_ | ||
|
||
#include <rclcpp/macros.hpp> | ||
#include <rclcpp/message_memory_strategy.hpp> | ||
|
||
namespace rclcpp | ||
{ | ||
namespace strategies | ||
{ | ||
namespace message_pool_memory_strategy | ||
{ | ||
|
||
template<typename MessageT, size_t size, | ||
typename std::enable_if<rosidl_generator_traits::has_fixed_size<MessageT>::value>::type * = | ||
nullptr> | ||
class MessagePoolMemoryStrategy | ||
: public message_memory_strategy::MessageMemoryStrategy<MessageT> | ||
{ | ||
public: | ||
RCLCPP_MAKE_SHARED_DEFINITIONS(MessagePoolMemoryStrategy); | ||
MessagePoolMemoryStrategy() | ||
: next_array_index_(0) | ||
{ | ||
for (size_t i = 0; i < size; ++i) { | ||
pool_[i].msg_ptr_ = std::make_shared<MessageT>(); | ||
pool_[i].used = false; | ||
} | ||
} | ||
|
||
std::shared_ptr<MessageT> borrow_message() | ||
{ | ||
size_t current_index = next_array_index_; | ||
next_array_index_ = (next_array_index_ + 1) % size; | ||
if (pool_[current_index].used) { | ||
throw std::runtime_error("Tried to access message that was still in use! Abort."); | ||
} | ||
pool_[current_index].msg_ptr_->~MessageT(); | ||
new (pool_[current_index].msg_ptr_.get())MessageT; | ||
|
||
pool_[current_index].used = true; | ||
return pool_[current_index].msg_ptr_; | ||
} | ||
|
||
void return_message(std::shared_ptr<MessageT> & msg) | ||
{ | ||
for (size_t i = 0; i < size; ++i) { | ||
if (pool_[i].msg_ptr_ == msg) { | ||
pool_[i].used = false; | ||
return; | ||
} | ||
} | ||
throw std::runtime_error("Unrecognized message ptr in return_message."); | ||
} | ||
|
||
protected: | ||
struct PoolMember | ||
{ | ||
std::shared_ptr<MessageT> msg_ptr_; | ||
bool used; | ||
}; | ||
|
||
std::array<PoolMember, size> pool_; | ||
size_t next_array_index_; | ||
|
||
}; | ||
|
||
} /* message_pool_memory_strategy */ | ||
} /* strategies */ | ||
} /* rclcpp */ | ||
#endif /* RCLCPP_RCLCPP_MSG_POOL_MEMORY_STRATEGY_HPP_ */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters