// This method accepts a code pointer and allows the NativeDelegateRecord // associated with it to be collected. In order to call this method // safely it must be certain that no references to the code ptr exist // in native code. private static unsafe void ReleaseCodePtr(IntPtr codePtr) { byte * codeBuffer = (byte *)codePtr.ToPointer(); int idx = *(int *)(codeBuffer + 7); Delegate del = IdxToDelegate(idx); del.CodePtr = IntPtr.Zero; CriticalSection.AcquireMutex(); try { FreeNativeDelegateRecord(idx); } finally { CriticalSection.ReleaseMutex(); } FixedFree(codePtr); }
private static IntPtr GetCodePtr(Delegate d, IntPtr callBackStub) { IntPtr codePtr = d.CodePtr; if (codePtr == IntPtr.Zero) { CriticalSection.AcquireMutex(); try { codePtr = d.CodePtr; if (codePtr == IntPtr.Zero) { int idx = AllocateNativeDelegateRecord(d); codePtr = CreateCodePtr(idx, callBackStub); d.CodePtr = codePtr; } } finally { CriticalSection.ReleaseMutex(); } } return(codePtr); }