15 #ifndef RCLCPP__ALLOCATOR__ALLOCATOR_COMMON_HPP_
16 #define RCLCPP__ALLOCATOR__ALLOCATOR_COMMON_HPP_
23 #include "rclcpp/allocator/allocator_deleter.hpp"
31 using clean_t = std::remove_cv_t<std::remove_reference_t<T>>;
34 template<
typename,
typename = std::
void_t<>>
41 decltype(std::declval<clean_t<T> &>().get_rcl_allocator())
46 decltype(std::declval<clean_t<T> &>().get_rcl_allocator()),
54 inline constexpr
bool has_get_rcl_allocator_v =
57 template<
typename T,
typename Alloc>
58 using AllocRebind =
typename std::allocator_traits<Alloc>::template rebind_traits<T>;
60 template<
typename Alloc>
61 [[deprecated(
"Conversion of C++ allocators to C style is not valid, as the size on deallocate"
62 "can not be determined. This will be remove in future versions of ros.")]]
63 void * retyped_allocate(
size_t size,
void * untyped_allocator)
65 auto typed_allocator =
static_cast<Alloc *
>(untyped_allocator);
66 if (!typed_allocator) {
67 throw std::runtime_error(
"Received incorrect allocator type");
69 return std::allocator_traits<Alloc>::allocate(*typed_allocator, size);
72 template<
typename Alloc>
73 [[deprecated(
"Conversion of C++ allocators to C style is not valid, as the size on deallocate"
74 "can not be determined. This will be remove in future versions of ros.")]]
75 void * retyped_zero_allocate(
size_t number_of_elem,
size_t size_of_elem,
void * untyped_allocator)
77 auto typed_allocator =
static_cast<Alloc *
>(untyped_allocator);
78 if (!typed_allocator) {
79 throw std::runtime_error(
"Received incorrect allocator type");
81 size_t size = number_of_elem * size_of_elem;
82 void * allocated_memory =
83 std::allocator_traits<Alloc>::allocate(*typed_allocator, size);
84 if (allocated_memory) {
85 std::memset(allocated_memory, 0, size);
87 return allocated_memory;
90 template<
typename T,
typename Alloc>
91 [[deprecated(
"Conversion of C++ allocators to C style is not valid, as the size on deallocate"
92 "can not be determined. This will be remove in future versions of ros.")]]
93 void retyped_deallocate(
void * untyped_pointer,
void * untyped_allocator)
95 auto typed_allocator =
static_cast<Alloc *
>(untyped_allocator);
96 if (!typed_allocator) {
97 throw std::runtime_error(
"Received incorrect allocator type");
99 auto typed_ptr =
static_cast<T *
>(untyped_pointer);
100 std::allocator_traits<Alloc>::deallocate(*typed_allocator, typed_ptr, 1);
103 template<
typename T,
typename Alloc>
104 [[deprecated(
"Conversion of C++ allocators to C style is not valid, as the size on deallocate"
105 "can not be determined. This will be remove in future versions of ros.")]]
106 void * retyped_reallocate(
void * untyped_pointer,
size_t size,
void * untyped_allocator)
108 auto typed_allocator =
static_cast<Alloc *
>(untyped_allocator);
109 if (!typed_allocator) {
110 throw std::runtime_error(
"Received incorrect allocator type");
112 auto typed_ptr =
static_cast<T *
>(untyped_pointer);
113 std::allocator_traits<Alloc>::deallocate(*typed_allocator, typed_ptr, 1);
114 return std::allocator_traits<Alloc>::allocate(*typed_allocator, size);
122 typename std::enable_if<!std::is_same<Alloc, std::allocator<void>>::value>::type * =
nullptr>
123 [[deprecated(
"Conversion of C++ allocators to C style is not valid, as the size on deallocate"
124 "can not be determined. This will be remove in future versions of ros. To suppress this warning"
125 "define the method 'rcl_allocator_t get_rcl_allocator()' on your allocator")]]
130 rcl_allocator.allocate = &retyped_allocate<Alloc>;
131 rcl_allocator.zero_allocate = &retyped_zero_allocate<Alloc>;
132 rcl_allocator.deallocate = &retyped_deallocate<T, Alloc>;
133 rcl_allocator.reallocate = &retyped_reallocate<T, Alloc>;
134 rcl_allocator.state = &allocator;
138 return rcl_allocator;
145 typename std::enable_if<std::is_same<Alloc, std::allocator<void>>::value>::type * =
nullptr>
#define rcl_get_default_allocator
Return a properly initialized rcl_allocator_t with default values.
rcutils_allocator_t rcl_allocator_t
Encapsulation of an allocator.
Versions of rosidl_typesupport_cpp::get_message_type_support_handle that handle adapted types.