From 44f5cddc2e40efe61d0454762666680a26ce724c Mon Sep 17 00:00:00 2001 From: Abtin Keshavarzian Date: Tue, 17 Mar 2026 16:50:39 -0700 Subject: [PATCH] [message] introduce `MessageAllocator` to unify allocation (#12702) This commit introduces the `MessageAllocator` template class using the CRTP pattern to provide a unified implementation of the `NewMessage()` methods. It standardizes the reserved header sizes for different message types within `ReservedHeaderSize`. This removes boilerplate code and redundant `NewMessage()` method implementations across the `Ip6`, `Icmp`, `Udp`, `Udp::Socket`, and `CoapBase` classes. --- src/core/BUILD.gn | 1 + src/core/api/ip6_api.cpp | 2 +- src/core/api/udp_api.cpp | 2 +- src/core/coap/coap.cpp | 27 +---- src/core/coap/coap.hpp | 29 +----- src/core/common/message_allocator.hpp | 142 ++++++++++++++++++++++++++ src/core/meshcop/border_agent.cpp | 4 +- src/core/meshcop/joiner_router.cpp | 2 +- src/core/net/icmp6.cpp | 6 +- src/core/net/icmp6.hpp | 12 +-- src/core/net/ip6.cpp | 10 -- src/core/net/ip6.hpp | 31 +----- src/core/net/nat64_translator.cpp | 2 +- src/core/net/tcp6.cpp | 2 +- src/core/net/udp6.cpp | 18 ---- src/core/net/udp6.hpp | 59 +---------- src/core/thread/mle.cpp | 4 +- tests/unit/test_checksum.cpp | 10 +- tests/unit/test_nat64.cpp | 4 +- 19 files changed, 182 insertions(+), 185 deletions(-) create mode 100644 src/core/common/message_allocator.hpp diff --git a/src/core/BUILD.gn b/src/core/BUILD.gn index 0c9386147..ff3091be0 100644 --- a/src/core/BUILD.gn +++ b/src/core/BUILD.gn @@ -462,6 +462,7 @@ openthread_core_files = [ "common/logging.hpp", "common/message.cpp", "common/message.hpp", + "common/message_allocator.hpp", "common/msg_backed_array.hpp", "common/non_copyable.hpp", "common/notifier.cpp", diff --git a/src/core/api/ip6_api.cpp b/src/core/api/ip6_api.cpp index 89c22f0d6..7e05a6cb1 100644 --- a/src/core/api/ip6_api.cpp +++ b/src/core/api/ip6_api.cpp @@ -132,7 +132,7 @@ exit: otMessage *otIp6NewMessage(otInstance *aInstance, const otMessageSettings *aSettings) { - return AsCoreType(aInstance).Get().NewMessage(0, Message::Settings::From(aSettings)); + return AsCoreType(aInstance).Get().NewMessage(Message::Settings::From(aSettings)); } otMessage *otIp6NewMessageFromBuffer(otInstance *aInstance, diff --git a/src/core/api/udp_api.cpp b/src/core/api/udp_api.cpp index dde53dc76..dcca993fc 100644 --- a/src/core/api/udp_api.cpp +++ b/src/core/api/udp_api.cpp @@ -39,7 +39,7 @@ using namespace ot; otMessage *otUdpNewMessage(otInstance *aInstance, const otMessageSettings *aSettings) { - return AsCoreType(aInstance).Get().NewMessage(0, Message::Settings::From(aSettings)); + return AsCoreType(aInstance).Get().NewMessage(Message::Settings::From(aSettings)); } otError otUdpOpen(otInstance *aInstance, otUdpSocket *aSocket, otUdpReceive aCallback, void *aContext) diff --git a/src/core/coap/coap.cpp b/src/core/coap/coap.cpp index c7491b4dd..1564ee530 100644 --- a/src/core/coap/coap.cpp +++ b/src/core/coap/coap.cpp @@ -145,27 +145,9 @@ void CoapBase::RemoveResource(Resource &aResource) aResource.SetNext(nullptr); } -Message *CoapBase::NewMessage(const Message::Settings &aSettings) -{ - Message *message = nullptr; - - VerifyOrExit((message = AsCoapMessagePtr(Get().NewMessage(0, aSettings))) != nullptr); - message->SetOffset(0); - -exit: - return message; -} - -Message *CoapBase::NewMessage(void) { return NewMessage(Message::Settings::GetDefault()); } - -Message *CoapBase::NewPriorityMessage(void) -{ - return NewMessage(Message::Settings(kWithLinkSecurity, Message::kPriorityNet)); -} - Message *CoapBase::AllocateAndInitPriorityConfirmablePostMessage(Uri aUri) { - return InitMessage(NewPriorityMessage(), kTypeConfirmable, aUri); + return InitMessage(NewNetPriorityMessage(), kTypeConfirmable, aUri); } Message *CoapBase::AllocateAndInitConfirmablePostMessage(Uri aUri) @@ -175,7 +157,7 @@ Message *CoapBase::AllocateAndInitConfirmablePostMessage(Uri aUri) Message *CoapBase::AllocateAndInitPriorityNonConfirmablePostMessage(Uri aUri) { - return InitMessage(NewPriorityMessage(), kTypeNonConfirmable, aUri); + return InitMessage(NewNetPriorityMessage(), kTypeNonConfirmable, aUri); } Message *CoapBase::AllocateAndInitNonConfirmablePostMessage(Uri aUri) @@ -190,12 +172,13 @@ Message *CoapBase::AllocateAndInitPostMessageTo(Uri aUri, const Ip6::Address &aD Message *CoapBase::AllocateAndInitPriorityPostMessageTo(Uri aUri, const Ip6::Address &aDestination) { - return InitMessage(NewPriorityMessage(), aDestination.IsMulticast() ? kTypeNonConfirmable : kTypeConfirmable, aUri); + return InitMessage(NewNetPriorityMessage(), aDestination.IsMulticast() ? kTypeNonConfirmable : kTypeConfirmable, + aUri); } Message *CoapBase::AllocateAndInitPriorityResponseFor(const Message &aRequest) { - return InitResponse(NewPriorityMessage(), aRequest); + return InitResponse(NewNetPriorityMessage(), aRequest); } Message *CoapBase::AllocateAndInitResponseFor(const Message &aRequest) { return InitResponse(NewMessage(), aRequest); } diff --git a/src/core/coap/coap.hpp b/src/core/coap/coap.hpp index 804a6febf..dedf5c59b 100644 --- a/src/core/coap/coap.hpp +++ b/src/core/coap/coap.hpp @@ -40,6 +40,7 @@ #include "common/linked_list.hpp" #include "common/locator.hpp" #include "common/message.hpp" +#include "common/message_allocator.hpp" #include "common/non_copyable.hpp" #include "common/owned_ptr.hpp" #include "common/timer.hpp" @@ -288,7 +289,10 @@ protected: /** * Implements the CoAP client and server. */ -class CoapBase : public InstanceLocator, private NonCopyable +class CoapBase + : public InstanceLocator, + public MessageAllocator, + private NonCopyable { public: /** @@ -345,29 +349,6 @@ public: */ void SetResponseFallback(ResponseFallback aHandler, void *aContext) { mResponseFallback.Set(aHandler, aContext); } - /** - * Allocates a new message with a CoAP header. - * - * @param[in] aSettings The message settings. - * - * @returns A pointer to the message or `nullptr` if failed to allocate message. - */ - Message *NewMessage(const Message::Settings &aSettings); - - /** - * Allocates a new message with a CoAP header with default settings. - * - * @returns A pointer to the message or `nullptr` if failed to allocate message. - */ - Message *NewMessage(void); - - /** - * Allocates a new message with a CoAP header that has Network Control priority level. - * - * @returns A pointer to the message or `nullptr` if failed to allocate message. - */ - Message *NewPriorityMessage(void); - /** * Allocates and initializes a new CoAP Confirmable Post message with Network Control priority level. * diff --git a/src/core/common/message_allocator.hpp b/src/core/common/message_allocator.hpp new file mode 100644 index 000000000..7f47c0d4f --- /dev/null +++ b/src/core/common/message_allocator.hpp @@ -0,0 +1,142 @@ +/* + * Copyright (c) 2026, The OpenThread Authors. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of the copyright holder nor the + * names of its contributors may be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +/** + * @file + * This file includes definitions for the message allocator. + */ + +#ifndef OT_CORE_COMMON_MESSAGE_ALLOCATOR_HPP_ +#define OT_CORE_COMMON_MESSAGE_ALLOCATOR_HPP_ + +#include "openthread-core-config.h" + +#include "common/message.hpp" +#include "net/ip6_headers.hpp" + +namespace ot { + +/** + * @addtogroup core-message + * + * @brief + * This module includes definitions for the message allocator base class. + * + * @{ + */ + +/** + * Defines constants for the reserved header sizes for different message types. + */ +struct ReservedHeaderSize +{ + /** + * The reserved header size for an IPv6 message. + */ + static constexpr uint16_t kIp6Message = sizeof(Ip6::Header) + sizeof(Ip6::HopByHopHeader) + sizeof(Ip6::MplOption); + + /** + * The reserved header size for a UDP/IPv6 message + */ + static constexpr uint16_t kUdpMessage = kIp6Message + sizeof(Ip6::UdpHeader); + + /** + * The reserved header size for an ICMPv6 message. + */ + static constexpr uint16_t kIcmp6Message = kIp6Message + sizeof(Ip6::Icmp6Header); + + /** + * The reserved header size for a CoAP message. + */ + static constexpr uint16_t kCoapMessage = kUdpMessage; +}; + +/** + * Defines a `MessageAllocator` object which provides `NewMessage` methods with a fixed reserved header size. + * + * Users of this class should follow CRTP-style inheritance, i.e., the `Type` class itself should publicly inherit + * from `MessageAllocator`. + * + * @tparam Type The type of the class that inherits from this one (CRTP). + * @tparam kReservedHeader The number of header bytes to reserve. + * @tparam kType The `Message::Type` value to use for the allocated message. + * @tparam MessageType The allocated message's type. MUST be sub-class of `ot::Message` (e.g. `Coap::Message`). + */ +template +class MessageAllocator +{ +public: + /** + * Allocates a new message with default settings (link security enabled and `kPriorityNormal`) and the + * `kReservedHeader` reserved header size. + * + * @returns A pointer to the message or `nullptr` if no buffers are available. + */ + MessageType *NewMessage(void) + { + return AsMessageType(static_cast(this)->template Get().Allocate(kType, kReservedHeader)); + } + + /** + * Allocates a new message with given settings and the `kReservedHeader` reserved header size. + * + * @param[in] aSettings The message settings. + * + * @returns A pointer to the message or `nullptr` if no buffers are available. + */ + MessageType *NewMessage(const Message::Settings &aSettings) + { + return AsMessageType( + static_cast(this)->template Get().Allocate(kType, kReservedHeader, aSettings)); + } + + /** + * Allocates a new message with link security enabled and `kPriorityNet` and the `kReservedHeader` reserved header + * size. + * + * @returns A pointer to the message or `nullptr` if no buffers are available. + */ + MessageType *NewNetPriorityMessage(void) { return NewMessage(Message::Settings(Message::kPriorityNet)); } + +protected: + MessageAllocator(void) = default; + +private: + static MessageType *AsMessageType(Message *aMessage) { return static_cast(aMessage); } +}; + +/** + * @} + */ + +} // namespace ot + +#endif // OT_CORE_COMMON_MESSAGE_ALLOCATOR_HPP_ diff --git a/src/core/meshcop/border_agent.cpp b/src/core/meshcop/border_agent.cpp index 3f9664f1d..dc55253a7 100644 --- a/src/core/meshcop/border_agent.cpp +++ b/src/core/meshcop/border_agent.cpp @@ -769,7 +769,7 @@ void Manager::CoapDtlsSession::HandleLeaderResponseToFwdTmf(const ForwardContext SuccessOrExit(error = aResult); - forwardMessage.Reset(NewPriorityMessage()); + forwardMessage.Reset(NewNetPriorityMessage()); VerifyOrExit(forwardMessage != nullptr, error = kErrorNoBufs); if (aResponse->GetCode() == Coap::kCodeChanged) @@ -926,7 +926,7 @@ void Manager::CoapDtlsSession::SendErrorMessage(Error aError, const Coap::Token OwnedPtr message; Coap::Message::Code code; - message.Reset(NewPriorityMessage()); + message.Reset(NewNetPriorityMessage()); VerifyOrExit(message != nullptr, error = kErrorNoBufs); code = (aError == kErrorParse) ? Coap::kCodeBadRequest : Coap::kCodeInternalError; diff --git a/src/core/meshcop/joiner_router.cpp b/src/core/meshcop/joiner_router.cpp index 11d85bb4e..21060bb48 100644 --- a/src/core/meshcop/joiner_router.cpp +++ b/src/core/meshcop/joiner_router.cpp @@ -168,7 +168,7 @@ template <> void JoinerRouter::HandleTmf(Coap::Msg &aMsg) SuccessOrExit(error = Tlv::FindTlvValueOffsetRange(aMsg.mMessage, Tlv::kJoinerDtlsEncapsulation, offsetRange)); - VerifyOrExit((message = mSocket.NewMessage(0, settings)) != nullptr, error = kErrorNoBufs); + VerifyOrExit((message = mSocket.NewMessage(settings)) != nullptr, error = kErrorNoBufs); SuccessOrExit(error = message->AppendBytesFromMessage(aMsg.mMessage, offsetRange)); diff --git a/src/core/net/icmp6.cpp b/src/core/net/icmp6.cpp index f5c0b6e75..b1600021f 100644 --- a/src/core/net/icmp6.cpp +++ b/src/core/net/icmp6.cpp @@ -47,8 +47,6 @@ Icmp::Icmp(Instance &aInstance) { } -Message *Icmp::NewMessage(void) { return Get().NewMessage(sizeof(Header)); } - Error Icmp::RegisterHandler(Handler &aHandler) { return mHandlers.Add(aHandler); } Error Icmp::UnregisterHandler(Handler &aHandler) { return mHandlers.Remove(aHandler); } @@ -103,7 +101,7 @@ Error Icmp::SendError(Header::Type aType, Header::Code aCode, const MessageInfo messageInfoLocal = aMessageInfo; - VerifyOrExit((message = Get().NewMessage(0, settings)) != nullptr, error = kErrorNoBufs); + VerifyOrExit((message = Get().NewMessage(settings)) != nullptr, error = kErrorNoBufs); // Prepare the ICMPv6 error message. We only include the IPv6 header // of the original message causing the error. @@ -189,7 +187,7 @@ Error Icmp::HandleEchoRequest(Message &aRequestMessage, const MessageInfo &aMess icmp6Header.Clear(); icmp6Header.SetType(Header::kTypeEchoReply); - if ((replyMessage = Get().NewMessage(0)) == nullptr) + if ((replyMessage = Get().NewMessage()) == nullptr) { LogDebg("Failed to allocate a new message"); ExitNow(); diff --git a/src/core/net/icmp6.hpp b/src/core/net/icmp6.hpp index 8fe4bdfa6..bfb1f7590 100644 --- a/src/core/net/icmp6.hpp +++ b/src/core/net/icmp6.hpp @@ -43,6 +43,7 @@ #include "common/encoding.hpp" #include "common/linked_list.hpp" #include "common/locator.hpp" +#include "common/message_allocator.hpp" #include "common/non_copyable.hpp" #include "net/ip6_headers.hpp" @@ -63,7 +64,9 @@ class Headers; /** * Implements ICMPv6. */ -class Icmp : public InstanceLocator, private NonCopyable +class Icmp : public InstanceLocator, + public MessageAllocator, + private NonCopyable { public: typedef Icmp6Header Header; ///< ICMPv6 header @@ -103,13 +106,6 @@ public: */ explicit Icmp(Instance &aInstance); - /** - * Returns a new ICMP message with sufficient header space reserved. - * - * @returns A pointer to the message or `nullptr` if no buffers are available. - */ - Message *NewMessage(void); - /** * Registers ICMPv6 handler. * diff --git a/src/core/net/ip6.cpp b/src/core/net/ip6.cpp index 1f3565606..755653f99 100644 --- a/src/core/net/ip6.cpp +++ b/src/core/net/ip6.cpp @@ -66,16 +66,6 @@ Ip6::Ip6(Instance &aInstance) #endif } -Message *Ip6::NewMessage(void) { return NewMessage(0); } - -Message *Ip6::NewMessage(uint16_t aReserved) { return NewMessage(aReserved, Message::Settings::GetDefault()); } - -Message *Ip6::NewMessage(uint16_t aReserved, const Message::Settings &aSettings) -{ - return Get().Allocate( - Message::kTypeIp6, sizeof(Header) + sizeof(HopByHopHeader) + sizeof(MplOption) + aReserved, aSettings); -} - Message *Ip6::NewMessageFromData(const uint8_t *aData, uint16_t aDataLength, const Message::Settings &aSettings) { Message *message = nullptr; diff --git a/src/core/net/ip6.hpp b/src/core/net/ip6.hpp index 733deb422..6cc41b3d8 100644 --- a/src/core/net/ip6.hpp +++ b/src/core/net/ip6.hpp @@ -48,6 +48,7 @@ #include "common/locator.hpp" #include "common/log.hpp" #include "common/message.hpp" +#include "common/message_allocator.hpp" #include "common/non_copyable.hpp" #include "common/owned_ptr.hpp" #include "common/time_ticker.hpp" @@ -102,7 +103,7 @@ namespace Ip6 { /** * Implements the core IPv6 message processing. */ -class Ip6 : public InstanceLocator, private NonCopyable +class Ip6 : public InstanceLocator, public MessageAllocator, private NonCopyable { friend class ot::Instance; friend class ot::TimeTicker; @@ -118,34 +119,6 @@ public: */ explicit Ip6(Instance &aInstance); - /** - * Allocates a new message buffer from the buffer pool with default settings (link security - * enabled and `kPriorityMedium`). - * - * @returns A pointer to the message or `nullptr` if insufficient message buffers are available. - */ - Message *NewMessage(void); - - /** - * Allocates a new message buffer from the buffer pool with default settings (link security - * enabled and `kPriorityMedium`). - * - * @param[in] aReserved The number of header bytes to reserve following the IPv6 header. - * - * @returns A pointer to the message or `nullptr` if insufficient message buffers are available. - */ - Message *NewMessage(uint16_t aReserved); - - /** - * Allocates a new message buffer from the buffer pool. - * - * @param[in] aReserved The number of header bytes to reserve following the IPv6 header. - * @param[in] aSettings The message settings. - * - * @returns A pointer to the message or `nullptr` if insufficient message buffers are available. - */ - Message *NewMessage(uint16_t aReserved, const Message::Settings &aSettings); - /** * Allocates a new message buffer from the buffer pool and writes the IPv6 datagram to the message. * diff --git a/src/core/net/nat64_translator.cpp b/src/core/net/nat64_translator.cpp index f5e6a8fd7..fe0f985c1 100644 --- a/src/core/net/nat64_translator.cpp +++ b/src/core/net/nat64_translator.cpp @@ -76,7 +76,7 @@ Translator::Translator(Instance &aInstance) Message *Translator::NewIp4Message(const Message::Settings &aSettings) { - Message *message = Get().NewMessage(sizeof(Ip6::Header) - sizeof(Ip4::Header), aSettings); + Message *message = Get().NewMessage(aSettings); if (message != nullptr) { diff --git a/src/core/net/tcp6.cpp b/src/core/net/tcp6.cpp index f7a2f852d..3a72826c8 100644 --- a/src/core/net/tcp6.cpp +++ b/src/core/net/tcp6.cpp @@ -946,7 +946,7 @@ extern "C" { otMessage *tcplp_sys_new_message(otInstance *aInstance) { Instance &instance = AsCoreType(aInstance); - Message *message = instance.Get().NewMessage(0); + Message *message = instance.Get().NewMessage(); if (message) { diff --git a/src/core/net/udp6.cpp b/src/core/net/udp6.cpp index cfa3e4f09..1e2ce44f9 100644 --- a/src/core/net/udp6.cpp +++ b/src/core/net/udp6.cpp @@ -87,15 +87,6 @@ Udp::Socket::Socket(Instance &aInstance, ReceiveHandler aHandler, void *aContext mContext = aContext; } -Message *Udp::Socket::NewMessage(void) { return NewMessage(0); } - -Message *Udp::Socket::NewMessage(uint16_t aReserved) { return NewMessage(aReserved, Message::Settings::GetDefault()); } - -Message *Udp::Socket::NewMessage(uint16_t aReserved, const Message::Settings &aSettings) -{ - return Get().NewMessage(aReserved, aSettings); -} - Error Udp::Socket::Open(NetifIdentifier aNetifId) { return Get().Open(*this, aNetifId, mHandler, mContext); } bool Udp::Socket::IsOpen(void) const { return Get().IsOpen(*this); } @@ -405,15 +396,6 @@ uint16_t Udp::GetEphemeralPort(void) return mEphemeralPort; } -Message *Udp::NewMessage(void) { return NewMessage(0); } - -Message *Udp::NewMessage(uint16_t aReserved) { return NewMessage(aReserved, Message::Settings::GetDefault()); } - -Message *Udp::NewMessage(uint16_t aReserved, const Message::Settings &aSettings) -{ - return Get().NewMessage(sizeof(Header) + aReserved, aSettings); -} - Error Udp::SendDatagram(Message &aMessage, MessageInfo &aMessageInfo) { Error error = kErrorNone; diff --git a/src/core/net/udp6.hpp b/src/core/net/udp6.hpp index 2d2ddeeab..b2ecc923a 100644 --- a/src/core/net/udp6.hpp +++ b/src/core/net/udp6.hpp @@ -44,6 +44,7 @@ #include "common/clearable.hpp" #include "common/linked_list.hpp" #include "common/locator.hpp" +#include "common/message_allocator.hpp" #include "common/non_copyable.hpp" #include "net/ip6_headers.hpp" @@ -79,7 +80,7 @@ enum NetifIdentifier : uint8_t /** * Implements core UDP message handling. */ -class Udp : public InstanceLocator, private NonCopyable +class Udp : public InstanceLocator, public MessageAllocator, private NonCopyable { public: typedef UdpHeader Header; ///< UDP header. @@ -175,7 +176,9 @@ public: /** * Implements a UDP/IPv6 socket. */ - class Socket : public InstanceLocator, public SocketHandle + class Socket : public InstanceLocator, + public SocketHandle, + public MessageAllocator { friend class Udp; @@ -189,32 +192,6 @@ public: */ Socket(Instance &aInstance, ReceiveHandler aHandler, void *aContext); - /** - * Returns a new UDP message with default settings (link security enabled and `kPriorityNormal`) - * - * @returns A pointer to the message or `nullptr` if no buffers are available. - */ - Message *NewMessage(void); - - /** - * Returns a new UDP message with default settings (link security enabled and `kPriorityNormal`) - * - * @param[in] aReserved The number of header bytes to reserve after the UDP header. - * - * @returns A pointer to the message or `nullptr` if no buffers are available. - */ - Message *NewMessage(uint16_t aReserved); - - /** - * Returns a new UDP message with sufficient header space reserved. - * - * @param[in] aReserved The number of header bytes to reserve after the UDP header. - * @param[in] aSettings The message settings (default is used if not provided). - * - * @returns A pointer to the message or `nullptr` if no buffers are available. - */ - Message *NewMessage(uint16_t aReserved, const Message::Settings &aSettings); - /** * Opens the UDP socket. * @@ -492,32 +469,6 @@ public: */ uint16_t GetEphemeralPort(void); - /** - * Returns a new UDP message with default settings (link security enabled and `kPriorityNormal`) - * - * @returns A pointer to the message or `nullptr` if no buffers are available. - */ - Message *NewMessage(void); - - /** - * Returns a new UDP message with default settings (link security enabled and `kPriorityNormal`) - * - * @param[in] aReserved The number of header bytes to reserve after the UDP header. - * - * @returns A pointer to the message or `nullptr` if no buffers are available. - */ - Message *NewMessage(uint16_t aReserved); - - /** - * Returns a new UDP message with sufficient header space reserved. - * - * @param[in] aReserved The number of header bytes to reserve after the UDP header. - * @param[in] aSettings The message settings. - * - * @returns A pointer to the message or `nullptr` if no buffers are available. - */ - Message *NewMessage(uint16_t aReserved, const Message::Settings &aSettings); - /** * Sends an IPv6 datagram. * diff --git a/src/core/thread/mle.cpp b/src/core/thread/mle.cpp index 57138bce9..218d1766f 100644 --- a/src/core/thread/mle.cpp +++ b/src/core/thread/mle.cpp @@ -2623,7 +2623,7 @@ void Mle::InformPreviousParent(void) Message *message = nullptr; Ip6::MessageInfo messageInfo; - VerifyOrExit((message = Get().NewMessage(0)) != nullptr, error = kErrorNoBufs); + VerifyOrExit((message = Get().NewMessage()) != nullptr, error = kErrorNoBufs); SuccessOrExit(error = message->SetLength(0)); messageInfo.SetSockAddr(GetMeshLocalEid()); @@ -3455,7 +3455,7 @@ Mle::TxMessage *Mle::NewMleMessage(Command aCommand) Message::Settings settings(kNoLinkSecurity, Message::kPriorityNet); uint8_t securitySuite; - message = static_cast(mSocket.NewMessage(0, settings)); + message = static_cast(mSocket.NewMessage(settings)); VerifyOrExit(message != nullptr, error = kErrorNoBufs); securitySuite = k154Security; diff --git a/tests/unit/test_checksum.cpp b/tests/unit/test_checksum.cpp index 88d9fe42b..2d09071ad 100644 --- a/tests/unit/test_checksum.cpp +++ b/tests/unit/test_checksum.cpp @@ -184,7 +184,7 @@ void TestUdpMessageChecksum(void) for (uint16_t size = kMinSize; size <= kMaxSize; size++) { - Message *message = instance->Get().NewMessage(sizeof(Ip6::Udp::Header)); + Message *message = instance->Get().NewMessage(); Ip6::Udp::Header udpHeader; Ip6::MessageInfo messageInfo; @@ -252,7 +252,7 @@ void TestIcmp6MessageChecksum(void) for (uint16_t size = kMinSize; size <= kMaxSize; size++) { - Message *message = instance->Get().NewMessage(sizeof(Ip6::Icmp::Header)); + Message *message = instance->Get().NewMessage(); Ip6::Icmp::Header icmp6Header; Ip6::MessageInfo messageInfo; @@ -327,7 +327,7 @@ void TestTcp4MessageChecksum(void) for (uint16_t size = kMinSize; size <= kMaxSize; size++) { - Message *message = instance->Get().NewMessage(sizeof(Ip4::Tcp::Header)); + Message *message = instance->Get().NewMessage(); Ip4::Tcp::Header tcpHeader; VerifyOrQuit(message != nullptr, "Ip6::NewMesssage() failed"); @@ -382,7 +382,7 @@ void TestUdp4MessageChecksum(void) for (uint16_t size = kMinSize; size <= kMaxSize; size++) { - Message *message = instance->Get().NewMessage(sizeof(Ip4::Udp::Header)); + Message *message = instance->Get().NewMessage(); Ip4::Udp::Header udpHeader; VerifyOrQuit(message != nullptr, "Ip6::NewMesssage() failed"); @@ -427,7 +427,7 @@ void TestIcmp4MessageChecksum(void) "\x28\x29\x2a\x2b\x2c\x2d\x2e\x2f\x30\x31\x32\x33\x34\x35\x36\x37"; uint16_t kChecksumForExampleMessage = 0x5594; Instance *instance = static_cast(testInitInstance()); - Message *message = instance->Get().NewMessage(sizeof(kExampleIcmpMessage)); + Message *message = instance->Get().NewMessage(); Ip4::Address source; Ip4::Address dest; diff --git a/tests/unit/test_nat64.cpp b/tests/unit/test_nat64.cpp index 0f2bad71c..b05e32653 100644 --- a/tests/unit/test_nat64.cpp +++ b/tests/unit/test_nat64.cpp @@ -117,7 +117,7 @@ void Verify6To4(const char *aTestName, uint16_t aIp4Length, Error aError) { - Message *message = sInstance->Get().NewMessage(0); + Message *message = sInstance->Get().NewMessage(); Error error; Log("- - - - - - - - - - - - - - - - - - - - - - - - - "); @@ -161,7 +161,7 @@ void Verify4To6(const char *aTestName, uint16_t aIp6Length, Error aError) { - Message *message = sInstance->Get().NewMessage(0); + Message *message = sInstance->Get().NewMessage(); Error error; Log("- - - - - - - - - - - - - - - - - - - - - - - - - ");