Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 458b80c

Browse files
committedSep 29, 2020
Drop the generic version of get_rcl_allocator.
This allows us to simplify a bunch of code as well.
1 parent 6e22e2f commit 458b80c

File tree

4 files changed

+11
-98
lines changed

4 files changed

+11
-98
lines changed
 

‎rclcpp/include/rclcpp/allocator/allocator_common.hpp

+4-52
Original file line numberDiff line numberDiff line change
@@ -29,64 +29,16 @@ namespace allocator
2929
template<typename T, typename Alloc>
3030
using 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.
8840
template<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();

‎rclcpp/include/rclcpp/publisher_options.hpp

+3-9
Original file line numberDiff line numberDiff line change
@@ -57,15 +57,13 @@ template<typename Allocator>
5757
struct PublisherOptionsWithAllocator : public PublisherOptionsBase
5858
{
5959
PublisherOptionsWithAllocator<Allocator>()
60-
: allocator_(new Allocator()),
61-
message_allocator_(*allocator_)
60+
: allocator_(new Allocator())
6261
{}
6362

6463
/// Constructor using base class as input.
6564
explicit PublisherOptionsWithAllocator(const PublisherOptionsBase & publisher_options_base)
6665
: PublisherOptionsBase(publisher_options_base),
67-
allocator_(new Allocator()),
68-
message_allocator_(*allocator_)
66+
allocator_(new Allocator())
6967
{}
7068

7169
/// Convert this class, and a rclcpp::QoS, into an rcl_publisher_options_t.
@@ -74,7 +72,7 @@ struct PublisherOptionsWithAllocator : public PublisherOptionsBase
7472
to_rcl_publisher_options(const rclcpp::QoS & qos) const
7573
{
7674
rcl_publisher_options_t result = rcl_publisher_get_default_options();
77-
result.allocator = get_rcl_allocator(message_allocator_);
75+
result.allocator = get_rcl_allocator(*allocator_);
7876
result.qos = qos.get_rmw_qos_profile();
7977

8078
// Apply payload to rcl_publisher_options if necessary.
@@ -94,11 +92,7 @@ struct PublisherOptionsWithAllocator : public PublisherOptionsBase
9492
}
9593

9694
private:
97-
using AllocatorTraits = std::allocator_traits<Allocator>;
98-
using MessageAllocatorT = typename AllocatorTraits::template rebind_alloc<MessageT>;
99-
10095
std::shared_ptr<Allocator> allocator_;
101-
MessageAllocatorT message_allocator_;
10296
};
10397

10498
using PublisherOptions = PublisherOptionsWithAllocator<std::allocator<void>>;

‎rclcpp/include/rclcpp/subscription_options.hpp

+4-10
Original file line numberDiff line numberDiff line change
@@ -79,16 +79,14 @@ template<typename Allocator>
7979
struct SubscriptionOptionsWithAllocator : public SubscriptionOptionsBase
8080
{
8181
SubscriptionOptionsWithAllocator<Allocator>()
82-
: allocator_(new Allocator()),
83-
message_allocator_(*allocator_)
82+
: allocator_(new Allocator())
8483
{}
8584

8685
/// Constructor using base class as input.
8786
explicit SubscriptionOptionsWithAllocator(
8887
const SubscriptionOptionsBase & subscription_options_base)
8988
: SubscriptionOptionsBase(subscription_options_base),
90-
allocator_(new Allocator()),
91-
message_allocator_(*allocator_)
89+
allocator_(new Allocator())
9290
{}
9391

9492
/// Convert this class, with a rclcpp::QoS, into an rcl_subscription_options_t.
@@ -101,7 +99,7 @@ struct SubscriptionOptionsWithAllocator : public SubscriptionOptionsBase
10199
to_rcl_subscription_options(const rclcpp::QoS & qos) const
102100
{
103101
rcl_subscription_options_t result = rcl_subscription_get_default_options();
104-
result.allocator = get_rcl_allocator(message_allocator_);
102+
result.allocator = get_rcl_allocator(*allocator_);
105103
result.qos = qos.get_rmw_qos_profile();
106104
result.rmw_subscription_options.ignore_local_publications = this->ignore_local_publications;
107105

@@ -117,15 +115,11 @@ struct SubscriptionOptionsWithAllocator : public SubscriptionOptionsBase
117115
std::shared_ptr<Allocator>
118116
get_allocator() const
119117
{
120-
return allocator_
118+
return allocator_;
121119
}
122120

123121
private:
124-
using AllocatorTraits = std::allocator_traits<Allocator>;
125-
using MessageAllocatorT = typename AllocatorTraits::template rebind_alloc<MessageT>;
126-
127122
std::shared_ptr<Allocator> allocator_;
128-
MessageAllocatorT message_allocator_;
129123
};
130124

131125
using SubscriptionOptions = SubscriptionOptionsWithAllocator<std::allocator<void>>;

‎rclcpp/test/rclcpp/allocator/test_allocator_common.cpp

-27
Original file line numberDiff line numberDiff line change
@@ -18,33 +18,6 @@
1818

1919
#include "rclcpp/allocator/allocator_common.hpp"
2020

21-
TEST(TestAllocatorCommon, retyped_allocate) {
22-
std::allocator<int> allocator;
23-
void * untyped_allocator = &allocator;
24-
void * allocated_mem =
25-
rclcpp::allocator::retyped_allocate<std::allocator<char>>(1u, untyped_allocator);
26-
ASSERT_NE(nullptr, allocated_mem);
27-
28-
auto code = [&untyped_allocator, allocated_mem]() {
29-
rclcpp::allocator::retyped_deallocate<std::allocator<int>>(
30-
allocated_mem, untyped_allocator);
31-
};
32-
EXPECT_NO_THROW(code());
33-
34-
allocated_mem = allocator.allocate(1);
35-
ASSERT_NE(nullptr, allocated_mem);
36-
void * reallocated_mem =
37-
rclcpp::allocator::retyped_reallocate<std::allocator<int>>(
38-
allocated_mem, 2u, untyped_allocator);
39-
ASSERT_NE(nullptr, reallocated_mem);
40-
41-
auto code2 = [&untyped_allocator, reallocated_mem]() {
42-
rclcpp::allocator::retyped_deallocate<std::allocator<int>>(
43-
reallocated_mem, untyped_allocator);
44-
};
45-
EXPECT_NO_THROW(code2());
46-
}
47-
4821
TEST(TestAllocatorCommon, get_rcl_allocator) {
4922
std::allocator<int> allocator;
5023
auto rcl_allocator = rclcpp::get_rcl_allocator(allocator);

0 commit comments

Comments
 (0)
Please sign in to comment.