Skip to content

Make Utf16CharMarshaller pin ref parameters - #59700

Merged
elinor-fung merged 5 commits into
dotnet:feature/use-dllimport-generatorfrom
elinor-fung:pinRefChar
Sep 28, 2021
Merged

Make Utf16CharMarshaller pin ref parameters#59700
elinor-fung merged 5 commits into
dotnet:feature/use-dllimport-generatorfrom
elinor-fung:pinRefChar

Conversation

@elinor-fung

@elinor-fungelinor-fung commented Sep 28, 2021

Copy link
Copy Markdown
Member

Make our char marshaller pin ref/out instead of copy just the char to a local.

ref char parameters are being used as string buffers in parts of the libraries.

Before:

internalstaticunsafepartialcharMethod(charp,incharpIn,refcharcRef,outcharpOut){
unsafe
{ushort__p_gen_native=default;ushort__pIn_gen_native=default;ushort__cRef_gen_native=default;pOut=default;ushort__pOut_gen_native=default;char__retVal=default;ushort__retVal_gen_native=default;//// Marshal//__p_gen_native=p;__pIn_gen_native=pIn;__cRef_gen_native=cRef;__retVal_gen_native=__PInvoke__(__p_gen_native,&__pIn_gen_native,&__cRef_gen_native,&__pOut_gen_native);//// Unmarshal//__retVal=(char)__retVal_gen_native;pOut=(char)__pOut_gen_native;cRef=(char)__cRef_gen_native;return__retVal;}[System.Runtime.InteropServices.DllImportAttribute("DNE",EntryPoint="Method",CharSet=System.Runtime.InteropServices.CharSet.Unicode)]externstaticunsafeushort__PInvoke__(ushortp,ushort*pIn,ushort*cRef,ushort*pOut);}

After:

internalstaticunsafepartialcharMethod(charp,incharpIn,refcharcRef,outcharpOut){
unsafe
{ushort__pIn_gen_native=default;pOut=default;char__retVal=default;ushort__retVal_gen_native=default;//// Marshal//__pIn_gen_native=pIn;
fixed (char*cRef__pinned=&cRef){
fixed (char*pOut__pinned=&pOut)__retVal_gen_native=__PInvoke__((ushort)p,&__pIn_gen_native,(ushort*)cRef__pinned,(ushort*)pOut__pinned);}//// Unmarshal//__retVal=(char)__retVal_gen_native;return__retVal;}[System.Runtime.InteropServices.DllImportAttribute("DNE",EntryPoint="Method",CharSet=System.Runtime.InteropServices.CharSet.Unicode)]externstaticunsafeushort__PInvoke__(ushortp,ushort*pIn,ushort*cRef,ushort*pOut);}

cc @AaronRobinsonMSFT@jkoritzinsky

@AaronRobinsonMSFT

Copy link
Copy Markdown
Member

@elinor-fung The in char pIn is still a by-ref so it should also get the pinned treatment I believe.

@elinor-fung

Copy link
Copy Markdown
MemberAuthor

I didn't do it for in because one of the tests checks that the value doesn't get updated, but it looks like the test was doing the wrong thing (double-checked built-in). Will update.

@elinor-fung

Copy link
Copy Markdown
MemberAuthor

Updated. Also added something to the compat doc about in for blittable types and Unicode char.

Comment threaddocs/design/libraries/DllImportGenerator/Compatibility.md Outdated
Co-authored-by: Aaron Robinson <arobins@microsoft.com>
@elinor-fung
elinor-fung merged commit b7283ef into dotnet:feature/use-dllimport-generatorSep 28, 2021
@elinor-fung
elinor-fung deleted the pinRefChar branch September 28, 2021 22:13
@ghostghost locked as resolved and limited conversation to collaborators Nov 3, 2021
Sign up for freeto subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants

@elinor-fung@AaronRobinsonMSFT