-
Notifications
You must be signed in to change notification settings - Fork 5.5k
Expand file tree
/
Copy pathComWrappers.NativeAot.cs
More file actions
207 lines (181 loc) · 11.1 KB
/
Copy pathComWrappers.NativeAot.cs
File metadata and controls
207 lines (181 loc) · 11.1 KB
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using System.Threading;
using Internal.Runtime;
namespace System.Runtime.InteropServices
{
/// <summary>
/// Class for managing wrappers of COM IUnknown types.
/// </summary>
public abstract partial class ComWrappers
{
internal sealed unsafe partial class ManagedObjectWrapperHolder
{
private static void RegisterIsRootedCallback()
{
delegate* unmanaged<IntPtr, bool> callback = &IsRootedCallback;
if (!RuntimeImports.RhRegisterRefCountedHandleCallback((nint)callback, MethodTable.Of<ManagedObjectWrapperHolder>()))
{
throw new OutOfMemoryException();
}
}
[UnmanagedCallersOnly]
private static bool IsRootedCallback(IntPtr pObj)
{
// We are paused in the GC, so this is safe.
ManagedObjectWrapperHolder* holder = (ManagedObjectWrapperHolder*)&pObj;
return holder->_wrapper->IsRooted;
}
private static IntPtr AllocateRefCountedHandle(ManagedObjectWrapperHolder holder)
{
return RuntimeImports.RhHandleAllocRefCounted(holder);
}
}
/// <summary>
/// Get the runtime provided IUnknown implementation.
/// </summary>
/// <param name="fpQueryInterface">Function pointer to QueryInterface.</param>
/// <param name="fpAddRef">Function pointer to AddRef.</param>
/// <param name="fpRelease">Function pointer to Release.</param>
public static unsafe void GetIUnknownImpl(out IntPtr fpQueryInterface, out IntPtr fpAddRef, out IntPtr fpRelease)
{
fpQueryInterface = (IntPtr)(delegate* unmanaged[MemberFunction]<IntPtr, Guid*, IntPtr*, int>)&ComWrappers.IUnknown_QueryInterface;
fpAddRef = (IntPtr)(delegate*<IntPtr, uint>)&RuntimeImports.RhIUnknown_AddRef; // Implemented in C/C++ to avoid GC transitions
fpRelease = (IntPtr)(delegate* unmanaged[MemberFunction]<IntPtr, uint>)&ComWrappers.IUnknown_Release;
}
internal static unsafe void GetUntrackedIUnknownImpl(out delegate* unmanaged[MemberFunction]<IntPtr, uint> fpAddRef, out delegate* unmanaged[MemberFunction]<IntPtr, uint> fpRelease)
{
// Implemented in C/C++ to avoid GC transitions during shutdown
fpAddRef = (delegate* unmanaged[MemberFunction]<IntPtr, uint>)(void*)(delegate*<IntPtr, uint>)&RuntimeImports.RhUntracked_AddRefRelease;
fpRelease = (delegate* unmanaged[MemberFunction]<IntPtr, uint>)(void*)(delegate*<IntPtr, uint>)&RuntimeImports.RhUntracked_AddRefRelease;
}
[UnmanagedCallersOnly(CallConvs = [typeof(CallConvMemberFunction)])]
internal static unsafe int IUnknown_QueryInterface(IntPtr pThis, Guid* guid, IntPtr* ppObject)
{
ManagedObjectWrapper* wrapper = ComInterfaceDispatch.ToManagedObjectWrapper((ComInterfaceDispatch*)pThis);
return wrapper->QueryInterface(in *guid, out *ppObject);
}
[UnmanagedCallersOnly(CallConvs = [typeof(CallConvMemberFunction)])]
internal static unsafe uint IUnknown_Release(IntPtr pThis)
{
ManagedObjectWrapper* wrapper = ComInterfaceDispatch.ToManagedObjectWrapper((ComInterfaceDispatch*)pThis);
uint refcount = wrapper->Release();
return refcount;
}
private static IntPtr GetTaggedImplCurrentVersion()
{
unsafe
{
return (IntPtr)(delegate* unmanaged[MemberFunction]<IntPtr, IntPtr, int>)&VtableImplementations.ITaggedImpl_IsCurrentVersion;
}
}
internal static unsafe IntPtr DefaultIUnknownVftblPtr => (IntPtr)Unsafe.AsPointer(in VtableImplementations.IUnknown);
internal static unsafe IntPtr TaggedImplVftblPtr => (IntPtr)Unsafe.AsPointer(in VtableImplementations.ITaggedImpl);
internal static unsafe IntPtr DefaultIReferenceTrackerTargetVftblPtr => (IntPtr)Unsafe.AsPointer(in VtableImplementations.IReferenceTrackerTarget);
/// <summary>
/// Define the vtable layout for the COM interfaces we provide.
/// </summary>
/// <remarks>
/// This is defined as a nested class to ensure that the vtable types are the only things initialized in the class's static constructor.
/// As long as that's the case, we can easily guarantee that they are pre-initialized and that we don't end up having startup code
/// needed to set up the vtable layouts.
/// </remarks>
private static class VtableImplementations
{
public unsafe struct IUnknownVftbl
{
public delegate* unmanaged[MemberFunction]<IntPtr, Guid*, IntPtr*, int> QueryInterface;
public delegate* unmanaged[MemberFunction]<IntPtr, int> AddRef;
public delegate* unmanaged[MemberFunction]<IntPtr, uint> Release;
}
public unsafe struct IReferenceTrackerTargetVftbl
{
public delegate* unmanaged[MemberFunction]<IntPtr, Guid*, IntPtr*, int> QueryInterface;
public delegate* unmanaged[MemberFunction]<IntPtr, int> AddRef;
public delegate* unmanaged[MemberFunction]<IntPtr, uint> Release;
public delegate* unmanaged[MemberFunction]<IntPtr, uint> AddRefFromReferenceTracker;
public delegate* unmanaged[MemberFunction]<IntPtr, uint> ReleaseFromReferenceTracker;
public delegate* unmanaged[MemberFunction]<IntPtr, uint> Peg;
public delegate* unmanaged[MemberFunction]<IntPtr, uint> Unpeg;
}
public unsafe struct ITaggedImplVftbl
{
public delegate* unmanaged[MemberFunction]<IntPtr, Guid*, IntPtr*, int> QueryInterface;
public delegate* unmanaged[MemberFunction]<IntPtr, int> AddRef;
public delegate* unmanaged[MemberFunction]<IntPtr, uint> Release;
public delegate* unmanaged[MemberFunction]<IntPtr, IntPtr, int> IsCurrentVersion;
}
[FixedAddressValueType]
public static readonly IUnknownVftbl IUnknown;
[FixedAddressValueType]
public static readonly IReferenceTrackerTargetVftbl IReferenceTrackerTarget;
[FixedAddressValueType]
public static readonly ITaggedImplVftbl ITaggedImpl;
[UnmanagedCallersOnly(CallConvs = [typeof(CallConvMemberFunction)])]
internal static unsafe int IReferenceTrackerTarget_QueryInterface(IntPtr pThis, Guid* guid, IntPtr* ppObject)
{
ManagedObjectWrapper* wrapper = ComInterfaceDispatch.ToManagedObjectWrapper((ComInterfaceDispatch*)pThis);
return wrapper->QueryInterfaceForTracker(in *guid, out *ppObject);
}
[UnmanagedCallersOnly(CallConvs = [typeof(CallConvMemberFunction)])]
internal static unsafe uint IReferenceTrackerTarget_AddRefFromReferenceTracker(IntPtr pThis)
{
ManagedObjectWrapper* wrapper = ComInterfaceDispatch.ToManagedObjectWrapper((ComInterfaceDispatch*)pThis);
return wrapper->AddRefFromReferenceTracker();
}
[UnmanagedCallersOnly(CallConvs = [typeof(CallConvMemberFunction)])]
internal static unsafe uint IReferenceTrackerTarget_ReleaseFromReferenceTracker(IntPtr pThis)
{
ManagedObjectWrapper* wrapper = ComInterfaceDispatch.ToManagedObjectWrapper((ComInterfaceDispatch*)pThis);
return wrapper->ReleaseFromReferenceTracker();
}
[UnmanagedCallersOnly(CallConvs = [typeof(CallConvMemberFunction)])]
internal static unsafe uint IReferenceTrackerTarget_Peg(IntPtr pThis)
{
ManagedObjectWrapper* wrapper = ComInterfaceDispatch.ToManagedObjectWrapper((ComInterfaceDispatch*)pThis);
return wrapper->Peg();
}
[UnmanagedCallersOnly(CallConvs = [typeof(CallConvMemberFunction)])]
internal static unsafe uint IReferenceTrackerTarget_Unpeg(IntPtr pThis)
{
ManagedObjectWrapper* wrapper = ComInterfaceDispatch.ToManagedObjectWrapper((ComInterfaceDispatch*)pThis);
return wrapper->Unpeg();
}
[UnmanagedCallersOnly(CallConvs = [typeof(CallConvMemberFunction)])]
internal static unsafe int ITaggedImpl_IsCurrentVersion(IntPtr pThis, IntPtr version)
{
return version == (IntPtr)(delegate* unmanaged[MemberFunction]<IntPtr, IntPtr, int>)&ITaggedImpl_IsCurrentVersion
? HResults.S_OK
: HResults.E_FAIL;
}
static unsafe VtableImplementations()
{
// Use the "pre-inited vtable" pattern to ensure that ILC can pre-compile these vtables.
GetIUnknownImpl(
fpQueryInterface: out *(nint*)&((IUnknownVftbl*)Unsafe.AsPointer(ref IUnknown))->QueryInterface,
fpAddRef: out *(nint*)&((IUnknownVftbl*)Unsafe.AsPointer(ref IUnknown))->AddRef,
fpRelease: out *(nint*)&((IUnknownVftbl*)Unsafe.AsPointer(ref IUnknown))->Release);
IReferenceTrackerTarget.QueryInterface = (delegate* unmanaged[MemberFunction]<IntPtr, Guid*, IntPtr*, int>)&IReferenceTrackerTarget_QueryInterface;
GetIUnknownImpl(
fpQueryInterface: out _,
fpAddRef: out *(nint*)&((IReferenceTrackerTargetVftbl*)Unsafe.AsPointer(ref IReferenceTrackerTarget))->AddRef,
fpRelease: out *(nint*)&((IReferenceTrackerTargetVftbl*)Unsafe.AsPointer(ref IReferenceTrackerTarget))->Release);
IReferenceTrackerTarget.AddRefFromReferenceTracker = (delegate* unmanaged[MemberFunction]<IntPtr, uint>)&IReferenceTrackerTarget_AddRefFromReferenceTracker;
IReferenceTrackerTarget.ReleaseFromReferenceTracker = (delegate* unmanaged[MemberFunction]<IntPtr, uint>)&IReferenceTrackerTarget_ReleaseFromReferenceTracker;
IReferenceTrackerTarget.Peg = (delegate* unmanaged[MemberFunction]<IntPtr, uint>)&IReferenceTrackerTarget_Peg;
IReferenceTrackerTarget.Unpeg = (delegate* unmanaged[MemberFunction]<IntPtr, uint>)&IReferenceTrackerTarget_Unpeg;
GetIUnknownImpl(
fpQueryInterface: out *(nint*)&((ITaggedImplVftbl*)Unsafe.AsPointer(ref ITaggedImpl))->QueryInterface,
fpAddRef: out *(nint*)&((ITaggedImplVftbl*)Unsafe.AsPointer(ref ITaggedImpl))->AddRef,
fpRelease: out *(nint*)&((ITaggedImplVftbl*)Unsafe.AsPointer(ref ITaggedImpl))->Release);
ITaggedImpl.IsCurrentVersion = (delegate* unmanaged[MemberFunction]<IntPtr, IntPtr, int>)&ITaggedImpl_IsCurrentVersion;
}
}
}
}