ROS 2 rclcpp + rcl - humble  humble
ROS 2 C++ Client Library with ROS Client Library
allocator_common.hpp
1 // Copyright 2015 Open Source Robotics Foundation, Inc.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #ifndef RCLCPP__ALLOCATOR__ALLOCATOR_COMMON_HPP_
16 #define RCLCPP__ALLOCATOR__ALLOCATOR_COMMON_HPP_
17 
18 #include <cstring>
19 #include <memory>
20 
21 #include "rcl/allocator.h"
22 
23 #include "rclcpp/allocator/allocator_deleter.hpp"
24 
25 namespace rclcpp
26 {
27 namespace allocator
28 {
29 
30 template<typename T, typename Alloc>
31 using AllocRebind = typename std::allocator_traits<Alloc>::template rebind_traits<T>;
32 
33 template<typename Alloc>
34 void * retyped_allocate(size_t size, void * untyped_allocator)
35 {
36  auto typed_allocator = static_cast<Alloc *>(untyped_allocator);
37  if (!typed_allocator) {
38  throw std::runtime_error("Received incorrect allocator type");
39  }
40  return std::allocator_traits<Alloc>::allocate(*typed_allocator, size);
41 }
42 
43 template<typename Alloc>
44 void * retyped_zero_allocate(size_t number_of_elem, size_t size_of_elem, void * untyped_allocator)
45 {
46  auto typed_allocator = static_cast<Alloc *>(untyped_allocator);
47  if (!typed_allocator) {
48  throw std::runtime_error("Received incorrect allocator type");
49  }
50  size_t size = number_of_elem * size_of_elem;
51  void * allocated_memory =
52  std::allocator_traits<Alloc>::allocate(*typed_allocator, size);
53  if (allocated_memory) {
54  std::memset(allocated_memory, 0, size);
55  }
56  return allocated_memory;
57 }
58 
59 template<typename T, typename Alloc>
60 void retyped_deallocate(void * untyped_pointer, void * untyped_allocator)
61 {
62  auto typed_allocator = static_cast<Alloc *>(untyped_allocator);
63  if (!typed_allocator) {
64  throw std::runtime_error("Received incorrect allocator type");
65  }
66  auto typed_ptr = static_cast<T *>(untyped_pointer);
67  std::allocator_traits<Alloc>::deallocate(*typed_allocator, typed_ptr, 1);
68 }
69 
70 template<typename T, typename Alloc>
71 void * retyped_reallocate(void * untyped_pointer, size_t size, void * untyped_allocator)
72 {
73  auto typed_allocator = static_cast<Alloc *>(untyped_allocator);
74  if (!typed_allocator) {
75  throw std::runtime_error("Received incorrect allocator type");
76  }
77  auto typed_ptr = static_cast<T *>(untyped_pointer);
78  std::allocator_traits<Alloc>::deallocate(*typed_allocator, typed_ptr, 1);
79  return std::allocator_traits<Alloc>::allocate(*typed_allocator, size);
80 }
81 
82 
83 // Convert a std::allocator_traits-formatted Allocator into an rcl allocator
84 template<
85  typename T,
86  typename Alloc,
87  typename std::enable_if<!std::is_same<Alloc, std::allocator<void>>::value>::type * = nullptr>
88 rcl_allocator_t get_rcl_allocator(Alloc & allocator)
89 {
91 #ifndef _WIN32
92  rcl_allocator.allocate = &retyped_allocate<Alloc>;
93  rcl_allocator.zero_allocate = &retyped_zero_allocate<Alloc>;
94  rcl_allocator.deallocate = &retyped_deallocate<T, Alloc>;
95  rcl_allocator.reallocate = &retyped_reallocate<T, Alloc>;
96  rcl_allocator.state = &allocator;
97 #else
98  (void)allocator; // Remove warning
99 #endif
100  return rcl_allocator;
101 }
102 
103 // TODO(jacquelinekay) Workaround for an incomplete implementation of std::allocator<void>
104 template<
105  typename T,
106  typename Alloc,
107  typename std::enable_if<std::is_same<Alloc, std::allocator<void>>::value>::type * = nullptr>
108 rcl_allocator_t get_rcl_allocator(Alloc & allocator)
109 {
110  (void)allocator;
111  return rcl_get_default_allocator();
112 }
113 
114 } // namespace allocator
115 } // namespace rclcpp
116 
117 #endif // RCLCPP__ALLOCATOR__ALLOCATOR_COMMON_HPP_
#define rcl_get_default_allocator
Return a properly initialized rcl_allocator_t with default values.
Definition: allocator.h:37
rcutils_allocator_t rcl_allocator_t
Encapsulation of an allocator.
Definition: allocator.h:31
Versions of rosidl_typesupport_cpp::get_message_type_support_handle that handle adapted types.