Skip to content

Commit 264c263

Browse files
committed
Implement get_rcl_allocator.
This is needed as a companion change to rclcpp #1324. Signed-off-by: Steve Wolter <[email protected]>
1 parent 49b12ed commit 264c263

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

demo_nodes_cpp/src/topics/allocator_tutorial.cpp

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,33 @@ constexpr bool operator!=(
9292
return false;
9393
}
9494

95+
// You need to overload get_rcl_allocator for custom allocators.
96+
// This function needs to build an instance of rcl_allocator_t
97+
// for use in C code.
98+
template<typename T>
99+
rcl_allocator_t get_rcl_allocator(MyAllocator<T>)
100+
{
101+
rcl_allocator_t rcl_allocator;
102+
rcl_allocator.allocate = [](size_t size, void *) {
103+
num_allocs++;
104+
return malloc(size);
105+
};
106+
rcl_allocator.deallocate = [](void * pointer, void *) {
107+
num_deallocs++;
108+
return free(pointer);
109+
};
110+
rcl_allocator.reallocate = [](void * pointer, size_t size, void *) {
111+
num_allocs++;
112+
num_deallocs++;
113+
return realloc(pointer, size);
114+
};
115+
rcl_allocator.zero_allocate = [](size_t nmemb, size_t size, void *) {
116+
num_allocs++;
117+
return calloc(nmemb, size);
118+
};
119+
return rcl_allocator;
120+
}
121+
95122
// Override global new and delete to count calls during execution.
96123

97124
static bool is_running = false;

0 commit comments

Comments
 (0)