镜像站点 · 本页由第三方 GitHub 只读镜像提供,非 GitHub 官方站点,不接受任何登录或凭据输入。前往 github.com
Skip to content

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

Open
mini-1235 wants to merge 6 commits into
ros2:rollingfrom
mini-1235:allocator-tutorial-get-rcl-allocator
Open

mini-1235 wants to merge 6 commits into
ros2:rollingfrom
mini-1235:allocator-tutorial-get-rcl-allocator

Conversation

@mini-1235

@mini-1235 mini-1235 commented Oct 12, 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

mini-1235 and others added 2 commits October 12, 2026 14:56
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 <noreply@anthropic.com>
Signed-off-by: Maurice <mauricepurnawan@gmail.com>
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 <noreply@anthropic.com>
Signed-off-by: Maurice <mauricepurnawan@gmail.com>
@mini-1235
mini-1235 force-pushed the allocator-tutorial-get-rcl-allocator branch from 75d4866 to 9c6683c Compare October 12, 2026 06:59

@fujitatomoya fujitatomoya left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm

@fujitatomoya

Copy link
Copy Markdown
Collaborator

@3di-for-openrobotics @keithkirkwood-3di @kacperbojakowski-3di @sarahfarnborough-3di

sorry, gotta need to ask some help here.

#6981 really make it difficult to complete the action and merge the PR. it does not really tell what went wrong and how to fix though, can you have a look?

@kacperbojakowski-3di

Copy link
Copy Markdown
Contributor

@3di-for-openrobotics @keithkirkwood-3di @kacperbojakowski-3di @sarahfarnborough-3di

sorry, gotta need to ask some help here.

#6981 really make it difficult to complete the action and merge the PR. it does not really tell what went wrong and how to fix though, can you have a look?

@fujitatomoya Hi! Yes, the solution causes PRs to fail, here is a hotfix to address that: #7294

…al-get-rcl-allocator

Signed-off-by: Maurice <mauricepurnawan@gmail.com>
github-actions[bot]

This comment was marked as outdated.

@kscottz kscottz left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mini-1235 appreciate the help! This is really helpful content but the grammar and code need some work to make this complex topic a bit more accessible to the reader.

I made some inline suggestions, but an easier solution would be to just pipe the change through an LLM to tighten up the language and add comments to most of the code.


For ``std::pmr``, a small wrapper around ``std::pmr::polymorphic_allocator`` can store the size of each block in a header in front of it:

.. code-block:: c++

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

CHORE: this is fairly complex code. Can you please get your favorite LLM to generate line-by-line comments to walk the user through the process?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I asked an LLM to do that, but the result felt a bit overwhelming to read, so I decided to keep only the key comments in 46236c3. Let me know what you think!

Co-authored-by: Katherine Scott <katherineAScott@gmail.com>
Signed-off-by: Maurice Alexander Purnawan <mauricepurnawan@gmail.com>
github-actions[bot]

This comment was marked as outdated.

Signed-off-by: Maurice <mauricepurnawan@gmail.com>
github-actions[bot]

This comment was marked as outdated.

@github-actions github-actions Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Documentation enhancements

This pull request is missing configured documentation enhancements or uses values outside the configured vocabulary (see tools/enhance.yaml).

source/ROS-Framework/client-libraries/Working-with-Client-Libraries/Allocator-Template-Tutorial.rst

  • Invalid .. meta:: values: experience (optional, unknown advanced; allowed: beginner, intermediate, expert; capitalisation is ignored)

This branch has not been deployed

No deployments
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.

4 participants