From 69b3522c70dc4e01ed14488cc05b0bd52dd65c44 Mon Sep 17 00:00:00 2001 From: Jan Kotas Date: Tue, 14 Mar 2017 09:06:56 -0700 Subject: [PATCH] Add Span and ReadOnlySpan from CoreCLR Signed-off-by: dotnet-bot Commit migrated from https://github.com/dotnet/coreclr/commit/dd52e03376af6d02ec1938099bbc796352437798 --- src/coreclr/src/mscorlib/shared/System/ReadOnlySpan.cs | 12 +++++++++++- src/coreclr/src/mscorlib/shared/System/Span.NonGeneric.cs | 8 ++++---- src/coreclr/src/mscorlib/shared/System/Span.cs | 12 +++++++++++- 3 files changed, 26 insertions(+), 6 deletions(-) diff --git a/src/coreclr/src/mscorlib/shared/System/ReadOnlySpan.cs b/src/coreclr/src/mscorlib/shared/System/ReadOnlySpan.cs index 35d4c4f..8ec431f 100644 --- a/src/coreclr/src/mscorlib/shared/System/ReadOnlySpan.cs +++ b/src/coreclr/src/mscorlib/shared/System/ReadOnlySpan.cs @@ -20,6 +20,9 @@ namespace System /// A byref or a native ptr. private readonly ByReference _pointer; /// The number of elements this ReadOnlySpan contains. +#if PROJECTN + [Bound] +#endif private readonly int _length; /// @@ -164,14 +167,21 @@ namespace System /// public T this[int index] { +#if PROJECTN + [BoundsChecking] + get + { + return Unsafe.Add(ref _pointer.Value, index); + } +#else [MethodImpl(MethodImplOptions.AggressiveInlining)] get { if ((uint)index >= (uint)_length) ThrowHelper.ThrowIndexOutOfRangeException(); - return Unsafe.Add(ref _pointer.Value, index); } +#endif } /// diff --git a/src/coreclr/src/mscorlib/shared/System/Span.NonGeneric.cs b/src/coreclr/src/mscorlib/shared/System/Span.NonGeneric.cs index acae5e4..4cdba21 100644 --- a/src/coreclr/src/mscorlib/shared/System/Span.NonGeneric.cs +++ b/src/coreclr/src/mscorlib/shared/System/Span.NonGeneric.cs @@ -165,12 +165,12 @@ namespace System if (byteLength == 0) return; -#if AMD64 +#if AMD64 && CORECLR if (byteLength > 4096) goto PInvoke; Unsafe.InitBlockUnaligned(ref b, 0, (uint)byteLength); return; -#else // AMD64 - // TODO: Optimize this method on X86 machine +#else + // TODO: Optimize other platforms to be on par with AMD64 CoreCLR // Note: It's important that this switch handles lengths at least up to 22. // See notes below near the main loop for why. @@ -472,7 +472,7 @@ namespace System } return; -#endif // AMD64 +#endif PInvoke: RuntimeImports.RhZeroMemory(ref b, byteLength); diff --git a/src/coreclr/src/mscorlib/shared/System/Span.cs b/src/coreclr/src/mscorlib/shared/System/Span.cs index 913c4a2..82ea35f 100644 --- a/src/coreclr/src/mscorlib/shared/System/Span.cs +++ b/src/coreclr/src/mscorlib/shared/System/Span.cs @@ -27,6 +27,9 @@ namespace System /// A byref or a native ptr. private readonly ByReference _pointer; /// The number of elements this Span contains. +#if PROJECTN + [Bound] +#endif private readonly int _length; /// @@ -179,14 +182,21 @@ namespace System /// public ref T this[int index] { +#if PROJECTN + [BoundsChecking] + get + { + return ref Unsafe.Add(ref _pointer.Value, index); + } +#else [MethodImpl(MethodImplOptions.AggressiveInlining)] get { if ((uint)index >= (uint)_length) ThrowHelper.ThrowIndexOutOfRangeException(); - return ref Unsafe.Add(ref _pointer.Value, index); } +#endif } /// -- 2.7.4