Skip to content

Remove some unnecessary fixed blocks #71317

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

Merged
merged 1 commit into from
Jun 27, 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 @@ -35,10 +35,7 @@ public bool IsIPv6
public override unsafe int GetHashCode()
{
HashCode h = default;
fixed (byte* ptr = Address)
{
h.AddBytes(new ReadOnlySpan<byte>(ptr, IsIPv6 ? IPv6AddressBytes : IPv4AddressBytes));
}
h.AddBytes(MemoryMarshal.CreateReadOnlySpan(ref Address[0], IsIPv6 ? IPv6AddressBytes : IPv4AddressBytes));
return h.ToHashCode();
}

Expand Down Expand Up @@ -71,11 +68,8 @@ public bool Equals(IPAddress other)
addressByteCount = IPv4AddressBytes;
}

fixed (byte* thisAddress = Address)
{
return new ReadOnlySpan<byte>(thisAddress, addressByteCount).SequenceEqual(
new ReadOnlySpan<byte>(other.Address, addressByteCount));
}
return MemoryMarshal.CreateReadOnlySpan(ref Address[0], addressByteCount).SequenceEqual(
new ReadOnlySpan<byte>(other.Address, addressByteCount));
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,8 @@ internal unsafe struct WIN32_FIND_DATA
private fixed char _cFileName[MAX_PATH];
private fixed char _cAlternateFileName[14];

internal ReadOnlySpan<char> cFileName
{
get { fixed (char* c = _cFileName) return new ReadOnlySpan<char>(c, MAX_PATH); }
}
internal ReadOnlySpan<char> cFileName =>
MemoryMarshal.CreateReadOnlySpan(ref _cFileName[0], MAX_PATH);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public struct FILE_FULL_DIR_INFORMATION
public uint EaSize;

private char _fileName;
public unsafe ReadOnlySpan<char> FileName { get { fixed (char* c = &_fileName) { return new ReadOnlySpan<char>(c, (int)FileNameLength / sizeof(char)); } } }
public unsafe ReadOnlySpan<char> FileName => MemoryMarshal.CreateReadOnlySpan(ref _fileName, (int)FileNameLength / sizeof(char));

/// <summary>
/// Gets the next info pointer or null if there are no more.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,7 @@ internal unsafe struct SecPkgContext_ApplicationProtocol
public ApplicationProtocolNegotiationExt ProtoNegoExt;
public byte ProtocolIdSize;
public fixed byte ProtocolId[MaxProtocolIdSize];
public ReadOnlySpan<byte> Protocol
{
get
{
fixed (byte* pid = ProtocolId)
{
return new ReadOnlySpan<byte>(pid, ProtocolIdSize);
}
}
}
public ReadOnlySpan<byte> Protocol =>
MemoryMarshal.CreateReadOnlySpan(ref ProtocolId[0], ProtocolIdSize);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,7 @@ public unsafe struct LOGFONT
public byte lfQuality;
public byte lfPitchAndFamily;
private fixed char _lfFaceName[LF_FACESIZE];
public Span<char> lfFaceName
{
get { fixed (char* c = _lfFaceName) { return new Span<char>(c, LF_FACESIZE); } }
}
public Span<char> lfFaceName => MemoryMarshal.CreateSpan(ref _lfFaceName[0], LF_FACESIZE);

public override string ToString()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,33 +50,25 @@ public Native(WAVEOUTCAPS managed)
wMid = managed.wMid;
wPid = managed.wPid;
vDriverVersion = managed.vDriverVersion;
fixed (char* pszPname = szPname)
{
managed.szPname.AsSpan().CopyTo(new Span<char>(pszPname, szPnameLength));
}
managed.szPname.CopyTo(MemoryMarshal.CreateSpan(ref szPname[0], szPnameLength));
dwFormats = managed.dwFormats;
wChannels = managed.wChannels;
wReserved1 = managed.wReserved1;
dwSupport = managed.dwSupport;
}

