Skip to content

Commit

Permalink
Remove the copying of returned values
Browse files Browse the repository at this point in the history
This improves performance and more importantly, fixes a memory leak caused by the original value not being destroyed.

Signed-off-by: Dimitar Dobrev <[email protected]>
  • Loading branch information
ddobrev committed Aug 21, 2019
1 parent 8815307 commit dde6d74
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 13 deletions.
9 changes: 7 additions & 2 deletions src/Generator/Generators/CSharp/CSharpMarshal.cs
Original file line number Diff line number Diff line change
Expand Up @@ -289,8 +289,13 @@ public override bool VisitClassDecl(Class @class)

if (returnType.IsAddress())
Context.Return.Write(HandleReturnedPointer(@class, qualifiedClass.Type));
else
Context.Return.Write($"{qualifiedClass}.{Helpers.CreateInstanceIdentifier}({Context.ReturnVarName})");
else if (Context.MarshalKind == MarshalKind.NativeField ||
Context.Function?.HasIndirectReturnTypeParameter == true ||
Context.Function?.OperatorKind == CXXOperatorKind.Subscript)
Context.Return.Write($@"{qualifiedClass}.{Helpers.CreateInstanceIdentifier}({
Context.ReturnVarName})");
else Context.Return.Write($@"{qualifiedClass}.{Helpers.CreateInstanceIdentifier}(new {
typePrinter.IntPtrType}(&{Context.ReturnVarName}))");

return true;
}
Expand Down
15 changes: 6 additions & 9 deletions src/Generator/Generators/CSharp/CSharpSources.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2808,6 +2808,7 @@ private void GenerateClassConstructor(Method method, Class @class)

var originalFunction = function.OriginalFunction ?? function;

var names = new List<string>();
if (originalFunction.HasIndirectReturnTypeParameter)
{
var indirectRetType = originalFunction.Parameters.First(
Expand All @@ -2825,8 +2826,8 @@ private void GenerateClassConstructor(Method method, Class @class)
Class retClass;
type.TryGetClass(out retClass);
var @class = retClass.OriginalClass ?? retClass;
WriteLine($@"var {Helpers.ReturnIdentifier} = new {
TypePrinter.PrintNative(@class)}();");
WriteLine($@"var {Helpers.ReturnIdentifier} = Marshal.AllocHGlobal({
@class.Layout.GetSize()});");
}
else
{
Expand All @@ -2839,13 +2840,15 @@ private void GenerateClassConstructor(Method method, Class @class)

WriteLine("{0} {1};", typeMap.CSharpSignatureType(typePrinterContext),
Helpers.ReturnIdentifier);
names.Add($"new IntPtr(&{Helpers.ReturnIdentifier})");
}
else
WriteLine("var {0} = {1};", construct);
}
if (names.Count == 0)
names.Add(Helpers.ReturnIdentifier);
}

var names = new List<string>();
foreach (var param in @params)
{
if (param.Param == operatorParam && needsInstance)
Expand All @@ -2862,12 +2865,6 @@ private void GenerateClassConstructor(Method method, Class @class)

var needsFixedThis = needsInstance && isValueType;

if (originalFunction.HasIndirectReturnTypeParameter)
{
var name = string.Format("new IntPtr(&{0})", Helpers.ReturnIdentifier);
names.Insert(0, name);
}

if (needsInstance)
{
var instanceIndex = GetInstanceParamIndex(method);
Expand Down
3 changes: 1 addition & 2 deletions src/Generator/Types/Std/Stdlib.cs
Original file line number Diff line number Diff line change
Expand Up @@ -382,8 +382,7 @@ public override void CSharpMarshalToManaged(CSharpMarshalContext ctx)
ctx.MarshalKind == MarshalKind.ReturnVariableArray;
ctx.Before.WriteLine($@"var {varBasicString} = {
basicString.Visit(typePrinter)}.{Helpers.CreateInstanceIdentifier}({
(usePointer ? string.Empty : $"new {typePrinter.IntPtrType}(&")}{
ctx.ReturnVarName}{(usePointer ? string.Empty : ")")});");
ctx.ReturnVarName});");
string @string = $"{qualifiedBasicString}Extensions.{data.Name}({varBasicString})";
if (usePointer)
{
Expand Down

0 comments on commit dde6d74

Please sign in to comment.