-
Notifications
You must be signed in to change notification settings - Fork 1
/
PinnedArray.vb
75 lines (57 loc) · 1.88 KB
/
PinnedArray.vb
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
Imports System
Imports System.Runtime.InteropServices
Namespace picotech
Public Class PinnedArray(Of T)
Implements IDisposable
Public _pinnedHandle As GCHandle
Private _channelCount As Integer
Public Sub New()
MyBase.New()
End Sub
Public Sub New(array As T())
MyClass.New()
_pinnedHandle = GCHandle.Alloc(array, GCHandleType.Pinned)
End Sub
Public Sub New(arraySize As Integer)
MyClass.New(New T(arraySize) {})
End Sub
Public ReadOnly Property AddrOfPinnedObject() As IntPtr
Get
Return _pinnedHandle.AddrOfPinnedObject()
End Get
End Property
Public ReadOnly Property Target As T()
Get
Return CType(_pinnedHandle.Target, T())
End Get
End Property
Default Public Property Item(ByVal index As Integer) As T
Get
End Get
Set(value As T)
End Set
End Property
Public Shared Widening Operator CType(ByVal a As PinnedArray(Of T)) As T()
If IsNothing(a) Then
Return Nothing
End If
Return TryCast(a._pinnedHandle.Target, T())
End Operator
#Region "IDisposable Support"
Private disposedValue As Boolean
' IDisposable
Protected Overridable Sub Dispose(disposing As Boolean)
If Me.disposedValue Then Return
Me.disposedValue = True
If disposing Then
'
End If
_pinnedHandle.Free()
End Sub
Public Sub Dispose() Implements IDisposable.Dispose
Dispose(True)
GC.SuppressFinalize(Me)
End Sub
#End Region
End Class
End Namespace