public IntPtr ContextData; // ThunkContextData pointer which will be stored in the context slot of the thunk public PInvokeDelegateThunk(Delegate del) { Thunk = RuntimeAugments.AllocateThunk(s_thunkPoolHeap); Debug.Assert(Thunk != IntPtr.Zero); if (Thunk == IntPtr.Zero) { // We've either run out of memory, or failed to allocate a new thunk due to some other bug. Now we should fail fast Environment.FailFast("Insufficient number of thunks."); } else { // // Allocate unmanaged memory for GCHandle of delegate and function pointer of open static delegate // We will store this pointer on the context slot of thunk data // unsafe { ContextData = (IntPtr)NativeMemory.Alloc((nuint)(2 * IntPtr.Size)); ThunkContextData *thunkData = (ThunkContextData *)ContextData; // allocate a weak GChandle for the delegate thunkData->Handle = GCHandle.Alloc(del, GCHandleType.Weak); // if it is an open static delegate get the function pointer thunkData->FunctionPtr = del.GetRawFunctionPointerForOpenStaticDelegate(); } } }
public static unsafe IntPtr AllocHGlobal(IntPtr cb) { return((nint)NativeMemory.Alloc((nuint)(nint)cb)); }