From 1dd7f3cadc0cd52a4c68518d4de13c457934a497 Mon Sep 17 00:00:00 2001 From: kasper3 <33230602+kasper3@users.noreply.github.com> Date: Fri, 23 Feb 2018 21:00:54 +0200 Subject: [PATCH 1/7] Return empty array if length is zero --- src/mscorlib/shared/System/Memory.cs | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/mscorlib/shared/System/Memory.cs b/src/mscorlib/shared/System/Memory.cs index c220274aef5c..0728d396a927 100644 --- a/src/mscorlib/shared/System/Memory.cs +++ b/src/mscorlib/shared/System/Memory.cs @@ -315,6 +315,11 @@ public unsafe MemoryHandle Retain(bool pin = false) /// public bool TryGetArray(out ArraySegment arraySegment) { + if (_length == 0) + { + return ArraySegment.Empty; + } + if (_index < 0) { if (((OwnedMemory)_object).TryGetArray(out var segment)) From 04664397d65df393783678c6ec92523103e2c7ad Mon Sep 17 00:00:00 2001 From: kasper3 <33230602+kasper3@users.noreply.github.com> Date: Fri, 23 Feb 2018 21:38:06 +0200 Subject: [PATCH 2/7] Return true --- src/mscorlib/shared/System/Memory.cs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/mscorlib/shared/System/Memory.cs b/src/mscorlib/shared/System/Memory.cs index 0728d396a927..b230ba7c7137 100644 --- a/src/mscorlib/shared/System/Memory.cs +++ b/src/mscorlib/shared/System/Memory.cs @@ -317,7 +317,8 @@ public bool TryGetArray(out ArraySegment arraySegment) { if (_length == 0) { - return ArraySegment.Empty; + arraySegment = ArraySegment.Empty; + return true; } if (_index < 0) From 98d681bd93ddd2396fb6a9795d48540aa297ff75 Mon Sep 17 00:00:00 2001 From: kasper3 <33230602+kasper3@users.noreply.github.com> Date: Fri, 23 Feb 2018 22:03:17 +0200 Subject: [PATCH 3/7] Add support for portable system --- src/mscorlib/shared/System/Memory.cs | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/mscorlib/shared/System/Memory.cs b/src/mscorlib/shared/System/Memory.cs index b230ba7c7137..961a825c15e8 100644 --- a/src/mscorlib/shared/System/Memory.cs +++ b/src/mscorlib/shared/System/Memory.cs @@ -317,7 +317,11 @@ public bool TryGetArray(out ArraySegment arraySegment) { if (_length == 0) { +#if FEATURE_PORTABLE_SPAN + arraySegment = new ArraySegment(Array.Empty); +#else arraySegment = ArraySegment.Empty; +#endif // FEATURE_PORTABLE_SPAN return true; } From 802903aa4a9543943396c3ba24f58cde43873953 Mon Sep 17 00:00:00 2001 From: kasper3 <33230602+kasper3@users.noreply.github.com> Date: Sat, 24 Feb 2018 10:58:12 +0200 Subject: [PATCH 4/7] Use portable span helper instead of Array.Empty --- src/mscorlib/shared/System/Memory.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/mscorlib/shared/System/Memory.cs b/src/mscorlib/shared/System/Memory.cs index 961a825c15e8..a6b0f6987009 100644 --- a/src/mscorlib/shared/System/Memory.cs +++ b/src/mscorlib/shared/System/Memory.cs @@ -318,7 +318,7 @@ public bool TryGetArray(out ArraySegment arraySegment) if (_length == 0) { #if FEATURE_PORTABLE_SPAN - arraySegment = new ArraySegment(Array.Empty); + arraySegment = new ArraySegment(SpanHelpers.PerTypeValues.EmptyArray); #else arraySegment = ArraySegment.Empty; #endif // FEATURE_PORTABLE_SPAN From 857863d705441e64d1628dc28a0ebf965f996f8b Mon Sep 17 00:00:00 2001 From: kasper3 <33230602+kasper3@users.noreply.github.com> Date: Sat, 24 Feb 2018 13:26:51 +0200 Subject: [PATCH 5/7] Move to end --- src/mscorlib/shared/System/Memory.cs | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) diff --git a/src/mscorlib/shared/System/Memory.cs b/src/mscorlib/shared/System/Memory.cs index a6b0f6987009..7ad898c2c006 100644 --- a/src/mscorlib/shared/System/Memory.cs +++ b/src/mscorlib/shared/System/Memory.cs @@ -315,16 +315,6 @@ public unsafe MemoryHandle Retain(bool pin = false) /// public bool TryGetArray(out ArraySegment arraySegment) { - if (_length == 0) - { -#if FEATURE_PORTABLE_SPAN - arraySegment = new ArraySegment(SpanHelpers.PerTypeValues.EmptyArray); -#else - arraySegment = ArraySegment.Empty; -#endif // FEATURE_PORTABLE_SPAN - return true; - } - if (_index < 0) { if (((OwnedMemory)_object).TryGetArray(out var segment)) @@ -338,6 +328,15 @@ public bool TryGetArray(out ArraySegment arraySegment) arraySegment = new ArraySegment(arr, _index, _length); return true; } + else if (_length == 0) + { +#if FEATURE_PORTABLE_SPAN + arraySegment = new ArraySegment(SpanHelpers.PerTypeValues.EmptyArray); +#else + arraySegment = ArraySegment.Empty; +#endif // FEATURE_PORTABLE_SPAN + return true; + } arraySegment = default(ArraySegment); return false; From 70365b82beb1d11f8db8bbe90f9793df76f18b57 Mon Sep 17 00:00:00 2001 From: kasper3 <33230602+kasper3@users.noreply.github.com> Date: Sat, 24 Feb 2018 13:31:23 +0200 Subject: [PATCH 6/7] Remove else --- src/mscorlib/shared/System/Memory.cs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/mscorlib/shared/System/Memory.cs b/src/mscorlib/shared/System/Memory.cs index 7ad898c2c006..4a1ce4dc9d79 100644 --- a/src/mscorlib/shared/System/Memory.cs +++ b/src/mscorlib/shared/System/Memory.cs @@ -328,7 +328,8 @@ public bool TryGetArray(out ArraySegment arraySegment) arraySegment = new ArraySegment(arr, _index, _length); return true; } - else if (_length == 0) + + if (_length == 0) { #if FEATURE_PORTABLE_SPAN arraySegment = new ArraySegment(SpanHelpers.PerTypeValues.EmptyArray); From afac8451f3945037879299b80288f13b22437da9 Mon Sep 17 00:00:00 2001 From: kasper3 <33230602+kasper3@users.noreply.github.com> Date: Sat, 24 Feb 2018 14:03:54 +0200 Subject: [PATCH 7/7] Return empty array if length is 0 in MemoryMarshal --- .../System/Runtime/InteropServices/MemoryMarshal.cs | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/mscorlib/shared/System/Runtime/InteropServices/MemoryMarshal.cs b/src/mscorlib/shared/System/Runtime/InteropServices/MemoryMarshal.cs index 7fabf1e2fe24..6544081df14c 100644 --- a/src/mscorlib/shared/System/Runtime/InteropServices/MemoryMarshal.cs +++ b/src/mscorlib/shared/System/Runtime/InteropServices/MemoryMarshal.cs @@ -35,6 +35,16 @@ public static bool TryGetArray(ReadOnlyMemory readOnlyMemory, out ArraySeg return true; } + if (length == 0) + { +#if FEATURE_PORTABLE_SPAN + arraySegment = new ArraySegment(SpanHelpers.PerTypeValues.EmptyArray); +#else + arraySegment = ArraySegment.Empty; +#endif // FEATURE_PORTABLE_SPAN + return true; + } + arraySegment = default; return false; }