@@ -29,64 +29,16 @@ namespace allocator
2929template <typename T, typename Alloc>
3030using AllocRebind = typename std::allocator_traits<Alloc>::template rebind_traits<T>;
3131
32- template <typename Alloc>
33- void * retyped_allocate (size_t size, void * untyped_allocator)
34- {
35- auto typed_allocator = static_cast <Alloc *>(untyped_allocator);
36- if (!typed_allocator) {
37- throw std::runtime_error (" Received incorrect allocator type" );
38- }
39- return std::allocator_traits<Alloc>::allocate (*typed_allocator, size);
40- }
41-
42- template <typename Alloc>
43- void retyped_deallocate (void * untyped_pointer, void * untyped_allocator)
44- {
45- auto typed_allocator = static_cast <Alloc *>(untyped_allocator);
46- if (!typed_allocator) {
47- throw std::runtime_error (" Received incorrect allocator type" );
48- }
49- auto typed_ptr = static_cast <typename Alloc::value_type *>(untyped_pointer);
50- std::allocator_traits<Alloc>::deallocate (*typed_allocator, typed_ptr, 1 );
51- }
52-
53- template <typename Alloc>
54- void * retyped_reallocate (void * untyped_pointer, size_t size, void * untyped_allocator)
55- {
56- auto typed_allocator = static_cast <Alloc *>(untyped_allocator);
57- if (!typed_allocator) {
58- throw std::runtime_error (" Received incorrect allocator type" );
59- }
60- auto typed_ptr = static_cast <typename Alloc::value_type *>(untyped_pointer);
61- std::allocator_traits<Alloc>::deallocate (*typed_allocator, typed_ptr, 1 );
62- return std::allocator_traits<Alloc>::allocate (*typed_allocator, size);
63- }
64-
6532} // namespace allocator
6633
67- // Deprecated: Generic converter from C++ allocator into RCL allocator.
68- // This allocator overallocates memory by 100x and invokes UB on free,
69- // see #1254.
70- template <typename Alloc>
71- rcl_allocator_t get_rcl_allocator (Alloc & allocator)
72- {
73- rcl_allocator_t rcl_allocator = rcl_get_default_allocator ();
74- #ifndef _WIN32
75- rcl_allocator.allocate = &allocator::retyped_allocate<Alloc>;
76- rcl_allocator.deallocate = &allocator::retyped_deallocate<Alloc>;
77- rcl_allocator.reallocate = &allocator::retyped_reallocate<Alloc>;
78- rcl_allocator.state = &allocator;
79- #else
80- (void )allocator; // Remove warning
81- #endif
82- return rcl_allocator;
83- }
84-
8534// Builds the RCL default allocator for the C++ standard allocator.
8635// This assumes that the user intent behind both allocators is the
8736// same: Using system malloc for allocation.
37+ //
38+ // If you're using a custom allocator in ROS, you'll need to provide
39+ // your own overload for this function.
8840template <typename T>
89- rcl_allocator_t get_rcl_allocator (std::allocator<T> & allocator)
41+ rcl_allocator_t get_rcl_allocator (std::allocator<T> allocator)
9042{
9143 (void )allocator; // Remove warning
9244 return rcl_get_default_allocator ();
0 commit comments