Skip to content
This repository has been archived by the owner on Jan 23, 2023. It is now read-only.
/ corefx Public archive

Commit

Permalink
Add GetReference and TryGetArray to MemoryMarshal (#15417)
Browse files Browse the repository at this point in the history
* Add GetReference and TryGetArray to MemoryMarshal

* Marking GetReference with AggressiveInlining

* Do not use ByReference as a return type.

* Addressing PR feedback.
  • Loading branch information
ahsonkhan authored and jkotas committed Dec 8, 2017
1 parent 4feaa86 commit 0919eb3
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/mscorlib/shared/System/ReadOnlyMemory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public readonly struct ReadOnlyMemory<T>
private readonly int _index;
private readonly int _length;

private const int RemoveOwnedFlagBitMask = 0x7FFFFFFF;
internal const int RemoveOwnedFlagBitMask = 0x7FFFFFFF;

/// <summary>
/// Creates a new memory over the entirety of the target array.
Expand Down
2 changes: 1 addition & 1 deletion src/mscorlib/shared/System/ReadOnlySpan.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ namespace System
public readonly ref struct ReadOnlySpan<T>
{
/// <summary>A byref or a native ptr.</summary>
private readonly ByReference<T> _pointer;
internal readonly ByReference<T> _pointer;
/// <summary>The number of elements this ReadOnlySpan contains.</summary>
#if PROJECTN
[Bound]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System.Buffers;
using System.Runtime.CompilerServices;

namespace System.Runtime.InteropServices
Expand All @@ -23,5 +24,30 @@ public static class MemoryMarshal
/// </remarks>
public static Memory<T> AsMemory<T>(ReadOnlyMemory<T> readOnlyMemory) =>
Unsafe.As<ReadOnlyMemory<T>, Memory<T>>(ref readOnlyMemory);

public static ref T GetReference<T>(Span<T> span) => ref span._pointer.Value;

public static ref readonly T GetReference<T>(ReadOnlySpan<T> span) => ref span._pointer.Value;

public static bool TryGetArray<T>(ReadOnlyMemory<T> readOnlyMemory, out ArraySegment<T> arraySegment)
{
object obj = readOnlyMemory.GetObjectStartLength(out int index, out int length);
if (index < 0)
{
if (((OwnedMemory<T>)obj).TryGetArray(out var segment))
{
arraySegment = new ArraySegment<T>(segment.Array, segment.Offset + (index & ReadOnlyMemory<T>.RemoveOwnedFlagBitMask), length);
return true;
}
}
else if (obj is T[] arr)
{
arraySegment = new ArraySegment<T>(arr, index, length);
return true;
}

arraySegment = default;
return false;
}
}
}
2 changes: 1 addition & 1 deletion src/mscorlib/shared/System/Span.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ namespace System
public readonly ref struct Span<T>
{
/// <summary>A byref or a native ptr.</summary>
private readonly ByReference<T> _pointer;
internal readonly ByReference<T> _pointer;
/// <summary>The number of elements this Span contains.</summary>
#if PROJECTN
[Bound]
Expand Down

0 comments on commit 0919eb3

Please sign in to comment.