-
Notifications
You must be signed in to change notification settings - Fork 4.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Update GetPinnableReference()
usage in multi-step custom type marshalling
#61539
Merged
jkoritzinsky
merged 6 commits into
dotnet:main
from
jkoritzinsky:getpinnablereference-struct-marshalling-design-update
Nov 20, 2021
Merged
Update GetPinnableReference()
usage in multi-step custom type marshalling
#61539
jkoritzinsky
merged 6 commits into
dotnet:main
from
jkoritzinsky:getpinnablereference-struct-marshalling-design-update
Nov 20, 2021
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
…ling Change GetPinnableReference() on a marshaller type to be used as a side effect when marshalling in all cases when a fixed() statement is usable. Use the Value property getter to get the value to pass to native in all cases. Update our marshallers and tests to follow this design update.
jkoritzinsky
added
area-System.Runtime.InteropServices
source-generator
Indicates an issue with a source generator feature
labels
Nov 13, 2021
AaronRobinsonMSFT
changed the title
Update GetPinnableReference() usage in multi-step custom tyep marshalling
Update Nov 13, 2021
GetPinnableReference()
usage in multi-step custom type marshalling
...ibraries/System.Runtime.InteropServices/gen/DllImportGenerator/AnalyzerReleases.Unshipped.md
Outdated
Show resolved
Hide resolved
...s/gen/Microsoft.Interop.SourceGeneration/Marshalling/ICustomNativeTypeMarshallingStrategy.cs
Show resolved
Hide resolved
Co-authored-by: Aaron Robinson <[email protected]>
This was referenced Nov 15, 2021
…e when using [Out]
Do you have a good before/after example for the generated code? |
Before: namespace DllImportGenerator.IntegrationTests
{
partial class NativeExportsNE
{
public unsafe partial class Arrays
{
[System.Runtime.CompilerServices.SkipLocalsInitAttribute]
public static partial int SumInArray(in int[] values, int numValues)
{
int __retVal;
//
// Setup
//
global::System.Runtime.InteropServices.GeneratedMarshalling.ArrayMarshaller<int> __values_gen_native__marshaller = default;
try
{
//
// Marshal
//
byte* __values_gen_native__marshaller__stackptr = stackalloc byte[global::System.Runtime.InteropServices.GeneratedMarshalling.ArrayMarshaller<int>.StackBufferSize];
__values_gen_native__marshaller = new(values, new System.Span<byte>(__values_gen_native__marshaller__stackptr, global::System.Runtime.InteropServices.GeneratedMarshalling.ArrayMarshaller<int>.StackBufferSize), sizeof(int));
__values_gen_native__marshaller.ManagedValues.CopyTo(System.Runtime.InteropServices.MemoryMarshal.Cast<byte, int>(__values_gen_native__marshaller.NativeValueStorage));
fixed (byte* __values_gen_native = &__values_gen_native__marshaller.GetPinnableReference())
__retVal = __PInvoke__(&__values_gen_native, numValues);
}
finally
{
//
// Cleanup
//
__values_gen_native__marshaller.FreeNative();
}
return __retVal;
//
// Local P/Invoke
//
[System.Runtime.InteropServices.DllImportAttribute("Microsoft.Interop.Tests.NativeExportsNE", EntryPoint = "sum_int_array_ref")]
extern static unsafe int __PInvoke__(byte** values, int numValues);
}
}
}
} After: namespace DllImportGenerator.IntegrationTests
{
partial class NativeExportsNE
{
public unsafe partial class Arrays
{
[System.Runtime.CompilerServices.SkipLocalsInitAttribute]
public static partial int SumInArray(in int[] values, int numValues)
{
byte* __values_gen_native = default;
int __retVal;
//
// Setup
//
global::System.Runtime.InteropServices.GeneratedMarshalling.ArrayMarshaller<int> __values_gen_native__marshaller = default;
try
{
//
// Marshal
//
byte* __values_gen_native__marshaller__stackptr = stackalloc byte[global::System.Runtime.InteropServices.GeneratedMarshalling.ArrayMarshaller<int>.StackBufferSize];
__values_gen_native__marshaller = new(values, new System.Span<byte>(__values_gen_native__marshaller__stackptr, global::System.Runtime.InteropServices.GeneratedMarshalling.ArrayMarshaller<int>.StackBufferSize), sizeof(int));
__values_gen_native__marshaller.ManagedValues.CopyTo(System.Runtime.InteropServices.MemoryMarshal.Cast<byte, int>(__values_gen_native__marshaller.NativeValueStorage));
fixed (byte* __values_gen_native__ignored = __values_gen_native__marshaller)
{
__values_gen_native = __values_gen_native__marshaller.Value;
{
__retVal = __PInvoke__(&__values_gen_native, numValues);
}
}
}
finally
{
//
// Cleanup
//
__values_gen_native__marshaller.FreeNative();
}
return __retVal;
//
// Local P/Invoke
//
[System.Runtime.InteropServices.DllImportAttribute("Microsoft.Interop.Tests.NativeExportsNE", EntryPoint = "sum_int_array_ref")]
extern static unsafe int __PInvoke__(byte** values, int numValues);
}
}
}
} |
jkoritzinsky
force-pushed
the
getpinnablereference-struct-marshalling-design-update
branch
from
November 16, 2021 21:37
893bc61
to
d66549a
Compare
… now that we're using spans internally for storage in all cases.
jkoritzinsky
force-pushed
the
getpinnablereference-struct-marshalling-design-update
branch
from
November 17, 2021 19:15
7e610c5
to
ada0693
Compare
…rence-struct-marshalling-design-update
AaronRobinsonMSFT
approved these changes
Nov 19, 2021
jkoritzinsky
deleted the
getpinnablereference-struct-marshalling-design-update
branch
November 20, 2021 02:16
This was referenced Nov 29, 2021
This was referenced Nov 30, 2021
ghost
locked as resolved and limited conversation to collaborators
Dec 20, 2021
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
Labels
area-System.Runtime.InteropServices
NO-SQUASH
The PR should not be squashed
source-generator
Indicates an issue with a source generator feature
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Change
GetPinnableReference()
on a marshaller type to be used as a side effect when marshalling in all cases when afixed()
statement is usable.Use the
Value
property getter to get the value to pass to native in all cases.Update our marshallers and tests to follow this design update. Also do some code cleanup along the way.
Fixes dotnet/runtimelab#1653 in two ways:
GetPinnableReference
can return a value that needs to be pinned that is not equal to the value that is passed to native code.fixed()
statement returned in the Pin stage (was required to implement 1.).