Skip to content

[message] add clone methods to MessageAllocator - #12704

Merged
jwhui merged 1 commit into
openthread:mainfrom
abtink:msg/allocator-clone
Mar 23, 2026
Merged

[message] add clone methods to MessageAllocator#12704
jwhui merged 1 commit into
openthread:mainfrom
abtink:msg/allocator-clone

Conversation

@abtink

@abtink abtink commented Mar 17, 2026

Copy link
Copy Markdown
Member

This commit adds CloneMessage(), CloneMessageWithoutFooter(), and CloneMessageWithout<Footer>() methods to the MessageAllocator class. These methods simplify creating copies of messages by automatically applying the correct kReservedHeader size. It also updates existing code in CoapBase, Dns::Client, Sntp::Client, and Mle to utilize these new methods.

Additionally, this commit updates the Clone() method in Message to be a template method, accepting a CloneMode to specify whether the cloned message should retain the reserved header or have no reserved header. The documentation for the clone methods has also been updated to clarify which message fields are copied during the cloning process.

@gemini-code-assist

Copy link
Copy Markdown
Contributor

Summary of Changes

Hello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request introduces a new MessageAllocator class designed to standardize and simplify the process of allocating and cloning messages within the system. By centralizing these operations, it reduces boilerplate code and ensures consistent handling of message headers and settings across different network protocols. The change refactors several core components to adopt this new allocator, leading to a cleaner and more maintainable codebase for message management.

Highlights

  • New MessageAllocator Class: Introduced a new MessageAllocator template class (src/core/common/message_allocator.hpp) to centralize message allocation and cloning logic, providing methods like NewMessage, NewNetPriorityMessage, CloneMessage, CloneMessageWithoutFooter, and CloneMessageWithout<Footer>.
  • Refactored Message Allocation: Migrated core components such as Ip6, Icmp, Udp, and CoapBase to inherit from the new MessageAllocator class, simplifying their message creation and cloning methods by leveraging the MessageAllocator's functionality.
  • Simplified API Calls: Removed redundant NewMessage overloads from Ip6, Icmp, Udp, and CoapBase classes, streamlining their interfaces and reducing code duplication.
  • Codebase Updates: Updated existing calls to message allocation and cloning functions across various modules (e.g., CoapBase, Dns::Client, Sntp::Client, Mle, BorderAgent, JoinerRouter, Nat64Translator, Tcp6) to utilize the new MessageAllocator methods.

🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console.

Changelog
  • src/core/BUILD.gn
    • Added common/message_allocator.hpp to the build configuration.
  • src/core/api/ip6_api.cpp
    • Updated otIp6NewMessage to use the simplified NewMessage overload without a reserved size parameter.
  • src/core/api/udp_api.cpp
    • Updated otUdpNewMessage to use the simplified NewMessage overload without a reserved size parameter.
  • src/core/coap/coap.cpp
    • Removed NewMessage and NewPriorityMessage methods.
    • Updated message allocation calls to NewNetPriorityMessage.
    • Replaced direct message cloning with CloneMessageWithout<Request::Metadata> and CloneMessageWithout<ResponseMetadata>.
  • src/core/coap/coap.hpp
    • Included common/message_allocator.hpp.
    • Modified CoapBase to inherit from MessageAllocator.
    • Removed declarations for NewMessage and NewPriorityMessage.
  • src/core/common/message_allocator.hpp
    • Added new file defining ReservedHeaderSize struct and MessageAllocator template class.
    • Implemented NewMessage, NewNetPriorityMessage, CloneMessage, CloneMessageWithoutFooter, and CloneMessageWithout<Footer> methods.
  • src/core/meshcop/border_agent.cpp
    • Updated NewPriorityMessage calls to NewNetPriorityMessage.
  • src/core/meshcop/joiner_router.cpp
    • Updated mSocket.NewMessage call to use the simplified overload.
  • src/core/net/dns_client.cpp
    • Replaced direct message cloning with mSocket.CloneMessage(aQuery).
  • src/core/net/icmp6.cpp
    • Removed NewMessage method.
    • Updated message allocation calls to use the simplified Ip6::NewMessage overloads.
  • src/core/net/icmp6.hpp
    • Included common/message_allocator.hpp.
    • Modified Icmp to inherit from MessageAllocator.
    • Removed declaration for NewMessage.
  • src/core/net/ip6.cpp
    • Removed NewMessage overloads that accepted aReserved parameters.
  • src/core/net/ip6.hpp
    • Included common/message_allocator.hpp.
    • Modified Ip6 to inherit from MessageAllocator.
    • Removed declarations for NewMessage overloads.
  • src/core/net/nat64_translator.cpp
    • Updated NewIp4Message to use the simplified Ip6::NewMessage overload.
  • src/core/net/sntp_client.cpp
    • Replaced direct message cloning with mSocket.CloneMessageWithout<QueryMetadata>(aMessage).
  • src/core/net/tcp6.cpp
    • Updated tcplp_sys_new_message to use the simplified Ip6::NewMessage overload.
  • src/core/net/udp6.cpp
    • Removed NewMessage overloads from Udp::Socket and Udp classes.
  • src/core/net/udp6.hpp
    • Included common/message_allocator.hpp.
    • Modified Udp and Udp::Socket to inherit from MessageAllocator.
    • Removed declarations for NewMessage overloads from Udp and Udp::Socket.
  • src/core/thread/mle.cpp
    • Updated Get<Ip6::Ip6>().NewMessage calls to use the simplified overload.
    • Updated message cloning in Attacher::SendParentRequest to use Get<Mle>().mSocket.CloneMessage(*message).
  • tests/unit/test_checksum.cpp
    • Updated Ip6::Ip6().NewMessage calls in checksum tests to use the simplified overload.
  • tests/unit/test_nat64.cpp
    • Updated Ip6::Ip6().NewMessage calls in NAT64 tests to use the simplified overload.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for GitHub and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Code Review

This pull request introduces a new MessageAllocator class to simplify message creation and cloning, which is a great improvement for code clarity and maintainability. The changes mostly involve adopting this new abstraction across the codebase. The implementation of MessageAllocator looks solid, but I've found a couple of issues. There's a missing header include in the new message_allocator.hpp file which would cause a compilation error. Additionally, a change in nat64_translator.cpp seems to incorrectly reduce the reserved message space, which could lead to runtime failures during IPv4-to-IPv6 translation. I've provided suggestions to address both issues.

Comment thread src/core/common/message_allocator.hpp
Comment thread src/core/net/nat64_translator.cpp
@github-actions

github-actions Bot commented Mar 17, 2026

Copy link
Copy Markdown

Merging #12704 into main

name branch text data bss total
ot-cli-ftd 366a021 482592 860 66540 549992
2598297 482608 860 66540 550008
+/- +16 +0 +0 +16
ot-ncp-ftd 366a021 448484 764 61776 511024
2598297 448516 764 61776 511056
+/- +32 +0 +0 +32
ot-cli-mtd 366a021 374880 764 50972 426616
2598297 374880 764 50972 426616
+/- +0 +0 +0 +0
ot-ncp-mtd 366a021 356012 764 46240 403016
2598297 356028 764 46240 403032
+/- +16 +0 +0 +16
ot-cli-ftd-br 366a021 598720 868 136596 736184
2598297 598736 868 136596 736200
+/- +16 +0 +0 +16
ot-rcp 366a021 63328 568 20804 84700
2598297 63328 568 20804 84700
+/- +0 +0 +0 +0
Library files
name branch text data bss total
libopenthread-ftd.a 366a021 250259 95 40319 290673
2598297 250287 95 40319 290701
+/- +28 +0 +0 +28
libopenthread-cli-ftd.a 366a021 61746 0 8091 69837
2598297 61746 0 8091 69837
+/- +0 +0 +0 +0
libopenthread-ncp-ftd.a 366a021 33581 0 5948 39529
2598297 33581 0 5948 39529
+/- +0 +0 +0 +0
libopenthread-mtd.a 366a021 168750 0 24783 193533
2598297 168774 0 24783 193557
+/- +24 +0 +0 +24
libopenthread-cli-mtd.a 366a021 41455 0 8059 49514
2598297 41455 0 8059 49514
+/- +0 +0 +0 +0
libopenthread-ncp-mtd.a 366a021 25805 0 5948 31753
2598297 25805 0 5948 31753
+/- +0 +0 +0 +0
libopenthread-ftd-br.a 366a021 367132 100 110335 477567
2598297 367160 100 110335 477595
+/- +28 +0 +0 +28
libopenthread-cli-ftd-br.a 366a021 80953 0 8131 89084
2598297 80953 0 8131 89084
+/- +0 +0 +0 +0
libopenthread-rcp.a 366a021 9944 0 5060 15004
2598297 9944 0 5060 15004
+/- +0 +0 +0 +0
libopenthread-radio.a 366a021 19715 0 238 19953
2598297 19715 0 238 19953
+/- +0 +0 +0 +0

@codecov

codecov Bot commented Mar 17, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 83.33333% with 5 lines in your changes missing coverage. Please review.
✅ Project coverage is 69.48%. Comparing base (366a021) to head (4936841).
⚠️ Report is 2 commits behind head on main.

Files with missing lines Patch % Lines
src/core/net/sntp_client.cpp 0.00% 3 Missing ⚠️
src/core/api/message_api.cpp 0.00% 1 Missing ⚠️
src/core/net/ip6.cpp 50.00% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main   #12704      +/-   ##
==========================================
- Coverage   70.60%   69.48%   -1.12%     
==========================================
  Files         665      697      +32     
  Lines       90533    93487    +2954     
