/
ArrayWithOffset.cs
67 lines (52 loc) · 2.17 KB
/
ArrayWithOffset.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System.Diagnostics.CodeAnalysis;
using System.Runtime.CompilerServices;
namespace System.Runtime.InteropServices
{
public readonly struct ArrayWithOffset : IEquatable<ArrayWithOffset>
{
private readonly object? m_array;
private readonly int m_offset;
private readonly int m_count;
// From MAX_SIZE_FOR_INTEROP in mlinfo.h
private const int MaxSizeForInterop = 0x7ffffff0;
public ArrayWithOffset(object? array, int offset)
{
int totalSize = 0;
if (array != null)
{
if (!(array is Array arrayObj) || (arrayObj.Rank != 1) || !Marshal.IsPinnable(arrayObj))
{
throw new ArgumentException(SR.ArgumentException_NotIsomorphic);
}
nuint nativeTotalSize = arrayObj.NativeLength * (nuint)arrayObj.GetElementSize();
if (nativeTotalSize > MaxSizeForInterop)
{
throw new ArgumentException(SR.Argument_StructArrayTooLarge);
}
totalSize = (int)nativeTotalSize;
}
if ((uint)offset > (uint)totalSize)
{
throw new IndexOutOfRangeException(SR.IndexOutOfRange_ArrayWithOffset);
}
m_array = array;
m_offset = offset;
m_count = totalSize - offset;
}
public object? GetArray() => m_array;
public int GetOffset() => m_offset;
public override int GetHashCode() => m_count + m_offset;
public override bool Equals([NotNullWhen(true)] object? obj)
{
return obj is ArrayWithOffset awo && Equals(awo);
}
public bool Equals(ArrayWithOffset obj)
{
return obj.m_array == m_array && obj.m_offset == m_offset && obj.m_count == m_count;
}
public static bool operator ==(ArrayWithOffset a, ArrayWithOffset b) => a.Equals(b);
public static bool operator !=(ArrayWithOffset a, ArrayWithOffset b) => !(a == b);
}
}