Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/runtime/interop.cs
Original file line number Diff line number Diff line change
Expand Up @@ -491,6 +491,9 @@ internal static Type GetPrototype(string name)
return pmap[name] as Type;
}


internal static Dictionary<IntPtr, Delegate> allocatedThunks = new Dictionary<IntPtr, Delegate>();

internal static ThunkInfo GetThunk(MethodInfo method, string funcType = null)
{
Type dt;
Expand All @@ -505,6 +508,7 @@ internal static ThunkInfo GetThunk(MethodInfo method, string funcType = null)
}
Delegate d = Delegate.CreateDelegate(dt, method);
var info = new ThunkInfo(d);
allocatedThunks[info.Address] = d;
return info;
}

Expand Down
7 changes: 4 additions & 3 deletions src/runtime/managedtype.cs
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ internal static int PyVisit(IntPtr ob, IntPtr visit, IntPtr arg)
{
return 0;
}
var visitFunc = (Interop.ObjObjFunc)Marshal.GetDelegateForFunctionPointer(visit, typeof(Interop.ObjObjFunc));
var visitFunc = NativeCall.GetDelegate<Interop.ObjObjFunc>(visit);
return visitFunc(ob, arg);
}

Expand All @@ -196,7 +196,7 @@ internal void CallTypeClear()
{
return;
}
var clearFunc = (Interop.InquiryFunc)Marshal.GetDelegateForFunctionPointer(clearPtr, typeof(Interop.InquiryFunc));
var clearFunc = NativeCall.GetDelegate<Interop.InquiryFunc>(clearPtr);
clearFunc(pyHandle);
}

Expand All @@ -214,7 +214,8 @@ internal void CallTypeTraverse(Interop.ObjObjFunc visitproc, IntPtr arg)
{
return;
}
var traverseFunc = (Interop.ObjObjArgFunc)Marshal.GetDelegateForFunctionPointer(traversePtr, typeof(Interop.ObjObjArgFunc));
var traverseFunc = NativeCall.GetDelegate<Interop.ObjObjArgFunc>(traversePtr);

var visiPtr = Marshal.GetFunctionPointerForDelegate(visitproc);
traverseFunc(pyHandle, visiPtr, arg);
}
Expand Down
11 changes: 8 additions & 3 deletions src/runtime/nativecall.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,15 @@ public static int Int_Call_3(IntPtr fp, IntPtr a1, IntPtr a2, IntPtr a3)
return d(a1, a2, a3);
}

private static T GetDelegate<T>(IntPtr fp) where T: Delegate
internal static T GetDelegate<T>(IntPtr fp) where T: Delegate
{
// Use Marshal.GetDelegateForFunctionPointer<> directly after upgrade the framework
return (T)Marshal.GetDelegateForFunctionPointer(fp, typeof(T));
Delegate d = null;
if (!Interop.allocatedThunks.TryGetValue(fp, out d))
Comment on lines +45 to +46
Copy link
Member

@lostmsu lostmsu Dec 18, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NIT: you don't need to assign an initial value to d. You could also do TryGetValue(fp, out var d).

{
// We don't cache this delegate because this is a pure delegate ot unmanaged.
d = Marshal.GetDelegateForFunctionPointer<T>(fp);
}
return (T)d;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You'd save a couple instructions with:

Delegate d; // no init
if (trygetvalue(out d)) {
  return (T)d;
} else {
  return Marshal.GetDelegate...(); // no cast
}

}
}
}