Fix StyleCop warning SA1121 (use built-in types)
[mono-project.git] / netcore / System.Private.CoreLib / shared / System / Runtime / InteropServices / ArrayWithOffset.cs
blob600b7748589ec3b62def9ea3935ddeb8714f5907
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
6 #if BIT64
7 using nuint = System.UInt64;
8 #else
9 using nuint = System.UInt32;
10 #endif
12 namespace System.Runtime.InteropServices
14 public struct ArrayWithOffset
16 private object? m_array;
17 private int m_offset;
18 private int m_count;
20 // From MAX_SIZE_FOR_INTEROP in mlinfo.h
21 private const int MaxSizeForInterop = 0x7ffffff0;
23 public ArrayWithOffset(object? array, int offset)
25 int totalSize = 0;
26 if (array != null)
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);
47 m_array = array;
48 m_offset = offset;
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)
70 return a.Equals(b);
73 public static bool operator !=(ArrayWithOffset a, ArrayWithOffset b)
75 return !(a == b);