This repository was archived by the owner on Jan 23, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4.8k
Add {RO}Span GetReference and ROMemory TryGetArray to MemoryMarshal #25789
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,125 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
| // See the LICENSE file in the project root for more information. | ||
|
|
||
| using Xunit; | ||
| using System.Runtime.CompilerServices; | ||
|
|
||
| using static System.TestHelpers; | ||
| using System.Runtime.InteropServices; | ||
|
|
||
| namespace System.SpanTests | ||
| { | ||
| public static partial class MemoryMarshalTests | ||
| { | ||
| [Fact] | ||
| public static void SpanGetReferenceArray() | ||
| { | ||
| int[] a = { 91, 92, 93, 94, 95 }; | ||
| Span<int> span = new Span<int>(a, 1, 3); | ||
| ref int pinnableReference = ref MemoryMarshal.GetReference(span); | ||
| Assert.True(Unsafe.AreSame(ref a[1], ref pinnableReference)); | ||
| } | ||
|
|
||
| [Fact] | ||
| public static void SpanGetReferenceArrayPastEnd() | ||
| { | ||
| // The only real difference between GetReference() and "ref span[0]" is that | ||
| // GetReference() of a zero-length won't throw an IndexOutOfRange. | ||
|
|
||
| int[] a = { 91, 92, 93, 94, 95 }; | ||
| Span<int> span = new Span<int>(a, a.Length, 0); | ||
| ref int pinnableReference = ref MemoryMarshal.GetReference(span); | ||
| ref int expected = ref Unsafe.Add<int>(ref a[a.Length - 1], 1); | ||
| Assert.True(Unsafe.AreSame(ref expected, ref pinnableReference)); | ||
| } | ||
|
|
||
| [Fact] | ||
| public static void SpanGetReferencePointer() | ||
| { | ||
| unsafe | ||
| { | ||
| int i = 42; | ||
| Span<int> span = new Span<int>(&i, 1); | ||
| ref int pinnableReference = ref MemoryMarshal.GetReference(span); | ||
| Assert.True(Unsafe.AreSame(ref i, ref pinnableReference)); | ||
| } | ||
| } | ||
|
|
||
| [Fact] | ||
| public static void SpanGetReferencePointerDangerousCreate1() | ||
| { | ||
| TestClass testClass = new TestClass(); | ||
| Span<char> span = Span<char>.DangerousCreate(testClass, ref testClass.C1, 3); | ||
|
|
||
| ref char pinnableReference = ref MemoryMarshal.GetReference(span); | ||
| Assert.True(Unsafe.AreSame(ref testClass.C1, ref pinnableReference)); | ||
| } | ||
|
|
||
| [Fact] | ||
| public static void SpanGetReferenceEmpty() | ||
| { | ||
| unsafe | ||
| { | ||
| Span<int> span = Span<int>.Empty; | ||
| ref int pinnableReference = ref MemoryMarshal.GetReference(span); | ||
| Assert.True(Unsafe.AreSame(ref Unsafe.AsRef<int>(null), ref pinnableReference)); | ||
| } | ||
| } | ||
|
|
||
| [Fact] | ||
| public static void ReadOnlySpanGetReferenceArray() | ||
| { | ||
| int[] a = { 91, 92, 93, 94, 95 }; | ||
| ReadOnlySpan<int> span = new ReadOnlySpan<int>(a, 1, 3); | ||
| ref int pinnableReference = ref Unsafe.AsRef(in MemoryMarshal.GetReference(span)); | ||
| Assert.True(Unsafe.AreSame(ref a[1], ref pinnableReference)); | ||
| } | ||
|
|
||
| [Fact] | ||
| public static void ReadOnlySpanGetReferenceArrayPastEnd() | ||
| { | ||
| // The only real difference between GetReference() and "ref span[0]" is that | ||
| // GetReference() of a zero-length won't throw an IndexOutOfRange. | ||
|
|
||
| int[] a = { 91, 92, 93, 94, 95 }; | ||
| ReadOnlySpan<int> span = new ReadOnlySpan<int>(a, a.Length, 0); | ||
| ref int pinnableReference = ref Unsafe.AsRef(in MemoryMarshal.GetReference(span)); | ||
| ref int expected = ref Unsafe.Add<int>(ref a[a.Length - 1], 1); | ||
| Assert.True(Unsafe.AreSame(ref expected, ref pinnableReference)); | ||
| } | ||
|
|
||
| [Fact] | ||
| public static void ReadOnlySpanGetReferencePointer() | ||
| { | ||
| unsafe | ||
| { | ||
| int i = 42; | ||
| ReadOnlySpan<int> span = new ReadOnlySpan<int>(&i, 1); | ||
| ref int pinnableReference = ref Unsafe.AsRef(in MemoryMarshal.GetReference(span)); | ||
| Assert.True(Unsafe.AreSame(ref i, ref pinnableReference)); | ||
| } | ||
| } | ||
|
|
||
| [Fact] | ||
| public static void ReadOnlySpanGetReferencePointerDangerousCreate1() | ||
| { | ||
| TestClass testClass = new TestClass(); | ||
| ReadOnlySpan<char> span = ReadOnlySpan<char>.DangerousCreate(testClass, ref testClass.C1, 3); | ||
|
|
||
| ref char pinnableReference = ref Unsafe.AsRef(in MemoryMarshal.GetReference(span)); | ||
| Assert.True(Unsafe.AreSame(ref testClass.C1, ref pinnableReference)); | ||
| } | ||
|
|
||
| [Fact] | ||
| public static void ReadOnlySpanGetReferenceEmpty() | ||
| { | ||
| unsafe | ||
| { | ||
| ReadOnlySpan<int> span = ReadOnlySpan<int>.Empty; | ||
| ref int pinnableReference = ref Unsafe.AsRef(in MemoryMarshal.GetReference(span)); | ||
| Assert.True(Unsafe.AreSame(ref Unsafe.AsRef<int>(null), ref pinnableReference)); | ||
| } | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,50 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // 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.InteropServices; | ||
| using Xunit; | ||
|
|
||
| namespace System.MemoryTests | ||
| { | ||
| public static partial class MemoryMarshalTests | ||
| { | ||
| [Fact] | ||
| public static void ReadOnlyMemoryTryGetArray() | ||
| { | ||
| int[] array = new int[10]; | ||
| ReadOnlyMemory<int> memory = array; | ||
| Assert.True(MemoryMarshal.TryGetArray(memory, out ArraySegment<int> segment)); | ||
| Assert.Equal(array.Length, segment.Count); | ||
|
|
||
| for (int i = segment.Offset; i < segment.Count + segment.Offset; i++) | ||
| { | ||
| Assert.Equal(array[i], segment.Array[i]); | ||
| } | ||
| } | ||
|
|
||
| [Fact] | ||
| public static void TryGetArrayFromDefaultMemory() | ||
| { | ||
| ReadOnlyMemory<int> memory = default; | ||
| Assert.False(MemoryMarshal.TryGetArray(memory, out ArraySegment<int> segment)); | ||
| Assert.True(segment.Equals(default)); | ||
| } | ||
|
|
||
| [Fact] | ||
| public static void OwnedMemoryTryGetArray() | ||
| { | ||
| int[] array = new int[10]; | ||
| OwnedMemory<int> owner = new CustomMemoryForTest<int>(array); | ||
| ReadOnlyMemory<int> memory = owner.Memory; | ||
| Assert.True(MemoryMarshal.TryGetArray(memory, out ArraySegment<int> segment)); | ||
| Assert.Equal(array.Length, segment.Count); | ||
|
|
||
| for (int i = segment.Offset; i < segment.Count + segment.Offset; i++) | ||
| { | ||
| Assert.Equal(array[i], segment.Array[i]); | ||
| } | ||
| } | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we need to check that the length <= segment.Length - index?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If length is greater than segment.Count (if, let's say someone implements OwnedMemory.TryGetArray incorrectly and returns a subset of the backing memory), then the ArraySegment constructor will already throw ArgumentException.
Regarding index, that only changes on a Slice operation, and we decrease the length accordingly whenever that happens. Therefore, I don't think an explicit check is necessary here.