From 264c2638d3267fb47ab77c9e7c885ebaca2d6a85 Mon Sep 17 00:00:00 2001 From: Steve Wolter Date: Tue, 29 Sep 2020 20:20:07 +0000 Subject: [PATCH 1/2] Implement get_rcl_allocator. This is needed as a companion change to rclcpp #1324. Signed-off-by: Steve Wolter --- .../src/topics/allocator_tutorial.cpp | 27 +++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/demo_nodes_cpp/src/topics/allocator_tutorial.cpp b/demo_nodes_cpp/src/topics/allocator_tutorial.cpp index b3878d39c..79a4a0d92 100644 --- a/demo_nodes_cpp/src/topics/allocator_tutorial.cpp +++ b/demo_nodes_cpp/src/topics/allocator_tutorial.cpp @@ -92,6 +92,33 @@ constexpr bool operator!=( return false; } +// You need to overload get_rcl_allocator for custom allocators. +// This function needs to build an instance of rcl_allocator_t +// for use in C code. +template +rcl_allocator_t get_rcl_allocator(MyAllocator) +{ + rcl_allocator_t rcl_allocator; + rcl_allocator.allocate = [](size_t size, void *) { + num_allocs++; + return malloc(size); + }; + rcl_allocator.deallocate = [](void * pointer, void *) { + num_deallocs++; + return free(pointer); + }; + rcl_allocator.reallocate = [](void * pointer, size_t size, void *) { + num_allocs++; + num_deallocs++; + return realloc(pointer, size); + }; + rcl_allocator.zero_allocate = [](size_t nmemb, size_t size, void *) { + num_allocs++; + return calloc(nmemb, size); + }; + return rcl_allocator; +} + // Override global new and delete to count calls during execution. static bool is_running = false; From c8ae0e0629aeda251b3f982d40a4a769184f625e Mon Sep 17 00:00:00 2001 From: Steve Wolter Date: Tue, 27 Oct 2020 12:12:45 +0000 Subject: [PATCH 2/2] Move get_rcl_allocator to the rclcpp namespace. Signed-off-by: Steve Wolter --- demo_nodes_cpp/src/topics/allocator_tutorial.cpp | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/demo_nodes_cpp/src/topics/allocator_tutorial.cpp b/demo_nodes_cpp/src/topics/allocator_tutorial.cpp index 79a4a0d92..93d170ece 100644 --- a/demo_nodes_cpp/src/topics/allocator_tutorial.cpp +++ b/demo_nodes_cpp/src/topics/allocator_tutorial.cpp @@ -92,7 +92,9 @@ constexpr bool operator!=( return false; } -// You need to overload get_rcl_allocator for custom allocators. +namespace rclcpp { + +// You need to overload rclcpp::get_rcl_allocator for custom allocators. // This function needs to build an instance of rcl_allocator_t // for use in C code. template @@ -119,6 +121,8 @@ rcl_allocator_t get_rcl_allocator(MyAllocator) return rcl_allocator; } +} // namespace rclcpp + // Override global new and delete to count calls during execution. static bool is_running = false;