forked from dotnet/runtime
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Mirror changes from dotnet/coreclr (dotnet/corefx#28534)
* Adding Memory.Pin() to eventually replace Memory.Retain(bool) (dotnet/corefx#17269) * Adding Memory.Pin() to eventually replace Memory.Retain(bool) * Fix copy/paste error and return default for when object is null. * Fix XML comments. Signed-off-by: dotnet-bot-corefx-mirror <[email protected]> * Add Memory.Pin to the ref and add tests. * Add Pin tests * Update calls to Retain(true) to now call Pin() * Remove extra space in tests. * Add Pin to the ApiCompatBaseline for uapaot. Commit migrated from dotnet/corefx@bb5c468
1 parent
68e8e69
commit c31efe6
Showing
17 changed files
with
345 additions
and
72 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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,133 @@ | ||
// 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 Xunit; | ||
|
||
namespace System.MemoryTests | ||
{ | ||
public static partial class MemoryTests | ||
{ | ||
[Fact] | ||
public static void MemoryPin() | ||
{ | ||
int[] array = { 1, 2, 3, 4, 5 }; | ||
Memory<int> memory = array; | ||
MemoryHandle handle = memory.Pin(); | ||
Assert.True(handle.HasPointer); | ||
unsafe | ||
{ | ||
int* pointer = (int*)handle.Pointer; | ||
|
||
GC.Collect(); | ||
|
||
for (int i = 0; i < memory.Length; i++) | ||
{ | ||
Assert.Equal(array[i], pointer[i]); | ||
} | ||
} | ||
handle.Dispose(); | ||
} | ||
|
||
[Fact] | ||
public static void MemoryFromEmptyArrayPin() | ||
{ | ||
Memory<int> memory = new int[0]; | ||
MemoryHandle handle = memory.Pin(); | ||
Assert.True(handle.HasPointer); | ||
handle.Dispose(); | ||
} | ||
|
||
[Fact] | ||
public static void DefaultMemoryPin() | ||
{ | ||
Memory<int> memory = default; | ||
MemoryHandle handle = memory.Pin(); | ||
Assert.False(handle.HasPointer); | ||
unsafe | ||
{ | ||
Assert.True(handle.Pointer == null); | ||
} | ||
handle.Dispose(); | ||
} | ||
|
||
[Fact] | ||
public static void MemoryPinAndSlice() | ||
{ | ||
int[] array = { 1, 2, 3, 4, 5 }; | ||
Memory<int> memory = array; | ||
memory = memory.Slice(1); | ||
MemoryHandle handle = memory.Pin(); | ||
Span<int> span = memory.Span; | ||
Assert.True(handle.HasPointer); | ||
unsafe | ||
{ | ||
int* pointer = (int*)handle.Pointer; | ||
|
||
GC.Collect(); | ||
|
||
for (int i = 0; i < memory.Length; i++) | ||
{ | ||
Assert.Equal(array[i + 1], pointer[i]); | ||
} | ||
|
||
for (int i = 0; i < memory.Length; i++) | ||
{ | ||
Assert.Equal(array[i + 1], span[i]); | ||
} | ||
} | ||
handle.Dispose(); | ||
} | ||
|
||
[Fact] | ||
public static void OwnedMemoryPin() | ||
{ | ||
int[] array = { 1, 2, 3, 4, 5 }; | ||
OwnedMemory<int> owner = new CustomMemoryForTest<int>(array); | ||
Memory<int> memory = owner.Memory; | ||
MemoryHandle handle = memory.Pin(); | ||
Assert.True(handle.HasPointer); | ||
unsafe | ||
{ | ||
int* pointer = (int*)handle.Pointer; | ||
|
||
GC.Collect(); | ||
|
||
for (int i = 0; i < memory.Length; i++) | ||
{ | ||
Assert.Equal(array[i], pointer[i]); | ||
} | ||
} | ||
handle.Dispose(); | ||
} | ||
|
||
[Fact] | ||
public static void OwnedMemoryPinAndSlice() | ||
{ | ||
int[] array = { 1, 2, 3, 4, 5 }; | ||
OwnedMemory<int> owner = new CustomMemoryForTest<int>(array); | ||
Memory<int> memory = owner.Memory.Slice(1); | ||
MemoryHandle handle = memory.Pin(); | ||
Span<int> span = memory.Span; | ||
Assert.True(handle.HasPointer); | ||
unsafe | ||
{ | ||
int* pointer = (int*)handle.Pointer; | ||
|
||
GC.Collect(); | ||
|
||
for (int i = 0; i < memory.Length; i++) | ||
{ | ||
Assert.Equal(array[i + 1], pointer[i]); | ||
} | ||
|
||
for (int i = 0; i < memory.Length; i++) | ||
{ | ||
Assert.Equal(array[i + 1], span[i]); | ||
} | ||
} | ||
handle.Dispose(); | ||
} | ||
} | ||
} |
This file contains 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 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 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
133 changes: 133 additions & 0 deletions
133
src/libraries/System.Memory/tests/ReadOnlyMemory/Pin.cs
This file contains 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,133 @@ | ||
// 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 Xunit; | ||
|
||
namespace System.MemoryTests | ||
{ | ||
public static partial class ReadOnlyMemoryTests | ||
{ | ||
[Fact] | ||
public static void MemoryPin() | ||
{ | ||
int[] array = { 1, 2, 3, 4, 5 }; | ||
ReadOnlyMemory<int> memory = array; | ||
MemoryHandle handle = memory.Pin(); | ||
Assert.True(handle.HasPointer); | ||
unsafe | ||
{ | ||
int* pointer = (int*)handle.Pointer; | ||
|
||
GC.Collect(); | ||
|
||
for (int i = 0; i < memory.Length; i++) | ||
{ | ||
Assert.Equal(array[i], pointer[i]); | ||
} | ||
} | ||
handle.Dispose(); | ||
} | ||
|
||
[Fact] | ||
public static void MemoryFromEmptyArrayPin() | ||
{ | ||
ReadOnlyMemory<int> memory = new int[0]; | ||
MemoryHandle handle = memory.Pin(); | ||
Assert.True(handle.HasPointer); | ||
handle.Dispose(); | ||
} | ||
|
||
[Fact] | ||
public static void DefaultMemoryPin() | ||
{ | ||
ReadOnlyMemory<int> memory = default; | ||
MemoryHandle handle = memory.Pin(); | ||
Assert.False(handle.HasPointer); | ||
unsafe | ||
{ | ||
Assert.True(handle.Pointer == null); | ||
} | ||
handle.Dispose(); | ||
} | ||
|
||
[Fact] | ||
public static void MemoryPinAndSlice() | ||
{ | ||
int[] array = { 1, 2, 3, 4, 5 }; | ||
ReadOnlyMemory<int> memory = array; | ||
memory = memory.Slice(1); | ||
MemoryHandle handle = memory.Pin(); | ||
ReadOnlySpan<int> span = memory.Span; | ||
Assert.True(handle.HasPointer); | ||
unsafe | ||
{ | ||
int* pointer = (int*)handle.Pointer; | ||
|
||
GC.Collect(); | ||
|
||
for (int i = 0; i < memory.Length; i++) | ||
{ | ||
Assert.Equal(array[i + 1], pointer[i]); | ||
} | ||
|
||
for (int i = 0; i < memory.Length; i++) | ||
{ | ||
Assert.Equal(array[i + 1], span[i]); | ||
} | ||
} | ||
handle.Dispose(); | ||
} | ||
|
||
[Fact] | ||
public static void OwnedMemoryPin() | ||
{ | ||
int[] array = { 1, 2, 3, 4, 5 }; | ||
OwnedMemory<int> owner = new CustomMemoryForTest<int>(array); | ||
ReadOnlyMemory<int> memory = owner.Memory; | ||
MemoryHandle handle = memory.Pin(); | ||
Assert.True(handle.HasPointer); | ||
unsafe | ||
{ | ||
int* pointer = (int*)handle.Pointer; | ||
|
||
GC.Collect(); | ||
|
||
for (int i = 0; i < memory.Length; i++) | ||
{ | ||
Assert.Equal(array[i], pointer[i]); | ||
} | ||
} | ||
handle.Dispose(); | ||
} | ||
|
||
[Fact] | ||
public static void OwnedMemoryPinAndSlice() | ||
{ | ||
int[] array = { 1, 2, 3, 4, 5 }; | ||
OwnedMemory<int> owner = new CustomMemoryForTest<int>(array); | ||
ReadOnlyMemory<int> memory = owner.Memory.Slice(1); | ||
MemoryHandle handle = memory.Pin(); | ||
ReadOnlySpan<int> span = memory.Span; | ||
Assert.True(handle.HasPointer); | ||
unsafe | ||
{ | ||
int* pointer = (int*)handle.Pointer; | ||
|
||
GC.Collect(); | ||
|
||
for (int i = 0; i < memory.Length; i++) | ||
{ | ||
Assert.Equal(array[i + 1], pointer[i]); | ||
} | ||
|
||
for (int i = 0; i < memory.Length; i++) | ||
{ | ||
Assert.Equal(array[i + 1], span[i]); | ||
} | ||
} | ||
handle.Dispose(); | ||
} | ||
} | ||
} |
This file contains 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 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 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 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 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 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
4 changes: 4 additions & 0 deletions
4
src/libraries/System.Runtime/src/ApiCompatBaseline.uapaot.txt
This file contains 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,4 @@ | ||
Compat issues with assembly System.Runtime: | ||
MembersMustExist : Member 'System.Memory<T>.Pin()' does not exist in the implementation but it does exist in the contract. | ||
MembersMustExist : Member 'System.ReadOnlyMemory<T>.Pin()' does not exist in the implementation but it does exist in the contract. | ||
Total Issues: 2 |
This file contains 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