public WAVEOUTCAPS ToManaged()
{
fixed (char* pszPname = szPname)
public WAVEOUTCAPS ToManaged() =>
new WAVEOUTCAPS
{
return new WAVEOUTCAPS
{
wMid = wMid,
wPid = wPid,
vDriverVersion = vDriverVersion,
szPname = new Span<char>(pszPname, szPnameLength).ToString(),
dwFormats = dwFormats,
wChannels = wChannels,
wReserved1 = wReserved1,
dwSupport = dwSupport,
};
}
}
wMid = wMid,
wPid = wPid,
vDriverVersion = vDriverVersion,
szPname = MemoryMarshal.CreateReadOnlySpan(ref szPname[0], szPnameLength).ToString(),
dwFormats = dwFormats,
wChannels = wChannels,
wReserved1 = wReserved1,
dwSupport = dwSupport,
};
}
}
#endif
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,10 +89,7 @@ public unsafe struct Native
public Native(IPv6MulticastRequest managed)
{
Debug.Assert(managed.MulticastAddress.Length == MulticastAddressLength);
fixed (void* dest = _multicastAddress)
{
managed.MulticastAddress.CopyTo(new Span<byte>(dest, MulticastAddressLength));
}
managed.MulticastAddress.CopyTo(MemoryMarshal.CreateSpan(ref _multicastAddress[0], MulticastAddressLength));
_interfaceIndex = managed.InterfaceIndex;
}

Expand All @@ -103,10 +100,7 @@ public IPv6MulticastRequest ToManaged()
MulticastAddress = new byte[MulticastAddressLength],
InterfaceIndex = _interfaceIndex
};
fixed (void* src = _multicastAddress)
{
new Span<byte>(src, 16).CopyTo(managed.MulticastAddress);
}
MemoryMarshal.CreateReadOnlySpan(ref _multicastAddress[0], MulticastAddressLength).CopyTo(managed.MulticastAddress);
return managed;
}
}
Expand Down
11 changes: 4 additions & 7 deletions src/libraries/Common/src/System/Net/NTAuthentication.Managed.cs
Original file line number Diff line number Diff line change
Expand Up @@ -424,13 +424,10 @@ private static unsafe void SetField(ref MessageField field, int length, int offs
throw new Win32Exception(NTE_FAIL);
}

fixed (void* ptr = &field)
{
Span<byte> span = new Span<byte>(ptr, sizeof(MessageField));
BinaryPrimitives.WriteInt16LittleEndian(span, (short)length);
BinaryPrimitives.WriteInt16LittleEndian(span.Slice(2), (short)length);
BinaryPrimitives.WriteInt32LittleEndian(span.Slice(4), offset);
}
Span<byte> span = MemoryMarshal.AsBytes(MemoryMarshal.CreateSpan(ref field, 1));
BinaryPrimitives.WriteInt16LittleEndian(span, (short)length);
BinaryPrimitives.WriteInt16LittleEndian(span.Slice(2), (short)length);
BinaryPrimitives.WriteInt32LittleEndian(span.Slice(4), offset);
}

private static void AddToPayload(ref MessageField field, ReadOnlySpan<byte> data, Span<byte> payload, ref int offset)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,28 +32,12 @@ private unsafe TcpConnectionInformation[] GetTcpConnections(bool listeners)
continue;
}

byte[] localBytes = new byte[nativeInfo.LocalEndPoint.NumAddressBytes];
fixed (byte* localBytesPtr = localBytes)
{
Buffer.MemoryCopy(nativeInfo.LocalEndPoint.AddressBytes, localBytesPtr, localBytes.Length, localBytes.Length);
}
IPAddress localIPAddress = new IPAddress(localBytes);
IPAddress localIPAddress = new IPAddress(new ReadOnlySpan<byte>(nativeInfo.LocalEndPoint.AddressBytes, checked((int)nativeInfo.LocalEndPoint.NumAddressBytes)));
IPEndPoint local = new IPEndPoint(localIPAddress, (int)nativeInfo.LocalEndPoint.Port);

IPAddress remoteIPAddress;
if (nativeInfo.RemoteEndPoint.NumAddressBytes == 0)
{
remoteIPAddress = IPAddress.Any;
}
else
{
byte[] remoteBytes = new byte[nativeInfo.RemoteEndPoint.NumAddressBytes];
fixed (byte* remoteBytesPtr = &remoteBytes[0])
{
Buffer.MemoryCopy(nativeInfo.RemoteEndPoint.AddressBytes, remoteBytesPtr, remoteBytes.Length, remoteBytes.Length);
}
remoteIPAddress = new IPAddress(remoteBytes);
}
IPAddress remoteIPAddress = nativeInfo.RemoteEndPoint.NumAddressBytes == 0 ?
IPAddress.Any :
new IPAddress(new ReadOnlySpan<byte>(nativeInfo.RemoteEndPoint.AddressBytes, checked((int)nativeInfo.RemoteEndPoint.NumAddressBytes)));

