Skip to content

Commit

Permalink
Revert "Replacing old-style C++ allocator with a polymorphic memory r…
Browse files Browse the repository at this point in the history
…esource (PMR) (#653)"

This reverts commit 49bbb52.
  • Loading branch information
clalancette committed Oct 13, 2023
1 parent 49bbb52 commit 14d4681
Showing 1 changed file with 53 additions and 20 deletions.
73 changes: 53 additions & 20 deletions demo_nodes_cpp/src/topics/allocator_tutorial.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
#include <chrono>
#include <list>
#include <memory>
#include <memory_resource>
#include <string>
#include <utility>

Expand All @@ -29,34 +28,69 @@ using namespace std::chrono_literals;
// For demonstration purposes only, not necessary for allocator_traits
static uint32_t num_allocs = 0;
static uint32_t num_deallocs = 0;
// A very simple custom memory resource. Counts calls to do_allocate and do_deallocate.
class CustomMemoryResource : public std::pmr::memory_resource
// A very simple custom allocator. Counts calls to allocate and deallocate.
template<typename T = void>
struct MyAllocator
{
private:
void * do_allocate(std::size_t bytes, std::size_t alignment) override
public:
using value_type = T;
using size_type = std::size_t;
using pointer = T *;
using const_pointer = const T *;
using difference_type = typename std::pointer_traits<pointer>::difference_type;

MyAllocator() noexcept
{
}

~MyAllocator() noexcept {}

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

T * allocate(size_t size, const void * = 0)
{
if (size == 0) {
return nullptr;
}
num_allocs++;
(void)alignment;
return std::malloc(bytes);
return static_cast<T *>(std::malloc(size * sizeof(T)));
}

void do_deallocate(
void * p, std::size_t bytes,
std::size_t alignment) override
void deallocate(T * ptr, size_t size)
{
(void)size;
if (!ptr) {
return;
}
num_deallocs++;
(void)bytes;
(void)alignment;
std::free(p);
std::free(ptr);
}

bool do_is_equal(
const std::pmr::memory_resource & other) const noexcept override
template<typename U>
struct rebind
{
return this == &other;
}
typedef MyAllocator<U> other;
};
};

template<typename T, typename U>
constexpr bool operator==(
const MyAllocator<T> &,
const MyAllocator<U> &) noexcept
{
return true;
}

template<typename T, typename U>
constexpr bool operator!=(
const MyAllocator<T> &,
const MyAllocator<U> &) noexcept
{
return false;
}

// Override global new and delete to count calls during execution.

Expand Down Expand Up @@ -96,7 +130,7 @@ void operator delete(void * ptr) noexcept
int main(int argc, char ** argv)
{
using rclcpp::memory_strategies::allocator_memory_strategy::AllocatorMemoryStrategy;
using Alloc = std::pmr::polymorphic_allocator<void>;
using Alloc = MyAllocator<void>;
using MessageAllocTraits =
rclcpp::allocator::AllocRebind<std_msgs::msg::UInt32, Alloc>;
using MessageAlloc = MessageAllocTraits::allocator_type;
Expand Down Expand Up @@ -150,8 +184,7 @@ int main(int argc, char ** argv)
};

// Create a custom allocator and pass the allocator to the publisher and subscriber.
CustomMemoryResource mem_resource{};
auto alloc = std::make_shared<Alloc>(&mem_resource);
auto alloc = std::make_shared<Alloc>();
rclcpp::PublisherOptionsWithAllocator<Alloc> publisher_options;
publisher_options.allocator = alloc;
auto publisher = node->create_publisher<std_msgs::msg::UInt32>(
Expand Down

0 comments on commit 14d4681

Please sign in to comment.