Skip to content

Commit 75e0173

Browse files
ahsonkhanjashook
authored and
jashook
committedDec 12, 2017
Add GetReference and TryGetArray to MemoryMarshal (dotnet#15417)
* Add GetReference and TryGetArray to MemoryMarshal * Marking GetReference with AggressiveInlining * Do not use ByReference as a return type. * Addressing PR feedback.
1 parent 8153e03 commit 75e0173

File tree

4 files changed

+29
-3
lines changed

4 files changed

+29
-3
lines changed
 

‎src/mscorlib/shared/System/ReadOnlyMemory.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public readonly struct ReadOnlyMemory<T>
2929
private readonly int _index;
3030
private readonly int _length;
3131

32-
private const int RemoveOwnedFlagBitMask = 0x7FFFFFFF;
32+
internal const int RemoveOwnedFlagBitMask = 0x7FFFFFFF;
3333

3434
/// <summary>
3535
/// Creates a new memory over the entirety of the target array.

‎src/mscorlib/shared/System/ReadOnlySpan.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ namespace System
2121
public readonly ref struct ReadOnlySpan<T>
2222
{
2323
/// <summary>A byref or a native ptr.</summary>
24-
private readonly ByReference<T> _pointer;
24+
internal readonly ByReference<T> _pointer;
2525
/// <summary>The number of elements this ReadOnlySpan contains.</summary>
2626
#if PROJECTN
2727
[Bound]

‎src/mscorlib/shared/System/Runtime/InteropServices/MemoryMarshal.cs

+26
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
// See the LICENSE file in the project root for more information.
44

5+
using System.Buffers;
56
using System.Runtime.CompilerServices;
67

78
namespace System.Runtime.InteropServices
@@ -23,5 +24,30 @@ public static class MemoryMarshal
2324
/// </remarks>
2425
public static Memory<T> AsMemory<T>(ReadOnlyMemory<T> readOnlyMemory) =>
2526
Unsafe.As<ReadOnlyMemory<T>, Memory<T>>(ref readOnlyMemory);
27+
28+
public static ref T GetReference<T>(Span<T> span) => ref span._pointer.Value;
29+
30+
public static ref readonly T GetReference<T>(ReadOnlySpan<T> span) => ref span._pointer.Value;
31+
32+
public static bool TryGetArray<T>(ReadOnlyMemory<T> readOnlyMemory, out ArraySegment<T> arraySegment)
33+
{
34+
object obj = readOnlyMemory.GetObjectStartLength(out int index, out int length);
35+
if (index < 0)
36+
{
37+
if (((OwnedMemory<T>)obj).TryGetArray(out var segment))
38+
{
39+
arraySegment = new ArraySegment<T>(segment.Array, segment.Offset + (index & ReadOnlyMemory<T>.RemoveOwnedFlagBitMask), length);
40+
return true;
41+
}
42+
}
43+
else if (obj is T[] arr)
44+
{
45+
arraySegment = new ArraySegment<T>(arr, index, length);
46+
return true;
47+
}
48+
49+
arraySegment = default;
50+
return false;
51+
}
2652
}
2753
}

‎src/mscorlib/shared/System/Span.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ namespace System
2727
public readonly ref struct Span<T>
2828
{
2929
/// <summary>A byref or a native ptr.</summary>
30-
private readonly ByReference<T> _pointer;
30+
internal readonly ByReference<T> _pointer;
3131
/// <summary>The number of elements this Span contains.</summary>
3232
#if PROJECTN
3333
[Bound]

0 commit comments

Comments
 (0)