Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Do not attempt to use void allocators for memory allocation. #1657

Merged
merged 1 commit into from
May 5, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 18 additions & 1 deletion rclcpp/include/rclcpp/publisher_options.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ struct PublisherOptionsWithAllocator : public PublisherOptionsBase
to_rcl_publisher_options(const rclcpp::QoS & qos) const
{
rcl_publisher_options_t result = rcl_publisher_get_default_options();
result.allocator = rclcpp::allocator::get_rcl_allocator<void>(*this->get_allocator());
result.allocator = this->get_rcl_allocator();
result.qos = qos.get_rmw_qos_profile();
result.rmw_publisher_options.require_unique_network_flow_endpoints =
this->require_unique_network_flow_endpoints;
Expand Down Expand Up @@ -113,9 +113,26 @@ struct PublisherOptionsWithAllocator : public PublisherOptionsBase
}

private:
using PlainAllocator =
typename std::allocator_traits<Allocator>::template rebind_alloc<char>;

rcl_allocator_t
get_rcl_allocator() const
{
if (!plain_allocator_storage_) {
plain_allocator_storage_ =
std::make_shared<PlainAllocator>(*this->get_allocator());
}
return rclcpp::allocator::get_rcl_allocator<char>(*plain_allocator_storage_);
}

// This is a temporal workaround, to make sure that get_allocator()
// always returns a copy of the same allocator.
mutable std::shared_ptr<Allocator> allocator_storage_;

// This is a temporal workaround, to keep the plain allocator that backs
// up the rcl allocator returned in rcl_publisher_options_t alive.
mutable std::shared_ptr<PlainAllocator> plain_allocator_storage_;
wjwwood marked this conversation as resolved.
Show resolved Hide resolved
};

using PublisherOptions = PublisherOptionsWithAllocator<std::allocator<void>>;
Expand Down
19 changes: 18 additions & 1 deletion rclcpp/include/rclcpp/subscription_options.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ struct SubscriptionOptionsWithAllocator : public SubscriptionOptionsBase
to_rcl_subscription_options(const rclcpp::QoS & qos) const
{
rcl_subscription_options_t result = rcl_subscription_get_default_options();
result.allocator = allocator::get_rcl_allocator<void>(*this->get_allocator());
result.allocator = this->get_rcl_allocator();
result.qos = qos.get_rmw_qos_profile();
result.rmw_subscription_options.ignore_local_publications = this->ignore_local_publications;
result.rmw_subscription_options.require_unique_network_flow_endpoints =
Expand All @@ -139,9 +139,26 @@ struct SubscriptionOptionsWithAllocator : public SubscriptionOptionsBase
}

private:
using PlainAllocator =
typename std::allocator_traits<Allocator>::template rebind_alloc<char>;

rcl_allocator_t
get_rcl_allocator() const
{
if (!plain_allocator_storage_) {
plain_allocator_storage_ =
std::make_shared<PlainAllocator>(*this->get_allocator());
}
return rclcpp::allocator::get_rcl_allocator<char>(*plain_allocator_storage_);
}

// This is a temporal workaround, to make sure that get_allocator()
// always returns a copy of the same allocator.
mutable std::shared_ptr<Allocator> allocator_storage_;

// This is a temporal workaround, to keep the plain allocator that backs
// up the rcl allocator returned in rcl_subscription_options_t alive.
mutable std::shared_ptr<PlainAllocator> plain_allocator_storage_;
};

using SubscriptionOptions = SubscriptionOptionsWithAllocator<std::allocator<void>>;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
static uint32_t num_allocs = 0;
static uint32_t num_deallocs = 0;
// A very simple custom allocator. Counts calls to allocate and deallocate.
template<typename T = void>
template<typename T>
struct MyAllocator
{
public:
Expand All @@ -58,10 +58,7 @@ struct MyAllocator
return nullptr;
}
num_allocs++;
// Use sizeof(char) in place for sizeof(void)
constexpr size_t value_size = sizeof(
typename std::conditional<!std::is_void<T>::value, T, char>::type);
return static_cast<T *>(std::malloc(size * value_size));
return static_cast<T *>(std::malloc(size * sizeof(T)));
}

void deallocate(T * ptr, size_t size)
Expand All @@ -81,6 +78,33 @@ struct MyAllocator
};
};

// Explicit specialization for void
template<>
struct MyAllocator<void>
{
public:
using value_type = void;
using pointer = void *;
using const_pointer = const void *;

MyAllocator() noexcept
{
}

~MyAllocator() noexcept {}

template<typename U>
MyAllocator(const MyAllocator<U> &) noexcept
{
}

template<typename U>
struct rebind
{
typedef MyAllocator<U> other;
};
};

template<typename T, typename U>
constexpr bool operator==(
const MyAllocator<T> &,
Expand Down