From 06a0c76a81fc62f4bce3cbe18fd013ba30126006 Mon Sep 17 00:00:00 2001 From: Jeremy Koritzinsky Date: Fri, 19 Nov 2021 18:16:07 -0800 Subject: Update `GetPinnableReference()` usage in multi-step custom type marshalling (#61539) * Update GetPinnableReference() usage in multi-step custom tyep marshalling 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. * Apply suggestions from code review Co-authored-by: Aaron Robinson * Don't emit the assingment to the Value property in the Unmarshal stage when using [Out] * Fix the unmarshalling conditions * Fix Utf16StringMarshaller to correctly handle the "null pointer" case now that we're using spans internally for storage in all cases. Co-authored-by: Aaron Robinson --- .../tests/Ancillary.Interop/SpanMarshallers.cs | 28 +++++++--------------- 1 file changed, 9 insertions(+), 19 deletions(-) (limited to 'src/libraries/System.Runtime.InteropServices/tests/Ancillary.Interop') diff --git a/src/libraries/System.Runtime.InteropServices/tests/Ancillary.Interop/SpanMarshallers.cs b/src/libraries/System.Runtime.InteropServices/tests/Ancillary.Interop/SpanMarshallers.cs index 453259feeda..d5543df3c88 100644 --- a/src/libraries/System.Runtime.InteropServices/tests/Ancillary.Interop/SpanMarshallers.cs +++ b/src/libraries/System.Runtime.InteropServices/tests/Ancillary.Interop/SpanMarshallers.cs @@ -20,20 +20,8 @@ namespace System.Runtime.InteropServices.GeneratedMarshalling } public ReadOnlySpanMarshaller(ReadOnlySpan managed, int sizeOfNativeElement) + :this(managed, Span.Empty, sizeOfNativeElement) { - _allocatedMemory = default; - _sizeOfNativeElement = sizeOfNativeElement; - if (managed.Length == 0) - { - _managedSpan = default; - NativeValueStorage = default; - return; - } - _managedSpan = managed; - _sizeOfNativeElement = sizeOfNativeElement; - int spaceToAllocate = managed.Length * sizeOfNativeElement; - _allocatedMemory = Marshal.AllocCoTaskMem(spaceToAllocate); - NativeValueStorage = new Span((void*)_allocatedMemory, spaceToAllocate); } public ReadOnlySpanMarshaller(ReadOnlySpan managed, Span stackSpace, int sizeOfNativeElement) @@ -81,8 +69,7 @@ namespace System.Runtime.InteropServices.GeneratedMarshalling { get { - Debug.Assert(_managedSpan.IsEmpty || _allocatedMemory != IntPtr.Zero); - return (byte*)_allocatedMemory; + return (byte*)Unsafe.AsPointer(ref GetPinnableReference()); } set { @@ -222,7 +209,7 @@ namespace System.Runtime.InteropServices.GeneratedMarshalling { if (_inner.ManagedValues.Length == 0) { - return (byte*)0x1; + return (byte*)0xa5a5a5a5; } return _inner.Value; } @@ -293,7 +280,7 @@ namespace System.Runtime.InteropServices.GeneratedMarshalling { if (_inner.ManagedValues.Length == 0) { - return (byte*)0x1; + return (byte*)0xa5a5a5a5; } return _inner.Value; } @@ -369,8 +356,11 @@ namespace System.Runtime.InteropServices.GeneratedMarshalling { get { - Debug.Assert(_data.IsEmpty || _allocatedMemory != null); - return _allocatedMemory; + if (_allocatedMemory != null) + { + return _allocatedMemory; + } + return (T*)Unsafe.AsPointer(ref GetPinnableReference()); } set { -- cgit v1.2.3