Skip to content

Commit

Permalink
Remove DangerousTryGetArray and hide DangerousGetPinnableReference (d…
Browse files Browse the repository at this point in the history
…otnet#25964)

* Remove DangerousTryGetArray and hide DangerousGetPinnableReference

* Mark ReadOnlySpan DangerousGPR as ref readonly and update uses.

* Remove readonly keyword from ReadOnlySpan DangerousGPR

* Remove Dangerous APIs from System.Runtime reference.
  • Loading branch information
ahsonkhan authored and jkotas committed Dec 24, 2017
1 parent 119b775 commit e746fa5
Show file tree
Hide file tree
Showing 11 changed files with 71 additions and 296 deletions.
6 changes: 0 additions & 6 deletions src/System.Memory/ref/System.Memory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@ public readonly ref struct ReadOnlySpan<T>
public void CopyTo(Span<T> destination) { }
[System.ComponentModel.EditorBrowsable(System.ComponentModel.EditorBrowsableState.Never)]
public static ReadOnlySpan<T> DangerousCreate(object obj, ref T objectData, int length) { throw null; }
[System.ComponentModel.EditorBrowsable(System.ComponentModel.EditorBrowsableState.Never)]
public ref T DangerousGetPinnableReference() { throw null; }
#pragma warning disable 0809 //warning CS0809: Obsolete member 'Span<T>.Equals(object)' overrides non-obsolete member 'object.Equals(object)'
[System.ObsoleteAttribute("Equals() on ReadOnlySpan will always throw an exception. Use == instead.")]
[System.ComponentModel.EditorBrowsable(System.ComponentModel.EditorBrowsableState.Never)]
Expand Down Expand Up @@ -63,8 +61,6 @@ public void Fill(T value) { }
public void CopyTo(Span<T> destination) { }
[System.ComponentModel.EditorBrowsable(System.ComponentModel.EditorBrowsableState.Never)]
public static Span<T> DangerousCreate(object obj, ref T objectData, int length) { throw null; }
[System.ComponentModel.EditorBrowsable(System.ComponentModel.EditorBrowsableState.Never)]
public ref T DangerousGetPinnableReference() { throw null; }
#pragma warning disable 0809 //warning CS0809: Obsolete member 'Span<T>.Equals(object)' overrides non-obsolete member 'object.Equals(object)'
[System.ObsoleteAttribute("Equals() on Span will always throw an exception. Use == instead.")]
[System.ComponentModel.EditorBrowsable(System.ComponentModel.EditorBrowsableState.Never)]
Expand Down Expand Up @@ -188,8 +184,6 @@ public void CopyTo(Memory<T> destination) { }
public ReadOnlySpan<T> Span { get { throw null; } }
public unsafe Buffers.MemoryHandle Retain(bool pin = false) { throw null; }
public T[] ToArray() { throw null; }
[System.ComponentModel.EditorBrowsable(System.ComponentModel.EditorBrowsableState.Never)]
public bool DangerousTryGetArray(out ArraySegment<T> arraySegment) { throw null; }
}

public readonly struct Memory<T>
Expand Down
133 changes: 67 additions & 66 deletions src/System.Memory/src/System/MemoryExtensions.cs

Large diffs are not rendered by default.

25 changes: 0 additions & 25 deletions src/System.Memory/src/System/ReadOnlyMemory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -236,31 +236,6 @@ public unsafe MemoryHandle Retain(bool pin = false)
return memoryHandle;
}

/// <summary>
/// Get an array segment from the underlying memory.
/// If unable to get the array segment, return false with a default array segment.
/// </summary>
[EditorBrowsable(EditorBrowsableState.Never)]
public bool DangerousTryGetArray(out ArraySegment<T> arraySegment)
{
if (_index < 0)
{
if (((OwnedMemory<T>)_object).TryGetArray(out var segment))
{
arraySegment = new ArraySegment<T>(segment.Array, segment.Offset + (_index & RemoveOwnedFlagBitMask), _length);
return true;
}
}
else if (_object is T[] arr)
{
arraySegment = new ArraySegment<T>(arr, _index, _length);
return true;
}

arraySegment = default;
return false;
}

