private static PInvokeDelegateThunk AllocateThunk(Delegate del) { if (s_thunkPoolHeap == null) { // TODO: Free s_thunkPoolHeap if the thread lose the race Interlocked.CompareExchange( ref s_thunkPoolHeap, RuntimeAugments.CreateThunksHeap(RuntimeImports.GetInteropCommonStubAddress()), null ); Debug.Assert(s_thunkPoolHeap != null); } var delegateThunk = new PInvokeDelegateThunk(del); // // For open static delegates set target to ReverseOpenStaticDelegateStub which calls the static function pointer directly // bool openStaticDelegate = del.GetRawFunctionPointerForOpenStaticDelegate() != IntPtr.Zero; IntPtr pTarget = RuntimeInteropData.GetDelegateMarshallingStub(del.GetTypeHandle(), openStaticDelegate); Debug.Assert(pTarget != IntPtr.Zero); RuntimeAugments.SetThunkData(s_thunkPoolHeap, delegateThunk.Thunk, delegateThunk.ContextData, pTarget); return(delegateThunk); }
private static PInvokeDelegateThunk AllocateThunk(Delegate del) { if (s_thunkPoolHeap == null) { // TODO: Free s_thunkPoolHeap if the thread lose the race Interlocked.CompareExchange( ref s_thunkPoolHeap, RuntimeAugments.CreateThunksHeap(RuntimeImports.GetInteropCommonStubAddress()), null ); Debug.Assert(s_thunkPoolHeap != null); } var delegateThunk = new PInvokeDelegateThunk(del); McgPInvokeDelegateData pinvokeDelegateData; if (!RuntimeAugments.InteropCallbacks.TryGetMarshallerDataForDelegate(del.GetTypeHandle(), out pinvokeDelegateData)) { Environment.FailFast("Couldn't find marshalling stubs for delegate."); } // // For open static delegates set target to ReverseOpenStaticDelegateStub which calls the static function pointer directly // IntPtr pTarget = del.GetRawFunctionPointerForOpenStaticDelegate() == IntPtr.Zero ? pinvokeDelegateData.ReverseStub : pinvokeDelegateData.ReverseOpenStaticDelegateStub; RuntimeAugments.SetThunkData(s_thunkPoolHeap, delegateThunk.Thunk, delegateThunk.ContextData, pTarget); return(delegateThunk); }
private static IntPtr GetOrAllocateThunk(Delegate del) { ConditionalWeakTable <Delegate, PInvokeDelegateThunk> pinvokeDelegates = GetPInvokeDelegates(); PInvokeDelegateThunk delegateThunk; // if the delegate already exists in the table return the allocated thunk for it if (pinvokeDelegates.TryGetValue(del, out delegateThunk)) { return(delegateThunk.Thunk); } if (s_thunkPoolHeap == null) { s_thunkPoolHeap = RuntimeAugments.CreateThunksHeap(RuntimeImports.GetInteropCommonStubAddress()); Debug.Assert(s_thunkPoolHeap != null); } delegateThunk = new PInvokeDelegateThunk(del); McgPInvokeDelegateData pinvokeDelegateData; if (!RuntimeAugments.InteropCallbacks.TryGetMarshallerDataForDelegate(del.GetTypeHandle(), out pinvokeDelegateData)) { Environment.FailFast("Couldn't find marshalling stubs for delegate."); } // // For open static delegates set target to ReverseOpenStaticDelegateStub which calls the static function pointer directly // IntPtr pTarget = del.GetRawFunctionPointerForOpenStaticDelegate() == IntPtr.Zero ? pinvokeDelegateData.ReverseStub : pinvokeDelegateData.ReverseOpenStaticDelegateStub; RuntimeAugments.SetThunkData(s_thunkPoolHeap, delegateThunk.Thunk, delegateThunk.ContextData, pTarget); // Add the delegate to the dictionary if it doesn't already exists delegateThunk = pinvokeDelegates.GetOrAdd(del, delegateThunk); return(delegateThunk.Thunk); }