Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use scoped more in dotnet/runtime #71589

Merged
merged 3 commits into from
Jul 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ internal static bool IncludeAccessor(MethodInfo? associate, bool nonPublic)

bool isInherited = declaredType != reflectedType;

Span<IntPtr> genericArgumentHandles = stackalloc IntPtr[0];
scoped Span<IntPtr> genericArgumentHandles = default;
RuntimeType[] genericArguments = declaredType.TypeHandle.GetInstantiationInternal();
if (genericArguments != null)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1411,8 +1411,8 @@ public RuntimeTypeHandle ResolveTypeHandle(int typeToken, RuntimeTypeHandle[]? t
RuntimeModule module = GetRuntimeModule();
ValidateModulePointer(module);

ReadOnlySpan<IntPtr> typeInstantiationContextHandles = stackalloc IntPtr[0];
ReadOnlySpan<IntPtr> methodInstantiationContextHandles = stackalloc IntPtr[0];
scoped ReadOnlySpan<IntPtr> typeInstantiationContextHandles = default;
scoped ReadOnlySpan<IntPtr> methodInstantiationContextHandles = default;

// defensive copy of user-provided array, per CopyRuntimeTypeHandles contract
if (typeInstantiationContext?.Length > 0)
Expand Down Expand Up @@ -1517,8 +1517,8 @@ public RuntimeFieldHandle ResolveFieldHandle(int fieldToken, RuntimeTypeHandle[]
RuntimeModule module = GetRuntimeModule();
ValidateModulePointer(module);

ReadOnlySpan<IntPtr> typeInstantiationContextHandles = stackalloc IntPtr[0];
ReadOnlySpan<IntPtr> methodInstantiationContextHandles = stackalloc IntPtr[0];
scoped ReadOnlySpan<IntPtr> typeInstantiationContextHandles = default;
scoped ReadOnlySpan<IntPtr> methodInstantiationContextHandles = default;

// defensive copy of user-provided array, per CopyRuntimeTypeHandles contract
if (typeInstantiationContext?.Length > 0)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1284,7 +1284,7 @@ private RuntimePropertyInfo[] PopulateProperties(Filter filter)

// All elements initialized to false.
int numVirtuals = RuntimeTypeHandle.GetNumVirtuals(declaringType);
Span<bool> usedSlots = stackalloc bool[0];
scoped Span<bool> usedSlots;
if (numVirtuals <= 128) // arbitrary stack limit
{
usedSlots = stackalloc bool[numVirtuals];
Expand Down
30 changes: 3 additions & 27 deletions src/libraries/Common/src/Interop/Windows/SspiCli/SSPIWrapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -201,20 +201,12 @@ public static int QuerySecurityContextToken(ISSPIInterface secModule, SafeDelete

public static bool QueryBlittableContextAttributes<T>(ISSPIInterface secModule, SafeDeleteContext securityContext, Interop.SspiCli.ContextAttribute contextAttribute, ref T attribute) where T : unmanaged
{
Span<T> span =
#if NETSTANDARD2_0
stackalloc T[1] { attribute };
#else
MemoryMarshal.CreateSpan(ref attribute, 1);
#endif
Span<T> span = new Span<T>(ref attribute);
int errorCode = secModule.QueryContextAttributes(
securityContext, contextAttribute,
MemoryMarshal.AsBytes(span),
null,
out SafeHandle? sspiHandle);
#if NETSTANDARD2_0
attribute = span[0];
#endif

using (sspiHandle)
{
Expand All @@ -230,20 +222,12 @@ public static bool QueryBlittableContextAttributes<T>(ISSPIInterface secModule,

public static bool QueryBlittableContextAttributes<T>(ISSPIInterface secModule, SafeDeleteContext securityContext, Interop.SspiCli.ContextAttribute contextAttribute, Type safeHandleType, out SafeHandle? sspiHandle, ref T attribute) where T : unmanaged
{
Span<T> span =
#if NETSTANDARD2_0
stackalloc T[1] { attribute };
#else
MemoryMarshal.CreateSpan(ref attribute, 1);
#endif
Span<T> span = new Span<T>(ref attribute);
int errorCode = secModule.QueryContextAttributes(
securityContext, contextAttribute,
MemoryMarshal.AsBytes(span),
safeHandleType,
out sspiHandle);
#if NETSTANDARD2_0
attribute = span[0];
#endif

if (errorCode != 0)
{
Expand Down Expand Up @@ -318,21 +302,13 @@ public static bool QueryContextAttributes_SECPKG_ATTR_REMOTE_CERT_CHAIN(ISSPIInt

public static bool QueryContextAttributes_SECPKG_ATTR_ISSUER_LIST_EX(ISSPIInterface secModule, SafeDeleteContext securityContext, ref Interop.SspiCli.SecPkgContext_IssuerListInfoEx ctx, out SafeHandle? sspiHandle)
{
Span<Interop.SspiCli.SecPkgContext_IssuerListInfoEx> buffer =
#if NETSTANDARD2_0
stackalloc Interop.SspiCli.SecPkgContext_IssuerListInfoEx[1] { ctx };
#else
MemoryMarshal.CreateSpan(ref ctx, 1);
#endif
Span<Interop.SspiCli.SecPkgContext_IssuerListInfoEx> buffer = new Span<Interop.SspiCli.SecPkgContext_IssuerListInfoEx>(ref ctx);
int errorCode = secModule.QueryContextAttributes(
securityContext,
Interop.SspiCli.ContextAttribute.SECPKG_ATTR_ISSUER_LIST_EX,
MemoryMarshal.AsBytes(buffer),
typeof(SafeFreeContextBuffer),
out sspiHandle);
#if NETSTANDARD2_0
ctx = buffer[0];
#endif

if (errorCode != 0)
{
Expand Down
13 changes: 3 additions & 10 deletions src/libraries/Common/src/System/HexConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -196,16 +196,9 @@ public static void EncodeToUtf16(ReadOnlySpan<byte> bytes, Span<char> chars, Cas
public static unsafe string ToString(ReadOnlySpan<byte> bytes, Casing casing = Casing.Upper)
{
#if NETFRAMEWORK || NETSTANDARD2_0
Span<char> result = stackalloc char[0];
if (bytes.Length > 16)
{
var array = new char[bytes.Length * 2];
result = array.AsSpan();
}
else
{
result = stackalloc char[bytes.Length * 2];
}
Span<char> result = bytes.Length > 16 ?
new char[bytes.Length * 2].AsSpan() :
stackalloc char[bytes.Length * 2];

int pos = 0;
foreach (byte b in bytes)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -428,7 +428,7 @@ private static unsafe void SetField(ref MessageField field, int length, int offs
throw new Win32Exception(NTE_FAIL);
}

Span<byte> span = MemoryMarshal.AsBytes(MemoryMarshal.CreateSpan(ref field, 1));
Span<byte> span = MemoryMarshal.AsBytes(new Span<MessageField>(ref field));
BinaryPrimitives.WriteInt16LittleEndian(span, (short)length);
BinaryPrimitives.WriteInt16LittleEndian(span.Slice(2), (short)length);
BinaryPrimitives.WriteInt32LittleEndian(span.Slice(4), offset);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ public override byte[] DeriveKeyTls(ECDiffieHellmanPublicKey otherPartyPublicKey
}

// Indicate that secret can hold stackallocs from nested scopes
Span<byte> secret = stackalloc byte[0];
scoped Span<byte> secret;

// Arbitrary limit. But it covers secp521r1, which is the biggest common case.
const int StackAllocMax = 66;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ public override byte[] DeriveKeyTls(ECDiffieHellmanPublicKey otherPartyPublicKey
secretLength = (int)secretLengthU;

// Indicate that secret can hold stackallocs from nested scopes
Span<byte> secret = stackalloc byte[0];
scoped Span<byte> secret;

// Arbitrary limit. But it covers secp521r1, which is the biggest common case.
const int StackAllocMax = 66;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ internal static unsafe int Decrypt(
using (IncrementalHash hasher = IncrementalHash.CreateHash(digestAlgorithmName))
{
Span<byte> buf = stackalloc byte[128];
ReadOnlySpan<byte> effectivePasswordBytes = stackalloc byte[0];
scoped ReadOnlySpan<byte> effectivePasswordBytes = default;
byte[]? rented = null;
System.Text.Encoding? encoding = null;

Expand Down Expand Up @@ -464,7 +464,7 @@ private static unsafe int Pbes2Decrypt(
Span<byte> destination)
{
Span<byte> buf = stackalloc byte[128];
ReadOnlySpan<byte> effectivePasswordBytes = stackalloc byte[0];
scoped ReadOnlySpan<byte> effectivePasswordBytes = default;
byte[]? rented = null;
System.Text.Encoding? encoding = null;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ private static void Derive(

// 4. Set I=S||P to be the concatenation of S and P.
int ILen = checked(SLen + PLen);
Span<byte> I = stackalloc byte[0];
scoped Span<byte> I;
byte[]? IRented = null;

if (ILen <= 1024)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ public override bool TryDecrypt(
// To prevent the OOB write, decrypt into a temporary buffer.
if (destination.Length < keySizeBytes)
{
Span<byte> tmp = stackalloc byte[0];
scoped Span<byte> tmp;
byte[]? rent = null;

// RSA up through 4096 stackalloc
Expand Down
6 changes: 3 additions & 3 deletions src/libraries/System.Console/src/System/ConsolePal.Unix.cs
Original file line number Diff line number Diff line change
Expand Up @@ -615,7 +615,7 @@ static unsafe bool AppendToStdInReaderUntil(byte toFind, StdInReader reader, Spa
}

// Otherwise, push it back into the reader's extra buffer.
reader.AppendExtraBuffer(MemoryMarshal.CreateReadOnlySpan(ref b, 1));
reader.AppendExtraBuffer(new ReadOnlySpan<byte>(in b));
}
}

Expand All @@ -636,7 +636,7 @@ static void ReadRowOrCol(int startExclusive, int endExclusive, StdInReader reade
}
else
{
reader.AppendExtraBuffer(MemoryMarshal.CreateReadOnlySpan(ref b, 1));
reader.AppendExtraBuffer(new ReadOnlySpan<byte>(in b));
}
}

Expand Down Expand Up @@ -1363,7 +1363,7 @@ internal static void WriteStdoutAnsiString(string? value, bool mayChangeCursorPo
if (string.IsNullOrEmpty(value))
return;

Span<byte> data = stackalloc byte[0];
scoped Span<byte> data;
if (value.Length <= 256) // except for extremely rare cases, ANSI escape strings are very short
{
data = stackalloc byte[Encoding.UTF8.GetMaxByteCount(value.Length)];
Expand Down
4 changes: 2 additions & 2 deletions src/libraries/System.Console/src/System/IO/ConsoleStream.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public override void Write(byte[] buffer, int offset, int count)
Write(new ReadOnlySpan<byte>(buffer, offset, count));
}

public override void WriteByte(byte value) => Write(MemoryMarshal.CreateReadOnlySpan(ref value, 1));
public override void WriteByte(byte value) => Write(new ReadOnlySpan<byte>(in value));

public override int Read(byte[] buffer, int offset, int count)
{
Expand All @@ -37,7 +37,7 @@ public override int Read(byte[] buffer, int offset, int count)
public override int ReadByte()
{
byte b = 0;
int result = Read(MemoryMarshal.CreateSpan(ref b, 1));
int result = Read(new Span<byte>(ref b));
return result != 0 ? b : -1;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ private void WriteGeneralizedTimeCore(
// We're only loading in sub-second ticks.
// Ticks are defined as 1e-7 seconds, so their printed form
// is at the longest "0.1234567", or 9 bytes.
Span<byte> fraction = stackalloc byte[0];
scoped Span<byte> fraction = default;

if (!omitFractionalSeconds)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ public override int Read(Span<byte> destination)
public override int ReadByte()
{
byte b = default;
return Read(MemoryMarshal.CreateSpan(ref b, 1)) == 1 ? b : -1;
return Read(new Span<byte>(ref b)) == 1 ? b : -1;
}

public override Task<int> ReadAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public override int Read(byte[] buffer, int offset, int count)
public override int ReadByte()
{
byte b = default;
int bytesRead = Read(MemoryMarshal.CreateSpan(ref b, 1));
int bytesRead = Read(new Span<byte>(ref b));
return bytesRead != 0 ? b : -1;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public override void Write(byte[] buffer, int offset, int count)
/// <para>The encoder ran into invalid data.</para></exception>
public override void WriteByte(byte value)
{
WriteCore(MemoryMarshal.CreateReadOnlySpan(ref value, 1));
WriteCore(new ReadOnlySpan<byte>(in value));
}

/// <summary>Writes a sequence of bytes to the current Brotli stream from a read-only byte span and advances the current position within this Brotli stream by the number of bytes written.</summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ public override int Read(Span<byte> buffer)
public override int ReadByte()
{
byte b = default;
return Read(MemoryMarshal.CreateSpan(ref b, 1)) == 1 ? b : -1;
return Read(new Span<byte>(ref b)) == 1 ? b : -1;
}

private void EnsureNotDisposed()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -488,7 +488,7 @@ public override void WriteByte(byte value)
}
else
{
WriteCore(MemoryMarshal.CreateReadOnlySpan(ref value, 1));
WriteCore(new ReadOnlySpan<byte>(in value));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ public override void WriteByte(byte value)
else
{
CheckDeflateStream();
_deflateStream.WriteCore(MemoryMarshal.CreateReadOnlySpan(ref value, 1));
_deflateStream.WriteCore(new ReadOnlySpan<byte>(in value));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1237,7 +1237,7 @@ public override void Write(ReadOnlySpan<byte> source)
}

public override void WriteByte(byte value) =>
Write(MemoryMarshal.CreateReadOnlySpan(ref value, 1));
Write(new ReadOnlySpan<byte>(in value));

public override Task WriteAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -329,7 +329,7 @@ public override int Read(Span<byte> destination)
public override int ReadByte()
{
byte b = default;
return Read(MemoryMarshal.CreateSpan(ref b, 1)) == 1 ? b : -1;
return Read(new Span<byte>(ref b)) == 1 ? b : -1;
}

public override Task<int> ReadAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken)
Expand Down Expand Up @@ -542,7 +542,7 @@ public override void Write(ReadOnlySpan<byte> source)
}

public override void WriteByte(byte value) =>
Write(MemoryMarshal.CreateReadOnlySpan(ref value, 1));
Write(new ReadOnlySpan<byte>(in value));

public override Task WriteAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken)
{
Expand Down
14 changes: 7 additions & 7 deletions src/libraries/System.Memory/ref/System.Memory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -312,28 +312,28 @@ public static partial class SequenceReaderExtensions
public readonly System.ReadOnlySpan<T> UnreadSpan { get { throw null; } }
public void Advance(long count) { }
public long AdvancePast(T value) { throw null; }
public long AdvancePastAny(System.ReadOnlySpan<T> values) { throw null; }
public long AdvancePastAny(scoped System.ReadOnlySpan<T> values) { throw null; }
public long AdvancePastAny(T value0, T value1) { throw null; }
public long AdvancePastAny(T value0, T value1, T value2) { throw null; }
public long AdvancePastAny(T value0, T value1, T value2, T value3) { throw null; }
public void AdvanceToEnd() { throw null; }
public bool IsNext(System.ReadOnlySpan<T> next, bool advancePast = false) { throw null; }
public bool IsNext(scoped System.ReadOnlySpan<T> next, bool advancePast = false) { throw null; }
public bool IsNext(T next, bool advancePast = false) { throw null; }
public void Rewind(long count) { }
public bool TryAdvanceTo(T delimiter, bool advancePastDelimiter = true) { throw null; }
public bool TryAdvanceToAny(System.ReadOnlySpan<T> delimiters, bool advancePastDelimiter = true) { throw null; }
public bool TryAdvanceToAny(scoped System.ReadOnlySpan<T> delimiters, bool advancePastDelimiter = true) { throw null; }
public readonly bool TryCopyTo(System.Span<T> destination) { throw null; }
public readonly bool TryPeek(out T value) { throw null; }
public readonly bool TryPeek(long offset, out T value) { throw null; }
public bool TryRead(out T value) { throw null; }
public bool TryReadTo(out System.Buffers.ReadOnlySequence<T> sequence, System.ReadOnlySpan<T> delimiter, bool advancePastDelimiter = true) { throw null; }
public bool TryReadTo(out System.Buffers.ReadOnlySequence<T> sequence, scoped System.ReadOnlySpan<T> delimiter, bool advancePastDelimiter = true) { throw null; }
public bool TryReadTo(out System.Buffers.ReadOnlySequence<T> sequence, T delimiter, bool advancePastDelimiter = true) { throw null; }
public bool TryReadTo(out System.Buffers.ReadOnlySequence<T> sequence, T delimiter, T delimiterEscape, bool advancePastDelimiter = true) { throw null; }
public bool TryReadTo(out System.ReadOnlySpan<T> span, System.ReadOnlySpan<T> delimiter, bool advancePastDelimiter = true) { throw null; }
public bool TryReadTo(out System.ReadOnlySpan<T> span, scoped System.ReadOnlySpan<T> delimiter, bool advancePastDelimiter = true) { throw null; }
public bool TryReadTo(out System.ReadOnlySpan<T> span, T delimiter, bool advancePastDelimiter = true) { throw null; }
public bool TryReadTo(out System.ReadOnlySpan<T> span, T delimiter, T delimiterEscape, bool advancePastDelimiter = true) { throw null; }
public bool TryReadToAny(out System.Buffers.ReadOnlySequence<T> sequence, System.ReadOnlySpan<T> delimiters, bool advancePastDelimiter = true) { throw null; }
public bool TryReadToAny(out System.ReadOnlySpan<T> span, System.ReadOnlySpan<T> delimiters, bool advancePastDelimiter = true) { throw null; }
public bool TryReadToAny(out System.Buffers.ReadOnlySequence<T> sequence, scoped System.ReadOnlySpan<T> delimiters, bool advancePastDelimiter = true) { throw null; }
public bool TryReadToAny(out System.ReadOnlySpan<T> span, scoped System.ReadOnlySpan<T> delimiters, bool advancePastDelimiter = true) { throw null; }
public bool TryReadExact(int count, out System.Buffers.ReadOnlySequence<T> sequence) { throw null; }
}
public readonly partial struct StandardFormat : System.IEquatable<System.Buffers.StandardFormat>
Expand Down
Loading