[netif] add helper methods to identify in-use netif address entries (#4520)
diff --git a/src/core/net/netif.cpp b/src/core/net/netif.cpp
index d401d77..6e14a0f 100644
--- a/src/core/net/netif.cpp
+++ b/src/core/net/netif.cpp
@@ -90,15 +90,13 @@
{
for (NetifUnicastAddress *entry = &mExtUnicastAddresses[0]; entry < OT_ARRAY_END(mExtUnicastAddresses); entry++)
{
- // To mark the address as unused/available, set the `mNext` to point back to itself.
- entry->mNext = entry;
+ entry->MarkAsNotInUse();
}
for (NetifMulticastAddress *entry = &mExtMulticastAddresses[0]; entry < OT_ARRAY_END(mExtMulticastAddresses);
entry++)
{
- // To mark the address as unused/available, set the `mNext` to point back to itself.
- entry->mNext = entry;
+ entry->MarkAsNotInUse();
}
}
@@ -342,13 +340,11 @@
VerifyOrExit(aIterator < num);
- // Find an available entry in the `mExtMulticastAddresses` array.
for (uint8_t i = aIterator; i < num; i++)
{
const NetifMulticastAddress &entry = mExtMulticastAddresses[i];
- // In an unused/available entry, `mNext` points back to the entry itself.
- if (entry.mNext != &entry)
+ if (entry.IsInUse())
{
aAddress = entry.GetAddress();
aIterator = i + 1;
@@ -378,11 +374,9 @@
VerifyOrExit(!IsMulticastSubscribed(aAddress), error = OT_ERROR_ALREADY);
- // Find an available entry in the `mExtMulticastAddresses` array.
for (entry = &mExtMulticastAddresses[0]; entry < OT_ARRAY_END(mExtMulticastAddresses); entry++)
{
- // In an unused/available entry, `mNext` points back to the entry itself.
- if (entry->mNext == entry)
+ if (!entry->IsInUse())
{
break;
}
@@ -429,8 +423,7 @@
VerifyOrExit(entry != NULL, error = OT_ERROR_NOT_FOUND);
- // To mark the address entry as unused/available, set the `mNext` pointer back to the entry itself.
- entry->mNext = entry;
+ entry->MarkAsNotInUse();
Get<Notifier>().Signal(OT_CHANGED_IP6_MULTICAST_UNSUBSCRIBED);
@@ -443,8 +436,7 @@
for (NetifMulticastAddress *entry = &mExtMulticastAddresses[0]; entry < OT_ARRAY_END(mExtMulticastAddresses);
entry++)
{
- // In unused entries, the `mNext` points back to the entry itself.
- if (entry->mNext != entry)
+ if (entry->IsInUse())
{
UnsubscribeExternalMulticast(entry->GetAddress());
}
@@ -508,11 +500,9 @@
}
}
- // Find an available entry in the `mExtUnicastAddresses` array.
for (entry = &mExtUnicastAddresses[0]; entry < OT_ARRAY_END(mExtUnicastAddresses); entry++)
{
- // In an unused/available entry, `mNext` points back to the entry itself.
- if (entry->mNext == entry)
+ if (!entry->IsInUse())
{
break;
}
@@ -560,8 +550,7 @@
VerifyOrExit(entry != NULL, error = OT_ERROR_NOT_FOUND);
- // To mark the address entry as unused/available, set the `mNext` pointer back to the entry itself.
- entry->mNext = entry;
+ entry->MarkAsNotInUse();
Get<Notifier>().Signal(OT_CHANGED_IP6_ADDRESS_REMOVED);
@@ -573,8 +562,7 @@
{
for (NetifUnicastAddress *entry = &mExtUnicastAddresses[0]; entry < OT_ARRAY_END(mExtUnicastAddresses); entry++)
{
- // In unused entries, the `mNext` points back to the entry itself.
- if (entry->mNext != entry)
+ if (entry->IsInUse())
{
RemoveExternalUnicastAddress(entry->GetAddress());
}
diff --git a/src/core/net/netif.hpp b/src/core/net/netif.hpp
index 853ab55..1dd4f8e 100644
--- a/src/core/net/netif.hpp
+++ b/src/core/net/netif.hpp
@@ -100,6 +100,12 @@
{
return mScopeOverrideValid ? static_cast<uint8_t>(mScopeOverride) : GetAddress().GetScope();
}
+
+private:
+ // In an unused/available entry (i.e., entry not present in a linked
+ // list), the next pointer is set to point back to the entry itself.
+ bool IsInUse(void) const { return GetNext() != this; }
+ void MarkAsNotInUse(void) { SetNext(this); }
};
/**
@@ -151,6 +157,12 @@
{
return static_cast<NetifMulticastAddress *>(const_cast<otNetifMulticastAddress *>(mNext));
}
+
+private:
+ // In an unused/available entry (i.e., entry not present in a linked
+ // list), the next pointer is set to point back to the entry itself.
+ bool IsInUse(void) const { return GetNext() != this; }
+ void MarkAsNotInUse(void) { mNext = this; }
};
/**