internal static bool TryFreeGCHandle(BorrowedReference reflectedClrObject, BorrowedReference type) { Debug.Assert(type != null); Debug.Assert(reflectedClrObject != null); Debug.Assert(IsManagedType(type) || IsManagedType(reflectedClrObject)); Debug.Assert(Runtime.PyObject_TypeCheck(reflectedClrObject, type)); int offset = Util.ReadInt32(type, Offsets.tp_clr_inst_offset); Debug.Assert(offset > 0); IntPtr raw = Util.ReadIntPtr(reflectedClrObject, offset); if (raw == IntPtr.Zero) { return(false); } var handle = (GCHandle)raw; handle.Free(); Util.WriteIntPtr(reflectedClrObject, offset, IntPtr.Zero); return(true); }
protected static void SetObjectDictNullable(BorrowedReference ob, StolenReference value) { BorrowedReference type = Runtime.PyObject_TYPE(ob); int instanceDictOffset = Util.ReadInt32(type, TypeOffset.tp_dictoffset); Debug.Assert(instanceDictOffset > 0); Runtime.ReplaceReference(ob, instanceDictOffset, value.AnalyzerWorkaround()); }
protected static BorrowedReference GetObjectDict(BorrowedReference ob) { BorrowedReference type = Runtime.PyObject_TYPE(ob); int instanceDictOffset = Util.ReadInt32(type, TypeOffset.tp_dictoffset); Debug.Assert(instanceDictOffset > 0); return(Util.ReadRef(ob, instanceDictOffset)); }
protected static void ClearObjectDict(BorrowedReference ob) { BorrowedReference type = Runtime.PyObject_TYPE(ob); int instanceDictOffset = Util.ReadInt32(type, TypeOffset.tp_dictoffset); Debug.Assert(instanceDictOffset > 0); Runtime.Py_CLEAR(ob, instanceDictOffset); }
static Offsets() { int pyTypeSize = Util.ReadInt32(Runtime.PyTypeType, TypeOffset.tp_basicsize); if (pyTypeSize < 0) { throw new InvalidOperationException(); } tp_clr_inst_offset = pyTypeSize; tp_clr_inst = tp_clr_inst_offset + IntPtr.Size; }
internal static void GetGCHandle(BorrowedReference reflectedClrObject, BorrowedReference type, out IntPtr handle) { Debug.Assert(reflectedClrObject != null); Debug.Assert(IsManagedType(type) || IsManagedType(reflectedClrObject)); Debug.Assert(Runtime.PyObject_TypeCheck(reflectedClrObject, type)); int gcHandleOffset = Util.ReadInt32(type, Offsets.tp_clr_inst_offset); Debug.Assert(gcHandleOffset > 0); handle = Util.ReadIntPtr(reflectedClrObject, gcHandleOffset); }
internal static void SetGCHandle(BorrowedReference reflectedClrObject, BorrowedReference type, GCHandle newHandle) { Debug.Assert(type != null); Debug.Assert(reflectedClrObject != null); Debug.Assert(IsManagedType(type) || IsManagedType(reflectedClrObject)); Debug.Assert(Runtime.PyObject_TypeCheck(reflectedClrObject, type)); int offset = Util.ReadInt32(type, Offsets.tp_clr_inst_offset); Debug.Assert(offset > 0); Util.WriteIntPtr(reflectedClrObject, offset, (IntPtr)newHandle); }