From 238b98b63177d4b92d79690aa70c6b592791acf7 Mon Sep 17 00:00:00 2001 From: Andrii Kurdiumov Date: Sun, 13 Jun 2021 22:24:09 +0600 Subject: [PATCH 1/4] Make Marshal.SizeOf AOT friendly See https://github.com/dotnet/corert/issues/5374#issuecomment-455706335 Current state in NativeAOT experiment is the same. --- Source/Libs/CairoSharp/Context.cs | 2 +- Source/Libs/GLibSharp/HookList.cs | 14 ++-- Source/Libs/GLibSharp/Marshaller.cs | 2 +- Source/Libs/GLibSharp/Object.cs | 78 +++++++++++----------- Source/Tools/GapiCodegen/ManualGen.cs | 2 +- Source/Tools/GapiCodegen/StructABIField.cs | 2 +- Source/Tools/GapiCodegen/StructField.cs | 2 +- Source/Tools/GapiCodegen/SymbolTable.cs | 2 +- 8 files changed, 52 insertions(+), 52 deletions(-) diff --git a/Source/Libs/CairoSharp/Context.cs b/Source/Libs/CairoSharp/Context.cs index 09de92921..319b8e530 100644 --- a/Source/Libs/CairoSharp/Context.cs +++ b/Source/Libs/CairoSharp/Context.cs @@ -65,7 +65,7 @@ namespace Cairo { // except in the case of Win64 where sizeof(long) // is 32 bits // - int ptr_size = Marshal.SizeOf (typeof (IntPtr)); + int ptr_size = Marshal.SizeOf(); PlatformID platform = Environment.OSVersion.Platform; if (platform == PlatformID.Win32NT || diff --git a/Source/Libs/GLibSharp/HookList.cs b/Source/Libs/GLibSharp/HookList.cs index 34df11c1f..803d0d78e 100644 --- a/Source/Libs/GLibSharp/HookList.cs +++ b/Source/Libs/GLibSharp/HookList.cs @@ -15,7 +15,7 @@ namespace GLib { static public AbiStruct abi_info = new AbiStruct(new List { new GLib.AbiField("seq_id" , 0 - , (uint) Marshal.SizeOf(typeof(ulong)) // seq_id + , (uint) Marshal.SizeOf() // seq_id , null , "hook_size" , (long) Marshal.OffsetOf(typeof(GHookList_seq_idAlign), "seq_id") @@ -23,7 +23,7 @@ namespace GLib { ), new GLib.AbiField("hook_size" , -1 - , (uint) Marshal.SizeOf(typeof(uint)) // hook_size + , (uint) Marshal.SizeOf() // hook_size , "seq_id" , "is_setup" , 1 @@ -31,7 +31,7 @@ namespace GLib { ), new GLib.AbiField("is_setup" , -1 - , (uint) Marshal.SizeOf(typeof(bool)) // is_setup + , (uint) Marshal.SizeOf() // is_setup , "hook_size" , "hooks" , 1 @@ -39,7 +39,7 @@ namespace GLib { ), new GLib.AbiField("hooks" , -1 - , (uint) Marshal.SizeOf(typeof(IntPtr)) // hooks + , (uint) Marshal.SizeOf() // hooks , "is_setup" , "dummy3" , (long) Marshal.OffsetOf(typeof(GHookList_hooksAlign), "hooks") @@ -47,7 +47,7 @@ namespace GLib { ), new GLib.AbiField("dummy3" , -1 - , (uint) Marshal.SizeOf(typeof(IntPtr)) // dummy3 + , (uint) Marshal.SizeOf() // dummy3 , "hooks" , "finalize_hook" , (long) Marshal.OffsetOf(typeof(GHookList_dummy3Align), "dummy3") @@ -55,7 +55,7 @@ namespace GLib { ), new GLib.AbiField("finalize_hook" , -1 - , (uint) Marshal.SizeOf(typeof(IntPtr)) // finalize_hook + , (uint) Marshal.SizeOf() // finalize_hook , "dummy3" , "dummy" , (long) Marshal.OffsetOf(typeof(GHookList_finalize_hookAlign), "finalize_hook") @@ -63,7 +63,7 @@ namespace GLib { ), new GLib.AbiField("dummy" , -1 - , (uint) Marshal.SizeOf(typeof(IntPtr)) * 2 // dummy + , (uint) Marshal.SizeOf() * 2 // dummy , "finalize_hook" , null , (long) Marshal.OffsetOf(typeof(GHookList_dummyAlign), "dummy") diff --git a/Source/Libs/GLibSharp/Marshaller.cs b/Source/Libs/GLibSharp/Marshaller.cs index def1d1f93..55683ffb2 100644 --- a/Source/Libs/GLibSharp/Marshaller.cs +++ b/Source/Libs/GLibSharp/Marshaller.cs @@ -364,7 +364,7 @@ namespace GLib { public static IntPtr StructArrayToNullTerminatedStructArrayIntPtr (T[] InputArray) { - int intPtrSize = Marshal.SizeOf (typeof (IntPtr)); + int intPtrSize = Marshal.SizeOf(); IntPtr mem = Marshal.AllocHGlobal ((InputArray.Length + 1) * intPtrSize); for (int i = 0; i < InputArray.Length; i++) { diff --git a/Source/Libs/GLibSharp/Object.cs b/Source/Libs/GLibSharp/Object.cs index 833b8e513..0062fd4db 100644 --- a/Source/Libs/GLibSharp/Object.cs +++ b/Source/Libs/GLibSharp/Object.cs @@ -939,7 +939,7 @@ namespace GLib { static public AbiStruct abi_info = new AbiStruct(new List { new GLib.AbiField("g_type_instance" , 0 - , (uint) Marshal.SizeOf(typeof(IntPtr)) // g_type_instance + , (uint) Marshal.SizeOf() // g_type_instance , null , "ref_count" , (long) Marshal.OffsetOf(typeof(GObject_g_type_instanceAlign), "g_type_instance") @@ -947,7 +947,7 @@ namespace GLib { ), new GLib.AbiField("ref_count" , -1 - , (uint) Marshal.SizeOf(typeof(uint)) // ref_count + , (uint) Marshal.SizeOf() // ref_count , "g_type_instance" , "qdata" , (long) Marshal.OffsetOf(typeof(GObject_ref_countAlign), "ref_count") @@ -955,7 +955,7 @@ namespace GLib { ), new GLib.AbiField("qdata" , -1 - , (uint) Marshal.SizeOf(typeof(IntPtr)) // qdata + , (uint) Marshal.SizeOf() // qdata , "ref_count" , null , (long) Marshal.OffsetOf(typeof(GObject_qdataAlign), "qdata") @@ -968,146 +968,146 @@ namespace GLib { static public AbiStruct class_abi = new AbiStruct(new List { new GLib.AbiField("type_class" , 0 - , (uint) Marshal.SizeOf(typeof(IntPtr)) // g_type_instance + , (uint) Marshal.SizeOf() // g_type_instance , null , "construct_props" - , (uint) Marshal.SizeOf(typeof(IntPtr)) // g_type_instance + , (uint) Marshal.SizeOf() // g_type_instance , 0 ), new GLib.AbiField("construct_props" , 0 - , (uint) Marshal.SizeOf(typeof(IntPtr)) // g_type_instance + , (uint) Marshal.SizeOf() // g_type_instance , "type_class" , "constructor_cb" - , (uint) Marshal.SizeOf(typeof(IntPtr)) // g_type_instance + , (uint) Marshal.SizeOf() // g_type_instance , 0 ), new GLib.AbiField("constructor_cb" , 0 - , (uint) Marshal.SizeOf(typeof(IntPtr)) // g_type_instance + , (uint) Marshal.SizeOf() // g_type_instance , "construct_props" , "set_prop_cb" - , (uint) Marshal.SizeOf(typeof(IntPtr)) // g_type_instance + , (uint) Marshal.SizeOf() // g_type_instance , 0 ), new GLib.AbiField("set_prop_cb" , 0 - , (uint) Marshal.SizeOf(typeof(IntPtr)) // g_type_instance + , (uint) Marshal.SizeOf() // g_type_instance , "constructor_cb" , "get_prop_cb" - , (uint) Marshal.SizeOf(typeof(IntPtr)) // g_type_instance + , (uint) Marshal.SizeOf() // g_type_instance , 0 ), new GLib.AbiField("get_prop_cb" , 0 - , (uint) Marshal.SizeOf(typeof(IntPtr)) // g_type_instance + , (uint) Marshal.SizeOf() // g_type_instance , "set_prop_cb" , "dispose" - , (uint) Marshal.SizeOf(typeof(IntPtr)) // g_type_instance + , (uint) Marshal.SizeOf() // g_type_instance , 0 ), new GLib.AbiField("dispose" , 0 - , (uint) Marshal.SizeOf(typeof(IntPtr)) // g_type_instance + , (uint) Marshal.SizeOf() // g_type_instance , "get_prop_cb" , "finalize" - , (uint) Marshal.SizeOf(typeof(IntPtr)) // g_type_instance + , (uint) Marshal.SizeOf() // g_type_instance , 0 ), new GLib.AbiField("finalize" , 0 - , (uint) Marshal.SizeOf(typeof(IntPtr)) // g_type_instance + , (uint) Marshal.SizeOf() // g_type_instance , "dispose" , "dispatch_properties_changed" - , (uint) Marshal.SizeOf(typeof(IntPtr)) // g_type_instance + , (uint) Marshal.SizeOf() // g_type_instance , 0 ), new GLib.AbiField("dispatch_properties_changed" , 0 - , (uint) Marshal.SizeOf(typeof(IntPtr)) // g_type_instance + , (uint) Marshal.SizeOf() // g_type_instance , "finalize" , "notify" - , (uint) Marshal.SizeOf(typeof(IntPtr)) // g_type_instance + , (uint) Marshal.SizeOf() // g_type_instance , 0 ), new GLib.AbiField("notify" , 0 - , (uint) Marshal.SizeOf(typeof(IntPtr)) // g_type_instance + , (uint) Marshal.SizeOf() // g_type_instance , "dispatch_properties_changed" , "constructed" - , (uint) Marshal.SizeOf(typeof(IntPtr)) // g_type_instance + , (uint) Marshal.SizeOf() // g_type_instance , 0 ), new GLib.AbiField("constructed" , 0 - , (uint) Marshal.SizeOf(typeof(IntPtr)) // g_type_instance + , (uint) Marshal.SizeOf() // g_type_instance , "notify" , "dummy1" - , (uint) Marshal.SizeOf(typeof(IntPtr)) // g_type_instance + , (uint) Marshal.SizeOf() // g_type_instance , 0 ), new GLib.AbiField("dummy1" , 0 - , (uint) Marshal.SizeOf(typeof(IntPtr)) // g_type_instance + , (uint) Marshal.SizeOf() // g_type_instance , "constructed" , "dummy2" - , (uint) Marshal.SizeOf(typeof(IntPtr)) // g_type_instance + , (uint) Marshal.SizeOf() // g_type_instance , 0 ), new GLib.AbiField("dummy2" , 0 - , (uint) Marshal.SizeOf(typeof(IntPtr)) // g_type_instance + , (uint) Marshal.SizeOf() // g_type_instance , "dummy1" , "dummy3" - , (uint) Marshal.SizeOf(typeof(IntPtr)) // g_type_instance + , (uint) Marshal.SizeOf() // g_type_instance , 0 ), new GLib.AbiField("dummy3" , 0 - , (uint) Marshal.SizeOf(typeof(IntPtr)) // g_type_instance + , (uint) Marshal.SizeOf() // g_type_instance , "dummy2" , "dummy4" - , (uint) Marshal.SizeOf(typeof(IntPtr)) // g_type_instance + , (uint) Marshal.SizeOf() // g_type_instance , 0 ), new GLib.AbiField("dummy3" , 0 - , (uint) Marshal.SizeOf(typeof(IntPtr)) // g_type_instance + , (uint) Marshal.SizeOf() // g_type_instance , "dummy2" , "dummy4" - , (uint) Marshal.SizeOf(typeof(IntPtr)) // g_type_instance + , (uint) Marshal.SizeOf() // g_type_instance , 0 ), new GLib.AbiField("dummy4" , 0 - , (uint) Marshal.SizeOf(typeof(IntPtr)) // g_type_instance + , (uint) Marshal.SizeOf() // g_type_instance , "dummy3" , "dummy5" - , (uint) Marshal.SizeOf(typeof(IntPtr)) // g_type_instance + , (uint) Marshal.SizeOf() // g_type_instance , 0 ), new GLib.AbiField("dummy5" , 0 - , (uint) Marshal.SizeOf(typeof(IntPtr)) // g_type_instance + , (uint) Marshal.SizeOf() // g_type_instance , "dummy4" , "dummy6" - , (uint) Marshal.SizeOf(typeof(IntPtr)) // g_type_instance + , (uint) Marshal.SizeOf() // g_type_instance , 0 ), new GLib.AbiField("dummy6" , 0 - , (uint) Marshal.SizeOf(typeof(IntPtr)) // g_type_instance + , (uint) Marshal.SizeOf() // g_type_instance , "dummy5" , "dummy7" - , (uint) Marshal.SizeOf(typeof(IntPtr)) // g_type_instance + , (uint) Marshal.SizeOf() // g_type_instance , 0 ), new GLib.AbiField("dummy7" , 0 - , (uint) Marshal.SizeOf(typeof(IntPtr)) // g_type_instance + , (uint) Marshal.SizeOf() // g_type_instance , "dummy6" , null - , (uint) Marshal.SizeOf(typeof(IntPtr)) // g_type_instance + , (uint) Marshal.SizeOf() // g_type_instance , 0 ), } diff --git a/Source/Tools/GapiCodegen/ManualGen.cs b/Source/Tools/GapiCodegen/ManualGen.cs index 78a67b67f..9945f03d5 100644 --- a/Source/Tools/GapiCodegen/ManualGen.cs +++ b/Source/Tools/GapiCodegen/ManualGen.cs @@ -68,7 +68,7 @@ namespace GtkSharp.Generation { } public override string GenerateGetSizeOf () { - return "(uint) Marshal.SizeOf(typeof(" + abi_type + "))"; + return "(uint) Marshal.SizeOf<" + abi_type + ">()"; } } } diff --git a/Source/Tools/GapiCodegen/StructABIField.cs b/Source/Tools/GapiCodegen/StructABIField.cs index 90c3104ae..93413e2dd 100644 --- a/Source/Tools/GapiCodegen/StructABIField.cs +++ b/Source/Tools/GapiCodegen/StructABIField.cs @@ -104,7 +104,7 @@ namespace GtkSharp.Generation { // Do not generate structs if the type is a simple pointer. if (IsCPointer()) - min_align = "(uint) Marshal.SizeOf(typeof(IntPtr))"; + min_align = "(uint) Marshal.SizeOf()"; if (IsBitfield) min_align = "1"; diff --git a/Source/Tools/GapiCodegen/StructField.cs b/Source/Tools/GapiCodegen/StructField.cs index 9503f5ed4..f9e756c5a 100644 --- a/Source/Tools/GapiCodegen/StructField.cs +++ b/Source/Tools/GapiCodegen/StructField.cs @@ -144,7 +144,7 @@ namespace GtkSharp.Generation { if (_enum != null && !is_pointer) res = "(uint) Marshal.SizeOf(System.Enum.GetUnderlyingType(typeof(" + cstype + ")))"; else - res = "(uint) Marshal.SizeOf(typeof(" + cstype + "))"; + res = "(uint) Marshal.SizeOf<" + cstype + ">()"; if (IsFixedSizeArray()) res += " * " + ArrayLength; diff --git a/Source/Tools/GapiCodegen/SymbolTable.cs b/Source/Tools/GapiCodegen/SymbolTable.cs index 0e1cdc92d..ccb9a9eea 100644 --- a/Source/Tools/GapiCodegen/SymbolTable.cs +++ b/Source/Tools/GapiCodegen/SymbolTable.cs @@ -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 ByRefGen ("GValue", "GLib.Value")); AddType (new SimpleGen ("GDestroyNotify", "GLib.DestroyNotify", "null", - "(uint) Marshal.SizeOf(typeof(IntPtr))")); + "(uint) Marshal.SizeOf()")); AddType (new SimpleGen ("GThread", "GLib.Thread", "null")); AddType (new ManualGen ("GBytes", "GLib.Bytes")); AddType (new SimpleGen ("GHookList", "GLib.HookList", "null", From 2c54a7824cf2ebfc40c2221957dfe85b968d7458 Mon Sep 17 00:00:00 2001 From: Andrii Kurdiumov Date: Wed, 21 Jul 2021 00:24:49 +0600 Subject: [PATCH 2/4] Update based on PR feedback --- Source/Libs/CairoSharp/Context.cs | 8 +- Source/Libs/CairoSharp/Glyph.cs | 2 +- Source/Libs/GLibSharp/HookList.cs | 16 ++-- Source/Libs/GLibSharp/Marshaller.cs | 8 +- Source/Libs/GLibSharp/Object.cs | 82 +++++++++---------- Source/Libs/GLibSharp/Signal.cs | 4 +- Source/Libs/GLibSharp/SignalClosure.cs | 6 +- Source/Libs/GLibSharp/TimeVal.cs | 6 +- Source/Libs/GdkSharp/Keymap.cs | 4 +- Source/Libs/GtkSharp/ColorSelection.cs | 2 +- Source/Libs/GtkSharp/ListStore.cs | 2 +- Source/Libs/GtkSharp/StatusIcon.cs | 2 +- Source/Libs/GtkSharp/Target.cs | 2 +- Source/Libs/GtkSharp/TreeModelAdapter.cs | 2 +- Source/Libs/GtkSharp/TreeModelFilter.cs | 2 +- Source/Libs/GtkSharp/TreeModelSort.cs | 2 +- Source/Libs/GtkSharp/TreeStore.cs | 2 +- Source/Libs/GtkSharp/Widget.cs | 4 +- Source/OldStuff/doc/en/GLib/Marshaller.xml | 2 +- Source/Tools/GapiCodegen/ByRefGen.cs | 2 +- Source/Tools/GapiCodegen/ClassBase.cs | 2 +- .../Tools/GapiCodegen/DefaultSignalHandler.cs | 2 +- Source/Tools/GapiCodegen/GenerationInfo.cs | 20 +++++ Source/Tools/GapiCodegen/ManualGen.cs | 2 +- Source/Tools/GapiCodegen/Parameter.cs | 2 +- Source/Tools/GapiCodegen/StructABIField.cs | 2 +- Source/Tools/GapiCodegen/StructBase.cs | 2 +- Source/Tools/GapiCodegen/StructField.cs | 4 +- Source/Tools/GapiCodegen/SymbolTable.cs | 2 +- 29 files changed, 109 insertions(+), 89 deletions(-) diff --git a/Source/Libs/CairoSharp/Context.cs b/Source/Libs/CairoSharp/Context.cs index 319b8e530..a30c67c3a 100644 --- a/Source/Libs/CairoSharp/Context.cs +++ b/Source/Libs/CairoSharp/Context.cs @@ -53,7 +53,7 @@ namespace Cairo { static int native_glyph_size, c_compiler_long_size; - static Context () + static unsafe Context () { // // This is used to determine what kind of structure @@ -65,7 +65,7 @@ namespace Cairo { // except in the case of Win64 where sizeof(long) // is 32 bits // - int ptr_size = Marshal.SizeOf(); + int ptr_size = sizeof(IntPtr); PlatformID platform = Environment.OSVersion.Platform; if (platform == PlatformID.Win32NT || @@ -74,10 +74,10 @@ namespace Cairo { platform == PlatformID.WinCE || ptr_size == 4){ c_compiler_long_size = 4; - native_glyph_size = Marshal.SizeOf (typeof (NativeGlyph_4byte_longs)); + native_glyph_size = Marshal.SizeOf (); } else { c_compiler_long_size = 8; - native_glyph_size = Marshal.SizeOf (typeof (Glyph)); + native_glyph_size = Marshal.SizeOf (); } } diff --git a/Source/Libs/CairoSharp/Glyph.cs b/Source/Libs/CairoSharp/Glyph.cs index ec9c2ff38..a5efd9357 100644 --- a/Source/Libs/CairoSharp/Glyph.cs +++ b/Source/Libs/CairoSharp/Glyph.cs @@ -75,7 +75,7 @@ namespace Cairo internal static IntPtr GlyphsToIntPtr (Glyph[] glyphs) { - int size = Marshal.SizeOf (glyphs[0]); + int size = Marshal.SizeOf (); IntPtr dest = Marshal.AllocHGlobal (size * glyphs.Length); long pos = dest.ToInt64 (); for (int i = 0; i < glyphs.Length; i++, pos += size) diff --git a/Source/Libs/GLibSharp/HookList.cs b/Source/Libs/GLibSharp/HookList.cs index 803d0d78e..07d2a4c0c 100644 --- a/Source/Libs/GLibSharp/HookList.cs +++ b/Source/Libs/GLibSharp/HookList.cs @@ -12,10 +12,10 @@ namespace GLib { public partial class HookList : GLib.Opaque { // Internal representation of the wrapped ABI structure. - static public AbiStruct abi_info = new AbiStruct(new List { + static public unsafe AbiStruct abi_info = new AbiStruct(new List { new GLib.AbiField("seq_id" , 0 - , (uint) Marshal.SizeOf() // seq_id + , (uint) sizeof (ulong) // seq_id , null , "hook_size" , (long) Marshal.OffsetOf(typeof(GHookList_seq_idAlign), "seq_id") @@ -23,7 +23,7 @@ namespace GLib { ), new GLib.AbiField("hook_size" , -1 - , (uint) Marshal.SizeOf() // hook_size + , (uint) sizeof (uint) // hook_size , "seq_id" , "is_setup" , 1 @@ -31,7 +31,7 @@ namespace GLib { ), new GLib.AbiField("is_setup" , -1 - , (uint) Marshal.SizeOf() // is_setup + , (uint) sizeof (bool) // is_setup , "hook_size" , "hooks" , 1 @@ -39,7 +39,7 @@ namespace GLib { ), new GLib.AbiField("hooks" , -1 - , (uint) Marshal.SizeOf() // hooks + , (uint) sizeof (IntPtr) // hooks , "is_setup" , "dummy3" , (long) Marshal.OffsetOf(typeof(GHookList_hooksAlign), "hooks") @@ -47,7 +47,7 @@ namespace GLib { ), new GLib.AbiField("dummy3" , -1 - , (uint) Marshal.SizeOf() // dummy3 + , (uint) sizeof (IntPtr) // dummy3 , "hooks" , "finalize_hook" , (long) Marshal.OffsetOf(typeof(GHookList_dummy3Align), "dummy3") @@ -55,7 +55,7 @@ namespace GLib { ), new GLib.AbiField("finalize_hook" , -1 - , (uint) Marshal.SizeOf() // finalize_hook + , (uint) sizeof (IntPtr) // finalize_hook , "dummy3" , "dummy" , (long) Marshal.OffsetOf(typeof(GHookList_finalize_hookAlign), "finalize_hook") @@ -63,7 +63,7 @@ namespace GLib { ), new GLib.AbiField("dummy" , -1 - , (uint) Marshal.SizeOf() * 2 // dummy + , (uint) sizeof (IntPtr) * 2 // dummy , "finalize_hook" , null , (long) Marshal.OffsetOf(typeof(GHookList_dummyAlign), "dummy") diff --git a/Source/Libs/GLibSharp/Marshaller.cs b/Source/Libs/GLibSharp/Marshaller.cs index 55683ffb2..23af0cd4d 100644 --- a/Source/Libs/GLibSharp/Marshaller.cs +++ b/Source/Libs/GLibSharp/Marshaller.cs @@ -356,19 +356,19 @@ namespace GLib { while (current != IntPtr.Zero) { Marshal.PtrToStructure (current, currentStruct); res.Add (currentStruct); - current = (IntPtr) ((long)current + Marshal.SizeOf (typeof (T))); + current = (IntPtr) ((long)current + Marshal.SizeOf ()); } return res.ToArray (); } - public static IntPtr StructArrayToNullTerminatedStructArrayIntPtr (T[] InputArray) + public static unsafe IntPtr StructArrayToNullTerminatedStructArrayIntPtr (T[] InputArray) { - int intPtrSize = Marshal.SizeOf(); + int intPtrSize = sizeof (IntPtr); IntPtr mem = Marshal.AllocHGlobal ((InputArray.Length + 1) * intPtrSize); for (int i = 0; i < InputArray.Length; i++) { - IntPtr structPtr = Marshal.AllocHGlobal (Marshal.SizeOf (typeof (T))); + IntPtr structPtr = Marshal.AllocHGlobal (Marshal.SizeOf ()); Marshal.StructureToPtr (InputArray[i], structPtr, false); // jump to next pointer Marshal.WriteIntPtr (mem, structPtr); diff --git a/Source/Libs/GLibSharp/Object.cs b/Source/Libs/GLibSharp/Object.cs index 0062fd4db..e75638754 100644 --- a/Source/Libs/GLibSharp/Object.cs +++ b/Source/Libs/GLibSharp/Object.cs @@ -936,10 +936,10 @@ namespace GLib { } // Internal representation of the wrapped ABI structure. - static public AbiStruct abi_info = new AbiStruct(new List { + static public unsafe AbiStruct abi_info = new AbiStruct(new List { new GLib.AbiField("g_type_instance" , 0 - , (uint) Marshal.SizeOf() // g_type_instance + , (uint) sizeof (IntPtr) // g_type_instance , null , "ref_count" , (long) Marshal.OffsetOf(typeof(GObject_g_type_instanceAlign), "g_type_instance") @@ -947,7 +947,7 @@ namespace GLib { ), new GLib.AbiField("ref_count" , -1 - , (uint) Marshal.SizeOf() // ref_count + , (uint) sizeof (uint) // ref_count , "g_type_instance" , "qdata" , (long) Marshal.OffsetOf(typeof(GObject_ref_countAlign), "ref_count") @@ -955,7 +955,7 @@ namespace GLib { ), new GLib.AbiField("qdata" , -1 - , (uint) Marshal.SizeOf() // qdata + , (uint) sizeof (IntPtr) // qdata , "ref_count" , null , (long) Marshal.OffsetOf(typeof(GObject_qdataAlign), "qdata") @@ -965,149 +965,149 @@ namespace GLib { ); // // Internal representation of the wrapped ABI structure. - static public AbiStruct class_abi = new AbiStruct(new List { + static public unsafe AbiStruct class_abi = new AbiStruct(new List { new GLib.AbiField("type_class" , 0 - , (uint) Marshal.SizeOf() // g_type_instance + , (uint) sizeof (IntPtr) // g_type_instance , null , "construct_props" - , (uint) Marshal.SizeOf() // g_type_instance + , (uint) sizeof (IntPtr) // g_type_instance , 0 ), new GLib.AbiField("construct_props" , 0 - , (uint) Marshal.SizeOf() // g_type_instance + , (uint) sizeof (IntPtr) // g_type_instance , "type_class" , "constructor_cb" - , (uint) Marshal.SizeOf() // g_type_instance + , (uint) sizeof (IntPtr) // g_type_instance , 0 ), new GLib.AbiField("constructor_cb" , 0 - , (uint) Marshal.SizeOf() // g_type_instance + , (uint) sizeof (IntPtr) // g_type_instance , "construct_props" , "set_prop_cb" - , (uint) Marshal.SizeOf() // g_type_instance + , (uint) sizeof (IntPtr) // g_type_instance , 0 ), new GLib.AbiField("set_prop_cb" , 0 - , (uint) Marshal.SizeOf() // g_type_instance + , (uint) sizeof (IntPtr) // g_type_instance , "constructor_cb" , "get_prop_cb" - , (uint) Marshal.SizeOf() // g_type_instance + , (uint) sizeof (IntPtr) // g_type_instance , 0 ), new GLib.AbiField("get_prop_cb" , 0 - , (uint) Marshal.SizeOf() // g_type_instance + , (uint) sizeof (IntPtr) // g_type_instance , "set_prop_cb" , "dispose" - , (uint) Marshal.SizeOf() // g_type_instance + , (uint) sizeof (IntPtr) // g_type_instance , 0 ), new GLib.AbiField("dispose" , 0 - , (uint) Marshal.SizeOf() // g_type_instance + , (uint) sizeof (IntPtr) // g_type_instance , "get_prop_cb" , "finalize" - , (uint) Marshal.SizeOf() // g_type_instance + , (uint) sizeof (IntPtr) // g_type_instance , 0 ), new GLib.AbiField("finalize" , 0 - , (uint) Marshal.SizeOf() // g_type_instance + , (uint) sizeof (IntPtr) // g_type_instance , "dispose" , "dispatch_properties_changed" - , (uint) Marshal.SizeOf() // g_type_instance + , (uint) sizeof (IntPtr) // g_type_instance , 0 ), new GLib.AbiField("dispatch_properties_changed" , 0 - , (uint) Marshal.SizeOf() // g_type_instance + , (uint) sizeof (IntPtr) // g_type_instance , "finalize" , "notify" - , (uint) Marshal.SizeOf() // g_type_instance + , (uint) sizeof (IntPtr) // g_type_instance , 0 ), new GLib.AbiField("notify" , 0 - , (uint) Marshal.SizeOf() // g_type_instance + , (uint) sizeof (IntPtr) // g_type_instance , "dispatch_properties_changed" , "constructed" - , (uint) Marshal.SizeOf() // g_type_instance + , (uint) sizeof (IntPtr) // g_type_instance , 0 ), new GLib.AbiField("constructed" , 0 - , (uint) Marshal.SizeOf() // g_type_instance + , (uint) sizeof (IntPtr) // g_type_instance , "notify" , "dummy1" - , (uint) Marshal.SizeOf() // g_type_instance + , (uint) sizeof (IntPtr) // g_type_instance , 0 ), new GLib.AbiField("dummy1" , 0 - , (uint) Marshal.SizeOf() // g_type_instance + , (uint) sizeof (IntPtr) // g_type_instance , "constructed" , "dummy2" - , (uint) Marshal.SizeOf() // g_type_instance + , (uint) sizeof (IntPtr) // g_type_instance , 0 ), new GLib.AbiField("dummy2" , 0 - , (uint) Marshal.SizeOf() // g_type_instance + , (uint) sizeof (IntPtr) // g_type_instance , "dummy1" , "dummy3" - , (uint) Marshal.SizeOf() // g_type_instance + , (uint) sizeof (IntPtr) // g_type_instance , 0 ), new GLib.AbiField("dummy3" , 0 - , (uint) Marshal.SizeOf() // g_type_instance + , (uint) sizeof (IntPtr) // g_type_instance , "dummy2" , "dummy4" - , (uint) Marshal.SizeOf() // g_type_instance + , (uint) sizeof (IntPtr) // g_type_instance , 0 ), new GLib.AbiField("dummy3" , 0 - , (uint) Marshal.SizeOf() // g_type_instance + , (uint) sizeof (IntPtr) // g_type_instance , "dummy2" , "dummy4" - , (uint) Marshal.SizeOf() // g_type_instance + , (uint) sizeof (IntPtr) // g_type_instance , 0 ), new GLib.AbiField("dummy4" , 0 - , (uint) Marshal.SizeOf() // g_type_instance + , (uint) sizeof (IntPtr) // g_type_instance , "dummy3" , "dummy5" - , (uint) Marshal.SizeOf() // g_type_instance + , (uint) sizeof (IntPtr) // g_type_instance , 0 ), new GLib.AbiField("dummy5" , 0 - , (uint) Marshal.SizeOf() // g_type_instance + , (uint) sizeof (IntPtr) // g_type_instance , "dummy4" , "dummy6" - , (uint) Marshal.SizeOf() // g_type_instance + , (uint) sizeof (IntPtr) // g_type_instance , 0 ), new GLib.AbiField("dummy6" , 0 - , (uint) Marshal.SizeOf() // g_type_instance + , (uint) sizeof (IntPtr) // g_type_instance , "dummy5" , "dummy7" - , (uint) Marshal.SizeOf() // g_type_instance + , (uint) sizeof (IntPtr) // g_type_instance , 0 ), new GLib.AbiField("dummy7" , 0 - , (uint) Marshal.SizeOf() // g_type_instance + , (uint) sizeof (IntPtr) // g_type_instance , "dummy6" , null - , (uint) Marshal.SizeOf() // g_type_instance + , (uint) sizeof (IntPtr) // g_type_instance , 0 ), } diff --git a/Source/Libs/GLibSharp/Signal.cs b/Source/Libs/GLibSharp/Signal.cs index dd94e6c38..180b4d585 100644 --- a/Source/Libs/GLibSharp/Signal.cs +++ b/Source/Libs/GLibSharp/Signal.cs @@ -92,7 +92,7 @@ namespace GLib { { object[] pvals = new object [n_pvals]; for (int i = 0; i < n_pvals; i++) { - IntPtr p = new IntPtr ((long) pvals_ptr + i * Marshal.SizeOf (typeof (Value))); + IntPtr p = new IntPtr ((long) pvals_ptr + i * Marshal.SizeOf ()); Value v = (Value) Marshal.PtrToStructure (p, typeof (Value)); pvals [i] = v.Val; } @@ -110,7 +110,7 @@ namespace GLib { bool NativeInvoker (InvocationHint ihint, object[] pvals) { - int val_sz = Marshal.SizeOf (typeof (Value)); + int val_sz = Marshal.SizeOf (); IntPtr buf = Marshal.AllocHGlobal (pvals.Length * val_sz); Value[] vals = new Value [pvals.Length]; for (int i = 0; i < pvals.Length; i++) { diff --git a/Source/Libs/GLibSharp/SignalClosure.cs b/Source/Libs/GLibSharp/SignalClosure.cs index 71c554195..7beee013d 100644 --- a/Source/Libs/GLibSharp/SignalClosure.cs +++ b/Source/Libs/GLibSharp/SignalClosure.cs @@ -72,7 +72,7 @@ namespace GLib { public SignalClosure (IntPtr obj, string signal_name, System.Type args_type) { - raw_closure = g_closure_new_simple (Marshal.SizeOf (typeof (GClosure)), IntPtr.Zero); + raw_closure = g_closure_new_simple (Marshal.SizeOf (), IntPtr.Zero); g_closure_set_marshal (raw_closure, Marshaler); g_closure_add_finalize_notifier (raw_closure, IntPtr.Zero, Notify); closures [raw_closure] = this; @@ -159,7 +159,7 @@ namespace GLib { args.Args = new object [n_param_vals - 1]; GLib.Value[] vals = new GLib.Value [n_param_vals - 1]; for (int i = 1; i < n_param_vals; i++) { - IntPtr ptr = new IntPtr (param_values.ToInt64 () + i * Marshal.SizeOf (typeof (Value))); + IntPtr ptr = new IntPtr (param_values.ToInt64 () + i * Marshal.SizeOf ()); vals [i - 1] = (Value) Marshal.PtrToStructure (ptr, typeof (Value)); args.Args [i - 1] = vals [i - 1].Val; } @@ -167,7 +167,7 @@ namespace GLib { closure.Invoke (ci_args); for (int i = 1; i < n_param_vals; i++) { vals [i - 1].Update (args.Args [i - 1]); - IntPtr ptr = new IntPtr (param_values.ToInt64 () + i * Marshal.SizeOf (typeof (Value))); + IntPtr ptr = new IntPtr (param_values.ToInt64 () + i * Marshal.SizeOf ()); Marshal.StructureToPtr (vals [i - 1], ptr, false); } if (return_val == IntPtr.Zero || args.RetVal == null) diff --git a/Source/Libs/GLibSharp/TimeVal.cs b/Source/Libs/GLibSharp/TimeVal.cs index da0a580cc..f1d6b983f 100644 --- a/Source/Libs/GLibSharp/TimeVal.cs +++ b/Source/Libs/GLibSharp/TimeVal.cs @@ -43,7 +43,7 @@ namespace GLib { static d_g_time_val_add g_time_val_add = FuncLoader.LoadFunction(FuncLoader.GetProcAddress(GLibrary.Load(Library.GLib), "g_time_val_add")); public void Add(long microseconds) { - IntPtr this_as_native = System.Runtime.InteropServices.Marshal.AllocHGlobal (System.Runtime.InteropServices.Marshal.SizeOf (this)); + IntPtr this_as_native = System.Runtime.InteropServices.Marshal.AllocHGlobal (System.Runtime.InteropServices.Marshal.SizeOf ()); System.Runtime.InteropServices.Marshal.StructureToPtr (this, this_as_native, false); g_time_val_add(this_as_native, new IntPtr (microseconds)); ReadNative (this_as_native, ref this); @@ -54,7 +54,7 @@ namespace GLib { static d_g_time_val_to_iso8601 g_time_val_to_iso8601 = FuncLoader.LoadFunction(FuncLoader.GetProcAddress(GLibrary.Load(Library.GLib), "g_time_val_to_iso8601")); public string ToIso8601() { - IntPtr this_as_native = System.Runtime.InteropServices.Marshal.AllocHGlobal (System.Runtime.InteropServices.Marshal.SizeOf (this)); + IntPtr this_as_native = System.Runtime.InteropServices.Marshal.AllocHGlobal (System.Runtime.InteropServices.Marshal.SizeOf ()); System.Runtime.InteropServices.Marshal.StructureToPtr (this, this_as_native, false); IntPtr raw_ret = g_time_val_to_iso8601(this_as_native); string ret = GLib.Marshaller.PtrToStringGFree(raw_ret); @@ -68,7 +68,7 @@ namespace GLib { public static bool FromIso8601(string iso_date, out GLib.TimeVal time_) { IntPtr native_iso_date = GLib.Marshaller.StringToPtrGStrdup (iso_date); - IntPtr native_time_ = Marshal.AllocHGlobal (Marshal.SizeOf (typeof (GLib.TimeVal))); + IntPtr native_time_ = Marshal.AllocHGlobal (Marshal.SizeOf ()); bool raw_ret = g_time_val_from_iso8601(native_iso_date, native_time_); bool ret = raw_ret; GLib.Marshaller.Free (native_iso_date); diff --git a/Source/Libs/GdkSharp/Keymap.cs b/Source/Libs/GdkSharp/Keymap.cs index ec7132798..e166f6240 100644 --- a/Source/Libs/GdkSharp/Keymap.cs +++ b/Source/Libs/GdkSharp/Keymap.cs @@ -39,7 +39,7 @@ namespace Gdk { int[] tmp = new int [count]; Marshal.Copy (keyval_ptr, tmp, 0, count); for (int i = 0; i < count; i++) { - IntPtr ptr = new IntPtr ((long) key_ptr + i * Marshal.SizeOf (typeof (KeymapKey))); + IntPtr ptr = new IntPtr ((long) key_ptr + i * Marshal.SizeOf ()); keyvals [i] = (uint) tmp [i]; keys [i] = KeymapKey.New (ptr); } @@ -61,7 +61,7 @@ namespace Gdk { if (gdk_keymap_get_entries_for_keyval(Handle, keyval, out key_ptr, out count)) { KeymapKey[] result = new KeymapKey [count]; for (int i = 0; i < count; i++) { - IntPtr ptr = new IntPtr ((long) key_ptr + i * Marshal.SizeOf (typeof (KeymapKey))); + IntPtr ptr = new IntPtr ((long) key_ptr + i * Marshal.SizeOf ()); result [i] = KeymapKey.New (ptr); } GLib.Marshaller.Free (key_ptr); diff --git a/Source/Libs/GtkSharp/ColorSelection.cs b/Source/Libs/GtkSharp/ColorSelection.cs index 7623dea5f..76bd71133 100644 --- a/Source/Libs/GtkSharp/ColorSelection.cs +++ b/Source/Libs/GtkSharp/ColorSelection.cs @@ -55,7 +55,7 @@ namespace Gtk { for (int i=0; i < n_colors; i++) { colors[i] = Gdk.Color.New(parsedColors); - parsedColors = (IntPtr) ((int)parsedColors + Marshal.SizeOf(colors[i])); + parsedColors = (IntPtr) ((int)parsedColors + Marshal.SizeOf ()); } return colors; } diff --git a/Source/Libs/GtkSharp/ListStore.cs b/Source/Libs/GtkSharp/ListStore.cs index 0870601c3..825ea0bf1 100644 --- a/Source/Libs/GtkSharp/ListStore.cs +++ b/Source/Libs/GtkSharp/ListStore.cs @@ -271,7 +271,7 @@ namespace Gtk { vals [2] = new GLib.Value (iter); inst_and_params.Append (vals [2]); int cnt = IterNChildren (); - IntPtr new_order_ptr = Marshal.AllocHGlobal (Marshal.SizeOf (typeof (int)) * cnt); + IntPtr new_order_ptr = Marshal.AllocHGlobal (sizeof(int) * cnt); Marshal.Copy (new_order, 0, new_order_ptr, cnt); vals [3] = new GLib.Value (new_order_ptr); inst_and_params.Append (vals [3]); diff --git a/Source/Libs/GtkSharp/StatusIcon.cs b/Source/Libs/GtkSharp/StatusIcon.cs index f0d8989c9..116a9d467 100644 --- a/Source/Libs/GtkSharp/StatusIcon.cs +++ b/Source/Libs/GtkSharp/StatusIcon.cs @@ -90,7 +90,7 @@ namespace Gtk { public bool GetGeometry(out Gdk.Screen screen, out Gdk.Rectangle area, out Gtk.Orientation orientation) { IntPtr native_screen; - IntPtr native_area = Marshal.AllocHGlobal (Marshal.SizeOf (typeof (Gdk.Rectangle))); + IntPtr native_area = Marshal.AllocHGlobal (Marshal.SizeOf ()); int native_orientation; bool ret = gtk_status_icon_get_geometry(Handle, out native_screen, native_area, out native_orientation); if (ret) { diff --git a/Source/Libs/GtkSharp/Target.cs b/Source/Libs/GtkSharp/Target.cs index 6b5893564..b53935132 100644 --- a/Source/Libs/GtkSharp/Target.cs +++ b/Source/Libs/GtkSharp/Target.cs @@ -33,7 +33,7 @@ namespace Gtk { IntPtr array_ptr = gtk_target_table_new_from_list (list.Handle, out n_targets); Gtk.TargetEntry[] ret = new Gtk.TargetEntry [n_targets]; - int unmanaged_struct_size = Marshal.SizeOf (typeof (Gtk.TargetEntry)); + int unmanaged_struct_size = Marshal.SizeOf (); for (int i = 0; i < n_targets; i++) { ret [i] = Gtk.TargetEntry.New (new IntPtr (array_ptr.ToInt64 () + i * unmanaged_struct_size)); } diff --git a/Source/Libs/GtkSharp/TreeModelAdapter.cs b/Source/Libs/GtkSharp/TreeModelAdapter.cs index ed0bc814f..4a0655654 100644 --- a/Source/Libs/GtkSharp/TreeModelAdapter.cs +++ b/Source/Libs/GtkSharp/TreeModelAdapter.cs @@ -161,7 +161,7 @@ namespace Gtk { vals [2] = new GLib.Value (iter); inst_and_params.Append (vals [2]); int cnt = IterNChildren (iter); - IntPtr new_order_ptr = Marshal.AllocHGlobal (Marshal.SizeOf (typeof (int)) * cnt); + IntPtr new_order_ptr = Marshal.AllocHGlobal (sizeof(int) * cnt); Marshal.Copy (new_order, 0, new_order_ptr, cnt); vals [3] = new GLib.Value (new_order_ptr); inst_and_params.Append (vals [3]); diff --git a/Source/Libs/GtkSharp/TreeModelFilter.cs b/Source/Libs/GtkSharp/TreeModelFilter.cs index 4cb7ed67f..1ad1372e7 100644 --- a/Source/Libs/GtkSharp/TreeModelFilter.cs +++ b/Source/Libs/GtkSharp/TreeModelFilter.cs @@ -185,7 +185,7 @@ namespace Gtk { vals [2] = new GLib.Value (iter); inst_and_params.Append (vals [2]); int cnt = IterNChildren (iter); - IntPtr new_order_ptr = Marshal.AllocHGlobal (Marshal.SizeOf (typeof (int)) * cnt); + IntPtr new_order_ptr = Marshal.AllocHGlobal (sizeof(int) * cnt); Marshal.Copy (new_order, 0, new_order_ptr, cnt); vals [3] = new GLib.Value (new_order_ptr); inst_and_params.Append (vals [3]); diff --git a/Source/Libs/GtkSharp/TreeModelSort.cs b/Source/Libs/GtkSharp/TreeModelSort.cs index 45fc3c572..2446bb21c 100644 --- a/Source/Libs/GtkSharp/TreeModelSort.cs +++ b/Source/Libs/GtkSharp/TreeModelSort.cs @@ -165,7 +165,7 @@ namespace Gtk { vals [2] = new GLib.Value (iter); inst_and_params.Append (vals [2]); int cnt = IterNChildren (iter); - IntPtr new_order_ptr = Marshal.AllocHGlobal (Marshal.SizeOf (typeof (int)) * cnt); + IntPtr new_order_ptr = Marshal.AllocHGlobal (sizeof(int) * cnt); Marshal.Copy (new_order, 0, new_order_ptr, cnt); vals [3] = new GLib.Value (new_order_ptr); inst_and_params.Append (vals [3]); diff --git a/Source/Libs/GtkSharp/TreeStore.cs b/Source/Libs/GtkSharp/TreeStore.cs index 573128d2a..44c163c7c 100644 --- a/Source/Libs/GtkSharp/TreeStore.cs +++ b/Source/Libs/GtkSharp/TreeStore.cs @@ -382,7 +382,7 @@ namespace Gtk { vals [2] = new GLib.Value (iter); inst_and_params.Append (vals [2]); int cnt = IterNChildren (iter); - IntPtr new_order_ptr = Marshal.AllocHGlobal (Marshal.SizeOf (typeof (int)) * cnt); + IntPtr new_order_ptr = Marshal.AllocHGlobal (sizeof(int) * cnt); Marshal.Copy (new_order, 0, new_order_ptr, cnt); vals [3] = new GLib.Value (new_order_ptr); inst_and_params.Append (vals [3]); diff --git a/Source/Libs/GtkSharp/Widget.cs b/Source/Libs/GtkSharp/Widget.cs index 78c74abd0..370f3ac18 100644 --- a/Source/Libs/GtkSharp/Widget.cs +++ b/Source/Libs/GtkSharp/Widget.cs @@ -65,7 +65,7 @@ namespace Gtk { static d_g_closure_set_marshal g_closure_set_marshal = FuncLoader.LoadFunction(FuncLoader.GetProcAddress(GLibrary.Load(Library.GObject), "g_closure_set_marshal")); static IntPtr CreateClosure (ClosureMarshal marshaler) { - IntPtr raw_closure = g_closure_new_simple (Marshal.SizeOf (typeof (GClosure)), IntPtr.Zero); + IntPtr raw_closure = g_closure_new_simple (Marshal.SizeOf (), IntPtr.Zero); g_closure_set_marshal (raw_closure, marshaler); return raw_closure; } @@ -149,7 +149,7 @@ namespace Gtk { { try { GLib.Value[] inst_and_params = new GLib.Value [n_param_vals]; - int gvalue_size = Marshal.SizeOf (typeof (GLib.Value)); + int gvalue_size = Marshal.SizeOf (); for (int idx = 0; idx < n_param_vals; idx++) inst_and_params [idx] = (GLib.Value) Marshal.PtrToStructure (new IntPtr (param_values.ToInt64 () + idx * gvalue_size), typeof (GLib.Value)); diff --git a/Source/OldStuff/doc/en/GLib/Marshaller.xml b/Source/OldStuff/doc/en/GLib/Marshaller.xml index dde9f77da..509bba1a8 100644 --- a/Source/OldStuff/doc/en/GLib/Marshaller.xml +++ b/Source/OldStuff/doc/en/GLib/Marshaller.xml @@ -508,7 +508,7 @@ - + Method diff --git a/Source/Tools/GapiCodegen/ByRefGen.cs b/Source/Tools/GapiCodegen/ByRefGen.cs index 232c3252b..70e3163fe 100644 --- a/Source/Tools/GapiCodegen/ByRefGen.cs +++ b/Source/Tools/GapiCodegen/ByRefGen.cs @@ -41,7 +41,7 @@ namespace GtkSharp.Generation { public string AllocNative () { - return "Marshal.AllocHGlobal (Marshal.SizeOf (typeof (" + QualifiedName + ")))"; + return "Marshal.AllocHGlobal (" + GenerationInfo.GetSizeOfExpression(QualifiedName) + ")"; } public string AllocNative (string var_name) diff --git a/Source/Tools/GapiCodegen/ClassBase.cs b/Source/Tools/GapiCodegen/ClassBase.cs index 0ff0bb58d..7a9704c69 100644 --- a/Source/Tools/GapiCodegen/ClassBase.cs +++ b/Source/Tools/GapiCodegen/ClassBase.cs @@ -227,7 +227,7 @@ namespace GtkSharp.Generation { sw.WriteLine (); sw.WriteLine ("\t\t// Internal representation of the wrapped structure ABI."); sw.WriteLine ("\t\tstatic GLib.AbiStruct _" + info_name + " = null;"); - sw.WriteLine ("\t\tstatic public " + _new + "GLib.AbiStruct " + info_name + " {"); + sw.WriteLine ("\t\tstatic public unsafe " + _new + "GLib.AbiStruct " + info_name + " {"); sw.WriteLine ("\t\t\tget {"); sw.WriteLine ("\t\t\t\tif (_" + info_name + " == null)"); diff --git a/Source/Tools/GapiCodegen/DefaultSignalHandler.cs b/Source/Tools/GapiCodegen/DefaultSignalHandler.cs index 989310808..0dc83066e 100644 --- a/Source/Tools/GapiCodegen/DefaultSignalHandler.cs +++ b/Source/Tools/GapiCodegen/DefaultSignalHandler.cs @@ -90,7 +90,7 @@ namespace GtkSharp.Generation { if (p.PassAs == "ref") sw.WriteLine ("\t\t\tIntPtr " + p.Name + "_ptr = GLib.Marshaller.StructureToPtrAlloc (" + p.Generatable.CallByName (p.Name) + ");"); else - sw.WriteLine ("\t\t\tIntPtr " + p.Name + "_ptr = Marshal.AllocHGlobal (Marshal.SizeOf (typeof (" + p.MarshalType + ")));"); + sw.WriteLine ("\t\t\tIntPtr " + p.Name + "_ptr = Marshal.AllocHGlobal (" + GenerationInfo.GetSizeOfExpression(p.MarshalType) + ");"); sw.WriteLine ("\t\t\tvals [" + (i + 1) + "] = new GLib.Value (" + p.Name + "_ptr);"); cleanup += "\t\t\t" + p.Name + " = " + p.FromNative ("(" + p.MarshalType + ") Marshal.PtrToStructure (" + p.Name + "_ptr, typeof (" + p.MarshalType + "))") + ";\n"; diff --git a/Source/Tools/GapiCodegen/GenerationInfo.cs b/Source/Tools/GapiCodegen/GenerationInfo.cs index 3472aff2e..b7ee41549 100644 --- a/Source/Tools/GapiCodegen/GenerationInfo.cs +++ b/Source/Tools/GapiCodegen/GenerationInfo.cs @@ -234,6 +234,26 @@ namespace GtkSharp.Generation { return sw; } + + internal static string GetSizeOfExpression(string cstype) + { + bool isBlittable = cstype == "IntPtr" + || cstype == "UIntPtr" + || cstype == "ulong" + || cstype == "long" + || cstype == "uint" + || cstype == "int" + || cstype == "ushort" + || cstype == "short" + || cstype == "byte" + || cstype == "sbyte" + || cstype == "char" + || cstype == "byte"; + if (isBlittable) + return "sizeof( " + cstype + " )"; + + return "Marshal.SizeOf<" + cstype + ">()"; + } } } diff --git a/Source/Tools/GapiCodegen/ManualGen.cs b/Source/Tools/GapiCodegen/ManualGen.cs index 9945f03d5..fcc7aa801 100644 --- a/Source/Tools/GapiCodegen/ManualGen.cs +++ b/Source/Tools/GapiCodegen/ManualGen.cs @@ -68,7 +68,7 @@ namespace GtkSharp.Generation { } public override string GenerateGetSizeOf () { - return "(uint) Marshal.SizeOf<" + abi_type + ">()"; + return "(uint) " + GenerationInfo.GetSizeOfExpression(abi_type); } } } diff --git a/Source/Tools/GapiCodegen/Parameter.cs b/Source/Tools/GapiCodegen/Parameter.cs index a77db28fc..33fa9dfc7 100644 --- a/Source/Tools/GapiCodegen/Parameter.cs +++ b/Source/Tools/GapiCodegen/Parameter.cs @@ -394,7 +394,7 @@ namespace GtkSharp.Generation { public override string[] Prepare { get { if (PassAs == "out") - return new string [] { "IntPtr native_" + CallName + " = Marshal.AllocHGlobal (Marshal.SizeOf (typeof (" + Generatable.QualifiedName + ")));"}; + return new string [] { "IntPtr native_" + CallName + " = Marshal.AllocHGlobal (" + GenerationInfo.GetSizeOfExpression(Generatable.QualifiedName) + ");"}; else return new string [] { "IntPtr native_" + CallName + " = " + (Generatable as IManualMarshaler).AllocNative (CallName) + ";"}; } diff --git a/Source/Tools/GapiCodegen/StructABIField.cs b/Source/Tools/GapiCodegen/StructABIField.cs index 93413e2dd..61f4f8965 100644 --- a/Source/Tools/GapiCodegen/StructABIField.cs +++ b/Source/Tools/GapiCodegen/StructABIField.cs @@ -104,7 +104,7 @@ namespace GtkSharp.Generation { // Do not generate structs if the type is a simple pointer. if (IsCPointer()) - min_align = "(uint) Marshal.SizeOf()"; + min_align = "(uint) sizeof(IntPtr)"; if (IsBitfield) min_align = "1"; diff --git a/Source/Tools/GapiCodegen/StructBase.cs b/Source/Tools/GapiCodegen/StructBase.cs index ad1c6b917..b3cf53431 100644 --- a/Source/Tools/GapiCodegen/StructBase.cs +++ b/Source/Tools/GapiCodegen/StructBase.cs @@ -298,7 +298,7 @@ namespace GtkSharp.Generation { public override void Prepare (StreamWriter sw, string indent) { - sw.WriteLine (indent + "IntPtr this_as_native = System.Runtime.InteropServices.Marshal.AllocHGlobal (System.Runtime.InteropServices.Marshal.SizeOf (this));"); + sw.WriteLine (indent + "IntPtr this_as_native = System.Runtime.InteropServices.Marshal.AllocHGlobal (System.Runtime.InteropServices.Marshal.SizeOf<" + QualifiedName + ">());"); sw.WriteLine (indent + "System.Runtime.InteropServices.Marshal.StructureToPtr (this, this_as_native, false);"); } diff --git a/Source/Tools/GapiCodegen/StructField.cs b/Source/Tools/GapiCodegen/StructField.cs index f9e756c5a..9b1b10428 100644 --- a/Source/Tools/GapiCodegen/StructField.cs +++ b/Source/Tools/GapiCodegen/StructField.cs @@ -142,9 +142,9 @@ namespace GtkSharp.Generation { var _enum = gen as EnumGen; if (_enum != null && !is_pointer) - res = "(uint) Marshal.SizeOf(System.Enum.GetUnderlyingType(typeof(" + cstype + ")))"; + res = "(uint) sizeof(" + cstype + ")"; else - res = "(uint) Marshal.SizeOf<" + cstype + ">()"; + res = "(uint) " + GenerationInfo.GetSizeOfExpression(cstype); if (IsFixedSizeArray()) res += " * " + ArrayLength; diff --git a/Source/Tools/GapiCodegen/SymbolTable.cs b/Source/Tools/GapiCodegen/SymbolTable.cs index ccb9a9eea..922fb2152 100644 --- a/Source/Tools/GapiCodegen/SymbolTable.cs +++ b/Source/Tools/GapiCodegen/SymbolTable.cs @@ -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 ByRefGen ("GValue", "GLib.Value")); AddType (new SimpleGen ("GDestroyNotify", "GLib.DestroyNotify", "null", - "(uint) Marshal.SizeOf()")); + "(uint) sizeof(IntPtr)")); AddType (new SimpleGen ("GThread", "GLib.Thread", "null")); AddType (new ManualGen ("GBytes", "GLib.Bytes")); AddType (new SimpleGen ("GHookList", "GLib.HookList", "null", From 1b73bc8042eca0d0ebd9dbb90080819b9f02d603 Mon Sep 17 00:00:00 2001 From: Andrii Kurdiumov Date: Wed, 21 Jul 2021 00:38:34 +0600 Subject: [PATCH 3/4] Address PR feedback --- Source/Libs/GLibSharp/HookList.cs | 2 +- Source/Tools/GapiCodegen/GenerationInfo.cs | 9 +++++++-- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/Source/Libs/GLibSharp/HookList.cs b/Source/Libs/GLibSharp/HookList.cs index 07d2a4c0c..3498454f5 100644 --- a/Source/Libs/GLibSharp/HookList.cs +++ b/Source/Libs/GLibSharp/HookList.cs @@ -31,7 +31,7 @@ namespace GLib { ), new GLib.AbiField("is_setup" , -1 - , (uint) sizeof (bool) // is_setup + , (uint) sizeof(uint) // is_setup , "hook_size" , "hooks" , 1 diff --git a/Source/Tools/GapiCodegen/GenerationInfo.cs b/Source/Tools/GapiCodegen/GenerationInfo.cs index b7ee41549..6ced13550 100644 --- a/Source/Tools/GapiCodegen/GenerationInfo.cs +++ b/Source/Tools/GapiCodegen/GenerationInfo.cs @@ -237,6 +237,7 @@ namespace GtkSharp.Generation { internal static string GetSizeOfExpression(string cstype) { + // See https://docs.microsoft.com/en-us/dotnet/framework/interop/blittable-and-non-blittable-types bool isBlittable = cstype == "IntPtr" || cstype == "UIntPtr" || cstype == "ulong" @@ -247,11 +248,15 @@ namespace GtkSharp.Generation { || cstype == "short" || cstype == "byte" || cstype == "sbyte" - || cstype == "char" - || cstype == "byte"; + || cstype == "float" + || cstype == "double"; if (isBlittable) return "sizeof( " + cstype + " )"; + // This is optimization based on https://github.com/GtkSharp/GtkSharp/pull/261#discussion_r673381869 + if (cstype == "bool") + return "sizeof( uint )"; + return "Marshal.SizeOf<" + cstype + ">()"; } } From 64f3f78c0f156b5a2b49931037f712cf2ab93be4 Mon Sep 17 00:00:00 2001 From: Andrii Kurdiumov Date: Thu, 22 Jul 2021 19:40:38 +0600 Subject: [PATCH 4/4] Address PR feedback --- Source/Libs/GLibSharp/HookList.cs | 2 +- Source/Tools/GapiCodegen/GenerationInfo.cs | 4 ---- 2 files changed, 1 insertion(+), 5 deletions(-) diff --git a/Source/Libs/GLibSharp/HookList.cs b/Source/Libs/GLibSharp/HookList.cs index 3498454f5..7af30e4e3 100644 --- a/Source/Libs/GLibSharp/HookList.cs +++ b/Source/Libs/GLibSharp/HookList.cs @@ -31,7 +31,7 @@ namespace GLib { ), new GLib.AbiField("is_setup" , -1 - , (uint) sizeof(uint) // is_setup + , (uint) Marshal.SizeOf() // is_setup , "hook_size" , "hooks" , 1 diff --git a/Source/Tools/GapiCodegen/GenerationInfo.cs b/Source/Tools/GapiCodegen/GenerationInfo.cs index 6ced13550..6ef86b008 100644 --- a/Source/Tools/GapiCodegen/GenerationInfo.cs +++ b/Source/Tools/GapiCodegen/GenerationInfo.cs @@ -253,10 +253,6 @@ namespace GtkSharp.Generation { if (isBlittable) return "sizeof( " + cstype + " )"; - // This is optimization based on https://github.com/GtkSharp/GtkSharp/pull/261#discussion_r673381869 - if (cstype == "bool") - return "sizeof( uint )"; - return "Marshal.SizeOf<" + cstype + ">()"; } }