Skip to content

Commit a38b50c

Browse files
authored
Remove some unnecessary fixed blocks (#71317)
1 parent aa06c89 commit a38b50c

File tree

13 files changed

+41
-124
lines changed

13 files changed

+41
-124
lines changed

src/libraries/Common/src/Interop/Unix/System.Native/Interop.IPAddress.cs

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,7 @@ public bool IsIPv6
3535
public override unsafe int GetHashCode()
3636
{
3737
HashCode h = default;
38-
fixed (byte* ptr = Address)
39-
{
40-
h.AddBytes(new ReadOnlySpan<byte>(ptr, IsIPv6 ? IPv6AddressBytes : IPv4AddressBytes));
41-
}
38+
h.AddBytes(MemoryMarshal.CreateReadOnlySpan(ref Address[0], IsIPv6 ? IPv6AddressBytes : IPv4AddressBytes));
4239
return h.ToHashCode();
4340
}
4441

@@ -71,11 +68,8 @@ public bool Equals(IPAddress other)
7168
addressByteCount = IPv4AddressBytes;
7269
}
7370

74-
fixed (byte* thisAddress = Address)
75-
{
76-
return new ReadOnlySpan<byte>(thisAddress, addressByteCount).SequenceEqual(
77-
new ReadOnlySpan<byte>(other.Address, addressByteCount));
78-
}
71+
return MemoryMarshal.CreateReadOnlySpan(ref Address[0], addressByteCount).SequenceEqual(
72+
new ReadOnlySpan<byte>(other.Address, addressByteCount));
7973
}
8074
}
8175
}

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

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,8 @@ internal unsafe struct WIN32_FIND_DATA
2222
private fixed char _cFileName[MAX_PATH];
2323
private fixed char _cAlternateFileName[14];
2424

25-
internal ReadOnlySpan<char> cFileName
26-
{
27-
get { fixed (char* c = _cFileName) return new ReadOnlySpan<char>(c, MAX_PATH); }
28-
}
25+
internal ReadOnlySpan<char> cFileName =>
26+
MemoryMarshal.CreateReadOnlySpan(ref _cFileName[0], MAX_PATH);
2927
}
3028
}
3129
}

src/libraries/Common/src/Interop/Windows/NtDll/Interop.FILE_FULL_DIR_INFORMATION.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public struct FILE_FULL_DIR_INFORMATION
5454
public uint EaSize;
5555

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

5959
/// <summary>
6060
/// Gets the next info pointer or null if there are no more.

src/libraries/Common/src/Interop/Windows/SChannel/Interop.SecPkgContext_ApplicationProtocol.cs

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -29,15 +29,7 @@ internal unsafe struct SecPkgContext_ApplicationProtocol
2929
public ApplicationProtocolNegotiationExt ProtoNegoExt;
3030
public byte ProtocolIdSize;
3131
public fixed byte ProtocolId[MaxProtocolIdSize];
32-
public ReadOnlySpan<byte> Protocol
33-
{
34-
get
35-
{
36-
fixed (byte* pid = ProtocolId)
37-
{
38-
return new ReadOnlySpan<byte>(pid, ProtocolIdSize);
39-
}
40-
}
41-
}
32+
public ReadOnlySpan<byte> Protocol =>
33+
MemoryMarshal.CreateReadOnlySpan(ref ProtocolId[0], ProtocolIdSize);
4234
}
4335
}

src/libraries/Common/src/Interop/Windows/User32/Interop.LOGFONT.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,7 @@ public unsafe struct LOGFONT
2727
public byte lfQuality;
2828
public byte lfPitchAndFamily;
2929
private fixed char _lfFaceName[LF_FACESIZE];
30-
public Span<char> lfFaceName
31-
{
32-
get { fixed (char* c = _lfFaceName) { return new Span<char>(c, LF_FACESIZE); } }
33-
}
30+
public Span<char> lfFaceName => MemoryMarshal.CreateSpan(ref _lfFaceName[0], LF_FACESIZE);
3431

3532
public override string ToString()
3633
{

src/libraries/Common/src/Interop/Windows/WinMm/Interop.waveOutGetDevCaps.cs

Lines changed: 12 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -50,33 +50,25 @@ public Native(WAVEOUTCAPS managed)
5050
wMid = managed.wMid;
5151
wPid = managed.wPid;
5252
vDriverVersion = managed.vDriverVersion;
53-
fixed (char* pszPname = szPname)
54-
{
55-
managed.szPname.AsSpan().CopyTo(new Span<char>(pszPname, szPnameLength));
56-
}
53+
managed.szPname.CopyTo(MemoryMarshal.CreateSpan(ref szPname[0], szPnameLength));
5754
dwFormats = managed.dwFormats;
5855
wChannels = managed.wChannels;
5956
wReserved1 = managed.wReserved1;
6057
dwSupport = managed.dwSupport;
6158
}
6259

63-
public WAVEOUTCAPS ToManaged()
64-
{
65-
fixed (char* pszPname = szPname)
60+
public WAVEOUTCAPS ToManaged() =>
61+
new WAVEOUTCAPS
6662
{
67-
return new WAVEOUTCAPS
68-
{
69-
wMid = wMid,
70-
wPid = wPid,
71-
vDriverVersion = vDriverVersion,
72-
szPname = new Span<char>(pszPname, szPnameLength).ToString(),
73-
dwFormats = dwFormats,
74-
wChannels = wChannels,
75-
wReserved1 = wReserved1,
76-
dwSupport = dwSupport,
77-
};
78-
}
79-
}
63+
wMid = wMid,
64+
wPid = wPid,
65+
vDriverVersion = vDriverVersion,
66+
szPname = MemoryMarshal.CreateReadOnlySpan(ref szPname[0], szPnameLength).ToString(),
67+
dwFormats = dwFormats,
68+
wChannels = wChannels,
69+
wReserved1 = wReserved1,
70+
dwSupport = dwSupport,
71+
};
8072
}
8173
}
8274
#endif

src/libraries/Common/src/Interop/Windows/WinSock/Interop.WinsockBSD.cs

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -89,10 +89,7 @@ public unsafe struct Native
8989
public Native(IPv6MulticastRequest managed)
9090
{
9191
Debug.Assert(managed.MulticastAddress.Length == MulticastAddressLength);
92-
fixed (void* dest = _multicastAddress)
93-
{
94-
managed.MulticastAddress.CopyTo(new Span<byte>(dest, MulticastAddressLength));
95-
}
92+
managed.MulticastAddress.CopyTo(MemoryMarshal.CreateSpan(ref _multicastAddress[0], MulticastAddressLength));
9693
_interfaceIndex = managed.InterfaceIndex;
9794
}
9895

@@ -103,10 +100,7 @@ public IPv6MulticastRequest ToManaged()
103100
MulticastAddress = new byte[MulticastAddressLength],
104101
InterfaceIndex = _interfaceIndex
105102
};
106-
fixed (void* src = _multicastAddress)
107-
{
108-
new Span<byte>(src, 16).CopyTo(managed.MulticastAddress);
109-
}
103+
MemoryMarshal.CreateReadOnlySpan(ref _multicastAddress[0], MulticastAddressLength).CopyTo(managed.MulticastAddress);
110104
return managed;
111105
}
112106
}

src/libraries/Common/src/System/Net/NTAuthentication.Managed.cs

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -424,13 +424,10 @@ private static unsafe void SetField(ref MessageField field, int length, int offs
424424
throw new Win32Exception(NTE_FAIL);
425425
}
426426

427-
fixed (void* ptr = &field)
428-
{
429-
Span<byte> span = new Span<byte>(ptr, sizeof(MessageField));
430-
BinaryPrimitives.WriteInt16LittleEndian(span, (short)length);
431-
BinaryPrimitives.WriteInt16LittleEndian(span.Slice(2), (short)length);
432-
BinaryPrimitives.WriteInt32LittleEndian(span.Slice(4), offset);
433-
}
427+
Span<byte> span = MemoryMarshal.AsBytes(MemoryMarshal.CreateSpan(ref field, 1));
428+
BinaryPrimitives.WriteInt16LittleEndian(span, (short)length);
429+
BinaryPrimitives.WriteInt16LittleEndian(span.Slice(2), (short)length);
430+
BinaryPrimitives.WriteInt32LittleEndian(span.Slice(4), offset);
434431
}
435432

436433
private static void AddToPayload(ref MessageField field, ReadOnlySpan<byte> data, Span<byte> payload, ref int offset)

src/libraries/System.Net.NetworkInformation/src/System/Net/NetworkInformation/BsdIPGlobalProperties.cs

Lines changed: 7 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -32,28 +32,12 @@ private unsafe TcpConnectionInformation[] GetTcpConnections(bool listeners)
3232
continue;
3333
}
3434

35-
byte[] localBytes = new byte[nativeInfo.LocalEndPoint.NumAddressBytes];
36-
fixed (byte* localBytesPtr = localBytes)
37-
{
38-
Buffer.MemoryCopy(nativeInfo.LocalEndPoint.AddressBytes, localBytesPtr, localBytes.Length, localBytes.Length);
39-
}
40-
IPAddress localIPAddress = new IPAddress(localBytes);
35+
IPAddress localIPAddress = new IPAddress(new ReadOnlySpan<byte>(nativeInfo.LocalEndPoint.AddressBytes, checked((int)nativeInfo.LocalEndPoint.NumAddressBytes)));
4136
IPEndPoint local = new IPEndPoint(localIPAddress, (int)nativeInfo.LocalEndPoint.Port);
4237

43-
IPAddress remoteIPAddress;
44-
if (nativeInfo.RemoteEndPoint.NumAddressBytes == 0)
45-
{
46-
remoteIPAddress = IPAddress.Any;
47-
}
48-
else
49-
{
50-
byte[] remoteBytes = new byte[nativeInfo.RemoteEndPoint.NumAddressBytes];
51-
fixed (byte* remoteBytesPtr = &remoteBytes[0])
52-
{
53-
Buffer.MemoryCopy(nativeInfo.RemoteEndPoint.AddressBytes, remoteBytesPtr, remoteBytes.Length, remoteBytes.Length);
54-
}
55-
remoteIPAddress = new IPAddress(remoteBytes);
56-
}
38+
IPAddress remoteIPAddress = nativeInfo.RemoteEndPoint.NumAddressBytes == 0 ?
39+
IPAddress.Any :
40+
new IPAddress(new ReadOnlySpan<byte>(nativeInfo.RemoteEndPoint.AddressBytes, checked((int)nativeInfo.RemoteEndPoint.NumAddressBytes)));
5741

5842
IPEndPoint remote = new IPEndPoint(remoteIPAddress, (int)nativeInfo.RemoteEndPoint.Port);
5943
connectionInformations[nextResultIndex++] = new SimpleTcpConnectionInformation(local, remote, state);
@@ -101,20 +85,9 @@ public override unsafe IPEndPoint[] GetActiveUdpListeners()
10185
{
10286
Interop.Sys.IPEndPointInfo endPointInfo = infos[i];
10387
int port = (int)endPointInfo.Port;
104-
IPAddress ipAddress;
105-
if (endPointInfo.NumAddressBytes == 0)
106-
{
107-
ipAddress = IPAddress.Any;
108-
}
109-
else
110-
{
111-
byte[] bytes = new byte[endPointInfo.NumAddressBytes];
112-
fixed (byte* bytesPtr = &bytes[0])
113-
{
114-
Buffer.MemoryCopy(endPointInfo.AddressBytes, bytesPtr, bytes.Length, bytes.Length);
115-
}
116-
ipAddress = new IPAddress(bytes);
117-
}
88+
IPAddress ipAddress = endPointInfo.NumAddressBytes == 0 ?
89+
IPAddress.Any :
90+
new IPAddress(new ReadOnlySpan<byte>(endPointInfo.AddressBytes, checked((int)endPointInfo.NumAddressBytes)));
11891

11992
endPoints[i] = new IPEndPoint(ipAddress, port);
12093
}

src/libraries/System.Net.NetworkInformation/src/System/Net/NetworkInformation/BsdIpInterfaceProperties.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ private static unsafe void OnGatewayFound(void* pContext, Interop.Sys.IpAddressI
9393
{
9494
ref Context context = ref Unsafe.As<byte, Context>(ref *(byte*)pContext);
9595

96-
IPAddress ipAddress = new IPAddress(new Span<byte>(gatewayAddressInfo->AddressBytes, gatewayAddressInfo->NumAddressBytes).ToArray());
96+
IPAddress ipAddress = new IPAddress(new ReadOnlySpan<byte>(gatewayAddressInfo->AddressBytes, gatewayAddressInfo->NumAddressBytes));
9797
if (ipAddress.IsIPv6LinkLocal)
9898
{
9999
// For Link-Local addresses add ScopeId as that is not part of the route entry.

src/libraries/System.Private.CoreLib/src/System/IO/Enumeration/FileSystemEntry.Unix.cs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// The .NET Foundation licenses this file to you under the MIT license.
33

44
using System.Diagnostics;
5+
using System.Runtime.InteropServices;
56

67
namespace System.IO.Enumeration
78
{
@@ -88,11 +89,8 @@ public ReadOnlySpan<char> FileName
8889
{
8990
if (_directoryEntry.NameLength != 0 && _fileName.Length == 0)
9091
{
91-
fixed (char* c = _fileNameBuffer)
92-
{
93-
Span<char> buffer = new Span<char>(c, Interop.Sys.DirectoryEntry.NameBufferSize);
94-
_fileName = _directoryEntry.GetName(buffer);
95-
}
92+
Span<char> buffer = MemoryMarshal.CreateSpan(ref _fileNameBuffer[0], Interop.Sys.DirectoryEntry.NameBufferSize);
93+
_fileName = _directoryEntry.GetName(buffer);
9694
}
9795

9896
return _fileName;

src/libraries/System.Private.CoreLib/src/System/Runtime/InteropServices/Marshal.Unix.cs

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,7 @@ internal static unsafe int StringToAnsiString(string s, byte* buffer, int buffer
3636
{
3737
Debug.Assert(bufferLength >= (s.Length + 1) * SystemMaxDBCSCharSize, "Insufficient buffer length passed to StringToAnsiString");
3838

39-
int convertedBytes;
40-
41-
fixed (char* pChar = s)
42-
{
43-
convertedBytes = Encoding.UTF8.GetBytes(pChar, s.Length, buffer, bufferLength);
44-
}
45-
39+
int convertedBytes = Encoding.UTF8.GetBytes(s, new Span<byte>(buffer, bufferLength));
4640
buffer[convertedBytes] = 0;
4741

4842
return convertedBytes;

src/libraries/System.Private.CoreLib/src/System/Runtime/InteropServices/Marshal.cs

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -995,14 +995,8 @@ private static unsafe IntPtr StringToHGlobalUTF8(string? s)
995995

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

998-
int nbWritten;
999998
byte* pbMem = (byte*)ptr;
1000-
1001-
fixed (char* firstChar = s)
1002-
{
1003-
nbWritten = Encoding.UTF8.GetBytes(firstChar, s.Length, pbMem, nb);
1004-
}
1005-
999+
int nbWritten = Encoding.UTF8.GetBytes(s, new Span<byte>(pbMem, nb));
10061000
pbMem[nbWritten] = 0;
10071001

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

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

1045-
int nbWritten;
10461039
byte* pbMem = (byte*)ptr;
1047-
1048-
fixed (char* firstChar = s)
1049-
{
1050-
nbWritten = Encoding.UTF8.GetBytes(firstChar, s.Length, pbMem, nb);
1051-
}
1052-
1040+
int nbWritten = Encoding.UTF8.GetBytes(s, new Span<byte>(pbMem, nb));
10531041
pbMem[nbWritten] = 0;
10541042

10551043
return ptr;

0 commit comments

Comments
 (0)