Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Transfer the ownership of method's thunk to caller(split from #958)#1003

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
filmor merged 8 commits intopythonnet:masterfromamos402:release-thunk
Dec 18, 2019
Merged
Show file tree
Hide file tree
Changes fromall 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
43 changes: 29 additions & 14 deletionssrc/runtime/interop.cs
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -4,6 +4,7 @@
using System.Runtime.InteropServices;
using System.Reflection;
using System.Text;
using System.Collections.Generic;

namespace Python.Runtime
{
Expand DownExpand Up@@ -334,7 +335,7 @@ internal class TypeFlags

internal class Interop
{
private staticArrayList keepAlive;
private staticList<ThunkInfo> keepAlive;
private static Hashtable pmap;

static Interop()
Expand All@@ -351,8 +352,7 @@ static Interop()
p[item.Name] = item;
}

keepAlive = new ArrayList();
Marshal.AllocHGlobal(IntPtr.Size);
keepAlive = new List<ThunkInfo>();
pmap = new Hashtable();

pmap["tp_dealloc"] = p["DestructorFunc"];
Expand DownExpand Up@@ -449,26 +449,23 @@ internal static Type GetPrototype(string name)
return pmap[name] as Type;
}

internal staticIntPtr GetThunk(MethodInfo method, string funcType = null)
internal staticThunkInfo GetThunk(MethodInfo method, string funcType = null)
{
Type dt;
if (funcType != null)
dt = typeof(Interop).GetNestedType(funcType) as Type;
else
dt = GetPrototype(method.Name);

if (dt!= null)
if (dt== null)
{
IntPtr tmp = Marshal.AllocHGlobal(IntPtr.Size);
Delegate d = Delegate.CreateDelegate(dt, method);
Thunk cb = new Thunk(d);
Marshal.StructureToPtr(cb, tmp, false);
IntPtr fp = Marshal.ReadIntPtr(tmp, 0);
Marshal.FreeHGlobal(tmp);
keepAlive.Add(d);
return fp;
return ThunkInfo.Empty;
}
return IntPtr.Zero;
Delegate d = Delegate.CreateDelegate(dt, method);
var info = new ThunkInfo(d);
// TODO: remove keepAlive when #958 merged, let the lifecycle of ThunkInfo transfer to caller.
keepAlive.Add(info);
return info;
}

[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
Expand DownExpand Up@@ -522,4 +519,22 @@ public Thunk(Delegate d)
fn = d;
}
}

internal class ThunkInfo
{
public readonly Delegate Target;
public readonly IntPtr Address;

public static readonly ThunkInfo Empty = new ThunkInfo(null);

public ThunkInfo(Delegate target)
{
if (target == null)
{
return;
}
Target = target;
Address = Marshal.GetFunctionPointerForDelegate(target);
}
}
}
6 changes: 4 additions & 2 deletionssrc/runtime/methodwrapper.cs
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -14,17 +14,19 @@ internal class MethodWrapper
public IntPtr ptr;
private bool _disposed = false;

private ThunkInfo _thunk;

public MethodWrapper(Type type, string name, string funcType = null)
{
// Turn the managed method into a function pointer

IntPtr fp = Interop.GetThunk(type.GetMethod(name), funcType);
_thunk = Interop.GetThunk(type.GetMethod(name), funcType);

// Allocate and initialize a PyMethodDef structure to represent
// the managed method, then create a PyCFunction.

mdef = Runtime.PyMem_Malloc(4 * IntPtr.Size);
TypeManager.WriteMethodDef(mdef, name,fp, 0x0003);
TypeManager.WriteMethodDef(mdef, name,_thunk.Address, 0x0003);
ptr = Runtime.PyCFunction_NewEx(mdef, IntPtr.Zero, IntPtr.Zero);
}

Expand Down
17 changes: 10 additions & 7 deletionssrc/runtime/typemanager.cs
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -204,8 +204,8 @@ internal static IntPtr CreateType(ManagedType impl, Type clrType)

static void InitializeSlot(IntPtr type, int slotOffset, MethodInfo method)
{
IntPtr thunk = Interop.GetThunk(method);
Marshal.WriteIntPtr(type, slotOffset, thunk);
var thunk = Interop.GetThunk(method);
Marshal.WriteIntPtr(type, slotOffset, thunk.Address);
}

internal static IntPtr CreateSubType(IntPtr py_name, IntPtr py_base_type, IntPtr py_dict)
Expand DownExpand Up@@ -344,16 +344,18 @@ internal static IntPtr CreateMetaType(Type impl)
// 4 int-ptrs in size.
IntPtr mdef = Runtime.PyMem_Malloc(3 * 4 * IntPtr.Size);
IntPtr mdefStart = mdef;
ThunkInfo thunkInfo = Interop.GetThunk(typeof(MetaType).GetMethod("__instancecheck__"), "BinaryFunc");
mdef = WriteMethodDef(
mdef,
"__instancecheck__",
Interop.GetThunk(typeof(MetaType).GetMethod("__instancecheck__"), "BinaryFunc")
thunkInfo.Address
);

thunkInfo = Interop.GetThunk(typeof(MetaType).GetMethod("__subclasscheck__"), "BinaryFunc");
mdef = WriteMethodDef(
mdef,
"__subclasscheck__",
Interop.GetThunk(typeof(MetaType).GetMethod("__subclasscheck__"), "BinaryFunc")
thunkInfo.Address
);

// FIXME: mdef is not used
Expand DownExpand Up@@ -710,7 +712,8 @@ internal static void InitializeSlots(IntPtr type, Type impl)
continue;
}

InitializeSlot(type, Interop.GetThunk(method), name);
var thunkInfo = Interop.GetThunk(method);
InitializeSlot(type, thunkInfo.Address, name);

seen.Add(name);
}
Expand All@@ -728,8 +731,8 @@ internal static void InitializeSlots(IntPtr type, Type impl)
// These have to be defined, though, so by default we fill these with
// static C# functions from this class.

var ret0 = Interop.GetThunk(((Func<IntPtr, int>)Return0).Method);
var ret1 = Interop.GetThunk(((Func<IntPtr, int>)Return1).Method);
var ret0 = Interop.GetThunk(((Func<IntPtr, int>)Return0).Method).Address;
var ret1 = Interop.GetThunk(((Func<IntPtr, int>)Return1).Method).Address;

if (native != null)
{
Expand Down

[8]ページ先頭

©2009-2025 Movatter.jp