diff options
author | Jeremy Koritzinsky <jekoritz@microsoft.com> | 2021-11-20 05:16:07 +0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-11-20 05:16:07 +0300 |
commit | 06a0c76a81fc62f4bce3cbe18fd013ba30126006 (patch) | |
tree | b3773fd5a360d1490f37b100317f7fbc6d114c09 /src/libraries/System.Runtime.InteropServices/tests/Ancillary.Interop | |
parent | 87b92fdf5c377e12fc46062042e62b4c8cc6a651 (diff) |
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 <arobins@microsoft.com>
* 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 <arobins@microsoft.com>
Diffstat (limited to 'src/libraries/System.Runtime.InteropServices/tests/Ancillary.Interop')
-rw-r--r-- | src/libraries/System.Runtime.InteropServices/tests/Ancillary.Interop/SpanMarshallers.cs | 28 |
1 files changed, 9 insertions, 19 deletions
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<T> managed, int sizeOfNativeElement) + :this(managed, Span<byte>.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<byte>((void*)_allocatedMemory, spaceToAllocate); } public ReadOnlySpanMarshaller(ReadOnlySpan<T> managed, Span<byte> 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 { |