Make Marshal.SizeOf AOT friendly

See https://github.com/dotnet/corert/issues/5374#issuecomment-455706335
Current state in NativeAOT experiment is the same.
This commit is contained in:
Andrii Kurdiumov 2021-06-13 22:24:09 +06:00
parent f77efced1a
commit 238b98b631
8 changed files with 52 additions and 52 deletions

View File

@ -65,7 +65,7 @@ namespace Cairo {
// except in the case of Win64 where sizeof(long) // except in the case of Win64 where sizeof(long)
// is 32 bits // is 32 bits
// //
int ptr_size = Marshal.SizeOf (typeof (IntPtr)); int ptr_size = Marshal.SizeOf<IntPtr>();
PlatformID platform = Environment.OSVersion.Platform; PlatformID platform = Environment.OSVersion.Platform;
if (platform == PlatformID.Win32NT || if (platform == PlatformID.Win32NT ||

View File

@ -15,7 +15,7 @@ namespace GLib {
static public AbiStruct abi_info = new AbiStruct(new List<AbiField> { static public AbiStruct abi_info = new AbiStruct(new List<AbiField> {
new GLib.AbiField("seq_id" new GLib.AbiField("seq_id"
, 0 , 0
, (uint) Marshal.SizeOf(typeof(ulong)) // seq_id , (uint) Marshal.SizeOf<ulong>() // seq_id
, null , null
, "hook_size" , "hook_size"
, (long) Marshal.OffsetOf(typeof(GHookList_seq_idAlign), "seq_id") , (long) Marshal.OffsetOf(typeof(GHookList_seq_idAlign), "seq_id")
@ -23,7 +23,7 @@ namespace GLib {
), ),
new GLib.AbiField("hook_size" new GLib.AbiField("hook_size"
, -1 , -1
, (uint) Marshal.SizeOf(typeof(uint)) // hook_size , (uint) Marshal.SizeOf<uint>() // hook_size
, "seq_id" , "seq_id"
, "is_setup" , "is_setup"
, 1 , 1
@ -31,7 +31,7 @@ namespace GLib {
), ),
new GLib.AbiField("is_setup" new GLib.AbiField("is_setup"
, -1 , -1
, (uint) Marshal.SizeOf(typeof(bool)) // is_setup , (uint) Marshal.SizeOf<bool>() // is_setup
, "hook_size" , "hook_size"
, "hooks" , "hooks"
, 1 , 1
@ -39,7 +39,7 @@ namespace GLib {
), ),
new GLib.AbiField("hooks" new GLib.AbiField("hooks"
, -1 , -1
, (uint) Marshal.SizeOf(typeof(IntPtr)) // hooks , (uint) Marshal.SizeOf<IntPtr>() // hooks
, "is_setup" , "is_setup"
, "dummy3" , "dummy3"
, (long) Marshal.OffsetOf(typeof(GHookList_hooksAlign), "hooks") , (long) Marshal.OffsetOf(typeof(GHookList_hooksAlign), "hooks")
@ -47,7 +47,7 @@ namespace GLib {
), ),
new GLib.AbiField("dummy3" new GLib.AbiField("dummy3"
, -1 , -1
, (uint) Marshal.SizeOf(typeof(IntPtr)) // dummy3 , (uint) Marshal.SizeOf<IntPtr>() // dummy3
, "hooks" , "hooks"
, "finalize_hook" , "finalize_hook"
, (long) Marshal.OffsetOf(typeof(GHookList_dummy3Align), "dummy3") , (long) Marshal.OffsetOf(typeof(GHookList_dummy3Align), "dummy3")
@ -55,7 +55,7 @@ namespace GLib {
), ),
new GLib.AbiField("finalize_hook" new GLib.AbiField("finalize_hook"
, -1 , -1
, (uint) Marshal.SizeOf(typeof(IntPtr)) // finalize_hook , (uint) Marshal.SizeOf<IntPtr>() // finalize_hook
, "dummy3" , "dummy3"
, "dummy" , "dummy"
, (long) Marshal.OffsetOf(typeof(GHookList_finalize_hookAlign), "finalize_hook") , (long) Marshal.OffsetOf(typeof(GHookList_finalize_hookAlign), "finalize_hook")
@ -63,7 +63,7 @@ namespace GLib {
), ),
new GLib.AbiField("dummy" new GLib.AbiField("dummy"
, -1 , -1
, (uint) Marshal.SizeOf(typeof(IntPtr)) * 2 // dummy , (uint) Marshal.SizeOf<IntPtr>() * 2 // dummy
, "finalize_hook" , "finalize_hook"
, null , null
, (long) Marshal.OffsetOf(typeof(GHookList_dummyAlign), "dummy") , (long) Marshal.OffsetOf(typeof(GHookList_dummyAlign), "dummy")

View File

@ -364,7 +364,7 @@ namespace GLib {
public static IntPtr StructArrayToNullTerminatedStructArrayIntPtr<T> (T[] InputArray) public static IntPtr StructArrayToNullTerminatedStructArrayIntPtr<T> (T[] InputArray)
{ {
int intPtrSize = Marshal.SizeOf (typeof (IntPtr)); int intPtrSize = Marshal.SizeOf<IntPtr>();
IntPtr mem = Marshal.AllocHGlobal ((InputArray.Length + 1) * intPtrSize); IntPtr mem = Marshal.AllocHGlobal ((InputArray.Length + 1) * intPtrSize);
for (int i = 0; i < InputArray.Length; i++) { for (int i = 0; i < InputArray.Length; i++) {

View File

@ -939,7 +939,7 @@ namespace GLib {
static public AbiStruct abi_info = new AbiStruct(new List<AbiField> { static public AbiStruct abi_info = new AbiStruct(new List<AbiField> {
new GLib.AbiField("g_type_instance" new GLib.AbiField("g_type_instance"
, 0 , 0
, (uint) Marshal.SizeOf(typeof(IntPtr)) // g_type_instance , (uint) Marshal.SizeOf<IntPtr>() // g_type_instance
, null , null
, "ref_count" , "ref_count"
, (long) Marshal.OffsetOf(typeof(GObject_g_type_instanceAlign), "g_type_instance") , (long) Marshal.OffsetOf(typeof(GObject_g_type_instanceAlign), "g_type_instance")
@ -947,7 +947,7 @@ namespace GLib {
), ),
new GLib.AbiField("ref_count" new GLib.AbiField("ref_count"
, -1 , -1
, (uint) Marshal.SizeOf(typeof(uint)) // ref_count , (uint) Marshal.SizeOf<uint>() // ref_count
, "g_type_instance" , "g_type_instance"
, "qdata" , "qdata"
, (long) Marshal.OffsetOf(typeof(GObject_ref_countAlign), "ref_count") , (long) Marshal.OffsetOf(typeof(GObject_ref_countAlign), "ref_count")
@ -955,7 +955,7 @@ namespace GLib {
), ),
new GLib.AbiField("qdata" new GLib.AbiField("qdata"
, -1 , -1
, (uint) Marshal.SizeOf(typeof(IntPtr)) // qdata , (uint) Marshal.SizeOf<IntPtr>() // qdata
, "ref_count" , "ref_count"
, null , null
, (long) Marshal.OffsetOf(typeof(GObject_qdataAlign), "qdata") , (long) Marshal.OffsetOf(typeof(GObject_qdataAlign), "qdata")
@ -968,146 +968,146 @@ namespace GLib {
static public AbiStruct class_abi = new AbiStruct(new List<AbiField> { static public AbiStruct class_abi = new AbiStruct(new List<AbiField> {
new GLib.AbiField("type_class" new GLib.AbiField("type_class"
, 0 , 0
, (uint) Marshal.SizeOf(typeof(IntPtr)) // g_type_instance , (uint) Marshal.SizeOf<IntPtr>() // g_type_instance
, null , null
, "construct_props" , "construct_props"
, (uint) Marshal.SizeOf(typeof(IntPtr)) // g_type_instance , (uint) Marshal.SizeOf<IntPtr>() // g_type_instance
, 0 , 0
), ),
new GLib.AbiField("construct_props" new GLib.AbiField("construct_props"
, 0 , 0
, (uint) Marshal.SizeOf(typeof(IntPtr)) // g_type_instance , (uint) Marshal.SizeOf<IntPtr>() // g_type_instance
, "type_class" , "type_class"
, "constructor_cb" , "constructor_cb"
, (uint) Marshal.SizeOf(typeof(IntPtr)) // g_type_instance , (uint) Marshal.SizeOf<IntPtr>() // g_type_instance
, 0 , 0
), ),
new GLib.AbiField("constructor_cb" new GLib.AbiField("constructor_cb"
, 0 , 0
, (uint) Marshal.SizeOf(typeof(IntPtr)) // g_type_instance , (uint) Marshal.SizeOf<IntPtr>() // g_type_instance
, "construct_props" , "construct_props"
, "set_prop_cb" , "set_prop_cb"
, (uint) Marshal.SizeOf(typeof(IntPtr)) // g_type_instance , (uint) Marshal.SizeOf<IntPtr>() // g_type_instance
, 0 , 0
), ),
new GLib.AbiField("set_prop_cb" new GLib.AbiField("set_prop_cb"
, 0 , 0
, (uint) Marshal.SizeOf(typeof(IntPtr)) // g_type_instance , (uint) Marshal.SizeOf<IntPtr>() // g_type_instance
, "constructor_cb" , "constructor_cb"
, "get_prop_cb" , "get_prop_cb"
, (uint) Marshal.SizeOf(typeof(IntPtr)) // g_type_instance , (uint) Marshal.SizeOf<IntPtr>() // g_type_instance
, 0 , 0
), ),
new GLib.AbiField("get_prop_cb" new GLib.AbiField("get_prop_cb"
, 0 , 0
, (uint) Marshal.SizeOf(typeof(IntPtr)) // g_type_instance , (uint) Marshal.SizeOf<IntPtr>() // g_type_instance
, "set_prop_cb" , "set_prop_cb"
, "dispose" , "dispose"
, (uint) Marshal.SizeOf(typeof(IntPtr)) // g_type_instance , (uint) Marshal.SizeOf<IntPtr>() // g_type_instance
, 0 , 0
), ),
new GLib.AbiField("dispose" new GLib.AbiField("dispose"
, 0 , 0
, (uint) Marshal.SizeOf(typeof(IntPtr)) // g_type_instance , (uint) Marshal.SizeOf<IntPtr>() // g_type_instance
, "get_prop_cb" , "get_prop_cb"
, "finalize" , "finalize"
, (uint) Marshal.SizeOf(typeof(IntPtr)) // g_type_instance , (uint) Marshal.SizeOf<IntPtr>() // g_type_instance
, 0 , 0
), ),
new GLib.AbiField("finalize" new GLib.AbiField("finalize"
, 0 , 0
, (uint) Marshal.SizeOf(typeof(IntPtr)) // g_type_instance , (uint) Marshal.SizeOf<IntPtr>() // g_type_instance
, "dispose" , "dispose"
, "dispatch_properties_changed" , "dispatch_properties_changed"
, (uint) Marshal.SizeOf(typeof(IntPtr)) // g_type_instance , (uint) Marshal.SizeOf<IntPtr>() // g_type_instance
, 0 , 0
), ),
new GLib.AbiField("dispatch_properties_changed" new GLib.AbiField("dispatch_properties_changed"
, 0 , 0
, (uint) Marshal.SizeOf(typeof(IntPtr)) // g_type_instance , (uint) Marshal.SizeOf<IntPtr>() // g_type_instance
, "finalize" , "finalize"
, "notify" , "notify"
, (uint) Marshal.SizeOf(typeof(IntPtr)) // g_type_instance , (uint) Marshal.SizeOf<IntPtr>() // g_type_instance
, 0 , 0
), ),
new GLib.AbiField("notify" new GLib.AbiField("notify"
, 0 , 0
, (uint) Marshal.SizeOf(typeof(IntPtr)) // g_type_instance , (uint) Marshal.SizeOf<IntPtr>() // g_type_instance
, "dispatch_properties_changed" , "dispatch_properties_changed"
, "constructed" , "constructed"
, (uint) Marshal.SizeOf(typeof(IntPtr)) // g_type_instance , (uint) Marshal.SizeOf<IntPtr>() // g_type_instance
, 0 , 0
), ),
new GLib.AbiField("constructed" new GLib.AbiField("constructed"
, 0 , 0
, (uint) Marshal.SizeOf(typeof(IntPtr)) // g_type_instance , (uint) Marshal.SizeOf<IntPtr>() // g_type_instance
, "notify" , "notify"
, "dummy1" , "dummy1"
, (uint) Marshal.SizeOf(typeof(IntPtr)) // g_type_instance , (uint) Marshal.SizeOf<IntPtr>() // g_type_instance
, 0 , 0
), ),
new GLib.AbiField("dummy1" new GLib.AbiField("dummy1"
, 0 , 0
, (uint) Marshal.SizeOf(typeof(IntPtr)) // g_type_instance , (uint) Marshal.SizeOf<IntPtr>() // g_type_instance
, "constructed" , "constructed"
, "dummy2" , "dummy2"
, (uint) Marshal.SizeOf(typeof(IntPtr)) // g_type_instance , (uint) Marshal.SizeOf<IntPtr>() // g_type_instance
, 0 , 0
), ),
new GLib.AbiField("dummy2" new GLib.AbiField("dummy2"
, 0 , 0
, (uint) Marshal.SizeOf(typeof(IntPtr)) // g_type_instance , (uint) Marshal.SizeOf<IntPtr>() // g_type_instance
, "dummy1" , "dummy1"
, "dummy3" , "dummy3"
, (uint) Marshal.SizeOf(typeof(IntPtr)) // g_type_instance , (uint) Marshal.SizeOf<IntPtr>() // g_type_instance
, 0 , 0
), ),
new GLib.AbiField("dummy3" new GLib.AbiField("dummy3"
, 0 , 0
, (uint) Marshal.SizeOf(typeof(IntPtr)) // g_type_instance , (uint) Marshal.SizeOf<IntPtr>() // g_type_instance
, "dummy2" , "dummy2"
, "dummy4" , "dummy4"
, (uint) Marshal.SizeOf(typeof(IntPtr)) // g_type_instance , (uint) Marshal.SizeOf<IntPtr>() // g_type_instance
, 0 , 0
), ),
new GLib.AbiField("dummy3" new GLib.AbiField("dummy3"
, 0 , 0
, (uint) Marshal.SizeOf(typeof(IntPtr)) // g_type_instance , (uint) Marshal.SizeOf<IntPtr>() // g_type_instance
, "dummy2" , "dummy2"
, "dummy4" , "dummy4"
, (uint) Marshal.SizeOf(typeof(IntPtr)) // g_type_instance , (uint) Marshal.SizeOf<IntPtr>() // g_type_instance
, 0 , 0
), ),
new GLib.AbiField("dummy4" new GLib.AbiField("dummy4"
, 0 , 0
, (uint) Marshal.SizeOf(typeof(IntPtr)) // g_type_instance , (uint) Marshal.SizeOf<IntPtr>() // g_type_instance
, "dummy3" , "dummy3"
, "dummy5" , "dummy5"
, (uint) Marshal.SizeOf(typeof(IntPtr)) // g_type_instance , (uint) Marshal.SizeOf<IntPtr>() // g_type_instance
, 0 , 0
), ),
new GLib.AbiField("dummy5" new GLib.AbiField("dummy5"
, 0 , 0
, (uint) Marshal.SizeOf(typeof(IntPtr)) // g_type_instance , (uint) Marshal.SizeOf<IntPtr>() // g_type_instance
, "dummy4" , "dummy4"
, "dummy6" , "dummy6"
, (uint) Marshal.SizeOf(typeof(IntPtr)) // g_type_instance , (uint) Marshal.SizeOf<IntPtr>() // g_type_instance
, 0 , 0
), ),
new GLib.AbiField("dummy6" new GLib.AbiField("dummy6"
, 0 , 0
, (uint) Marshal.SizeOf(typeof(IntPtr)) // g_type_instance , (uint) Marshal.SizeOf<IntPtr>() // g_type_instance
, "dummy5" , "dummy5"
, "dummy7" , "dummy7"
, (uint) Marshal.SizeOf(typeof(IntPtr)) // g_type_instance , (uint) Marshal.SizeOf<IntPtr>() // g_type_instance
, 0 , 0
), ),
new GLib.AbiField("dummy7" new GLib.AbiField("dummy7"
, 0 , 0
, (uint) Marshal.SizeOf(typeof(IntPtr)) // g_type_instance , (uint) Marshal.SizeOf<IntPtr>() // g_type_instance
, "dummy6" , "dummy6"
, null , null
, (uint) Marshal.SizeOf(typeof(IntPtr)) // g_type_instance , (uint) Marshal.SizeOf<IntPtr>() // g_type_instance
, 0 , 0
), ),
} }

View File

@ -68,7 +68,7 @@ namespace GtkSharp.Generation {
} }
public override string GenerateGetSizeOf () { public override string GenerateGetSizeOf () {
return "(uint) Marshal.SizeOf(typeof(" + abi_type + "))"; return "(uint) Marshal.SizeOf<" + abi_type + ">()";
} }
} }
} }

View File

@ -104,7 +104,7 @@ namespace GtkSharp.Generation {
// Do not generate structs if the type is a simple pointer. // Do not generate structs if the type is a simple pointer.
if (IsCPointer()) if (IsCPointer())
min_align = "(uint) Marshal.SizeOf(typeof(IntPtr))"; min_align = "(uint) Marshal.SizeOf<IntPtr>()";
if (IsBitfield) if (IsBitfield)
min_align = "1"; min_align = "1";

View File

@ -144,7 +144,7 @@ namespace GtkSharp.Generation {
if (_enum != null && !is_pointer) if (_enum != null && !is_pointer)
res = "(uint) Marshal.SizeOf(System.Enum.GetUnderlyingType(typeof(" + cstype + ")))"; res = "(uint) Marshal.SizeOf(System.Enum.GetUnderlyingType(typeof(" + cstype + ")))";
else else
res = "(uint) Marshal.SizeOf(typeof(" + cstype + "))"; res = "(uint) Marshal.SizeOf<" + cstype + ">()";
if (IsFixedSizeArray()) if (IsFixedSizeArray())
res += " * " + ArrayLength; res += " * " + ArrayLength;

View File

@ -150,7 +150,7 @@ namespace GtkSharp.Generation {
AddType (new MarshalGen ("GType", "GLib.GType", "IntPtr", "{0}.Val", "new GLib.GType({0})", "GLib.GType.None")); AddType (new MarshalGen ("GType", "GLib.GType", "IntPtr", "{0}.Val", "new GLib.GType({0})", "GLib.GType.None"));
AddType (new ByRefGen ("GValue", "GLib.Value")); AddType (new ByRefGen ("GValue", "GLib.Value"));
AddType (new SimpleGen ("GDestroyNotify", "GLib.DestroyNotify", "null", AddType (new SimpleGen ("GDestroyNotify", "GLib.DestroyNotify", "null",
"(uint) Marshal.SizeOf(typeof(IntPtr))")); "(uint) Marshal.SizeOf<IntPtr>()"));
AddType (new SimpleGen ("GThread", "GLib.Thread", "null")); AddType (new SimpleGen ("GThread", "GLib.Thread", "null"));
AddType (new ManualGen ("GBytes", "GLib.Bytes")); AddType (new ManualGen ("GBytes", "GLib.Bytes"));
AddType (new SimpleGen ("GHookList", "GLib.HookList", "null", AddType (new SimpleGen ("GHookList", "GLib.HookList", "null",