==========================================
+ Hits        63921    64964    +1043     
- Misses      26612    28523    +1911     
Files with missing lines Coverage Δ
src/core/coap/coap.cpp 82.08% <100.00%> (+19.70%) ⬆️
src/core/common/message.cpp 93.09% <100.00%> (-1.97%) ⬇️
src/core/common/message.hpp 97.24% <ø> (-0.61%) ⬇️
src/core/common/message_allocator.hpp 100.00% <100.00%> (ø)
src/core/net/dns_client.cpp 81.21% <100.00%> (+3.48%) ⬆️
src/core/net/ip6_mpl.cpp 90.30% <100.00%> (+2.42%) ⬆️
src/core/thread/mle.cpp 85.71% <100.00%> (+1.04%) ⬆️
tests/unit/test_aes.cpp 100.00% <100.00%> (ø)
tests/unit/test_message.cpp 98.47% <100.00%> (ø)
src/core/api/message_api.cpp 45.28% <0.00%> (ø)
... and 2 more

... and 335 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@abtink
abtink force-pushed the msg/allocator-clone branch from c5238eb to 9cf8a1f Compare March 19, 2026 02:13
@abtink abtink changed the title (UNDER WORK) [message] add clone methods to MessageAllocator [message] add clone methods to MessageAllocator Mar 19, 2026
@abtink

abtink commented Mar 19, 2026

Copy link
Copy Markdown
Member Author

/gemini review

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Code Review

This pull request introduces new CloneMessage(), CloneMessageWithoutFooter(), and CloneMessageWithout<Footer>() methods to the MessageAllocator class, simplifying message copying with automatic kReservedHeader size application. It also updates existing code in CoapBase, Dns::Client, Sntp::Client, and Mle to utilize these new methods. Additionally, the Clone() method in Message is updated to be a template method, accepting a CloneMode to specify whether the cloned message should retain the reserved header or have no reserved header. The documentation for the clone methods has also been updated to clarify which message fields are copied during the cloning process. The review focuses on correctness and maintainability, ensuring the new methods are used appropriately and the code adheres to the project's coding style.

Comment thread tests/nexus/platform/nexus_infra_if.cpp
Comment thread src/core/coap/coap.cpp
Comment thread tests/unit/test_message.cpp
Comment thread tests/unit/test_message.cpp
Comment thread src/core/net/ip6.cpp
Comment thread tests/nexus/test_dtls.cpp
Comment thread tests/unit/test_aes.cpp
Comment thread tests/unit/test_message.cpp
Comment thread src/core/common/message_allocator.hpp
Comment thread src/core/common/message.hpp
@abtink

abtink commented Mar 19, 2026

Copy link
Copy Markdown
Member Author

/gemini review

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Code Review

This pull request introduces new CloneMessage(), CloneMessageWithoutFooter(), and CloneMessageWithout<Footer>() methods to the MessageAllocator class for simplifying message copying with correct header sizes. It also updates existing code in CoapBase, Dns::Client, Sntp::Client, and Mle to utilize these new methods. Additionally, the Clone() method in Message is updated to be a template method, accepting a CloneMode to specify whether the cloned message should retain the reserved header or have no reserved header. The documentation for the clone methods has also been updated to clarify which message fields are copied during the cloning process. The review focuses on correctness and maintainability, ensuring proper usage of the new clone methods and adherence to the project's coding style, as defined in the repository's style guide.

Comment thread tests/unit/test_message.cpp
Comment thread src/core/coap/coap.cpp
Comment thread src/core/common/message.hpp
Comment thread src/core/net/ip6.cpp
@abtink
abtink marked this pull request as ready for review March 19, 2026 21:02
@jwhui

jwhui commented Mar 23, 2026

Copy link
Copy Markdown
Member

@abtink , can you help resolve conflicts?

This commit adds `CloneMessage()`, `CloneMessageWithoutFooter()`,
and `CloneMessageWithout<Footer>()` methods to the `MessageAllocator`
class. These methods simplify creating copies of messages by
automatically applying the correct `kReservedHeader` size. It also
updates existing code in `CoapBase`, `Dns::Client`, `Sntp::Client`,
and `Mle` to utilize these new methods.

Additionally, this commit updates the `Clone()` method in `Message`
to be a template method, accepting a `CloneMode` to specify whether
the cloned message should retain the reserved header or have no
reserved header. The documentation for the clone methods has also
been updated to clarify which message fields are copied during the
cloning process.
@abtink
abtink force-pushed the msg/allocator-clone branch from 9cf8a1f to 4936841 Compare March 23, 2026 18:09
@jwhui
jwhui merged commit 78ddbf7 into openthread:main Mar 23, 2026
102 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants