Skip to content
This repository has been archived by the owner on Aug 2, 2023. It is now read-only.

Commit

Permalink
Remove use of DangerousGetPinnableReference and use Memory.GetReferen…
Browse files Browse the repository at this point in the history
…ce instead (#1985)

* Remove use of DangerousGetPinnableReference and use Memory.GetReference instead

* Misses some references to DangerousGetPinnableReference in sample projects
  • Loading branch information
ahsonkhan authored Dec 22, 2017
1 parent 98c6f39 commit 1c979fa
Show file tree
Hide file tree
Showing 28 changed files with 111 additions and 91 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

using Microsoft.Net.Interop;
using System;
using System.Runtime.InteropServices;

namespace Microsoft.Net.Sockets
{
Expand Down Expand Up @@ -163,7 +164,7 @@ public void Close()
public unsafe int Send(ReadOnlySpan<byte> buffer)
{
// TODO: This can work with Span<byte> because it's synchronous but we need async pinning support
fixed (byte* bytes = &buffer.DangerousGetPinnableReference())
fixed (byte* bytes = &MemoryMarshal.GetReference(buffer))
{
IntPtr pointer = new IntPtr(bytes);
return SendPinned(pointer, buffer.Length);
Expand Down Expand Up @@ -193,7 +194,7 @@ public int Send(byte[] bytes, int offset, int count)
public unsafe int Receive(Span<byte> buffer)
{
// TODO: This can work with Span<byte> because it's synchronous but we need async pinning support
fixed (byte* bytes = &buffer.DangerousGetPinnableReference())
fixed (byte* bytes = &MemoryMarshal.GetReference(buffer))
{
IntPtr pointer = new IntPtr(bytes);
return ReceivePinned(pointer, buffer.Length);
Expand Down
12 changes: 6 additions & 6 deletions samples/System.Numerics.Tensors.Samples/TensorSamples/Access.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,19 +59,19 @@ public static void RunSample()

public static unsafe void DumpContentsSparse(CompressedSparseTensor<double> tensor)
{
fixed (double* valuesPtr = &tensor.Values.Span.DangerousGetPinnableReference())
fixed (int* compressedCountsPtr = &tensor.CompressedCounts.Span.DangerousGetPinnableReference())
fixed (int* indicesPtr = &tensor.Indices.Span.DangerousGetPinnableReference())
fixed (int* stridesPtr = &tensor.Strides.DangerousGetPinnableReference())
fixed (double* valuesPtr = &MemoryMarshal.GetReference(tensor.Values.Span))
fixed (int* compressedCountsPtr = &MemoryMarshal.GetReference(tensor.CompressedCounts.Span))
fixed (int* indicesPtr = &MemoryMarshal.GetReference(tensor.Indices.Span))
fixed (int* stridesPtr = &MemoryMarshal.GetReference(tensor.Strides))
{
DumpContentsSparse(valuesPtr, compressedCountsPtr, indicesPtr, stridesPtr, tensor.Rank, tensor.NonZeroCount);
}
}

public static unsafe void DumpContentsDense(DenseTensor<double> tensor)
{
fixed (double* valuesPtr = &tensor.Buffer.Span.DangerousGetPinnableReference())
fixed (int* stridesPtr = &tensor.Strides.DangerousGetPinnableReference())
fixed (double* valuesPtr = &MemoryMarshal.GetReference(tensor.Buffer.Span))
fixed (int* stridesPtr = &MemoryMarshal.GetReference(tensor.Strides))
{
DumpContentsDense(valuesPtr, stridesPtr, tensor.Rank, (int)tensor.Length);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@ public static void RunSample()

public static unsafe double ScalarPowerSparse(CompressedSparseTensor<double> tensor, int exponent)
{
fixed (double* valuesPtr = &tensor.Values.Span.DangerousGetPinnableReference())
fixed (int* compressedCountsPtr = &tensor.CompressedCounts.Span.DangerousGetPinnableReference())
fixed (int* indicesPtr = &tensor.Indices.Span.DangerousGetPinnableReference())
fixed (double* valuesPtr = &MemoryMarshal.GetReference(tensor.Values.Span))
fixed (int* compressedCountsPtr = &MemoryMarshal.GetReference(tensor.CompressedCounts.Span))
fixed (int* indicesPtr = &MemoryMarshal.GetReference(tensor.Indices.Span))
{
return ScalarPowerSparse(valuesPtr, compressedCountsPtr, indicesPtr, tensor.Dimensions.ToArray(), tensor.Rank, tensor.NonZeroCount, exponent);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public static unsafe DenseTensor<double> GetMultiplicationTable(int maxNumber)

var result = new DenseTensor<double>(dimensions);

fixed (double* dataPtr = &result.Buffer.Span.DangerousGetPinnableReference())
fixed (double* dataPtr = &MemoryMarshal.GetReference(result.Buffer.Span))
{
GetMultTablePreAllocated(maxNumber, dataPtr, result.Buffer.Length);
}
Expand All @@ -43,7 +43,7 @@ public static unsafe DenseTensor<double> GetMultiplicationTable(int maxNumber)

public static unsafe double GetRowSum(DenseTensor<double> tensor, int row)
{
fixed(double* dataPtr = &tensor.Buffer.Span.DangerousGetPinnableReference())
fixed(double* dataPtr = &MemoryMarshal.GetReference(tensor.Buffer.Span))
{
return GetRowSum(dataPtr, tensor.Dimensions.ToArray(), tensor.Rank, row);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public static DenseTensor<double> GetMultiplicationTable(int maxNumber)

public static unsafe double GetRowSum(DenseTensor<double> tensor, int row)
{
fixed(double* dataPtr = &tensor.Buffer.Span.DangerousGetPinnableReference())
fixed(double* dataPtr = &MemoryMarshal.GetReference(tensor.Buffer.Span))
{
return GetRowSum(dataPtr, tensor.Dimensions.ToArray(), tensor.Rank, row);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,15 +63,15 @@ public static DenseTensor<double> GetMultiplicationTable(int maxNumber)

public static unsafe double GetRowSum(DenseTensor<double> tensor, int row)
{
fixed(double* dataPtr = &tensor.Buffer.Span.DangerousGetPinnableReference())
fixed(double* dataPtr = &MemoryMarshal.GetReference(tensor.Buffer.Span))
{
return GetRowSum(dataPtr, tensor.Dimensions.ToArray(), tensor.Rank, row);
}
}

public static unsafe void GetMultiplicationTablePreallocated(int maxNumber, DenseTensor<double> tensor)
{
fixed (double* dataPtr = &tensor.Buffer.Span.DangerousGetPinnableReference())
fixed (double* dataPtr = &MemoryMarshal.GetReference(tensor.Buffer.Span))
{
GetMultTablePreAllocated(maxNumber, dataPtr, tensor.Buffer.Length);
}
Expand Down
4 changes: 2 additions & 2 deletions src/System.Azure.Experimental/System/Azure/Sha256.Windows.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public static Sha256 Create(byte[] seed)
public int OutputSize => 256 / 8; // Sha256 length in bytes
public unsafe void Append(ReadOnlySpan<byte> input)
{
fixed (byte* pInput = &input.DangerousGetPinnableReference())
fixed (byte* pInput = &MemoryMarshal.GetReference(input))
{
var result = BCryptHashData(_hash, pInput, input.Length, 0);
if (result != 0) throw new Exception();
Expand All @@ -43,7 +43,7 @@ public bool TryWrite(Span<byte> buffer, out int written, StandardFormat format =

unsafe
{
fixed (byte* pOutput = &buffer.DangerousGetPinnableReference())
fixed (byte* pOutput = &MemoryMarshal.GetReference(buffer))
{
var result = BCryptFinishHash(_hash, pOutput, OutputSize, 0);
if (result != 0) throw new Exception();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using System.Diagnostics;
using System.Runtime.CompilerServices;
using System.Numerics;
using System.Runtime.InteropServices;

namespace System.Buffers
{
Expand Down Expand Up @@ -233,7 +234,7 @@ public static bool TryIndicesOf(this Span<byte> buffer, byte value, Span<int> in
return false;
}

return TryIndicesOf(ref buffer.DangerousGetPinnableReference(), value, length, indices, out numberOfIndices);
return TryIndicesOf(ref MemoryMarshal.GetReference(buffer), value, length, indices, out numberOfIndices);
}

public static bool TryIndicesOf(this ReadOnlySpan<byte> buffer, byte value, Span<int> indices, out int numberOfIndices)
Expand All @@ -245,7 +246,7 @@ public static bool TryIndicesOf(this ReadOnlySpan<byte> buffer, byte value, Span
return false;
}

return TryIndicesOf(ref buffer.DangerousGetPinnableReference(), value, length, indices, out numberOfIndices);
return TryIndicesOf(ref MemoryMarshal.GetReference(buffer), value, length, indices, out numberOfIndices);
}

private unsafe static bool TryIndicesOf(ref byte searchSpace, byte value, int length, Span<int> indices, out int numberOfIndices)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

using System.Diagnostics;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Text.Utf8;

namespace System.Buffers.Text
Expand Down Expand Up @@ -34,7 +35,7 @@ public void WriteBytes(byte[] bytes)
if (bytes.Length > 0 && free.Length >= bytes.Length)
{
ref byte pSource = ref bytes[0];
ref byte pDest = ref free.DangerousGetPinnableReference();
ref byte pDest = ref MemoryMarshal.GetReference(free);

Unsafe.CopyBlockUnaligned(ref pDest, ref pSource, (uint)bytes.Length);

Expand Down Expand Up @@ -63,7 +64,7 @@ public void WriteBytes(byte[] bytes, int index, int length)
if (length > 0 && free.Length >= length)
{
ref byte pSource = ref bytes[index];
ref byte pDest = ref free.DangerousGetPinnableReference();
ref byte pDest = ref MemoryMarshal.GetReference(free);

Unsafe.CopyBlockUnaligned(ref pDest, ref pSource, (uint)length);

Expand Down Expand Up @@ -189,7 +190,7 @@ private void WriteBytesChunked(byte[] bytes, int index, int length)
var chunkLength = Math.Min(length, free.Length);

ref byte pSource = ref bytes[index];
ref byte pDest = ref free.DangerousGetPinnableReference();
ref byte pDest = ref MemoryMarshal.GetReference(free);

Unsafe.CopyBlockUnaligned(ref pDest, ref pSource, (uint)chunkLength);

Expand Down
15 changes: 8 additions & 7 deletions src/System.IO.Compression.Brotli/System/IO/Compression/Brotli.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@
// See the LICENSE file in the project root for more information.
using System.Buffers;
using System.IO.Compression.Resources;
using System.Runtime.InteropServices;

#if BIT64
using nuint = System.UInt64;
#else
using nuint = System.UInt32;
#endif
#endif

namespace System.IO.Compression
{
Expand Down Expand Up @@ -152,8 +153,8 @@ public static OperationStatus FlushEncoder(ReadOnlySpan<byte> source, Span<byte>
unsafe
{
IntPtr bufIn, bufOut;
fixed (byte* inBytes = &source.DangerousGetPinnableReference())
fixed (byte* outBytes = &destination.DangerousGetPinnableReference())
fixed (byte* inBytes = &MemoryMarshal.GetReference(source))
fixed (byte* outBytes = &MemoryMarshal.GetReference(destination))
{
bufIn = new IntPtr(inBytes);
bufOut = new IntPtr(outBytes);
Expand Down Expand Up @@ -186,8 +187,8 @@ public static OperationStatus Compress(ReadOnlySpan<byte> source, Span<byte> des
IntPtr bufIn, bufOut;
while (bytesConsumed > 0)
{
fixed (byte* inBytes = &source.DangerousGetPinnableReference())
fixed (byte* outBytes = &destination.DangerousGetPinnableReference())
fixed (byte* inBytes = &MemoryMarshal.GetReference(source))
fixed (byte* outBytes = &MemoryMarshal.GetReference(destination))
{
bufIn = new IntPtr(inBytes);
bufOut = new IntPtr(outBytes);
Expand Down Expand Up @@ -218,8 +219,8 @@ public static OperationStatus Decompress(ReadOnlySpan<byte> source, Span<byte> d
unsafe
{
IntPtr bufIn, bufOut;
fixed (byte* inBytes = &source.DangerousGetPinnableReference())
fixed (byte* outBytes = &destination.DangerousGetPinnableReference())
fixed (byte* inBytes = &MemoryMarshal.GetReference(source))
fixed (byte* outBytes = &MemoryMarshal.GetReference(destination))
{
bufIn = new IntPtr(inBytes);
bufOut = new IntPtr(outBytes);
Expand Down
2 changes: 1 addition & 1 deletion src/System.IO.Pipelines.File/FileReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ private class ReadOperation
public unsafe void Read()
{
var buffer = Writer.Alloc(2048);
fixed (byte* source = &buffer.Buffer.Span.DangerousGetPinnableReference())
fixed (byte* source = &MemoryMarshal.GetReference(buffer.Buffer.Span))
{
var count = buffer.Buffer.Length;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Buffers;
using System.Buffers.Text;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Text.Parsing;
using System.Text.Utf8;

Expand Down Expand Up @@ -133,7 +134,7 @@ public unsafe static string GetAsciiString(this Span<byte> span)
var asciiString = new string('\0', len);

fixed (char* destination = asciiString)
fixed (byte* source = &span.DangerousGetPinnableReference()) {
fixed (byte* source = &MemoryMarshal.GetReference(span)) {
if (!AsciiUtilities.TryGetAsciiString(source, destination, len)) {
ThrowInvalidOperation();
}
Expand Down Expand Up @@ -167,7 +168,7 @@ public unsafe static string GetAsciiString(this ReadOnlyBuffer buffer)

foreach (var memory in buffer)
{
fixed (byte* source = &memory.Span.DangerousGetPinnableReference())
fixed (byte* source = &MemoryMarshal.GetReference(memory.Span))
{
if (!AsciiUtilities.TryGetAsciiString(source, output + offset, memory.Length))
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// See the LICENSE file in the project root for more information.

using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;

namespace System.IO.Pipelines
{
Expand Down Expand Up @@ -31,7 +32,7 @@ public void Write(byte[] source)
if (source.Length > 0 && _span.Length >= source.Length)
{
ref byte pSource = ref source[0];
ref byte pDest = ref _span.DangerousGetPinnableReference();
ref byte pDest = ref MemoryMarshal.GetReference(_span);

Unsafe.CopyBlockUnaligned(ref pDest, ref pSource, (uint)source.Length);

Expand All @@ -58,7 +59,7 @@ public void Write(byte[] source, int offset, int length)
if (length > 0 && _span.Length >= length)
{
ref byte pSource = ref source[offset];
ref byte pDest = ref _span.DangerousGetPinnableReference();
ref byte pDest = ref MemoryMarshal.GetReference(_span);

Unsafe.CopyBlockUnaligned(ref pDest, ref pSource, (uint)length);

Expand All @@ -84,7 +85,7 @@ private void WriteMultiBuffer(byte[] source, int offset, int length)
var writable = Math.Min(remaining, _span.Length);

ref byte pSource = ref source[offset];
ref byte pDest = ref _span.DangerousGetPinnableReference();
ref byte pDest = ref MemoryMarshal.GetReference(_span);

Unsafe.CopyBlockUnaligned(ref pDest, ref pSource, (uint)writable);

Expand All @@ -102,4 +103,4 @@ public void Ensure(int count = 1)
_span = _writableBuffer.Buffer.Span;
}
}
}
}
4 changes: 2 additions & 2 deletions src/System.Net.Libuv/System/Net/Libuv/Stream.cs
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ public unsafe void TryWrite(ReadOnlySpan<byte> data)
// This can work with Span<byte> because it's synchronous but we need pinning support
EnsureNotDisposed();

fixed (byte* source = &data.DangerousGetPinnableReference())
fixed (byte* source = &MemoryMarshal.GetReference(data))
{
var length = data.Length;

Expand All @@ -109,7 +109,7 @@ public unsafe void TryWrite(ReadOnlyMemory<byte> data)
// This can work with Span<byte> because it's synchronous but we need pinning support
EnsureNotDisposed();

fixed (byte* source = &data.Span.DangerousGetPinnableReference())
fixed (byte* source = &MemoryMarshal.GetReference(data.Span))
{
var length = data.Length;

Expand Down
13 changes: 7 additions & 6 deletions src/System.Text.Http.Parser/HttpParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using System.IO.Pipelines;
using System.Numerics;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Text.Http.Parser.Internal;
using Position = System.Buffers.Position;

Expand Down Expand Up @@ -69,7 +70,7 @@ public unsafe bool ParseRequestLine<T>(T handler, in ReadOnlyBuffer buffer, out
}

// Fix and parse the span
fixed (byte* data = &span.DangerousGetPinnableReference())
fixed (byte* data = &MemoryMarshal.GetReference(span))
{
ParseRequestLine(ref handler, data, span.Length);
}
Expand Down Expand Up @@ -102,7 +103,7 @@ public unsafe bool ParseRequestLine<T>(ref T handler, in ReadOnlyBytes buffer, o
}

// Fix and parse the span
fixed (byte* data = &span.DangerousGetPinnableReference())
fixed (byte* data = &MemoryMarshal.GetReference(span))
{
ParseRequestLine(ref handler, data, span.Length);
}
Expand Down Expand Up @@ -248,7 +249,7 @@ public unsafe bool ParseHeaders<T>(T handler, in ReadOnlyBuffer buffer, out Posi
var span = reader.Span;
var remaining = span.Length - reader.Index;

fixed (byte* pBuffer = &span.DangerousGetPinnableReference())
fixed (byte* pBuffer = &MemoryMarshal.GetReference(span))
{
while (remaining > 0)
{
Expand Down Expand Up @@ -333,7 +334,7 @@ public unsafe bool ParseHeaders<T>(T handler, in ReadOnlyBuffer buffer, out Posi
var headerSpan = buffer.Slice(current, lineEnd).ToSpan();
length = headerSpan.Length;

fixed (byte* pHeader = &headerSpan.DangerousGetPinnableReference())
fixed (byte* pHeader = &MemoryMarshal.GetReference(headerSpan))
{
TakeSingleHeader(pHeader, length, ref handler);
}
Expand Down Expand Up @@ -381,7 +382,7 @@ public unsafe bool ParseHeaders<T>(ref T handler, ReadOnlyBytes buffer, out int

while (true)
{
fixed (byte* pBuffer = &currentSpan.DangerousGetPinnableReference())
fixed (byte* pBuffer = &MemoryMarshal.GetReference(currentSpan))
{
while (remaining > 0)
{
Expand Down Expand Up @@ -449,7 +450,7 @@ public unsafe bool ParseHeaders<T>(ref T handler, ReadOnlyBytes buffer, out int
var headerSpan = buffer.Slice(index, end - index + 1).ToSpan();
length = headerSpan.Length;

fixed (byte* pHeader = &headerSpan.DangerousGetPinnableReference())
fixed (byte* pHeader = &MemoryMarshal.GetReference(headerSpan))
{
TakeSingleHeader(pHeader, length, ref handler);
}
Expand Down
Loading

0 comments on commit 1c979fa

Please sign in to comment.