Skip to content

Commit 4da6b9a

Browse files
authored
Unify ARRAY_SIZE/STRING_LENGTH-like macros (#61537)
* Unify ARRAY_SIZE/STRING_LENGTH-like macros * Cleanup trailing whitespaces in changeset files * Use ArrLen in the JIT * Keep using G_N_ELEMENTS in mono * Resolve name conflict in test
1 parent 292632f commit 4da6b9a

File tree

250 files changed

+3227
-3331
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

250 files changed

+3227
-3331
lines changed

src/coreclr/ToolBox/superpmi/superpmi/jitdebugger.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -146,9 +146,9 @@ HRESULT GetCurrentModuleFileName(__out_ecount(*pcchBuffer) LPWSTR pBuffer, __ino
146146
// Get the appname to look up in the exclusion or inclusion list.
147147
WCHAR appPath[MAX_PATH + 2];
148148

149-
DWORD ret = WszGetModuleFileName(NULL, appPath, NumItems(appPath));
149+
DWORD ret = WszGetModuleFileName(NULL, appPath, ARRAY_SIZE(appPath));
150150

151-
if ((ret == NumItems(appPath)) || (ret == 0))
151+
if ((ret == ARRAY_SIZE(appPath)) || (ret == 0))
152152
{
153153
// The module file name exceeded maxpath, or GetModuleFileName failed.
154154
return E_UNEXPECTED;
@@ -201,7 +201,7 @@ BOOL IsCurrentModuleFileNameInAutoExclusionList()
201201
}
202202

203203
WCHAR wszAppName[MAX_PATH];
204-
DWORD cchAppName = NumItems(wszAppName);
204+
DWORD cchAppName = ARRAY_SIZE(wszAppName);
205205

206206
// Get the appname to look up in the exclusion or inclusion list.
207207
if (GetCurrentModuleFileName(wszAppName, &cchAppName) != S_OK)
@@ -320,7 +320,7 @@ HRESULT GetDebuggerSettingInfoWorker(__out_ecount_part_opt(*pcchDebuggerString,
320320

321321
// Get the appname to look up in DebugApplications key.
322322
WCHAR wzAppName[MAX_PATH];
323-
DWORD cchAppName = NumItems(wzAppName);
323+
DWORD cchAppName = ARRAY_SIZE(wzAppName);
324324
long iValue;
325325

326326
// Check DebugApplications setting
@@ -340,7 +340,7 @@ HRESULT GetDebuggerSettingInfoWorker(__out_ecount_part_opt(*pcchDebuggerString,
340340
if ((ret == ERROR_SUCCESS) && (valueType == REG_SZ) && (valueSize / sizeof(WCHAR) < MAX_PATH))
341341
{
342342
WCHAR wzAutoKey[MAX_PATH];
343-
valueSize = NumItems(wzAutoKey) * sizeof(WCHAR);
343+
valueSize = ARRAY_SIZE(wzAutoKey) * sizeof(WCHAR);
344344
RegQueryValueExW(hKey, kUnmanagedDebuggerAutoValue, NULL, NULL, reinterpret_cast<LPBYTE>(wzAutoKey),
345345
&valueSize);
346346

@@ -366,7 +366,7 @@ BOOL LaunchJITDebugger()
366366
BOOL fSuccess = FALSE;
367367

368368
WCHAR debugger[1000];
369-
GetDebuggerSettingInfo(debugger, NumItems(debugger), NULL);
369+
GetDebuggerSettingInfo(debugger, ARRAY_SIZE(debugger), NULL);
370370

371371
SECURITY_ATTRIBUTES sa;
372372
sa.nLength = sizeof(sa);

src/coreclr/ToolBox/superpmi/superpmi/methodstatsemitter.cpp

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -49,11 +49,11 @@ void MethodStatsEmitter::Emit(int methodNumber, MethodContext* mc, ULONGLONG fir
4949
if (mc->dumpMethodMD5HashToBuffer(md5Hash, MD5_HASH_BUFFER_SIZE) != MD5_HASH_BUFFER_SIZE)
5050
md5Hash[0] = 0;
5151

52-
charCount += sprintf_s(rowData + charCount, _countof(rowData) - charCount, "%s,", md5Hash);
52+
charCount += sprintf_s(rowData + charCount, ARRAY_SIZE(rowData) - charCount, "%s,", md5Hash);
5353
}
5454
if (strchr(statsTypes, '*') != NULL || strchr(statsTypes, 'n') != NULL || strchr(statsTypes, 'N') != NULL)
5555
{
56-
charCount += sprintf_s(rowData + charCount, _countof(rowData) - charCount, "%d,", methodNumber);
56+
charCount += sprintf_s(rowData + charCount, ARRAY_SIZE(rowData) - charCount, "%d,", methodNumber);
5757
}
5858
if (strchr(statsTypes, '*') != NULL || strchr(statsTypes, 'i') != NULL || strchr(statsTypes, 'I') != NULL)
5959
{
@@ -63,7 +63,7 @@ void MethodStatsEmitter::Emit(int methodNumber, MethodContext* mc, ULONGLONG fir
6363
CORINFO_OS os = CORINFO_WINNT;
6464
mc->repCompileMethod(&info, &flags, &os);
6565

66-
charCount += sprintf_s(rowData + charCount, _countof(rowData) - charCount, "%d,", info.ILCodeSize);
66+
charCount += sprintf_s(rowData + charCount, ARRAY_SIZE(rowData) - charCount, "%d,", info.ILCodeSize);
6767
}
6868
if (strchr(statsTypes, '*') != NULL || strchr(statsTypes, 'a') != NULL || strchr(statsTypes, 'A') != NULL)
6969
{
@@ -76,12 +76,12 @@ void MethodStatsEmitter::Emit(int methodNumber, MethodContext* mc, ULONGLONG fir
7676
else
7777
codeSize = 0; // this is likely a thin mc
7878

79-
charCount += sprintf_s(rowData + charCount, _countof(rowData) - charCount, "%d,", codeSize);
79+
charCount += sprintf_s(rowData + charCount, ARRAY_SIZE(rowData) - charCount, "%d,", codeSize);
8080
}
8181
if (strchr(statsTypes, '*') != NULL || strchr(statsTypes, 't') != NULL || strchr(statsTypes, 'T') != NULL)
8282
{
8383
charCount +=
84-
sprintf_s(rowData + charCount, _countof(rowData) - charCount, "%llu,%llu,", firstTime, secondTime);
84+
sprintf_s(rowData + charCount, ARRAY_SIZE(rowData) - charCount, "%llu,%llu,", firstTime, secondTime);
8585
}
8686

8787
// get rid of the final ',' and replace it with a '\n'
@@ -106,15 +106,15 @@ void MethodStatsEmitter::SetStatsTypes(char* types)
106106
DWORD bytesWritten = 0;
107107

108108
if (strchr(statsTypes, '*') != NULL || strchr(statsTypes, 'h') != NULL || strchr(statsTypes, 'H') != NULL)
109-
charCount += sprintf_s(rowHeader + charCount, _countof(rowHeader) - charCount, "HASH,");
109+
charCount += sprintf_s(rowHeader + charCount, ARRAY_SIZE(rowHeader) - charCount, "HASH,");
110110
if (strchr(statsTypes, '*') != NULL || strchr(statsTypes, 'n') != NULL || strchr(statsTypes, 'N') != NULL)
111-
charCount += sprintf_s(rowHeader + charCount, _countof(rowHeader) - charCount, "METHOD_NUMBER,");
111+
charCount += sprintf_s(rowHeader + charCount, ARRAY_SIZE(rowHeader) - charCount, "METHOD_NUMBER,");
112112
if (strchr(statsTypes, '*') != NULL || strchr(statsTypes, 'i') != NULL || strchr(statsTypes, 'I') != NULL)
113-
charCount += sprintf_s(rowHeader + charCount, _countof(rowHeader) - charCount, "IL_CODE_SIZE,");
113+
charCount += sprintf_s(rowHeader + charCount, ARRAY_SIZE(rowHeader) - charCount, "IL_CODE_SIZE,");
114114
if (strchr(statsTypes, '*') != NULL || strchr(statsTypes, 'a') != NULL || strchr(statsTypes, 'A') != NULL)
115-
charCount += sprintf_s(rowHeader + charCount, _countof(rowHeader) - charCount, "ASM_CODE_SIZE,");
115+
charCount += sprintf_s(rowHeader + charCount, ARRAY_SIZE(rowHeader) - charCount, "ASM_CODE_SIZE,");
116116
if (strchr(statsTypes, '*') != NULL || strchr(statsTypes, 't') != NULL || strchr(statsTypes, 'T') != NULL)
117-
charCount += sprintf_s(rowHeader + charCount, _countof(rowHeader) - charCount, "Time1,Time2,");
117+
charCount += sprintf_s(rowHeader + charCount, ARRAY_SIZE(rowHeader) - charCount, "Time1,Time2,");
118118

119119
// get rid of the final ',' and replace it with a '\n'
120120
rowHeader[charCount - 1] = '\n';

src/coreclr/debug/createdump/createdump.h

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,6 @@
66
#define ___in _SAL1_Source_(__in, (), _In_)
77
#define ___out _SAL1_Source_(__out, (), _Out_)
88

9-
#ifndef _countof
10-
#define _countof(x) (sizeof(x)/sizeof(x[0]))
11-
#endif
12-
139
extern void trace_printf(const char* format, ...);
1410
extern void trace_verbose_printf(const char* format, ...);
1511
extern bool g_diagnostics;

src/coreclr/debug/daccess/daccess.cpp

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1570,7 +1570,7 @@ DacInstanceManager::Add(DAC_INSTANCE* inst)
15701570
// use only one block, so improving data locality across blocks (i.e. keeping the buckets of the
15711571
// hash table together) should help.
15721572
newBlock = (HashInstanceKeyBlock*)
1573-
ClrVirtualAlloc(NULL, HASH_INSTANCE_BLOCK_ALLOC_SIZE*NumItems(m_hash), MEM_COMMIT, PAGE_READWRITE);
1573+
ClrVirtualAlloc(NULL, HASH_INSTANCE_BLOCK_ALLOC_SIZE*ARRAY_SIZE(m_hash), MEM_COMMIT, PAGE_READWRITE);
15741574
}
15751575
if (!newBlock)
15761576
{
@@ -1587,7 +1587,7 @@ DacInstanceManager::Add(DAC_INSTANCE* inst)
15871587
}
15881588
else
15891589
{
1590-
for (DWORD j = 0; j < NumItems(m_hash); j++)
1590+
for (DWORD j = 0; j < ARRAY_SIZE(m_hash); j++)
15911591
{
15921592
m_hash[j] = newBlock;
15931593
newBlock->next = NULL; // The previously allocated block
@@ -2029,7 +2029,7 @@ void DacInstanceManager::Flush(bool fSaveBlock)
20292029
}
20302030

20312031
#if defined(DAC_HASHTABLE)
2032-
for (int i = NumItems(m_hash) - 1; i >= 0; i--)
2032+
for (int i = STRING_LENGTH(m_hash); i >= 0; i--)
20332033
{
20342034
HashInstanceKeyBlock* block = m_hash[i];
20352035
HashInstanceKeyBlock* next;
@@ -2061,7 +2061,7 @@ DacInstanceManager::ClearEnumMemMarker(void)
20612061
ULONG i;
20622062
DAC_INSTANCE* inst;
20632063

2064-
for (i = 0; i < NumItems(m_hash); i++)
2064+
for (i = 0; i < ARRAY_SIZE(m_hash); i++)
20652065
{
20662066
HashInstanceKeyBlock* block = m_hash[i];
20672067
while (block)
@@ -2131,7 +2131,7 @@ DacInstanceManager::DumpAllInstances(
21312131
int total = 0;
21322132
#endif // #if defined(DAC_MEASURE_PERF)
21332133

2134-
for (i = 0; i < NumItems(m_hash); i++)
2134+
for (i = 0; i < ARRAY_SIZE(m_hash); i++)
21352135
{
21362136

21372137
#if defined(DAC_MEASURE_PERF)
@@ -6636,7 +6636,7 @@ ClrDataAccess::GetMetaDataFromHost(PEAssembly* pPEAssembly,
66366636
ulRvaHint,
66376637
isNGEN,
66386638
uniPath,
6639-
NumItems(uniPath)))
6639+
ARRAY_SIZE(uniPath)))
66406640
{
66416641
return NULL;
66426642
}
@@ -6700,24 +6700,24 @@ ClrDataAccess::GetMetaDataFromHost(PEAssembly* pPEAssembly,
67006700
imageTimestamp,
67016701
imageSize,
67026702
uniPath,
6703-
NumItems(uniPath)))
6703+
ARRAY_SIZE(uniPath)))
67046704
{
67056705
goto ErrExit;
67066706
}
67076707

67086708
#if defined(FEATURE_CORESYSTEM)
67096709
const WCHAR* ilExtension = W("dll");
67106710
WCHAR ngenImageName[MAX_LONGPATH] = {0};
6711-
if (wcscpy_s(ngenImageName, NumItems(ngenImageName), uniPath) != 0)
6711+
if (wcscpy_s(ngenImageName, ARRAY_SIZE(ngenImageName), uniPath) != 0)
67126712
{
67136713
goto ErrExit;
67146714
}
6715-
if (wcscpy_s(uniPath, NumItems(uniPath), ngenImageName) != 0)
6715+
if (wcscpy_s(uniPath, ARRAY_SIZE(uniPath), ngenImageName) != 0)
67166716
{
67176717
goto ErrExit;
67186718
}
67196719
// Transform NGEN image name into IL Image name
6720-
if (!GetILImageNameFromNgenImage(ilExtension, uniPath, NumItems(uniPath)))
6720+
if (!GetILImageNameFromNgenImage(ilExtension, uniPath, ARRAY_SIZE(uniPath)))
67216721
{
67226722
goto ErrExit;
67236723
}

src/coreclr/debug/daccess/inspect.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2296,7 +2296,7 @@ ClrDataTypeDefinition::GetName(
22962296
if ((status =
22972297
GetFullClassNameFromMetadata(m_module->GetMDImport(),
22982298
m_token,
2299-
NumItems(classNameBuf),
2299+
ARRAY_SIZE(classNameBuf),
23002300
classNameBuf)) == S_OK)
23012301
{
23022302
status = ConvertUtf8(classNameBuf, bufLen, nameLen, nameBuf);

src/coreclr/debug/daccess/request.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -631,7 +631,7 @@ ClrDataAccess::GetRegisterName(int regNum, unsigned int count, __out_z __inout_e
631631
if (callerFrame)
632632
regNum = -regNum-1;
633633

634-
if ((unsigned int)regNum >= _countof(regs))
634+
if ((unsigned int)regNum >= ARRAY_SIZE(regs))
635635
return E_UNEXPECTED;
636636

637637

@@ -1371,8 +1371,8 @@ ClrDataAccess::GetMethodDescName(CLRDATA_ADDRESS methodDesc, unsigned int count,
13711371
{
13721372
WCHAR path[MAX_LONGPATH];
13731373
COUNT_T nChars = 0;
1374-
if (pModule->GetPath().DacGetUnicode(NumItems(path), path, &nChars) &&
1375-
nChars > 0 && nChars <= NumItems(path))
1374+
if (pModule->GetPath().DacGetUnicode(ARRAY_SIZE(path), path, &nChars) &&
1375+
nChars > 0 && nChars <= ARRAY_SIZE(path))
13761376
{
13771377
WCHAR* pFile = path + nChars - 1;
13781378
while ((pFile >= path) && (*pFile != W('\\')))
@@ -3274,7 +3274,7 @@ HRESULT ClrDataAccess::GetHandleEnum(ISOSHandleEnum **ppHandleEnum)
32743274
#endif // FEATURE_COMINTEROP
32753275
};
32763276

3277-
return GetHandleEnumForTypes(types, _countof(types), ppHandleEnum);
3277+
return GetHandleEnumForTypes(types, ARRAY_SIZE(types), ppHandleEnum);
32783278
}
32793279

32803280
HRESULT ClrDataAccess::GetHandleEnumForTypes(unsigned int types[], unsigned int count, ISOSHandleEnum **ppHandleEnum)
@@ -3317,7 +3317,7 @@ HRESULT ClrDataAccess::GetHandleEnumForGC(unsigned int gen, ISOSHandleEnum **ppH
33173317

33183318
DacHandleWalker *walker = new DacHandleWalker();
33193319

3320-
HRESULT hr = walker->Init(this, types, _countof(types), gen);
3320+
HRESULT hr = walker->Init(this, types, ARRAY_SIZE(types), gen);
33213321
if (SUCCEEDED(hr))
33223322
hr = walker->QueryInterface(__uuidof(ISOSHandleEnum), (void**)ppHandleEnum);
33233323

src/coreclr/debug/daccess/stack.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1346,7 +1346,7 @@ ClrDataFrame::ValueFromDebugInfo(MetaSig* sig,
13461346
else
13471347
{
13481348
numLocs = NativeVarLocations(varInfo[i].loc, &m_context,
1349-
NumItems(locs), locs);
1349+
ARRAY_SIZE(locs), locs);
13501350
}
13511351

13521352
if (numLocs == 1 && !locs[0].contextReg)

src/coreclr/debug/daccess/task.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3139,7 +3139,7 @@ ClrDataMethodDefinition::GetName(
31393139

31403140
status = GetFullMethodNameFromMetadata(m_module->GetMDImport(),
31413141
m_token,
3142-
NumItems(methName),
3142+
ARRAY_SIZE(methName),
31433143
methName);
31443144
if (status == S_OK)
31453145
{
@@ -3885,7 +3885,7 @@ ClrDataMethodInstance::GetName(
38853885
wcscpy_s(name, bufLen, nameUnk);
38863886
if (nameLen != NULL)
38873887
{
3888-
*nameLen = _countof(nameUnk);
3888+
*nameLen = ARRAY_SIZE(nameUnk);
38893889
}
38903890
status = S_OK;
38913891
}

src/coreclr/debug/di/module.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5102,11 +5102,11 @@ HRESULT CordbNativeCode::GetCallSignature(ULONG32 ILoffset, mdToken *pClass, mdT
51025102
CordbILCode *pCode = this->m_pFunction->GetILCode();
51035103
BYTE buffer[3];
51045104
ULONG32 fetched = 0;
5105-
HRESULT hr = pCode->GetCode(ILoffset, ILoffset+_countof(buffer), _countof(buffer), buffer, &fetched);
5105+
HRESULT hr = pCode->GetCode(ILoffset, ILoffset+ARRAY_SIZE(buffer), ARRAY_SIZE(buffer), buffer, &fetched);
51065106

51075107
if (FAILED(hr))
51085108
return hr;
5109-
else if (fetched != _countof(buffer))
5109+
else if (fetched != ARRAY_SIZE(buffer))
51105110
return CORDBG_E_INVALID_OPCODE;
51115111

51125112
// tail. - fe 14 (ECMA III.2.4)
@@ -5165,7 +5165,7 @@ HRESULT CordbNativeCode::GetReturnValueLiveOffsetImpl(Instantiation *currentInst
51655165
BYTE nativeBuffer[8];
51665166

51675167
ULONG32 fetched = 0;
5168-
IfFailRet(GetCode(pMap->nativeStartOffset, pMap->nativeStartOffset+_countof(nativeBuffer), _countof(nativeBuffer), nativeBuffer, &fetched));
5168+
IfFailRet(GetCode(pMap->nativeStartOffset, pMap->nativeStartOffset+ARRAY_SIZE(nativeBuffer), ARRAY_SIZE(nativeBuffer), nativeBuffer, &fetched));
51695169

51705170
int skipBytes = 0;
51715171

@@ -5177,12 +5177,12 @@ HRESULT CordbNativeCode::GetReturnValueLiveOffsetImpl(Instantiation *currentInst
51775177
{
51785178
skipBytes++;
51795179

5180-
for (int j = 1; j < _countof(nativeBuffer) && nativeBuffer[j] == nop_opcode; ++j)
5180+
for (int j = 1; j < ARRAY_SIZE(nativeBuffer) && nativeBuffer[j] == nop_opcode; ++j)
51815181
skipBytes++;
51825182

51835183
// We must have at least one skip byte since the outer while ensures it. Thus we always need to reread
51845184
// the buffer at the end of this loop.
5185-
IfFailRet(GetCode(pMap->nativeStartOffset+skipBytes, pMap->nativeStartOffset+skipBytes+_countof(nativeBuffer), _countof(nativeBuffer), nativeBuffer, &fetched));
5185+
IfFailRet(GetCode(pMap->nativeStartOffset+skipBytes, pMap->nativeStartOffset+skipBytes+ARRAY_SIZE(nativeBuffer), ARRAY_SIZE(nativeBuffer), nativeBuffer, &fetched));
51865186
}
51875187
#endif
51885188

src/coreclr/debug/di/process.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2709,15 +2709,15 @@ HRESULT CordbRefEnum::Next(ULONG celt, COR_GC_REFERENCE refs[], ULONG *pceltFetc
27092709
if (SUCCEEDED(hr))
27102710
{
27112711
DacGcReference dacRefs[32];
2712-
ULONG toFetch = _countof(dacRefs);
2712+
ULONG toFetch = ARRAY_SIZE(dacRefs);
27132713
ULONG total = 0;
27142714

2715-
for (ULONG c = 0; SUCCEEDED(hr) && c < (celt/_countof(dacRefs) + 1); ++c)
2715+
for (ULONG c = 0; SUCCEEDED(hr) && c < (celt/ARRAY_SIZE(dacRefs) + 1); ++c)
27162716
{
27172717
// Fetch 32 references at a time, the last time, only fetch the remainder (that is, if
27182718
// the user didn't fetch a multiple of 32).
2719-
if (c == celt/_countof(dacRefs))
2720-
toFetch = celt % _countof(dacRefs);
2719+
if (c == celt/ARRAY_SIZE(dacRefs))
2720+
toFetch = celt % ARRAY_SIZE(dacRefs);
27212721

27222722
ULONG fetched = 0;
27232723
hr = process->GetDAC()->WalkRefs(mRefHandle, toFetch, dacRefs, &fetched);
@@ -7739,7 +7739,7 @@ HRESULT CordbProcess::GetRuntimeOffsets()
77397739
m_runtimeOffsets.m_notifyRSOfSyncCompleteBPAddr,
77407740
};
77417741

7742-
const int NumFlares = NumItems(flares);
7742+
const int NumFlares = ARRAY_SIZE(flares);
77437743

77447744
// Ensure that all of the flares are unique.
77457745
for(int i = 0; i < NumFlares; i++)
@@ -10135,7 +10135,7 @@ HRESULT CordbRCEventThread::SendIPCEvent(CordbProcess* process,
1013510135
// Note that in case of a tie (multiple handles signaled), WaitForMultipleObjects gives
1013610136
// priority to the handle earlier in the array.
1013710137
HANDLE waitSet[] = { process->GetEventChannel()->GetRightSideEventAckHandle(), hLSProcess, hHelperThread};
10138-
DWORD cWaitSet = NumItems(waitSet);
10138+
DWORD cWaitSet = ARRAY_SIZE(waitSet);
1013910139
if (hHelperThread == NULL)
1014010140
{
1014110141
cWaitSet--;

0 commit comments

Comments
 (0)