[code-utils] adding helper macro OT_ARRAY_LENGTH() (#2743)

This commit adds helper macro `OT_ARRAY_LENGTH()` to get the number
of elements in an array.
This commit is contained in:
Abtin Keshavarzian
2018-05-29 15:53:53 -07:00
committed by Jonathan Hui
parent ae7984d3a7
commit 2abd7a2822
16 changed files with 61 additions and 61 deletions
+5 -6
View File
@@ -393,7 +393,7 @@ otError Interpreter::ParseUnsignedLong(char *argv, unsigned long &value)
void Interpreter::ProcessHelp(int argc, char *argv[])
{
for (unsigned int i = 0; i < sizeof(sCommands) / sizeof(sCommands[0]); i++)
for (unsigned int i = 0; i < OT_ARRAY_LENGTH(sCommands); i++)
{
mServer->OutputFormat("%s\r\n", sCommands[i].mName);
}
@@ -3443,7 +3443,7 @@ void Interpreter::ProcessLine(char *aBuf, uint16_t aBufLength, Server &aServer)
mServer->OutputFormat("under diagnostics mode, execute 'diag stop' before running any other commands.\r\n"));
#endif
for (i = 0; i < sizeof(sCommands) / sizeof(sCommands[0]); i++)
for (i = 0; i < OT_ARRAY_LENGTH(sCommands); i++)
{
if (strcmp(cmd, sCommands[i].mName) == 0)
{
@@ -3454,7 +3454,7 @@ void Interpreter::ProcessLine(char *aBuf, uint16_t aBufLength, Server &aServer)
// Check user defined commands if built-in command
// has not been found
if (i == sizeof(sCommands) / sizeof(sCommands[0]))
if (i == OT_ARRAY_LENGTH(sCommands))
{
for (i = 0; i < mUserCommandsLength; i++)
{
@@ -3494,14 +3494,13 @@ void Interpreter::HandleNetifStateChanged(uint32_t aFlags)
VerifyOrExit((aFlags & OT_CHANGED_THREAD_NETDATA) != 0);
#ifndef OTDLL
otIp6SlaacUpdate(mInstance, mSlaacAddresses, sizeof(mSlaacAddresses) / sizeof(mSlaacAddresses[0]),
otIp6CreateRandomIid, NULL);
otIp6SlaacUpdate(mInstance, mSlaacAddresses, OT_ARRAY_LENGTH(mSlaacAddresses), otIp6CreateRandomIid, NULL);
#if OPENTHREAD_ENABLE_DHCP6_SERVER
otDhcp6ServerUpdate(mInstance);
#endif // OPENTHREAD_ENABLE_DHCP6_SERVER
#if OPENTHREAD_ENABLE_DHCP6_CLIENT
otDhcp6ClientUpdate(mInstance, mDhcpAddresses, sizeof(mDhcpAddresses) / sizeof(mDhcpAddresses[0]), NULL);
otDhcp6ClientUpdate(mInstance, mDhcpAddresses, OT_ARRAY_LENGTH(mDhcpAddresses), NULL);
#endif // OPENTHREAD_ENABLE_DHCP6_CLIENT
#endif
+2 -2
View File
@@ -199,7 +199,7 @@ otError Dataset::Process(otInstance *aInstance, int argc, char *argv[], Server &
ExitNow(error = Print(sDataset));
}
for (unsigned int i = 0; i < sizeof(sCommands) / sizeof(sCommands[0]); i++)
for (unsigned int i = 0; i < OT_ARRAY_LENGTH(sCommands); i++)
{
if (strcmp(argv[0], sCommands[i].mName) == 0)
{
@@ -214,7 +214,7 @@ exit:
otError Dataset::ProcessHelp(otInstance *aInstance, int argc, char *argv[])
{
for (unsigned int i = 0; i < sizeof(sCommands) / sizeof(sCommands[0]); i++)
for (unsigned int i = 0; i < OT_ARRAY_LENGTH(sCommands); i++)
{
sServer->OutputFormat("%s\r\n", sCommands[i].mName);
}
+2 -2
View File
@@ -56,7 +56,7 @@ UdpExample::UdpExample(Interpreter &aInterpreter)
otError UdpExample::ProcessHelp(int argc, char *argv[])
{
for (unsigned int i = 0; i < sizeof(sCommands) / sizeof(sCommands[0]); i++)
for (unsigned int i = 0; i < OT_ARRAY_LENGTH(sCommands); i++)
{
mInterpreter.mServer->OutputFormat("%s\r\n", sCommands[i].mName);
}
@@ -180,7 +180,7 @@ otError UdpExample::Process(int argc, char *argv[])
{
otError error = OT_ERROR_PARSE;
for (size_t i = 0; i < sizeof(sCommands) / sizeof(sCommands[0]); i++)
for (size_t i = 0; i < OT_ARRAY_LENGTH(sCommands); i++)
{
if (strcmp(argv[0], sCommands[i].mName) == 0)
{
+10
View File
@@ -38,6 +38,16 @@
#include "utils/wrap_stdbool.h"
/**
* This macro calculates the number of elements in an array.
*
* @param[in] aArray Name of the array variable.
*
* @returns Number of elements in the array.
*
*/
#define OT_ARRAY_LENGTH(aArray) (sizeof(aArray) / sizeof(aArray[0]))
// Calculates the aligned variable size.
#define otALIGNED_VAR_SIZE(size, align_type) (((size) + (sizeof(align_type) - 1)) / sizeof(align_type))
+7 -7
View File
@@ -156,7 +156,7 @@ otError Commissioner::SendCommissionerSet(void)
steeringData.Init();
steeringData.Clear();
for (size_t i = 0; i < sizeof(mJoiners) / sizeof(mJoiners[0]); i++)
for (size_t i = 0; i < OT_ARRAY_LENGTH(mJoiners); i++)
{
if (!mJoiners[i].mValid)
{
@@ -187,7 +187,7 @@ exit:
void Commissioner::ClearJoiners(void)
{
for (size_t i = 0; i < sizeof(mJoiners) / sizeof(mJoiners[0]); i++)
for (size_t i = 0; i < OT_ARRAY_LENGTH(mJoiners); i++)
{
mJoiners[i].mValid = false;
}
@@ -204,7 +204,7 @@ otError Commissioner::AddJoiner(const Mac::ExtAddress *aEui64, const char *aPSKd
VerifyOrExit(strlen(aPSKd) <= Dtls::kPskMaxLength, error = OT_ERROR_INVALID_ARGS);
RemoveJoiner(aEui64, 0); // remove immediately
for (size_t i = 0; i < sizeof(mJoiners) / sizeof(mJoiners[0]); i++)
for (size_t i = 0; i < OT_ARRAY_LENGTH(mJoiners); i++)
{
if (mJoiners[i].mValid)
{
@@ -259,7 +259,7 @@ otError Commissioner::RemoveJoiner(const Mac::ExtAddress *aEui64, uint32_t aDela
VerifyOrExit(mState == OT_COMMISSIONER_STATE_ACTIVE, error = OT_ERROR_INVALID_STATE);
for (size_t i = 0; i < sizeof(mJoiners) / sizeof(mJoiners[0]); i++)
for (size_t i = 0; i < OT_ARRAY_LENGTH(mJoiners); i++)
{
if (!mJoiners[i].mValid)
{
@@ -366,7 +366,7 @@ void Commissioner::HandleJoinerExpirationTimer(void)
uint32_t now = TimerMilli::GetNow();
// Remove Joiners.
for (size_t i = 0; i < sizeof(mJoiners) / sizeof(mJoiners[0]); i++)
for (size_t i = 0; i < OT_ARRAY_LENGTH(mJoiners); i++)
{
if (!mJoiners[i].mValid)
{
@@ -389,7 +389,7 @@ void Commissioner::UpdateJoinerExpirationTimer(void)
uint32_t nextTimeout = 0xffffffff;
// Check if timer should be set for next Joiner.
for (size_t i = 0; i < sizeof(mJoiners) / sizeof(mJoiners[0]); i++)
for (size_t i = 0; i < OT_ARRAY_LENGTH(mJoiners); i++)
{
{
if (!mJoiners[i].mValid)
@@ -821,7 +821,7 @@ void Commissioner::HandleRelayReceive(Coap::Header &aHeader, Message &aMessage,
memcpy(mJoinerIid, joinerIid.GetIid(), sizeof(mJoinerIid));
mJoinerIid[0] ^= 0x2;
for (size_t i = 0; i < sizeof(mJoiners) / sizeof(mJoiners[0]); i++)
for (size_t i = 0; i < OT_ARRAY_LENGTH(mJoiners); i++)
{
if (!mJoiners[i].mValid)
{
+9 -9
View File
@@ -88,13 +88,13 @@ Netif::Netif(Instance &aInstance, int8_t aInterfaceId)
, mMulticastPromiscuous(false)
, mNext(NULL)
{
for (size_t i = 0; i < sizeof(mExtUnicastAddresses) / sizeof(mExtUnicastAddresses[0]); i++)
for (size_t i = 0; i < OT_ARRAY_LENGTH(mExtUnicastAddresses); i++)
{
// To mark the address as unused/available, set the `mNext` to point back to itself.
mExtUnicastAddresses[i].mNext = &mExtUnicastAddresses[i];
}
for (size_t i = 0; i < sizeof(mExtMulticastAddresses) / sizeof(mExtMulticastAddresses[0]); i++)
for (size_t i = 0; i < OT_ARRAY_LENGTH(mExtMulticastAddresses); i++)
{
// To mark the address as unused/available, set the `mNext` to point back to itself.
mExtMulticastAddresses[i].mNext = &mExtMulticastAddresses[i];
@@ -240,7 +240,7 @@ exit:
otError Netif::GetNextExternalMulticast(uint8_t &aIterator, Address &aAddress)
{
otError error = OT_ERROR_NOT_FOUND;
size_t num = sizeof(mExtMulticastAddresses) / sizeof(mExtMulticastAddresses[0]);
size_t num = OT_ARRAY_LENGTH(mExtMulticastAddresses);
NetifMulticastAddress *entry;
VerifyOrExit(aIterator < num);
@@ -267,7 +267,7 @@ otError Netif::SubscribeExternalMulticast(const Address &aAddress)
{
otError error = OT_ERROR_NONE;
NetifMulticastAddress *entry;
size_t num = sizeof(mExtMulticastAddresses) / sizeof(mExtMulticastAddresses[0]);
size_t num = OT_ARRAY_LENGTH(mExtMulticastAddresses);
if (IsMulticastSubscribed(aAddress))
{
@@ -301,7 +301,7 @@ otError Netif::UnsubscribeExternalMulticast(const Address &aAddress)
otError error = OT_ERROR_NONE;
NetifMulticastAddress *entry;
NetifMulticastAddress *last = NULL;
size_t num = sizeof(mExtMulticastAddresses) / sizeof(mExtMulticastAddresses[0]);
size_t num = OT_ARRAY_LENGTH(mExtMulticastAddresses);
for (entry = mMulticastAddresses; entry; entry = entry->GetNext())
{
@@ -338,7 +338,7 @@ exit:
void Netif::UnsubscribeAllExternalMulticastAddresses(void)
{
size_t num = sizeof(mExtMulticastAddresses) / sizeof(mExtMulticastAddresses[0]);
size_t num = OT_ARRAY_LENGTH(mExtMulticastAddresses);
for (NetifMulticastAddress *entry = &mExtMulticastAddresses[0]; num > 0; num--, entry++)
{
@@ -408,7 +408,7 @@ otError Netif::AddExternalUnicastAddress(const NetifUnicastAddress &aAddress)
{
otError error = OT_ERROR_NONE;
NetifUnicastAddress *entry;
size_t num = sizeof(mExtUnicastAddresses) / sizeof(mExtUnicastAddresses[0]);
size_t num = OT_ARRAY_LENGTH(mExtUnicastAddresses);
VerifyOrExit(!aAddress.GetAddress().IsLinkLocal(), error = OT_ERROR_INVALID_ARGS);
@@ -454,7 +454,7 @@ otError Netif::RemoveExternalUnicastAddress(const Address &aAddress)
otError error = OT_ERROR_NONE;
NetifUnicastAddress *entry;
NetifUnicastAddress *last = NULL;
size_t num = sizeof(mExtUnicastAddresses) / sizeof(mExtUnicastAddresses[0]);
size_t num = OT_ARRAY_LENGTH(mExtUnicastAddresses);
for (entry = mUnicastAddresses; entry; entry = entry->GetNext())
{
@@ -491,7 +491,7 @@ exit:
void Netif::RemoveAllExternalUnicastAddresses(void)
{
size_t num = sizeof(mExtUnicastAddresses) / sizeof(mExtUnicastAddresses[0]);
size_t num = OT_ARRAY_LENGTH(mExtUnicastAddresses);
for (NetifUnicastAddress *entry = &mExtUnicastAddresses[0]; num > 0; num--, entry++)
{
+3 -3
View File
@@ -138,7 +138,7 @@ Mle::Mle(Instance &aInstance)
#if OPENTHREAD_ENABLE_SERVICE
// Service Alocs
for (size_t i = 0; i < sizeof(mServiceAlocs) / sizeof(mServiceAlocs[0]); i++)
for (size_t i = 0; i < OT_ARRAY_LENGTH(mServiceAlocs); i++)
{
memset(&mServiceAlocs[i], 0, sizeof(mServiceAlocs[i]));
@@ -774,7 +774,7 @@ otError Mle::SetMeshLocalPrefix(const uint8_t *aMeshLocalPrefix)
#if OPENTHREAD_ENABLE_SERVICE
for (uint8_t i = 0; i < sizeof(mServiceAlocs) / sizeof(mServiceAlocs[0]); i++)
for (uint8_t i = 0; i < OT_ARRAY_LENGTH(mServiceAlocs); i++)
{
if (HostSwap16(mServiceAlocs[i].GetAddress().mFields.m16[7]) != Mac::kShortAddrInvalid)
{
@@ -1387,7 +1387,7 @@ void Mle::UpdateServiceAlocs(void)
int i = 0;
NetworkData::Leader & leaderData = netif.GetNetworkDataLeader();
otNetworkDataIterator serviceIterator = OT_NETWORK_DATA_ITERATOR_INIT;
int serviceAlocsLength = sizeof(mServiceAlocs) / sizeof(mServiceAlocs[0]);
int serviceAlocsLength = OT_ARRAY_LENGTH(mServiceAlocs);
VerifyOrExit(mRole != OT_DEVICE_ROLE_DISABLED);
+3 -1
View File
@@ -28,6 +28,8 @@
#include "changed_props_set.hpp"
#include "common/code_utils.hpp"
namespace ot {
namespace Ncp {
@@ -87,7 +89,7 @@ const ChangedPropsSet::Entry ChangedPropsSet::mSupportedProps[] =
uint8_t ChangedPropsSet::GetNumEntries(void) const
{
return (sizeof(mSupportedProps) / sizeof(mSupportedProps[0]));
return OT_ARRAY_LENGTH(mSupportedProps);
}
void ChangedPropsSet::Add(spinel_prop_key_t aPropKey, spinel_status_t aStatus)
+4 -21
View File
@@ -1305,39 +1305,22 @@ NcpBase::PropertyHandler NcpBase::FindPropertyHandler(spinel_prop_key_t aKey, co
NcpBase::PropertyHandler NcpBase::FindGetPropertyHandler(spinel_prop_key_t aKey)
{
return FindPropertyHandler(
aKey,
mGetPropertyHandlerTable,
sizeof(mGetPropertyHandlerTable) / sizeof(mGetPropertyHandlerTable[0])
);
return FindPropertyHandler(aKey, mGetPropertyHandlerTable, OT_ARRAY_LENGTH(mGetPropertyHandlerTable));
}
NcpBase::PropertyHandler NcpBase::FindSetPropertyHandler(spinel_prop_key_t aKey)
{
return FindPropertyHandler(
aKey,
mSetPropertyHandlerTable,
sizeof(mSetPropertyHandlerTable) / sizeof(mSetPropertyHandlerTable[0])
);
return FindPropertyHandler(aKey, mSetPropertyHandlerTable, OT_ARRAY_LENGTH(mSetPropertyHandlerTable));
}
NcpBase::PropertyHandler NcpBase::FindInsertPropertyHandler(spinel_prop_key_t aKey)
{
return FindPropertyHandler(
aKey,
mInsertPropertyHandlerTable,
sizeof(mInsertPropertyHandlerTable) / sizeof(mInsertPropertyHandlerTable[0])
);
return FindPropertyHandler(aKey, mInsertPropertyHandlerTable, OT_ARRAY_LENGTH(mInsertPropertyHandlerTable));
}
NcpBase::PropertyHandler NcpBase::FindRemovePropertyHandler(spinel_prop_key_t aKey)
{
return FindPropertyHandler(
aKey,
mRemovePropertyHandlerTable,
sizeof(mRemovePropertyHandlerTable) / sizeof(mRemovePropertyHandlerTable[0])
);
return FindPropertyHandler(aKey, mRemovePropertyHandlerTable, OT_ARRAY_LENGTH(mRemovePropertyHandlerTable));
}
// Returns `true` and updates the `aError` on success.
+1 -1
View File
@@ -3010,7 +3010,7 @@ void NcpBase::ProcessThreadChangedFlags(void)
// Convert OT_CHANGED flags to corresponding NCP property update.
for (unsigned i = 0; i < sizeof(kFlags) / sizeof(kFlags[0]); i++)
for (unsigned i = 0; i < OT_ARRAY_LENGTH(kFlags); i++)
{
uint32_t threadFlag = kFlags[i].mThreadFlag;
+2 -1
View File
@@ -32,6 +32,7 @@
#include <openthread/openthread.h>
#include "test_util.h"
#include "common/code_utils.hpp"
#include "common/instance.hpp"
#include "thread/topology.hpp"
@@ -132,7 +133,7 @@ void TestChildIp6Address(void)
numAddresses++;
for (uint8_t index = 0; index < static_cast<uint8_t>(sizeof(ip6Addresses) / sizeof(ip6Addresses[0])); index++)
for (uint8_t index = 0; index < static_cast<uint8_t>(OT_ARRAY_LENGTH(ip6Addresses)); index++)
{
VerifyOrQuit(numAddresses < kMaxChildIp6Addresses, "Too many IPv6 addresses in the unit test");
SuccessOrQuit(addresses[numAddresses++].FromString(ip6Addresses[index]),
+4 -3
View File
@@ -32,6 +32,7 @@
#include <openthread/openthread.h>
#include "test_util.h"
#include "common/code_utils.hpp"
#include "common/instance.hpp"
#include "thread/child_table.hpp"
@@ -109,7 +110,7 @@ void VerifyChildTableContent(ChildTable &aTable, uint8_t aChildListLength, const
{
printf("Test ChildTable with %d entries", aChildListLength);
for (uint8_t k = 0; k < sizeof(kAllFilters) / sizeof(kAllFilters[0]); k++)
for (uint8_t k = 0; k < OT_ARRAY_LENGTH(kAllFilters); k++)
{
ChildTable::StateFilter filter = kAllFilters[k];
@@ -296,7 +297,7 @@ void TestChildTable(void)
},
};
const uint8_t testListLength = sizeof(testChildList) / sizeof(testChildList[0]);
const uint8_t testListLength = OT_ARRAY_LENGTH(testChildList);
uint8_t testNumAllowedChildren = 2;
@@ -314,7 +315,7 @@ void TestChildTable(void)
VerifyOrQuit(table->GetMaxChildrenAllowed() == table->GetMaxChildren(),
"GetMaxChildrenAllowed() initial value is incorrect ");
for (uint8_t i = 0; i < sizeof(kAllFilters) / sizeof(kAllFilters[0]); i++)
for (uint8_t i = 0; i < OT_ARRAY_LENGTH(kAllFilters); i++)
{
ChildTable::StateFilter filter = kAllFilters[i];
+2 -1
View File
@@ -28,6 +28,7 @@
#include <openthread/openthread.h>
#include "common/code_utils.hpp"
#include "thread/link_quality.hpp"
#include "utils/wrap_string.h"
@@ -427,7 +428,7 @@ void TestSuccessRateTracker(void)
// Adding success/failure at different rates and checking the RateTracker rate for every sample
for (uint16_t testRound = 0; testRound < sizeof(kWeightLimit) / sizeof(kWeightLimit[0]) * 2; testRound++)
for (uint16_t testRound = 0; testRound < OT_ARRAY_LENGTH(kWeightLimit) * 2; testRound++)
{
uint16_t weightLimit;
bool reverseLogic;
+2 -1
View File
@@ -28,6 +28,7 @@
#include <openthread/openthread.h>
#include "common/code_utils.hpp"
#include "common/debug.hpp"
#include "mac/mac.hpp"
#include "mac/mac_frame.hpp"
@@ -68,7 +69,7 @@ void TestMacHeader(void)
Mac::Frame::kSecMic32 | Mac::Frame::kKeyIdMode2, 19},
};
for (unsigned i = 0; i < sizeof(tests) / sizeof(tests[0]); i++)
for (unsigned i = 0; i < OT_ARRAY_LENGTH(tests); i++)
{
uint8_t psdu[Mac::Frame::kMTU];
Mac::Frame frame;
+3 -2
View File
@@ -28,6 +28,7 @@
#include <openthread/config.h>
#include "common/code_utils.hpp"
#include "common/instance.hpp"
#include "thread/network_data_local.hpp"
@@ -108,7 +109,7 @@ void TestNetworkDataIterator(void)
printf("\nTest #1: Network data 1");
printf("\n-------------------------------------------------");
for (uint8_t i = 0; i < sizeof(routes) / sizeof(routes[0]); i++)
for (uint8_t i = 0; i < OT_ARRAY_LENGTH(routes); i++)
{
SuccessOrQuit(netData.GetNextExternalRoute(&iter, &config), "GetNextExternalRoute() failed\n");
PrintExternalRouteConfig(config);
@@ -158,7 +159,7 @@ void TestNetworkDataIterator(void)
printf("\nTest #2: Network data 2");
printf("\n-------------------------------------------------");
for (uint8_t i = 0; i < sizeof(routes) / sizeof(routes[0]); i++)
for (uint8_t i = 0; i < OT_ARRAY_LENGTH(routes); i++)
{
SuccessOrQuit(netData.GetNextExternalRoute(&iter, &config), "GetNextExternalRoute() failed\n");
PrintExternalRouteConfig(config);
+2 -1
View File
@@ -28,6 +28,7 @@
#include "test_platform.h"
#include "common/code_utils.hpp"
#include "common/debug.hpp"
#include "common/instance.hpp"
#include "common/timer.hpp"
@@ -550,7 +551,7 @@ int TestTenTimers(void)
size_t i;
for (i = 0; i < sizeof(kTimeShift) / sizeof(kTimeShift[0]); i++)
for (i = 0; i < OT_ARRAY_LENGTH(kTimeShift); i++)
{
TenTimers(kTimeShift[i]);
}