1 // Licensed to the .NET Foundation under one or more agreements.
2 // The .NET Foundation licenses this file to you under the MIT license.
3 // See the LICENSE file in the project root for more information.
5 #pragma warning disable SA1121 // explicitly using type aliases instead of built-in types
7 using nuint
= System
.UInt64
;
9 using nuint
= System
.UInt32
;
12 namespace System
.Runtime
.InteropServices
14 public struct ArrayWithOffset
16 private object? m_array
;
20 // From MAX_SIZE_FOR_INTEROP in mlinfo.h
21 private const int MaxSizeForInterop
= 0x7ffffff0;
23 public ArrayWithOffset(object? array
, int offset
)
28 if (!(array
is Array arrayObj
) || (arrayObj
.Rank
!= 1) || !Marshal
.IsPinnable(arrayObj
))
30 throw new ArgumentException(SR
.ArgumentException_NotIsomorphic
);
33 nuint nativeTotalSize
= (nuint
)arrayObj
.LongLength
* (nuint
)arrayObj
.GetElementSize();
34 if (nativeTotalSize
> MaxSizeForInterop
)
36 throw new ArgumentException(SR
.Argument_StructArrayTooLarge
);
39 totalSize
= (int)nativeTotalSize
;
42 if ((uint)offset
> (uint)totalSize
)
44 throw new IndexOutOfRangeException(SR
.IndexOutOfRange_ArrayWithOffset
);
49 m_count
= totalSize
- offset
;
52 public object? GetArray() => m_array
;
54 public int GetOffset() => m_offset
;
56 public override int GetHashCode() => m_count
+ m_offset
;
58 public override bool Equals(object? obj
)
60 return obj
is ArrayWithOffset
&& Equals((ArrayWithOffset
)obj
);
63 public bool Equals(ArrayWithOffset obj
)
65 return obj
.m_array
== m_array
&& obj
.m_offset
== m_offset
&& obj
.m_count
== m_count
;
68 public static bool operator ==(ArrayWithOffset a
, ArrayWithOffset b
)
73 public static bool operator !=(ArrayWithOffset a
, ArrayWithOffset b
)