From 60411f890726c180de49d3ce7e0431d1f9f107c8 Mon Sep 17 00:00:00 2001 From: drawaes Date: Thu, 2 Nov 2017 22:50:31 +0000 Subject: [PATCH 1/2] DangerousCreateXXX methods --- src/mscorlib/System.Private.CoreLib.csproj | 3 +- src/mscorlib/shared/System/Memory.cs | 9 ++++++ .../Runtime/InteropServices/MemoryMarshal.cs | 31 +++++++++++++++++++ 3 files changed, 42 insertions(+), 1 deletion(-) create mode 100644 src/mscorlib/src/System/Runtime/InteropServices/MemoryMarshal.cs diff --git a/src/mscorlib/System.Private.CoreLib.csproj b/src/mscorlib/System.Private.CoreLib.csproj index 72368f0c089e..cf3eeedd9290 100644 --- a/src/mscorlib/System.Private.CoreLib.csproj +++ b/src/mscorlib/System.Private.CoreLib.csproj @@ -654,6 +654,7 @@ + @@ -690,4 +691,4 @@ $(IntermediateOutputPath)\System.Private.CoreLib.res - + \ No newline at end of file diff --git a/src/mscorlib/shared/System/Memory.cs b/src/mscorlib/shared/System/Memory.cs index ecb33e891865..ae89faa92f4f 100644 --- a/src/mscorlib/shared/System/Memory.cs +++ b/src/mscorlib/shared/System/Memory.cs @@ -90,6 +90,15 @@ internal Memory(OwnedMemory owner, int index, int length) _length = length; } + // Constructor for internal use only + [MethodImpl(MethodImplOptions.AggressiveInlining)] + internal Memory(int start, int length, T[] array) + { + _arrayOrOwnedMemory = array; + _index = start; + _length = length; + } + /// /// Defines an implicit conversion of an array to a /// diff --git a/src/mscorlib/src/System/Runtime/InteropServices/MemoryMarshal.cs b/src/mscorlib/src/System/Runtime/InteropServices/MemoryMarshal.cs new file mode 100644 index 000000000000..b5abacc839bc --- /dev/null +++ b/src/mscorlib/src/System/Runtime/InteropServices/MemoryMarshal.cs @@ -0,0 +1,31 @@ +// 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. + +namespace src.System.Runtime.InteropServices +{ + using System; + using System.Runtime.CompilerServices; + + public static class MemoryMarshal + { + /// + /// Creates a span over the range specified. However this does no bounds checking therefore is dangerous + /// all validation should be done prior to calling this method + /// + /// The target array. + /// The index at which to begin the span. + /// The number of items in the span. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static Span DangerousCreateSpan(T[] array, int start, int length) => new Span(ref Unsafe.Add(ref Unsafe.As(ref array.GetRawSzArrayData()), start), length); + + /// + /// Creates a memory instance over the range. However this does no bounds checking therefore is dangerous + /// + /// The target array. + /// The index at which to begin the span. + /// The number of items in the span. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static Memory DangerousCreateMemory(T[] array, int start, int length) => new Memory(start, length, array); + } +} From 8ea363b8fc347c9828428fe9bfc05b5e1f63318d Mon Sep 17 00:00:00 2001 From: drawaes Date: Thu, 2 Nov 2017 23:04:33 +0000 Subject: [PATCH 2/2] Remove dangerous --- .../src/System/Runtime/InteropServices/MemoryMarshal.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/mscorlib/src/System/Runtime/InteropServices/MemoryMarshal.cs b/src/mscorlib/src/System/Runtime/InteropServices/MemoryMarshal.cs index b5abacc839bc..840c64359c83 100644 --- a/src/mscorlib/src/System/Runtime/InteropServices/MemoryMarshal.cs +++ b/src/mscorlib/src/System/Runtime/InteropServices/MemoryMarshal.cs @@ -17,7 +17,7 @@ public static class MemoryMarshal /// The index at which to begin the span. /// The number of items in the span. [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static Span DangerousCreateSpan(T[] array, int start, int length) => new Span(ref Unsafe.Add(ref Unsafe.As(ref array.GetRawSzArrayData()), start), length); + public static Span CreateSpan(T[] array, int start, int length) => new Span(ref Unsafe.Add(ref Unsafe.As(ref array.GetRawSzArrayData()), start), length); /// /// Creates a memory instance over the range. However this does no bounds checking therefore is dangerous @@ -26,6 +26,6 @@ public static class MemoryMarshal /// The index at which to begin the span. /// The number of items in the span. [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static Memory DangerousCreateMemory(T[] array, int start, int length) => new Memory(start, length, array); + public static Memory CreateMemory(T[] array, int start, int length) => new Memory(start, length, array); } }