/// <summary>
/// Copies the contents from the memory into a new array. This heap
/// allocates, so should generally be avoided, however it is sometimes
Expand Down
2 changes: 1 addition & 1 deletion src/System.Memory/src/System/ReadOnlySpan.cs
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,7 @@ public T[] ToArray()
/// </summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
[EditorBrowsable(EditorBrowsableState.Never)]
public ref T DangerousGetPinnableReference()
internal ref T DangerousGetPinnableReference()
{
if (_pinnable == null)
unsafe { return ref Unsafe.AsRef<T>(_byteOffset.ToPointer()); }
Expand Down
2 changes: 1 addition & 1 deletion src/System.Memory/src/System/Span.cs
Original file line number Diff line number Diff line change
Expand Up @@ -425,7 +425,7 @@ public T[] ToArray()
/// </summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
[EditorBrowsable(EditorBrowsableState.Never)]
public ref T DangerousGetPinnableReference()
internal ref T DangerousGetPinnableReference()
{
if (_pinnable == null)
unsafe { return ref Unsafe.AsRef<T>(_byteOffset.ToPointer()); }
Expand Down
3 changes: 2 additions & 1 deletion src/System.Memory/src/System/SpanHelpers.BinarySearch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

using System.Collections.Generic;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;

#if !netstandard
using Internal.Runtime.CompilerServices;
Expand All @@ -21,7 +22,7 @@ internal static int BinarySearch<T, TComparable>(
if (comparable == null)
ThrowHelper.ThrowArgumentNullException(ExceptionArgument.comparable);
// TODO: Make `ref readonly`/`in` when Unsafe.Add(ReadOnly) supports it
return BinarySearch(ref span.DangerousGetPinnableReference(), span.Length, comparable);
return BinarySearch(ref MemoryMarshal.GetReference(span), span.Length, comparable);
}

// TODO: Make spanStart `ref readonly`/`in` when Unsafe.Add(ReadOnly) supports it
Expand Down
49 changes: 0 additions & 49 deletions src/System.Memory/tests/ReadOnlyMemory/DangerousTryGetArray.cs

This file was deleted.

This file was deleted.

69 changes: 0 additions & 69 deletions src/System.Memory/tests/Span/DangerousGetPinnableReference.cs

This file was deleted.

3 changes: 0 additions & 3 deletions src/System.Memory/tests/System.Memory.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
<Compile Include="Span\CtorArrayIntInt.cs" />
<Compile Include="Span\CtorPointerInt.cs" />
<Compile Include="Span\DangerousCreate.cs" />
<Compile Include="Span\DangerousGetPinnableReference.cs" />
<Compile Include="Span\Empty.cs" />
<Compile Include="Span\EndsWith.byte.cs" />
<Compile Include="Span\EndsWith.T.cs" />
Expand Down Expand Up @@ -65,7 +64,6 @@
<Compile Include="ReadOnlySpan\CtorArrayIntInt.cs" />
<Compile Include="ReadOnlySpan\CtorPointerInt.cs" />
<Compile Include="ReadOnlySpan\DangerousCreate.cs" />
<Compile Include="ReadOnlySpan\DangerousGetPinnableReference.cs" />
<Compile Include="ReadOnlySpan\Empty.cs" />
<Compile Include="ReadOnlySpan\EndsWith.byte.cs" />
<Compile Include="ReadOnlySpan\EndsWith.T.cs" />
Expand Down Expand Up @@ -125,7 +123,6 @@
<Compile Include="ReadOnlyMemory\CopyTo.cs" />
<Compile Include="ReadOnlyMemory\CtorArray.cs" />
<Compile Include="ReadOnlyMemory\CtorArrayIntInt.cs" />
<Compile Include="ReadOnlyMemory\DangerousTryGetArray.cs" />
<Compile Include="ReadOnlyMemory\Empty.cs" />
<Compile Include="ReadOnlyMemory\Equality.cs" />
<Compile Include="ReadOnlyMemory\GetHashCode.cs" />
Expand Down
6 changes: 0 additions & 6 deletions src/System.Runtime/ref/System.Runtime.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1899,8 +1899,6 @@ public void CopyTo(Memory<T> destination) { }
public ReadOnlySpan<T> Span { get { throw null; } }
public unsafe Buffers.MemoryHandle Retain(bool pin = false) { throw null; }
public T[] ToArray() { throw null; }
[System.ComponentModel.EditorBrowsable(System.ComponentModel.EditorBrowsableState.Never)]
public bool DangerousTryGetArray(out ArraySegment<T> arraySegment) { throw null; }
}
[System.Runtime.InteropServices.StructLayoutAttribute(System.Runtime.InteropServices.LayoutKind.Sequential)]
public readonly ref struct ReadOnlySpan<T>
Expand All @@ -1916,8 +1914,6 @@ public readonly ref struct ReadOnlySpan<T>
public void CopyTo(Span<T> destination) { }
[System.ComponentModel.EditorBrowsable(System.ComponentModel.EditorBrowsableState.Never)]
public static ReadOnlySpan<T> DangerousCreate(object obj, ref T objectData, int length) { throw null; }
[System.ComponentModel.EditorBrowsable(System.ComponentModel.EditorBrowsableState.Never)]
public ref T DangerousGetPinnableReference() { throw null; }
#pragma warning disable 0809
[System.ObsoleteAttribute("Equals() on ReadOnlySpan will always throw an exception. Use == instead.")]
[System.ComponentModel.EditorBrowsable(System.ComponentModel.EditorBrowsableState.Never)]
Expand Down Expand Up @@ -2116,8 +2112,6 @@ public void Fill(T value) { }
public void CopyTo(Span<T> destination) { }
[System.ComponentModel.EditorBrowsable(System.ComponentModel.EditorBrowsableState.Never)]
public static Span<T> DangerousCreate(object obj, ref T objectData, int length) { throw null; }
[System.ComponentModel.EditorBrowsable(System.ComponentModel.EditorBrowsableState.Never)]
public ref T DangerousGetPinnableReference() { throw null; }
#pragma warning disable 0809
[System.ObsoleteAttribute("Equals() on Span will always throw an exception. Use == instead.")]
[System.ComponentModel.EditorBrowsable(System.ComponentModel.EditorBrowsableState.Never)]
Expand Down

0 comments on commit e746fa5

Please sign in to comment.