IPEndPoint remote = new IPEndPoint(remoteIPAddress, (int)nativeInfo.RemoteEndPoint.Port);
connectionInformations[nextResultIndex++] = new SimpleTcpConnectionInformation(local, remote, state);
Expand Down Expand Up @@ -101,20 +85,9 @@ public override unsafe IPEndPoint[] GetActiveUdpListeners()
{
Interop.Sys.IPEndPointInfo endPointInfo = infos[i];
int port = (int)endPointInfo.Port;
IPAddress ipAddress;
if (endPointInfo.NumAddressBytes == 0)
{
ipAddress = IPAddress.Any;
}
else
{
byte[] bytes = new byte[endPointInfo.NumAddressBytes];
fixed (byte* bytesPtr = &bytes[0])
{
Buffer.MemoryCopy(endPointInfo.AddressBytes, bytesPtr, bytes.Length, bytes.Length);
}
ipAddress = new IPAddress(bytes);
}
IPAddress ipAddress = endPointInfo.NumAddressBytes == 0 ?
IPAddress.Any :
new IPAddress(new ReadOnlySpan<byte>(endPointInfo.AddressBytes, checked((int)endPointInfo.NumAddressBytes)));

endPoints[i] = new IPEndPoint(ipAddress, port);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ private static unsafe void OnGatewayFound(void* pContext, Interop.Sys.IpAddressI
{
ref Context context = ref Unsafe.As<byte, Context>(ref *(byte*)pContext);

IPAddress ipAddress = new IPAddress(new Span<byte>(gatewayAddressInfo->AddressBytes, gatewayAddressInfo->NumAddressBytes).ToArray());
IPAddress ipAddress = new IPAddress(new ReadOnlySpan<byte>(gatewayAddressInfo->AddressBytes, gatewayAddressInfo->NumAddressBytes));
if (ipAddress.IsIPv6LinkLocal)
{
// For Link-Local addresses add ScopeId as that is not part of the route entry.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// The .NET Foundation licenses this file to you under the MIT license.

using System.Diagnostics;
using System.Runtime.InteropServices;

namespace System.IO.Enumeration
{
Expand Down Expand Up @@ -88,11 +89,8 @@ public ReadOnlySpan<char> FileName
{
if (_directoryEntry.NameLength != 0 && _fileName.Length == 0)
{
fixed (char* c = _fileNameBuffer)
{
Span<char> buffer = new Span<char>(c, Interop.Sys.DirectoryEntry.NameBufferSize);
_fileName = _directoryEntry.GetName(buffer);
}
Span<char> buffer = MemoryMarshal.CreateSpan(ref _fileNameBuffer[0], Interop.Sys.DirectoryEntry.NameBufferSize);
_fileName = _directoryEntry.GetName(buffer);
}

return _fileName;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,7 @@ internal static unsafe int StringToAnsiString(string s, byte* buffer, int buffer
{
Debug.Assert(bufferLength >= (s.Length + 1) * SystemMaxDBCSCharSize, "Insufficient buffer length passed to StringToAnsiString");

int convertedBytes;

fixed (char* pChar = s)
{
convertedBytes = Encoding.UTF8.GetBytes(pChar, s.Length, buffer, bufferLength);
}

int convertedBytes = Encoding.UTF8.GetBytes(s, new Span<byte>(buffer, bufferLength));
buffer[convertedBytes] = 0;

return convertedBytes;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -995,14 +995,8 @@ private static unsafe IntPtr StringToHGlobalUTF8(string? s)

IntPtr ptr = AllocHGlobal(checked(nb + 1));

int nbWritten;
byte* pbMem = (byte*)ptr;

fixed (char* firstChar = s)
{
nbWritten = Encoding.UTF8.GetBytes(firstChar, s.Length, pbMem, nb);
}

int nbWritten = Encoding.UTF8.GetBytes(s, new Span<byte>(pbMem, nb));
pbMem[nbWritten] = 0;

return ptr;
Expand Down Expand Up @@ -1042,14 +1036,8 @@ public static unsafe IntPtr StringToCoTaskMemUTF8(string? s)

IntPtr ptr = AllocCoTaskMem(checked(nb + 1));

int nbWritten;
byte* pbMem = (byte*)ptr;

fixed (char* firstChar = s)
{
nbWritten = Encoding.UTF8.GetBytes(firstChar, s.Length, pbMem, nb);
}

int nbWritten = Encoding.UTF8.GetBytes(s, new Span<byte>(pbMem, nb));
pbMem[nbWritten] = 0;

return ptr;
Expand Down