Skip to content
Merged
Changes from 1 commit
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
Prev Previous commit
Next Next commit
allocate space for GCHandle in instances of CLR Metatype (which are t…
…ypes themselves)
  • Loading branch information
lostmsu committed Dec 18, 2020
commit 9682dc6e3b3f9231d7a801ae3202a65ed68fbcca
17 changes: 10 additions & 7 deletions src/runtime/typemanager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ internal static IntPtr GetTypeHandle(ManagedType obj, Type type)
/// </summary>
internal static IntPtr CreateType(Type impl)
{
IntPtr type = AllocateTypeObject(impl.Name);
IntPtr type = AllocateTypeObject(impl.Name, metatype: Runtime.PyTypeType);
int ob_size = ObjectOffset.Size(type);

// Set tp_basicsize to the size of our managed instance objects.
Expand Down Expand Up @@ -212,7 +212,7 @@ internal static IntPtr CreateType(ManagedType impl, Type clrType)
base_ = bc.pyHandle;
}

IntPtr type = AllocateTypeObject(name);
IntPtr type = AllocateTypeObject(name, Runtime.PyCLRMetaType);

Marshal.WriteIntPtr(type, TypeOffset.ob_type, Runtime.PyCLRMetaType);
Runtime.XIncref(Runtime.PyCLRMetaType);
Expand Down Expand Up @@ -427,12 +427,15 @@ internal static IntPtr CreateMetaType(Type impl, out SlotsHolder slotsHolder)
// the standard type slots, and has to subclass PyType_Type for
// certain functions in the C runtime to work correctly with it.

IntPtr type = AllocateTypeObject("CLR Metatype");
IntPtr py_type = Runtime.PyTypeType;
IntPtr type = AllocateTypeObject("CLR Metatype", metatype: Runtime.PyTypeType);

IntPtr py_type = Runtime.PyTypeType;
Marshal.WriteIntPtr(type, TypeOffset.tp_base, py_type);
Runtime.XIncref(py_type);

int size = TypeOffset.magic() + IntPtr.Size;
Marshal.WriteIntPtr(type, TypeOffset.tp_basicsize, new IntPtr(size));

const int flags = TypeFlags.Default
| TypeFlags.Managed
| TypeFlags.HeapType
Expand Down Expand Up @@ -522,7 +525,7 @@ internal static IntPtr BasicSubType(string name, IntPtr base_, Type impl)
// Utility to create a subtype of a std Python type, but with
// a managed type able to override implementation

IntPtr type = AllocateTypeObject(name);
IntPtr type = AllocateTypeObject(name, metatype: Runtime.PyTypeType);
//Marshal.WriteIntPtr(type, TypeOffset.tp_basicsize, (IntPtr)obSize);
//Marshal.WriteIntPtr(type, TypeOffset.tp_itemsize, IntPtr.Zero);

Expand Down Expand Up @@ -564,9 +567,9 @@ internal static IntPtr BasicSubType(string name, IntPtr base_, Type impl)
/// <summary>
/// Utility method to allocate a type object &amp; do basic initialization.
/// </summary>
internal static IntPtr AllocateTypeObject(string name)
internal static IntPtr AllocateTypeObject(string name, IntPtr metatype)
{
IntPtr type = Runtime.PyType_GenericAlloc(Runtime.PyTypeType, 0);
IntPtr type = Runtime.PyType_GenericAlloc(metatype, 0);
// Clr type would not use __slots__,
// and the PyMemberDef after PyHeapTypeObject will have other uses(e.g. type handle),
// thus set the ob_size to 0 for avoiding slots iterations.
Expand Down