Browse Source

Merge pull request #770 from ha11owed/master

Let the marshal determine the size of the structure
pull/778/head
João Matos 9 years ago committed by GitHub
parent
commit
3d0e0fe303
  1. 31
      src/Generator/Generators/CSharp/CSharpSources.cs
  2. 1
      src/Generator/Options.cs

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

@ -561,8 +561,15 @@ namespace CppSharp.Generators.CSharp
public void GenerateClassInternals(Class @class) public void GenerateClassInternals(Class @class)
{ {
PushBlock(CSharpBlockKind.InternalsClass); PushBlock(CSharpBlockKind.InternalsClass);
WriteLine("[StructLayout(LayoutKind.Explicit, Size = {0})]", if (Options.GenerateSequentialLayout)
@class.Layout.Size); {
WriteLine("[StructLayout(LayoutKind.Sequential)]");
}
else
{
WriteLine("[StructLayout(LayoutKind.Explicit, Size = {0})]",
@class.Layout.Size);
}
GenerateClassInternalHead(@class); GenerateClassInternalHead(@class);
WriteStartBraceIndent(); WriteStartBraceIndent();
@ -772,7 +779,14 @@ namespace CppSharp.Generators.CSharp
PushBlock(CSharpBlockKind.Field); PushBlock(CSharpBlockKind.Field);
WriteLine("[FieldOffset({0})]", field.Offset); if (Options.GenerateSequentialLayout)
{
// For the sequential layout there is no need to set an offset
}
else
{
WriteLine("[FieldOffset({0})]", field.Offset);
}
TypePrinter.PushMarshalKind(MarshalKind.NativeField); TypePrinter.PushMarshalKind(MarshalKind.NativeField);
var fieldTypePrinted = field.QualifiedType.CSharpType(TypePrinter); var fieldTypePrinted = field.QualifiedType.CSharpType(TypePrinter);
@ -2126,7 +2140,7 @@ namespace CppSharp.Generators.CSharp
copyCtorMethod.IsGenerated) copyCtorMethod.IsGenerated)
{ {
// Allocate memory for a new native object and call the ctor. // Allocate memory for a new native object and call the ctor.
WriteLine("var ret = Marshal.AllocHGlobal({0});", @class.Layout.Size); WriteLine("var ret = Marshal.AllocHGlobal(Marshal.SizeOf(sizeof({0})));", @internal);
TypePrinter.PushContext(TypePrinterContextKind.Native); TypePrinter.PushContext(TypePrinterContextKind.Native);
WriteLine($"{@class.Visit(TypePrinter)}.{GetFunctionNativeIdentifier(copyCtorMethod)}(ret, new global::System.IntPtr(&native));", WriteLine($"{@class.Visit(TypePrinter)}.{GetFunctionNativeIdentifier(copyCtorMethod)}(ret, new global::System.IntPtr(&native));",
@class.Visit(TypePrinter), @class.Visit(TypePrinter),
@ -2136,7 +2150,7 @@ namespace CppSharp.Generators.CSharp
} }
else else
{ {
WriteLine("var ret = Marshal.AllocHGlobal({0});", @class.Layout.Size); WriteLine("var ret = Marshal.AllocHGlobal(Marshal.SizeOf(sizeof({0})));", @internal);
WriteLine("*({0}*) ret = native;", @internal); WriteLine("*({0}*) ret = native;", @internal);
WriteLine("return ret.ToPointer();"); WriteLine("return ret.ToPointer();");
} }
@ -2577,8 +2591,11 @@ namespace CppSharp.Generators.CSharp
private void GenerateClassConstructor(Method method, Class @class) private void GenerateClassConstructor(Method method, Class @class)
{ {
WriteLine("{0} = Marshal.AllocHGlobal({1});", Helpers.InstanceIdentifier, TypePrinter.PushContext(TypePrinterContextKind.Native);
@class.Layout.Size); var @internal = (@class.IsAbstractImpl ? @class.BaseClass : @class).Visit(TypePrinter);
TypePrinter.PopContext();
WriteLine("{0} = Marshal.AllocHGlobal(Marshal.SizeOf(sizeof({1})));", Helpers.InstanceIdentifier,
@internal);
WriteLine("{0} = true;", Helpers.OwnsNativeInstanceIdentifier); WriteLine("{0} = true;", Helpers.OwnsNativeInstanceIdentifier);
WriteLine("NativeToManagedMap[{0}] = this;", Helpers.InstanceIdentifier); WriteLine("NativeToManagedMap[{0}] = this;", Helpers.InstanceIdentifier);

1
src/Generator/Options.cs

@ -161,6 +161,7 @@ namespace CppSharp
public bool OutputInteropIncludes; public bool OutputInteropIncludes;
public bool GenerateFunctionTemplates; public bool GenerateFunctionTemplates;
public bool GenerateInternalImports; public bool GenerateInternalImports;
public bool GenerateSequentialLayout;
public bool UseHeaderDirectories; public bool UseHeaderDirectories;
/// <summary> /// <summary>

Loading…
Cancel
Save