Skip to content

Commit

Permalink
Don't create copies of indirectly 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 <dpldobrev@protonmail.com>
  • Loading branch information
ddobrev committed Aug 20, 2019
1 parent 8815307 commit a417467
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 11 deletions.
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 @@ public void GenerateFunctionCall(string functionName, List<Parameter> parameters

var originalFunction = function.OriginalFunction ?? function;

var names = new List<string>();
if (originalFunction.HasIndirectReturnTypeParameter)
{
var indirectRetType = originalFunction.Parameters.First(
Expand All @@ -2825,8 +2826,8 @@ public void GenerateFunctionCall(string functionName, List<Parameter> parameters
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 @@ public void GenerateFunctionCall(string functionName, List<Parameter> parameters

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 @@ public void GenerateFunctionCall(string functionName, List<Parameter> parameters

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 a417467

Please sign in to comment.