-
Notifications
You must be signed in to change notification settings - Fork 5.6k
Add public API MemoryMarshal.GetArrayDataReference #1036
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
GrabYourPitchforks
merged 7 commits into
dotnet:master
from
GrabYourPitchforks:getrawszarraydata
Jan 8, 2020
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
44ee428
Add MemoryMarshal.GetRawArrayData
GrabYourPitchforks 3c25b18
Add ref APIs and unit tests
GrabYourPitchforks f7ee871
Merge remote-tracking branch 'origin/master' into getrawszarraydata
GrabYourPitchforks 559a501
PR feedback: fix typos, refactor code, add crossgen2 support
GrabYourPitchforks 4c86daf
Merge remote-tracking branch 'origin/master' into getrawszarraydata
GrabYourPitchforks b48a643
Rename GetRawArrayData to GetArrayDataReference
GrabYourPitchforks 8e048f8
Add missing libraries files
GrabYourPitchforks 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
27 changes: 27 additions & 0 deletions
27
...lr/src/System.Private.CoreLib/src/System/Runtime/InteropServices/MemoryMarshal.CoreCLR.cs
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,27 @@ | ||
| // 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.Runtime.CompilerServices; | ||
| using System.Runtime.Versioning; | ||
| using Internal.Runtime.CompilerServices; | ||
|
|
||
| namespace System.Runtime.InteropServices | ||
| { | ||
| public static partial class MemoryMarshal | ||
| { | ||
| /// <summary> | ||
| /// Returns a reference to the 0th element of <paramref name="array"/>. If the array is empty, returns a reference to where the 0th element | ||
| /// would have been stored. Such a reference may be used for pinning but must never be dereferenced. | ||
| /// </summary> | ||
| /// <exception cref="NullReferenceException"><paramref name="array"/> is <see langword="null"/>.</exception> | ||
| /// <remarks> | ||
| /// This method does not perform array variance checks. The caller must manually perform any array variance checks | ||
| /// if the caller wishes to write to the returned reference. | ||
| /// </remarks> | ||
| [Intrinsic] | ||
| [NonVersionable] | ||
| public static ref T GetArrayDataReference<T>(T[] array) => | ||
| ref Unsafe.As<byte, T>(ref Unsafe.As<RawArrayData>(array).Data); | ||
| } | ||
| } |
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
35 changes: 35 additions & 0 deletions
35
src/coreclr/src/tools/Common/TypeSystem/IL/Stubs/MemoryMarshalIntrinsics.cs
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,35 @@ | ||
| // 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 Internal.TypeSystem; | ||
|
|
||
| using Debug = System.Diagnostics.Debug; | ||
|
|
||
| namespace Internal.IL.Stubs | ||
| { | ||
| /// <summary> | ||
| /// Provides method bodies for System.Runtime.InteropServices.MemoryMarshal intrinsics. | ||
| /// </summary> | ||
| public static class MemoryMarshalIntrinsics | ||
| { | ||
| public static MethodIL EmitIL(MethodDesc method) | ||
| { | ||
| Debug.Assert(((MetadataType)method.OwningType).Name == "MemoryMarshal"); | ||
| string methodName = method.Name; | ||
|
|
||
| if (methodName == "GetArrayDataReference") | ||
| { | ||
| ILEmitter emit = new ILEmitter(); | ||
| ILCodeStream codeStream = emit.NewCodeStream(); | ||
| codeStream.EmitLdArg(0); | ||
| codeStream.Emit(ILOpcode.ldflda, emit.NewToken(method.Context.SystemModule.GetKnownType("System.Runtime.CompilerServices", "RawArrayData").GetField("Data"))); | ||
| codeStream.Emit(ILOpcode.ret); | ||
| return emit.Link(method); | ||
| } | ||
|
|
||
| // unknown method | ||
| return null; | ||
| } | ||
| } | ||
| } |
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
49 changes: 49 additions & 0 deletions
49
src/libraries/System.Memory/tests/MemoryMarshal/GetArrayDataReference.cs
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,49 @@ | ||
| // 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 System.Runtime.InteropServices; | ||
|
|
||
| namespace System.SpanTests | ||
| { | ||
| public static partial class MemoryMarshalTests | ||
| { | ||
| [Fact] | ||
| public static void GetArrayDataReference_NullInput_ThrowsNullRef() | ||
| { | ||
| Assert.Throws<NullReferenceException>(() => MemoryMarshal.GetArrayDataReference((object[])null)); | ||
| } | ||
|
|
||
| [Fact] | ||
| public static void GetArrayDataReference_NonEmptyInput_ReturnsRefToFirstElement() | ||
| { | ||
| int[] theArray = new int[] { 10, 20, 30 }; | ||
| Assert.True(Unsafe.AreSame(ref theArray[0], ref MemoryMarshal.GetArrayDataReference(theArray))); | ||
| } | ||
|
|
||
| [Fact] | ||
| public static unsafe void GetArrayDataReference_EmptyInput_ReturnsRefToWhereFirstElementWouldBe() | ||
| { | ||
| int[] theArray = new int[0]; | ||
|
|
||
| ref int theRef = ref MemoryMarshal.GetArrayDataReference(theArray); | ||
|
|
||
| Assert.True(Unsafe.AsPointer(ref theRef) != null); | ||
| Assert.True(Unsafe.AreSame(ref theRef, ref MemoryMarshal.GetReference(theArray.AsSpan()))); | ||
| } | ||
|
|
||
| [Fact] | ||
| public static void GetArrayDataReference_IgnoresArrayVarianceChecks() | ||
| { | ||
| string[] strArr = new string[] { "Hello" }; | ||
|
|
||
| // 'ref object' instead of 'ref string' because GetArrayDataReference skips array variance checks. | ||
| // We can deref it but we must not write to it unless we know the value being written is also a string. | ||
| ref object refObj = ref MemoryMarshal.GetArrayDataReference<object>(strArr); | ||
|
|
||
| Assert.True(Unsafe.AreSame(ref refObj, ref Unsafe.As<string, object>(ref strArr[0]))); | ||
| } | ||
| } | ||
| } |
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
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.