Skip to content

Allocator tutorial: provide get_rcl_allocator() for the pmr allocator (backport #7296) - #7456

Merged
fujitatomoya merged 1 commit into
lyricalfrom
mergify/bp/lyrical/pr-7296
Nov 11, 2026
Merged

fujitatomoya merged 1 commit into
lyricalfrom
mergify/bp/lyrical/pr-7296

Conversation

@mergify

@mergify mergify Bot commented Nov 11, 2026

Copy link
Copy Markdown
Contributor

Description

Updates the Allocator Template Tutorial to match the pmr demo in ros2/demos (demo_nodes_cpp/src/topics/allocator_tutorial_pmr.cpp), which now gives its allocator a get_rcl_allocator() member.

Without that member, rclcpp falls back to a generic C++-to-rcl allocator conversion that passes a wrong size to deallocate (see ros2/rclcpp#3058). That fallback is deprecated, so the tutorial's code produced this warning when built against current Rolling:

rclcpp/publisher_options.hpp:137:35: warning: 'get_rcl_allocator<char, std::pmr::polymorphic_allocator<char>, nullptr>' is deprecated: Conversion of C++ allocators to C style is not valid, as the size on deallocatecan not be determined. This will be remove in future versions of ros. To suppress this warningdefine the method 'rcl_allocator_t get_rcl_allocator()' on your allocator [-Wdeprecated-declarations]

The same warning is raised from subscription_options.hpp and message_memory_strategy.hpp.

  • Adds a "Providing an allocator for rcl" section. rclcpp is built on rcl, which also allocates memory, so rclcpp needs a C-style rcl_allocator_t to send those allocations to the custom allocator too. The section explains why the callbacks cannot just forward to the C++ allocator: rcl's deallocate does not pass the block size, but std::pmr::memory_resource::deallocate needs it.
  • Shows the full PmrAllocator implementation from the demo: a thin subclass of std::pmr::polymorphic_allocator whose get_rcl_allocator() stores each block's size in a header in front of it. The callbacks are noexcept and return nullptr on failure, like malloc.
  • Changes the example's Alloc alias from std::pmr::polymorphic_allocator<void> to PmrAllocator<void>.

Fixes # (issue)

Did you use Generative AI?

Yes. This PR description was AI-generated with Claude Code (Claude Opus 5.5), and Claude Code (Claude Opus 5.5) also helped write the documentation changes.

Additional Information

Companion change in ros2/demos: ros2/demos#817

Checked locally with the repository's CI targets: make test (doc8), make lint (sphinx-lint), make spellcheck (codespell) and make html (Sphinx with warnings treated as errors).

🤖 Generated with Claude Code


This is an automatic backport of pull request #7296 done by [Mergify](https://mergify.com).

…#7296)

* Allocator tutorial: provide get_rcl_allocator() for the pmr allocator

rclcpp falls back to a deprecated C++-to-rcl allocator conversion when the
allocator has no get_rcl_allocator() member; that conversion passes a wrong
size to deallocate (see ros2/rclcpp#3058). Explain why rcl needs its own
allocator, show a std::pmr::polymorphic_allocator wrapper that provides one
by storing the block size in a header, and use it in the example main.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: Maurice <[email protected]>

* Allocator tutorial: show the full PmrAllocator implementation

Replace the elided allocate/deallocate bodies with the complete code from
demo_nodes_cpp's allocator_tutorial_pmr.cpp, including reallocate_block,
zero_allocate_block and the required includes, and drop the note about
the fallback conversion.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: Maurice <[email protected]>

* Apply batched suggestions from code review

Co-authored-by: Katherine Scott <[email protected]>
Signed-off-by: Maurice Alexander Purnawan <[email protected]>

* Clarify allocator tutorial code and fix sentence lint

Signed-off-by: Maurice <[email protected]>

---------

Signed-off-by: Maurice <[email protected]>
Signed-off-by: Maurice Alexander Purnawan <[email protected]>
Co-authored-by: Claude Opus 5.5 <[email protected]>
Co-authored-by: Katherine Scott <[email protected]>
Co-authored-by: Tomoya Fujita <[email protected]>
(cherry picked from commit 2108c88)
@fujitatomoya
fujitatomoya enabled auto-merge (squash) November 11, 2026 06:09
@github-actions

Copy link
Copy Markdown

HTML artifacts: https://github.com/ros2/ros2_documentation/actions/runs/42970411556/artifacts/13832529587.

To view the resulting site:

  1. Click on the above link to download the artifacts archive
  2. Extract it
  3. Open html-artifacts-7456/index.html in your favorite browser

@fujitatomoya
fujitatomoya merged commit 31761c8 into lyrical Nov 11, 2026
6 checks passed
@fujitatomoya
fujitatomoya deleted the mergify/bp/lyrical/pr-7296 branch November 11, 2026 06:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants