forked from space-ros/demos
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: William Woodall <[email protected]>
- Loading branch information
Showing
4 changed files
with
159 additions
and
0 deletions.
There are no files selected for viewing
10 changes: 10 additions & 0 deletions
10
space_ros_memory_allocation_demo/launch/talker_listener.launch.xml
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,10 @@ | ||
<launch> | ||
<arg name="use_memory_tools" default="false" /> | ||
<set_env | ||
name="LD_PRELOAD" | ||
value="/opt/ros/rolling/lib/libmemory_tools_interpose.so" | ||
if="$(var use_memory_tools)" | ||
/> | ||
<node pkg="space_ros_memory_allocation_demo" exec="talker_main" output="screen" /> | ||
<node pkg="space_ros_memory_allocation_demo" exec="listener_main" output="screen" /> | ||
</launch> |
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,67 @@ | ||
// Copyright 2022 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 SPACE_ROS_MEMORY_ALLOCATION_DEMO__LISTENER_HPP_ | ||
#define SPACE_ROS_MEMORY_ALLOCATION_DEMO__LISTENER_HPP_ | ||
|
||
#include "rclcpp/rclcpp.hpp" | ||
|
||
#include "std_msgs/msg/string.hpp" | ||
|
||
#include "space_ros_memory_allocation_demo/visibility_control.h" | ||
#include "space_ros_memory_allocation_demo/memory_allocator.hpp" | ||
|
||
#include "expected_push_pop.hpp" | ||
|
||
namespace space_ros_memory_allocation_demo | ||
{ | ||
|
||
// TODO(wjwwood): Cannot use this until we fix https://github.com/ros2/rosidl/issues/566 | ||
// using String = std_msgs::msg::String_<MemoryAllocator>; | ||
using String = std_msgs::msg::String; | ||
|
||
// Create a Listener class that subclasses the generic rclcpp::Node base class. | ||
// The main function below will instantiate the class as a ROS node. | ||
class Listener : public rclcpp::Node | ||
{ | ||
public: | ||
SPACE_ROS_MEMORY_ALLOCATION_DEMO_PUBLIC | ||
explicit Listener( | ||
const rclcpp::NodeOptions & options, | ||
std::pmr::memory_resource * memory_resource = std::pmr::get_default_resource()) | ||
: Node("listener", options) | ||
{ | ||
RCUTILS_UNUSED(memory_resource); | ||
// Create a callback function for when messages are received. | ||
// Variations of this function also exist using, for example UniquePtr for zero-copy transport. | ||
auto callback = | ||
[this](std_msgs::msg::String::ConstSharedPtr msg) -> void | ||
{ | ||
RCLCPP_INFO(this->get_logger(), "I heard: [%s]", msg->data.c_str()); | ||
this->get_node_base_interface()->get_context()->shutdown("done"); | ||
}; | ||
// Create a subscription to the topic which can be matched with one or more compatible ROS | ||
// publishers. | ||
// Note that not all publishers on the same topic with the same type will be compatible: | ||
// they must have compatible Quality of Service policies. | ||
sub_ = create_subscription<std_msgs::msg::String>("chatter", 10, callback); | ||
} | ||
|
||
private: | ||
rclcpp::Subscription<std_msgs::msg::String>::SharedPtr sub_; | ||
}; | ||
|
||
} // namespace space_ros_memory_allocation_demo | ||
|
||
#endif // SPACE_ROS_MEMORY_ALLOCATION_DEMO__LISTENER_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,20 @@ | ||
// Copyright 2022 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. | ||
|
||
#include "rclcpp/rclcpp.hpp" | ||
#include "rclcpp_components/register_node_macro.hpp" | ||
|
||
#include "listener.hpp" | ||
|
||
RCLCPP_COMPONENTS_REGISTER_NODE(space_ros_memory_allocation_demo::Listener) |
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,62 @@ | ||
// Copyright 2022 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. | ||
|
||
#include <memory> | ||
|
||
#include "osrf_testing_tools_cpp/memory_tools/memory_tools.hpp" | ||
#include "rclcpp/rclcpp.hpp" | ||
|
||
#include "common_main.hpp" | ||
#include "expected_push_pop.hpp" | ||
#include "listener.hpp" | ||
|
||
int | ||
actual_main(int argc, char const * argv[], space_ros_memory_allocation_demo::MemoryAllocator & ma) | ||
{ | ||
using std::pmr::polymorphic_allocator; | ||
// Create a C-style allocator that wraps the pmr C++ allocator. | ||
polymorphic_allocator<void> pa(&ma); | ||
rcl_allocator_t rcl_allocator = | ||
rclcpp::allocator::get_rcl_allocator<void, polymorphic_allocator<void>>(pa); | ||
|
||
using rclcpp::Context; | ||
rclcpp::InitOptions io(rcl_allocator); | ||
auto context = std::allocate_shared<Context, polymorphic_allocator<Context>>(&ma); | ||
context->init(argc, argv, io); | ||
|
||
rclcpp::NodeOptions no(rcl_allocator); | ||
no.context(context); | ||
using space_ros_memory_allocation_demo::Listener; | ||
auto listener = | ||
std::allocate_shared<Listener, std::pmr::polymorphic_allocator<Listener>>(&ma, no, &ma); | ||
|
||
// TODO(wjwwood): use ma in executor memory_strategy | ||
rclcpp::ExecutorOptions eo; | ||
eo.context = context; | ||
rclcpp::executors::SingleThreadedExecutor executor(eo); | ||
|
||
executor.add_node(listener); | ||
EXPECT_NO_MEMORY_OPERATIONS( | ||
{ | ||
executor.spin(); | ||
}); | ||
|
||
return 0; | ||
} | ||
|
||
int | ||
main(int argc, char const * argv[]) | ||
{ | ||
return common_main(argc, argv, actual_main); | ||
} |