Skip to content
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

Add internal Array.NativeLength property #51589

Merged
merged 3 commits into from
Apr 21, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 11 additions & 8 deletions src/coreclr/System.Private.CoreLib/src/System/Array.CoreCLR.cs
Original file line number Diff line number Diff line change
Expand Up @@ -138,8 +138,8 @@ public static unsafe void Copy(Array sourceArray, Array destinationArray, int le
MethodTable* pMT = RuntimeHelpers.GetMethodTable(sourceArray);
if (pMT == RuntimeHelpers.GetMethodTable(destinationArray) &&
!pMT->IsMultiDimensionalArray &&
(uint)length <= (nuint)sourceArray.LongLength &&
(uint)length <= (nuint)destinationArray.LongLength)
(uint)length <= sourceArray.NativeLength &&
(uint)length <= destinationArray.NativeLength)
{
nuint byteCount = (uint)length * (nuint)pMT->ComponentSize;
ref byte src = ref Unsafe.As<RawArrayData>(sourceArray).Data;
Expand Down Expand Up @@ -169,8 +169,8 @@ public static unsafe void Copy(Array sourceArray, int sourceIndex, Array destina
if (pMT == RuntimeHelpers.GetMethodTable(destinationArray) &&
!pMT->IsMultiDimensionalArray &&
length >= 0 && sourceIndex >= 0 && destinationIndex >= 0 &&
(uint)(sourceIndex + length) <= (nuint)sourceArray.LongLength &&
(uint)(destinationIndex + length) <= (nuint)destinationArray.LongLength)
(uint)(sourceIndex + length) <= sourceArray.NativeLength &&
(uint)(destinationIndex + length) <= destinationArray.NativeLength)
{
nuint elementSize = (nuint)pMT->ComponentSize;
nuint byteCount = (uint)length * elementSize;
Expand Down Expand Up @@ -214,9 +214,9 @@ private static unsafe void Copy(Array sourceArray, int sourceIndex, Array destin
throw new ArgumentOutOfRangeException(nameof(destinationIndex), SR.ArgumentOutOfRange_ArrayLB);
destinationIndex -= dstLB;

if ((uint)(sourceIndex + length) > (nuint)sourceArray.LongLength)
if ((uint)(sourceIndex + length) > sourceArray.NativeLength)
throw new ArgumentException(SR.Arg_LongerThanSrcArray, nameof(sourceArray));
if ((uint)(destinationIndex + length) > (nuint)destinationArray.LongLength)
if ((uint)(destinationIndex + length) > destinationArray.NativeLength)
throw new ArgumentException(SR.Arg_LongerThanDestArray, nameof(destinationArray));

if (sourceArray.GetType() == destinationArray.GetType() || IsSimpleCopy(sourceArray, destinationArray))
Expand Down Expand Up @@ -287,7 +287,7 @@ public static unsafe void Clear(Array array, int index, int length)

int offset = index - lowerBound;

if (index < lowerBound || offset < 0 || length < 0 || (uint)(offset + length) > (nuint)array.LongLength)
if (index < lowerBound || offset < 0 || length < 0 || (uint)(offset + length) > array.NativeLength)
ThrowHelper.ThrowIndexOutOfRangeException();

nuint elementSize = pMT->ComponentSize;
Expand Down Expand Up @@ -419,7 +419,10 @@ public unsafe void SetValue(object? value, params int[] indices)

public int Length => checked((int)Unsafe.As<RawArrayData>(this).Length);

public long LongLength => Unsafe.As<RawArrayData>(this).Length;
// This could return a length greater than int.MaxValue
internal nuint NativeLength => Unsafe.As<RawArrayData>(this).Length;

public long LongLength => (long)NativeLength;

public unsafe int Rank
{
Expand Down
6 changes: 3 additions & 3 deletions src/libraries/System.Private.CoreLib/src/System/Buffer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public static unsafe void BlockCopy(Array src, int srcOffset, Array dst, int dst
if (dst == null)
throw new ArgumentNullException(nameof(dst));

nuint uSrcLen = (nuint)src.LongLength;
nuint uSrcLen = src.NativeLength;
if (src.GetType() != typeof(byte[]))
{
if (!src.GetCorElementTypeOfElementType().IsPrimitiveType())
Expand All @@ -37,7 +37,7 @@ public static unsafe void BlockCopy(Array src, int srcOffset, Array dst, int dst
nuint uDstLen = uSrcLen;
if (src != dst)
{
uDstLen = (nuint)dst.LongLength;
uDstLen = dst.NativeLength;
if (dst.GetType() != typeof(byte[]))
{
if (!dst.GetCorElementTypeOfElementType().IsPrimitiveType())
Expand Down Expand Up @@ -73,7 +73,7 @@ public static int ByteLength(Array array)
if (!array.GetCorElementTypeOfElementType().IsPrimitiveType())
throw new ArgumentException(SR.Arg_MustBePrimArray, nameof(array));

nuint byteLength = (nuint)array.LongLength * (nuint)array.GetElementSize();
nuint byteLength = array.NativeLength * (nuint)array.GetElementSize();

// This API is explosed both as Buffer.ByteLength and also used indirectly in argument
// checks for Buffer.GetByte/SetByte.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public ArrayWithOffset(object? array, int offset)
throw new ArgumentException(SR.ArgumentException_NotIsomorphic);
}

nuint nativeTotalSize = (nuint)arrayObj.LongLength * (nuint)arrayObj.GetElementSize();
nuint nativeTotalSize = arrayObj.NativeLength * (nuint)arrayObj.GetElementSize();
if (nativeTotalSize > MaxSizeForInterop)
{
throw new ArgumentException(SR.Argument_StructArrayTooLarge);
Expand Down
8 changes: 7 additions & 1 deletion src/mono/System.Private.CoreLib/src/System/Array.Mono.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,12 @@ public int Length
get => Length;
}

// This could return a length greater than int.MaxValue
internal nuint NativeLength
{
get => (nuint)Unsafe.As<RawData>(this).Count;
}

public long LongLength
{
get
Expand Down Expand Up @@ -62,7 +68,7 @@ public static unsafe void Clear(Array array, int index, int length)

int lowerBound = array.GetLowerBound(0);
int elementSize = array.GetElementSize();
nuint numComponents = (nuint)(nint)Unsafe.As<RawData>(array).Count;
nuint numComponents = array.NativeLength;

int offset = index - lowerBound;

Expand Down