Skip to content
This repository was archived by the owner on Jul 22, 2023. It is now read-only.

Commit 498fc8c

Browse files
committed
Manually merge - ManagedDataOffsets
1 parent cb65af3 commit 498fc8c

File tree

3 files changed

+34
-18
lines changed

3 files changed

+34
-18
lines changed

src/runtime/interop.cs

Lines changed: 29 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
using System.Runtime.InteropServices;
66
using System.Reflection;
77
using System.Text;
8-
using System.Collections.Generic;
98
using System.Linq;
109

1110
namespace Python.Runtime
@@ -70,15 +69,20 @@ public ModulePropertyAttribute()
7069
}
7170
}
7271

73-
internal static partial class ManagedDataOffsets
72+
internal static partial class TypeOffset
7473
{
75-
static class Helper
76-
{
77-
public static int magic;
78-
public static readonly Dictionary<string, int> NameMapping = new Dictionary<string, int>();
79-
}
74+
public static int magic() => ManagedDataOffsets.Magic;
75+
}
8076

81-
static TypeOffset()
77+
internal static class ManagedDataOffsets
78+
{
79+
public static int Magic { get; private set; }
80+
public static readonly Dictionary<string, int> NameMapping = new Dictionary<string, int>();
81+
82+
public static readonly int ob_data;
83+
public static readonly int ob_dict;
84+
85+
static ManagedDataOffsets()
8286
{
8387
Type type = typeof(TypeOffset);
8488
FieldInfo[] fields = type.GetFields();
@@ -88,18 +92,30 @@ static TypeOffset()
8892
int offset = i * size;
8993
FieldInfo fi = fields[i];
9094
fi.SetValue(null, offset);
91-
Helper.NameMapping[fi.Name] = offset;
95+
NameMapping[fi.Name] = offset;
9296
}
9397
// XXX: Use the members after PyHeapTypeObject as magic slot
94-
Helper.magic = members;
98+
Magic = TypeOffset.members;
9599
}
96100

97-
public static int magic() => Helper.magic;
98-
99101
public static int GetSlotOffset(string name)
100102
{
101-
return Helper.NameMapping[name];
103+
return NameMapping[name];
102104
}
105+
106+
private static int BaseOffset(IntPtr type)
107+
{
108+
Debug.Assert(type != IntPtr.Zero);
109+
int typeSize = Marshal.ReadInt32(type, TypeOffset.tp_basicsize);
110+
Debug.Assert(typeSize > 0 && typeSize <= ExceptionOffset.Size());
111+
return typeSize;
112+
}
113+
114+
public static int DataOffset(IntPtr type)
115+
{
116+
return BaseOffset(type) + ob_data;
117+
}
118+
103119
public static int DictOffset(IntPtr type)
104120
{
105121
return BaseOffset(type) + ob_dict;

src/runtime/managedtype.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -226,12 +226,12 @@ protected static void ClearObjectDict(IntPtr ob)
226226

227227
protected static IntPtr GetObjectDict(IntPtr ob)
228228
{
229-
return Marshal.ReadIntPtr(ob, ObjectOffset.DictOffset(ob));
229+
return Marshal.ReadIntPtr(ob, ObjectOffset.TypeDictOffset(ob));
230230
}
231231

232232
protected static void SetObjectDict(IntPtr ob, IntPtr value)
233233
{
234-
Marshal.WriteIntPtr(ob, ObjectOffset.DictOffset(ob), value);
234+
Marshal.WriteIntPtr(ob, ObjectOffset.TypeDictOffset(ob), value);
235235
}
236236
}
237237
}

src/runtime/typemanager.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -638,7 +638,7 @@ internal static void InitializeSlots(IntPtr type, Type impl, SlotsHolder slotsHo
638638
{
639639
continue;
640640
}
641-
var offset = TypeOffset.GetSlotOffset(slot);
641+
var offset = ManagedDataOffsets.GetSlotOffset(slot);
642642
Marshal.WriteIntPtr(type, offset, SlotsHolder.GetDefaultSlot(offset));
643643
}
644644
}
@@ -656,7 +656,7 @@ internal static void InitializeSlots(IntPtr type, Type impl, SlotsHolder slotsHo
656656
/// <param name="canOverride">Can override the slot when it existed</param>
657657
static void InitializeSlot(IntPtr type, IntPtr slot, string name, bool canOverride = true)
658658
{
659-
var offset = TypeOffset.GetSlotOffset(name);
659+
var offset = ManagedDataOffsets.GetSlotOffset(name);
660660
if (!canOverride && Marshal.ReadIntPtr(type, offset) != IntPtr.Zero)
661661
{
662662
return;
@@ -693,7 +693,7 @@ static void InitializeSlot(IntPtr type, int slotOffset, MethodInfo method, Slots
693693

694694
static bool IsSlotSet(IntPtr type, string name)
695695
{
696-
int offset = TypeOffset.GetSlotOffset(name);
696+
int offset = ManagedDataOffsets.GetSlotOffset(name);
697697
return Marshal.ReadIntPtr(type, offset) != IntPtr.Zero;
698698
}
699699

0 commit comments

Comments
 (0)