未验证 提交 a38b50c3 编写于 作者: S Stephen Toub 提交者: GitHub

Remove some unnecessary fixed blocks (#71317)

上级 aa06c896
...@@ -35,10 +35,7 @@ public bool IsIPv6 ...@@ -35,10 +35,7 @@ public bool IsIPv6
public override unsafe int GetHashCode() public override unsafe int GetHashCode()
{ {
HashCode h = default; HashCode h = default;
fixed (byte* ptr = Address) h.AddBytes(MemoryMarshal.CreateReadOnlySpan(ref Address[0], IsIPv6 ? IPv6AddressBytes : IPv4AddressBytes));
{
h.AddBytes(new ReadOnlySpan<byte>(ptr, IsIPv6 ? IPv6AddressBytes : IPv4AddressBytes));
}
return h.ToHashCode(); return h.ToHashCode();
} }
...@@ -71,11 +68,8 @@ public bool Equals(IPAddress other) ...@@ -71,11 +68,8 @@ public bool Equals(IPAddress other)
addressByteCount = IPv4AddressBytes; addressByteCount = IPv4AddressBytes;
} }
fixed (byte* thisAddress = Address) return MemoryMarshal.CreateReadOnlySpan(ref Address[0], addressByteCount).SequenceEqual(
{ new ReadOnlySpan<byte>(other.Address, addressByteCount));
return new ReadOnlySpan<byte>(thisAddress, addressByteCount).SequenceEqual(
new ReadOnlySpan<byte>(other.Address, addressByteCount));
}
} }
} }
} }
......
...@@ -22,10 +22,8 @@ internal unsafe struct WIN32_FIND_DATA ...@@ -22,10 +22,8 @@ internal unsafe struct WIN32_FIND_DATA
private fixed char _cFileName[MAX_PATH]; private fixed char _cFileName[MAX_PATH];
private fixed char _cAlternateFileName[14]; private fixed char _cAlternateFileName[14];
internal ReadOnlySpan<char> cFileName internal ReadOnlySpan<char> cFileName =>
{ MemoryMarshal.CreateReadOnlySpan(ref _cFileName[0], MAX_PATH);
get { fixed (char* c = _cFileName) return new ReadOnlySpan<char>(c, MAX_PATH); }
}
} }
} }
} }
...@@ -54,7 +54,7 @@ public struct FILE_FULL_DIR_INFORMATION ...@@ -54,7 +54,7 @@ public struct FILE_FULL_DIR_INFORMATION
public uint EaSize; public uint EaSize;
private char _fileName; 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> /// <summary>
/// Gets the next info pointer or null if there are no more. /// Gets the next info pointer or null if there are no more.
......
...@@ -29,15 +29,7 @@ internal unsafe struct SecPkgContext_ApplicationProtocol ...@@ -29,15 +29,7 @@ internal unsafe struct SecPkgContext_ApplicationProtocol
public ApplicationProtocolNegotiationExt ProtoNegoExt; public ApplicationProtocolNegotiationExt ProtoNegoExt;
public byte ProtocolIdSize; public byte ProtocolIdSize;
public fixed byte ProtocolId[MaxProtocolIdSize]; public fixed byte ProtocolId[MaxProtocolIdSize];
public ReadOnlySpan<byte> Protocol public ReadOnlySpan<byte> Protocol =>
{ MemoryMarshal.CreateReadOnlySpan(ref ProtocolId[0], ProtocolIdSize);
get
{
fixed (byte* pid = ProtocolId)
{
return new ReadOnlySpan<byte>(pid, ProtocolIdSize);
}
}
}
} }
} }
...@@ -27,10 +27,7 @@ public unsafe struct LOGFONT ...@@ -27,10 +27,7 @@ public unsafe struct LOGFONT
public byte lfQuality; public byte lfQuality;
public byte lfPitchAndFamily; public byte lfPitchAndFamily;
private fixed char _lfFaceName[LF_FACESIZE]; private fixed char _lfFaceName[LF_FACESIZE];
public Span<char> lfFaceName public Span<char> lfFaceName => MemoryMarshal.CreateSpan(ref _lfFaceName[0], LF_FACESIZE);
{
get { fixed (char* c = _lfFaceName) { return new Span<char>(c, LF_FACESIZE); } }
}
public override string ToString() public override string ToString()
{ {
......
...@@ -50,33 +50,25 @@ public Native(WAVEOUTCAPS managed) ...@@ -50,33 +50,25 @@ public Native(WAVEOUTCAPS managed)
wMid = managed.wMid; wMid = managed.wMid;
wPid = managed.wPid; wPid = managed.wPid;
vDriverVersion = managed.vDriverVersion; vDriverVersion = managed.vDriverVersion;
fixed (char* pszPname = szPname) managed.szPname.CopyTo(MemoryMarshal.CreateSpan(ref szPname[0], szPnameLength));
{
managed.szPname.AsSpan().CopyTo(new Span<char>(pszPname, szPnameLength));
}
dwFormats = managed.dwFormats; dwFormats = managed.dwFormats;
wChannels = managed.wChannels; wChannels = managed.wChannels;
wReserved1 = managed.wReserved1; wReserved1 = managed.wReserved1;
dwSupport = managed.dwSupport; dwSupport = managed.dwSupport;
} }
public WAVEOUTCAPS ToManaged() public WAVEOUTCAPS ToManaged() =>
{ new WAVEOUTCAPS
fixed (char* pszPname = szPname)
{ {
return new WAVEOUTCAPS wMid = wMid,
{ wPid = wPid,
wMid = wMid, vDriverVersion = vDriverVersion,
wPid = wPid, szPname = MemoryMarshal.CreateReadOnlySpan(ref szPname[0], szPnameLength).ToString(),
vDriverVersion = vDriverVersion, dwFormats = dwFormats,
szPname = new Span<char>(pszPname, szPnameLength).ToString(), wChannels = wChannels,
dwFormats = dwFormats, wReserved1 = wReserved1,
wChannels = wChannels, dwSupport = dwSupport,
wReserved1 = wReserved1, };
dwSupport = dwSupport,
};
}
}
} }
} }
#endif #endif
......
...@@ -89,10 +89,7 @@ public unsafe struct Native ...@@ -89,10 +89,7 @@ public unsafe struct Native
public Native(IPv6MulticastRequest managed) public Native(IPv6MulticastRequest managed)
{ {
Debug.Assert(managed.MulticastAddress.Length == MulticastAddressLength); Debug.Assert(managed.MulticastAddress.Length == MulticastAddressLength);
fixed (void* dest = _multicastAddress) managed.MulticastAddress.CopyTo(MemoryMarshal.CreateSpan(ref _multicastAddress[0], MulticastAddressLength));
{
managed.MulticastAddress.CopyTo(new Span<byte>(dest, MulticastAddressLength));
}
_interfaceIndex = managed.InterfaceIndex; _interfaceIndex = managed.InterfaceIndex;
} }
...@@ -103,10 +100,7 @@ public IPv6MulticastRequest ToManaged() ...@@ -103,10 +100,7 @@ public IPv6MulticastRequest ToManaged()
MulticastAddress = new byte[MulticastAddressLength], MulticastAddress = new byte[MulticastAddressLength],
InterfaceIndex = _interfaceIndex InterfaceIndex = _interfaceIndex
}; };
fixed (void* src = _multicastAddress) MemoryMarshal.CreateReadOnlySpan(ref _multicastAddress[0], MulticastAddressLength).CopyTo(managed.MulticastAddress);
{
new Span<byte>(src, 16).CopyTo(managed.MulticastAddress);
}
return managed; return managed;
} }
} }
......
...@@ -424,13 +424,10 @@ private static unsafe void SetField(ref MessageField field, int length, int offs ...@@ -424,13 +424,10 @@ private static unsafe void SetField(ref MessageField field, int length, int offs
throw new Win32Exception(NTE_FAIL); throw new Win32Exception(NTE_FAIL);
} }
fixed (void* ptr = &field) Span<byte> span = MemoryMarshal.AsBytes(MemoryMarshal.CreateSpan(ref field, 1));
{ BinaryPrimitives.WriteInt16LittleEndian(span, (short)length);
Span<byte> span = new Span<byte>(ptr, sizeof(MessageField)); BinaryPrimitives.WriteInt16LittleEndian(span.Slice(2), (short)length);
BinaryPrimitives.WriteInt16LittleEndian(span, (short)length); BinaryPrimitives.WriteInt32LittleEndian(span.Slice(4), offset);
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) private static void AddToPayload(ref MessageField field, ReadOnlySpan<byte> data, Span<byte> payload, ref int offset)
......
...@@ -32,28 +32,12 @@ private unsafe TcpConnectionInformation[] GetTcpConnections(bool listeners) ...@@ -32,28 +32,12 @@ private unsafe TcpConnectionInformation[] GetTcpConnections(bool listeners)
continue; continue;
} }
byte[] localBytes = new byte[nativeInfo.LocalEndPoint.NumAddressBytes]; IPAddress localIPAddress = new IPAddress(new ReadOnlySpan<byte>(nativeInfo.LocalEndPoint.AddressBytes, checked((int)nativeInfo.LocalEndPoint.NumAddressBytes)));
fixed (byte* localBytesPtr = localBytes)
{
Buffer.MemoryCopy(nativeInfo.LocalEndPoint.AddressBytes, localBytesPtr, localBytes.Length, localBytes.Length);
}
IPAddress localIPAddress = new IPAddress(localBytes);
IPEndPoint local = new IPEndPoint(localIPAddress, (int)nativeInfo.LocalEndPoint.Port); IPEndPoint local = new IPEndPoint(localIPAddress, (int)nativeInfo.LocalEndPoint.Port);
IPAddress remoteIPAddress; IPAddress remoteIPAddress = nativeInfo.RemoteEndPoint.NumAddressBytes == 0 ?
if (nativeInfo.RemoteEndPoint.NumAddressBytes == 0) IPAddress.Any :
{ new IPAddress(new ReadOnlySpan<byte>(nativeInfo.RemoteEndPoint.AddressBytes, checked((int)nativeInfo.RemoteEndPoint.NumAddressBytes)));
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);
}
IPEndPoint remote = new IPEndPoint(remoteIPAddress, (int)nativeInfo.RemoteEndPoint.Port); IPEndPoint remote = new IPEndPoint(remoteIPAddress, (int)nativeInfo.RemoteEndPoint.Port);
connectionInformations[nextResultIndex++] = new SimpleTcpConnectionInformation(local, remote, state); connectionInformations[nextResultIndex++] = new SimpleTcpConnectionInformation(local, remote, state);
...@@ -101,20 +85,9 @@ public override unsafe IPEndPoint[] GetActiveUdpListeners() ...@@ -101,20 +85,9 @@ public override unsafe IPEndPoint[] GetActiveUdpListeners()
{ {
Interop.Sys.IPEndPointInfo endPointInfo = infos[i]; Interop.Sys.IPEndPointInfo endPointInfo = infos[i];
int port = (int)endPointInfo.Port; int port = (int)endPointInfo.Port;
IPAddress ipAddress; IPAddress ipAddress = endPointInfo.NumAddressBytes == 0 ?
if (endPointInfo.NumAddressBytes == 0) IPAddress.Any :
{ new IPAddress(new ReadOnlySpan<byte>(endPointInfo.AddressBytes, checked((int)endPointInfo.NumAddressBytes)));
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);
}
endPoints[i] = new IPEndPoint(ipAddress, port); endPoints[i] = new IPEndPoint(ipAddress, port);
} }
......
...@@ -93,7 +93,7 @@ private static unsafe void OnGatewayFound(void* pContext, Interop.Sys.IpAddressI ...@@ -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); 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) if (ipAddress.IsIPv6LinkLocal)
{ {
// For Link-Local addresses add ScopeId as that is not part of the route entry. // For Link-Local addresses add ScopeId as that is not part of the route entry.
......
...@@ -2,6 +2,7 @@ ...@@ -2,6 +2,7 @@
// The .NET Foundation licenses this file to you under the MIT license. // The .NET Foundation licenses this file to you under the MIT license.
using System.Diagnostics; using System.Diagnostics;
using System.Runtime.InteropServices;
namespace System.IO.Enumeration namespace System.IO.Enumeration
{ {
...@@ -88,11 +89,8 @@ public ReadOnlySpan<char> FileName ...@@ -88,11 +89,8 @@ public ReadOnlySpan<char> FileName
{ {
if (_directoryEntry.NameLength != 0 && _fileName.Length == 0) if (_directoryEntry.NameLength != 0 && _fileName.Length == 0)
{ {
fixed (char* c = _fileNameBuffer) Span<char> buffer = MemoryMarshal.CreateSpan(ref _fileNameBuffer[0], Interop.Sys.DirectoryEntry.NameBufferSize);
{ _fileName = _directoryEntry.GetName(buffer);
Span<char> buffer = new Span<char>(c, Interop.Sys.DirectoryEntry.NameBufferSize);
_fileName = _directoryEntry.GetName(buffer);
}
} }
return _fileName; return _fileName;
......
...@@ -36,13 +36,7 @@ internal static unsafe int StringToAnsiString(string s, byte* buffer, int buffer ...@@ -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"); Debug.Assert(bufferLength >= (s.Length + 1) * SystemMaxDBCSCharSize, "Insufficient buffer length passed to StringToAnsiString");
int convertedBytes; int convertedBytes = Encoding.UTF8.GetBytes(s, new Span<byte>(buffer, bufferLength));
fixed (char* pChar = s)
{
convertedBytes = Encoding.UTF8.GetBytes(pChar, s.Length, buffer, bufferLength);
}
buffer[convertedBytes] = 0; buffer[convertedBytes] = 0;
return convertedBytes; return convertedBytes;
......
...@@ -995,14 +995,8 @@ private static unsafe IntPtr StringToHGlobalUTF8(string? s) ...@@ -995,14 +995,8 @@ private static unsafe IntPtr StringToHGlobalUTF8(string? s)
IntPtr ptr = AllocHGlobal(checked(nb + 1)); IntPtr ptr = AllocHGlobal(checked(nb + 1));
int nbWritten;
byte* pbMem = (byte*)ptr; byte* pbMem = (byte*)ptr;
int nbWritten = Encoding.UTF8.GetBytes(s, new Span<byte>(pbMem, nb));
fixed (char* firstChar = s)
{
nbWritten = Encoding.UTF8.GetBytes(firstChar, s.Length, pbMem, nb);
}
pbMem[nbWritten] = 0; pbMem[nbWritten] = 0;
return ptr; return ptr;
...@@ -1042,14 +1036,8 @@ public static unsafe IntPtr StringToCoTaskMemUTF8(string? s) ...@@ -1042,14 +1036,8 @@ public static unsafe IntPtr StringToCoTaskMemUTF8(string? s)
IntPtr ptr = AllocCoTaskMem(checked(nb + 1)); IntPtr ptr = AllocCoTaskMem(checked(nb + 1));
int nbWritten;
byte* pbMem = (byte*)ptr; byte* pbMem = (byte*)ptr;
int nbWritten = Encoding.UTF8.GetBytes(s, new Span<byte>(pbMem, nb));
fixed (char* firstChar = s)
{
nbWritten = Encoding.UTF8.GetBytes(firstChar, s.Length, pbMem, nb);
}
pbMem[nbWritten] = 0; pbMem[nbWritten] = 0;
return ptr; return ptr;
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册