-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathPinnedArray.ps1
87 lines (72 loc) · 2.28 KB
/
PinnedArray.ps1
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
Add-Type -TypeDefinition @'
namespace PowerShellUtils
{
using System;
using System.Runtime.InteropServices;
public sealed class PinnedArray<T> : IDisposable
{
private readonly T[] array;
private readonly GCHandle gcHandle;
private bool isDisposed = false;
public static implicit operator T[](PinnedArray<T> pinnedArray)
{
return pinnedArray.Array;
}
public T this[int key]
{
get
{
if (isDisposed) { throw new ObjectDisposedException("PinnedArray"); }
return array[key];
}
set
{
if (isDisposed) { throw new ObjectDisposedException("PinnedArray"); }
array[key] = value;
}
}
public T[] Array
{
get
{
if (isDisposed) { throw new ObjectDisposedException("PinnedArray"); }
return array;
}
}
public int Length
{
get
{
if (isDisposed) { throw new ObjectDisposedException("PinnedArray"); }
return array.Length;
}
}
public int Count
{
get { return Length; }
}
public PinnedArray(uint count)
{
array = new T[count];
gcHandle = GCHandle.Alloc(Array, GCHandleType.Pinned);
}
public PinnedArray(T[] array)
{
if (array == null) { throw new ArgumentNullException("array"); }
this.array = array;
gcHandle = GCHandle.Alloc(this.array, GCHandleType.Pinned);
}
~PinnedArray()
{
Dispose();
}
public void Dispose()
{
if (isDisposed) { return; }
if (array != null) { System.Array.Clear(array, 0, array.Length); }
if (gcHandle != null) { gcHandle.Free(); }
isDisposed = true;
}
}
}
'@