Skip to content
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

Add support for null arrays in the blittable array marshaller's pinning optimization. #1063

Conversation

jkoritzinsky
Copy link
Member

Example generated code:

void Foo(int[] p)
{
   ref int __byref_p = ref (p == null ? ref System.Runtime.CompilerServices.Unsafe.NullRef<int>() : ref System.Runtime.InteropServices.MemoryMarshal.GetArrayDataReference(p));
   fixed (int* __p_gen_native= &p)
   {
        Foo__PInvoke__(__p_gen_native);
   }
}

Fixes #1051

@jkoritzinsky jkoritzinsky added the area-DllImportGenerator Source Generated stubs for P/Invokes in C# label May 3, 2021
@jkotas
Copy link
Member

jkotas commented May 3, 2021

ref System.Runtime.CompilerServices.Unsafe.NullRef<int>()

Cheaper way to create a null ref in this case is ref *(int*)0. It does not require inlined method call.

@jkoritzinsky jkoritzinsky merged commit c436fda into dotnet:feature/DllImportGenerator May 4, 2021
@jkoritzinsky jkoritzinsky deleted the null-blittable-array branch May 4, 2021 03:23
jkoritzinsky added a commit to jkoritzinsky/runtime that referenced this pull request Sep 20, 2021
…ng optimization. (dotnet/runtimelab#1063)

* Add support for null arrays in the blittable array marshaller's pinning optimization.

* Add comment block about the behavior.

* Use pointer casting instead of Unsafe.NullRef since we are already using pointers.

Commit migrated from dotnet/runtimelab@c436fda
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-DllImportGenerator Source Generated stubs for P/Invokes in C#
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants