Browse Source

Sped up the generated C# by using sizeof instead of Marshal.SizeOf.

They are equivalent in our case because we only use blittable types.

Signed-off-by: Dimitar Dobrev <dpldobrev@protonmail.com>
pull/787/head
Dimitar Dobrev 8 years ago
parent
commit
9b7a372a3c
  1. 6
      src/Generator/Generators/CSharp/CSharpSources.cs

6
src/Generator/Generators/CSharp/CSharpSources.cs

@ -2010,7 +2010,7 @@ namespace CppSharp.Generators.CSharp @@ -2010,7 +2010,7 @@ namespace CppSharp.Generators.CSharp
copyCtorMethod.IsGenerated)
{
// Allocate memory for a new native object and call the ctor.
WriteLine($"var ret = Marshal.AllocHGlobal(Marshal.SizeOf(typeof({@internal})));");
WriteLine($"var ret = Marshal.AllocHGlobal(sizeof({@internal}));");
TypePrinter.PushContext(TypePrinterContextKind.Native);
WriteLine($"{@class.Visit(TypePrinter)}.{GetFunctionNativeIdentifier(copyCtorMethod)}(ret, new global::System.IntPtr(&native));",
@class.Visit(TypePrinter),
@ -2020,7 +2020,7 @@ namespace CppSharp.Generators.CSharp @@ -2020,7 +2020,7 @@ namespace CppSharp.Generators.CSharp
}
else
{
WriteLine($"var ret = Marshal.AllocHGlobal(Marshal.SizeOf(typeof({@internal})));");
WriteLine($"var ret = Marshal.AllocHGlobal(sizeof({@internal}));");
WriteLine($"*({@internal}*) ret = native;");
WriteLine("return ret.ToPointer();");
}
@ -2461,7 +2461,7 @@ namespace CppSharp.Generators.CSharp @@ -2461,7 +2461,7 @@ namespace CppSharp.Generators.CSharp
TypePrinter.PushContext(TypePrinterContextKind.Native);
var @internal = (@class.IsAbstractImpl ? @class.BaseClass : @class).Visit(TypePrinter);
TypePrinter.PopContext();
WriteLine($"{Helpers.InstanceIdentifier} = Marshal.AllocHGlobal(Marshal.SizeOf(typeof({@internal})));");
WriteLine($"{Helpers.InstanceIdentifier} = Marshal.AllocHGlobal(sizeof({@internal}));");
WriteLine($"{Helpers.OwnsNativeInstanceIdentifier} = true;");
WriteLine($"NativeToManagedMap[{Helpers.InstanceIdentifier}] = this;");

Loading…
Cancel
Save