From faadc5baed0e173c27373d4183e84aee64a1ac12 Mon Sep 17 00:00:00 2001 From: Abtin Keshavarzian Date: Fri, 20 Jun 2025 19:21:30 -0700 Subject: [PATCH] [message] remove `MessagePool` from the `Message::Metadata` (#11616) This commit simplifies the `Message::Metadata` so that it no longer tracks the `MessagePool`. Instead the `Message` now tracks the `ot::Instance` it is associated with and acts as a `GetProvider`, allowing access to any component within `Instance`, including the `MessagePool`. --- src/core/common/locator.hpp | 11 ++++++++++- src/core/common/message.cpp | 15 +++++++++------ src/core/common/message.hpp | 27 ++++++++++++++------------- 3 files changed, 33 insertions(+), 20 deletions(-) diff --git a/src/core/common/locator.hpp b/src/core/common/locator.hpp index 8b1f82217..c05ed718c 100644 --- a/src/core/common/locator.hpp +++ b/src/core/common/locator.hpp @@ -57,6 +57,15 @@ extern uint64_t gInstanceRaw[]; * @{ */ +#if !OPENTHREAD_CONFIG_MULTIPLE_INSTANCE_ENABLE +/** + * Gets the single OpenThread instance. + * + * @returns The single OpenThread instance. + */ +inline Instance &GetSingleInstance(void) { return *reinterpret_cast(&gInstanceRaw); } +#endif + /** * Implements `Get()` method for different `Type` objects belonging to the OpenThread * instance. @@ -110,7 +119,7 @@ public: #if OPENTHREAD_CONFIG_MULTIPLE_INSTANCE_ENABLE Instance &GetInstance(void) const { return *mInstance; } #else - Instance &GetInstance(void) const { return *reinterpret_cast(&gInstanceRaw); } + Instance &GetInstance(void) const { return GetSingleInstance(); } #endif protected: diff --git a/src/core/common/message.cpp b/src/core/common/message.cpp index 45be0d99e..54a28d8c4 100644 --- a/src/core/common/message.cpp +++ b/src/core/common/message.cpp @@ -66,7 +66,10 @@ Message *MessagePool::Allocate(Message::Type aType, uint16_t aReserveHeader, con VerifyOrExit((message = static_cast(NewBuffer(aSettings.GetPriority()))) != nullptr); ClearAllBytes(*message); - message->SetMessagePool(this); + +#if OPENTHREAD_CONFIG_MULTIPLE_INSTANCE_ENABLE + message->GetMetadata().mInstance = &GetInstance(); +#endif message->SetType(aType); message->SetReserved(aReserveHeader); message->SetLinkSecurityEnabled(aSettings.IsLinkSecurityEnabled()); @@ -229,7 +232,7 @@ Error Message::ResizeMessage(uint16_t aLength) { if (curBuffer->GetNextBuffer() == nullptr) { - curBuffer->SetNextBuffer(GetMessagePool()->NewBuffer(GetPriority())); + curBuffer->SetNextBuffer(Get().NewBuffer(GetPriority())); VerifyOrExit(curBuffer->GetNextBuffer() != nullptr, error = kErrorNoBufs); } @@ -241,13 +244,13 @@ Error Message::ResizeMessage(uint16_t aLength) curBuffer = curBuffer->GetNextBuffer(); lastBuffer->SetNextBuffer(nullptr); - GetMessagePool()->FreeBuffers(curBuffer); + Get().FreeBuffers(curBuffer); exit: return error; } -void Message::Free(void) { GetMessagePool()->Free(this); } +void Message::Free(void) { Get().Free(this); } Message *Message::GetNext(void) const { @@ -421,7 +424,7 @@ Error Message::PrependBytes(const void *aBuf, uint16_t aLength) while (aLength > GetReserved()) { - VerifyOrExit((newBuffer = GetMessagePool()->NewBuffer(GetPriority())) != nullptr, error = kErrorNoBufs); + VerifyOrExit((newBuffer = Get().NewBuffer(GetPriority())) != nullptr, error = kErrorNoBufs); newBuffer->SetNextBuffer(GetNextBuffer()); SetNextBuffer(newBuffer); @@ -769,7 +772,7 @@ Message *Message::Clone(uint16_t aLength) const uint16_t offset; aLength = Min(GetLength(), aLength); - messageCopy = GetMessagePool()->Allocate(GetType(), GetReserved(), settings); + messageCopy = Get().Allocate(GetType(), GetReserved(), settings); VerifyOrExit(messageCopy != nullptr, error = kErrorNoBufs); SuccessOrExit(error = messageCopy->AppendBytesFromMessage(*this, 0, aLength)); diff --git a/src/core/common/message.hpp b/src/core/common/message.hpp index bc372f9f0..177fe2ee9 100644 --- a/src/core/common/message.hpp +++ b/src/core/common/message.hpp @@ -190,6 +190,9 @@ public: protected: struct Metadata { +#if OPENTHREAD_CONFIG_MULTIPLE_INSTANCE_ENABLE + Instance *mInstance; +#endif bool mDirectTx : 1; // Whether a direct transmission is required. bool mLinkSecurity : 1; // Whether link security is enabled. bool mInPriorityQ : 1; // Whether the message is queued in normal or priority queue. @@ -227,13 +230,12 @@ protected: #if OPENTHREAD_CONFIG_TIME_SYNC_ENABLE int64_t mNetworkTimeOffset; // The time offset to the Thread network time, in microseconds. #endif - TimeMilli mTimestamp; // The message timestamp. - Message *mNext; // Next message in a doubly linked list. - Message *mPrev; // Previous message in a doubly linked list. - MessagePool *mMessagePool; // Message pool for this message. - void *mQueue; // The queue where message is queued (if any). Queue type from `mInPriorityQ`. - RssAverager mRssAverager; // The averager maintaining the received signal strength (RSS) average. - LqiAverager mLqiAverager; // The averager maintaining the Link quality indicator (LQI) average. + TimeMilli mTimestamp; // The message timestamp. + Message *mNext; // Next message in a doubly linked list. + Message *mPrev; // Previous message in a doubly linked list. + void *mQueue; // The queue where message is queued (if any). Queue type from `mInPriorityQ`. + RssAverager mRssAverager; // The averager maintaining the received signal strength (RSS) average. + LqiAverager mLqiAverager; // The averager maintaining the Link quality indicator (LQI) average. #if OPENTHREAD_FTD ChildMask mChildMask; // ChildMask to indicate which sleepy children need to receive this. #endif @@ -479,7 +481,11 @@ public: * * @returns A reference to the `Instance`. */ - Instance &GetInstance(void) const; +#if OPENTHREAD_CONFIG_MULTIPLE_INSTANCE_ENABLE + Instance &GetInstance(void) const { return *GetMetadata().mInstance; } +#else + Instance &GetInstance(void) const { return GetSingleInstance(); } +#endif /** * Frees this message buffer. @@ -1546,9 +1552,6 @@ private: AsConst(this)->GetNextChunk(aLength, static_cast(aChunk)); } - MessagePool *GetMessagePool(void) const { return GetMetadata().mMessagePool; } - void SetMessagePool(MessagePool *aMessagePool) { GetMetadata().mMessagePool = aMessagePool; } - bool IsInAQueue(void) const { return (GetMetadata().mQueue != nullptr); } void SetMessageQueue(MessageQueue *aMessageQueue); void SetPriorityQueue(PriorityQueue *aPriorityQueue); @@ -1916,8 +1919,6 @@ private: uint16_t mMaxAllocated; }; -inline Instance &Message::GetInstance(void) const { return GetMessagePool()->GetInstance(); } - /** * @} */