Skip to content

Commit 00bddf9

Browse files
authored
Replace some read-only use of Span with ReadOnlySpan (#104864)
1 parent e711500 commit 00bddf9

File tree

46 files changed

+120
-114
lines changed

Some content is hidden

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

46 files changed

+120
-114
lines changed

src/coreclr/System.Private.CoreLib/src/System/Threading/WaitHandle.CoreCLR.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ public abstract partial class WaitHandle
1111
[MethodImpl(MethodImplOptions.InternalCall)]
1212
private static extern int WaitOneCore(IntPtr waitHandle, int millisecondsTimeout, bool useTrivialWaits);
1313

14-
private static unsafe int WaitMultipleIgnoringSyncContextCore(Span<IntPtr> waitHandles, bool waitAll, int millisecondsTimeout)
14+
private static unsafe int WaitMultipleIgnoringSyncContextCore(ReadOnlySpan<IntPtr> waitHandles, bool waitAll, int millisecondsTimeout)
1515
{
1616
fixed (IntPtr* pWaitHandles = &MemoryMarshal.GetReference(waitHandles))
1717
{

src/libraries/Common/src/Interop/BSD/System.Native/Interop.Sysctl.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ internal static partial class Sys
2222
// This is 'raw' sysctl call, only wrapped to allocate memory if needed
2323
// caller always needs to free returned buffer using Marshal.FreeHGlobal()
2424

25-
internal static unsafe void Sysctl(Span<int> name, ref byte* value, ref int len)
25+
internal static unsafe void Sysctl(ReadOnlySpan<int> name, ref byte* value, ref int len)
2626
{
2727
fixed (int* ptr = &MemoryMarshal.GetReference(name))
2828
{

src/libraries/Common/src/Interop/FreeBSD/Interop.Process.GetProcInfo.cs

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -186,22 +186,9 @@ public unsafe struct @kinfo_proc
186186
/// <param name="count">The number of kinfo_proc entries returned.</param>
187187
public static unsafe kinfo_proc* GetProcInfo(int pid, bool threads, out int count)
188188
{
189-
Span<int> sysctlName = stackalloc int[4];
190-
191-
if (pid == 0)
192-
{
193-
// get all processes
194-
sysctlName[3] = 0;
195-
sysctlName[2] = KERN_PROC_PROC;
196-
}
197-
else
198-
{
199-
// get specific process, possibly with threads
200-
sysctlName[3] = pid;
201-
sysctlName[2] = KERN_PROC_PID | (threads ? KERN_PROC_INC_THREAD : 0);
202-
}
203-
sysctlName[1] = KERN_PROC;
204-
sysctlName[0] = CTL_KERN;
189+
ReadOnlySpan<int> sysctlName = pid == 0 ?
190+
[CTL_KERN, KERN_PROC, KERN_PROC_PROC, 0] : // get all processes
191+
[CTL_KERN, KERN_PROC, KERN_PROC_PID | (threads ? KERN_PROC_INC_THREAD : 0), pid]; // get specific process, possibly with threads
205192

206193
byte* pBuffer = null;
207194
int bytesLength = 0;

src/libraries/Common/src/Interop/FreeBSD/Interop.Process.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ internal static unsafe int[] ListAllPids()
7777
/// <param name="pid">The PID of the process</param>
7878
public static unsafe string GetProcPath(int pid)
7979
{
80-
Span<int> sysctlName = [CTL_KERN, KERN_PROC, KERN_PROC_PATHNAME, pid];
80+
ReadOnlySpan<int> sysctlName = [CTL_KERN, KERN_PROC, KERN_PROC_PATHNAME, pid];
8181
byte* pBuffer = null;
8282
int bytesLength = 0;
8383

src/libraries/Common/src/Interop/OSX/Interop.CoreFoundation.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ internal static unsafe SafeCreateHandle CFArrayCreate(IntPtr[] values, UIntPtr n
154154
/// </summary>
155155
/// <param name="values">The values to put in the array</param>
156156
/// <returns>Returns a valid SafeCreateHandle to a CFArray on success; otherwise, returns an invalid SafeCreateHandle</returns>
157-
internal static unsafe SafeCreateHandle CFArrayCreate(Span<IntPtr> values)
157+
internal static unsafe SafeCreateHandle CFArrayCreate(ReadOnlySpan<IntPtr> values)
158158
{
159159
fixed (IntPtr* pValues = &MemoryMarshal.GetReference(values))
160160
{

src/libraries/Common/src/Interop/OSX/System.Security.Cryptography.Native.Apple/Interop.Ssl.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ private static partial int AppleCryptoNative_SslIsHostnameMatch(
174174
[LibraryImport(Interop.Libraries.AppleCryptoNative, EntryPoint = "AppleCryptoNative_SslSetCertificateAuthorities")]
175175
internal static partial int SslSetCertificateAuthorities(SafeSslHandle sslHandle, SafeCreateHandle certificateOrArray, int replaceExisting);
176176

177-
internal static unsafe void SslSetCertificateAuthorities(SafeSslHandle sslHandle, Span<IntPtr> certificates, bool replaceExisting)
177+
internal static unsafe void SslSetCertificateAuthorities(SafeSslHandle sslHandle, ReadOnlySpan<IntPtr> certificates, bool replaceExisting)
178178
{
179179
using (SafeCreateHandle cfCertRefs = CoreFoundation.CFArrayCreate(certificates))
180180
{

src/libraries/Common/src/Interop/Windows/BCrypt/Interop.BCryptEncryptDecrypt.RSA.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ private static unsafe int BCryptEncryptRsa(
5252
BCryptEncryptFlags dwFlags)
5353
{
5454
// BCryptEncrypt does not accept null/0, only non-null/0.
55-
Span<byte> notNull = stackalloc byte[1];
55+
ReadOnlySpan<byte> notNull = stackalloc byte[1];
5656
scoped ReadOnlySpan<byte> effectiveSource;
5757

5858
if (source.IsEmpty)

src/libraries/Common/src/Interop/Windows/IpHlpApi/Interop.NetworkInformation.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -503,7 +503,7 @@ internal static unsafe partial uint GetAdaptersAddresses(
503503
uint* outBufLen);
504504

505505
[LibraryImport(Interop.Libraries.IpHlpApi)]
506-
internal static unsafe partial uint GetBestInterfaceEx(Span<byte> ipAddress, int* index);
506+
internal static unsafe partial uint GetBestInterfaceEx(ReadOnlySpan<byte> ipAddress, int* index);
507507

508508
[LibraryImport(Interop.Libraries.IpHlpApi)]
509509
internal static partial uint GetIfEntry2(ref MibIfRow2 pIfRow);

src/libraries/Common/src/Interop/Windows/Kernel32/Interop.FormatMessage.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ internal static unsafe string GetMessage(int errorCode, IntPtr moduleHandle)
6161
int length = FormatMessage(flags | FORMAT_MESSAGE_ALLOCATE_BUFFER, moduleHandle, unchecked((uint)errorCode), 0, &nativeMsgPtr, 0, IntPtr.Zero);
6262
if (length > 0)
6363
{
64-
return GetAndTrimString(new Span<char>((char*)nativeMsgPtr, length));
64+
return GetAndTrimString(new ReadOnlySpan<char>((char*)nativeMsgPtr, length));
6565
}
6666
}
6767
finally
@@ -74,7 +74,7 @@ internal static unsafe string GetMessage(int errorCode, IntPtr moduleHandle)
7474
return $"Unknown error (0x{errorCode:x})";
7575
}
7676

77-
private static string GetAndTrimString(Span<char> buffer)
77+
private static string GetAndTrimString(ReadOnlySpan<char> buffer)
7878
{
7979
int length = buffer.Length;
8080
while (length > 0 && buffer[length - 1] <= 32)

src/libraries/Common/src/Interop/Windows/NCrypt/Interop.NCryptDeriveKeyMaterial.cs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -218,26 +218,22 @@ internal static unsafe byte[] DeriveKeyMaterialTls(
218218
byte[] seed,
219219
SecretAgreementFlags flags)
220220
{
221-
Span<NCryptBuffer> buffers = stackalloc NCryptBuffer[2];
222-
223221
fixed (byte* pLabel = label, pSeed = seed)
224222
{
225223
NCryptBuffer labelBuffer = default;
226224
labelBuffer.cbBuffer = label.Length;
227225
labelBuffer.BufferType = BufferType.KdfTlsLabel;
228226
labelBuffer.pvBuffer = new IntPtr(pLabel);
229-
buffers[0] = labelBuffer;
230227

231228
NCryptBuffer seedBuffer = default;
232229
seedBuffer.cbBuffer = seed.Length;
233230
seedBuffer.BufferType = BufferType.KdfTlsSeed;
234231
seedBuffer.pvBuffer = new IntPtr(pSeed);
235-
buffers[1] = seedBuffer;
236232

237233
return DeriveKeyMaterial(
238234
secretAgreement,
239235
BCryptNative.KeyDerivationFunction.Tls,
240-
buffers,
236+
[labelBuffer, seedBuffer],
241237
flags);
242238
}
243239
}

0 commit comments

Comments
 (0)