From c6cf78036bd12f3895e4c7f1f4938da60c8959a0 Mon Sep 17 00:00:00 2001 From: carlossanlop <1175054+carlossanlop@users.noreply.github.com> Date: Tue, 5 Jul 2022 15:15:15 -0700 Subject: [PATCH 1/2] Fix S.R.InteropServices remarks --- .../Marshalling/AnsiStringMarshaller.cs | 10 +--------- .../Marshalling/ArrayMarshaller.cs | 18 +----------------- .../Marshalling/BStrStringMarshaller.cs | 10 +--------- .../Marshalling/CustomTypeMarshallerKind.cs | 2 -- .../Marshalling/PointerArrayMarshaller.cs | 18 +----------------- .../Marshalling/Utf16StringMarshaller.cs | 8 -------- .../Marshalling/Utf8StringMarshaller.cs | 10 +--------- 7 files changed, 5 insertions(+), 71 deletions(-) diff --git a/src/libraries/System.Private.CoreLib/src/System/Runtime/InteropServices/Marshalling/AnsiStringMarshaller.cs b/src/libraries/System.Private.CoreLib/src/System/Runtime/InteropServices/Marshalling/AnsiStringMarshaller.cs index 6d729263bda5e0..5baea94b9372d4 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Runtime/InteropServices/Marshalling/AnsiStringMarshaller.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Runtime/InteropServices/Marshalling/AnsiStringMarshaller.cs @@ -32,8 +32,8 @@ public AnsiStringMarshaller(string? str) /// /// The must not be movable - that is, it should not be /// on the managed heap or it should be pinned. - /// /// + /// public AnsiStringMarshaller(string? str, Span buffer) { _allocated = false; @@ -65,18 +65,14 @@ public AnsiStringMarshaller(string? str, Span buffer) /// /// Returns the native value representing the string. /// - /// /// - /// public byte* ToNativeValue() => _nativeValue; /// /// Sets the native value representing the string. /// /// The native value. - /// /// - /// public void FromNativeValue(byte* value) { _nativeValue = value; @@ -86,17 +82,13 @@ public void FromNativeValue(byte* value) /// /// Returns the managed string. /// - /// /// - /// public string? ToManaged() => Marshal.PtrToStringAnsi((IntPtr)_nativeValue); /// /// Frees native resources. /// - /// /// - /// public void FreeNative() { if (_allocated) diff --git a/src/libraries/System.Private.CoreLib/src/System/Runtime/InteropServices/Marshalling/ArrayMarshaller.cs b/src/libraries/System.Private.CoreLib/src/System/Runtime/InteropServices/Marshalling/ArrayMarshaller.cs index ac65ab279cc1eb..07386d0864c62d 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Runtime/InteropServices/Marshalling/ArrayMarshaller.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Runtime/InteropServices/Marshalling/ArrayMarshaller.cs @@ -50,8 +50,8 @@ public ArrayMarshaller(T[]? array, int sizeOfNativeElement) /// /// The must not be movable - that is, it should not be /// on the managed heap or it should be pinned. - /// /// + /// public ArrayMarshaller(T[]? array, Span buffer, int sizeOfNativeElement) { _allocatedMemory = default; @@ -83,9 +83,7 @@ public ArrayMarshaller(T[]? array, Span buffer, int sizeOfNativeElement) /// Gets a span that points to the memory where the managed values of the array are stored. /// /// Span over managed values of the array. - /// /// - /// public ReadOnlySpan GetManagedValuesSource() => _managedArray; /// @@ -93,9 +91,7 @@ public ArrayMarshaller(T[]? array, Span buffer, int sizeOfNativeElement) /// /// Length of the array. /// Span where managed values of the array should be stored. - /// /// - /// public Span GetManagedValuesDestination(int length) => _allocatedMemory == IntPtr.Zero ? null : _managedArray = new T[length]; /// @@ -103,9 +99,7 @@ public ArrayMarshaller(T[]? array, Span buffer, int sizeOfNativeElement) /// /// Length of the array. /// Span over the native values of the array. - /// /// - /// public ReadOnlySpan GetNativeValuesSource(int length) { if (_allocatedMemory == IntPtr.Zero) @@ -120,9 +114,7 @@ public ReadOnlySpan GetNativeValuesSource(int length) /// Returns a span that points to the memory where the native values of the array should be stored. /// /// Span where native values of the array should be stored. - /// /// - /// public Span GetNativeValuesDestination() => _span; /// @@ -133,18 +125,14 @@ public ReadOnlySpan GetNativeValuesSource(int length) /// /// Returns the native value representing the array. /// - /// /// - /// public byte* ToNativeValue() => (byte*)Unsafe.AsPointer(ref GetPinnableReference()); /// /// Sets the native value representing the array. /// /// The native value. - /// /// - /// public void FromNativeValue(byte* value) { _allocatedMemory = (IntPtr)value; @@ -153,17 +141,13 @@ public void FromNativeValue(byte* value) /// /// Returns the managed array. /// - /// /// - /// public T[]? ToManaged() => _managedArray; /// /// Frees native resources. /// - /// /// - /// public void FreeNative() { Marshal.FreeCoTaskMem(_allocatedMemory); diff --git a/src/libraries/System.Private.CoreLib/src/System/Runtime/InteropServices/Marshalling/BStrStringMarshaller.cs b/src/libraries/System.Private.CoreLib/src/System/Runtime/InteropServices/Marshalling/BStrStringMarshaller.cs index ce5a83a69e1abb..4f246a26bc3265 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Runtime/InteropServices/Marshalling/BStrStringMarshaller.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Runtime/InteropServices/Marshalling/BStrStringMarshaller.cs @@ -34,8 +34,8 @@ public BStrStringMarshaller(string? str) /// /// The must not be movable - that is, it should not be /// on the managed heap or it should be pinned. - /// /// + /// public BStrStringMarshaller(string? str, Span buffer) { _allocated = false; @@ -78,18 +78,14 @@ public BStrStringMarshaller(string? str, Span buffer) /// /// Returns the native value representing the string. /// - /// /// - /// public void* ToNativeValue() => _ptrToFirstChar; /// /// Sets the native value representing the string. /// /// The native value. - /// /// - /// public void FromNativeValue(void* value) { _ptrToFirstChar = value; @@ -99,9 +95,7 @@ public void FromNativeValue(void* value) /// /// Returns the managed string. /// - /// /// - /// public string? ToManaged() { if (_ptrToFirstChar is null) @@ -113,9 +107,7 @@ public void FromNativeValue(void* value) /// /// Frees native resources. /// - /// /// - /// public void FreeNative() { if (_allocated) diff --git a/src/libraries/System.Private.CoreLib/src/System/Runtime/InteropServices/Marshalling/CustomTypeMarshallerKind.cs b/src/libraries/System.Private.CoreLib/src/System/Runtime/InteropServices/Marshalling/CustomTypeMarshallerKind.cs index f19d59b0fbe1cf..02cba782fd6e08 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Runtime/InteropServices/Marshalling/CustomTypeMarshallerKind.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Runtime/InteropServices/Marshalling/CustomTypeMarshallerKind.cs @@ -10,9 +10,7 @@ namespace System.Runtime.InteropServices.Marshalling /// /// The shape of a custom type marshaller for usage in source-generated interop scenarios. /// - /// /// - /// public enum CustomTypeMarshallerKind { /// diff --git a/src/libraries/System.Private.CoreLib/src/System/Runtime/InteropServices/Marshalling/PointerArrayMarshaller.cs b/src/libraries/System.Private.CoreLib/src/System/Runtime/InteropServices/Marshalling/PointerArrayMarshaller.cs index 18e1bd162847e7..03e2f3d2459262 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Runtime/InteropServices/Marshalling/PointerArrayMarshaller.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Runtime/InteropServices/Marshalling/PointerArrayMarshaller.cs @@ -50,8 +50,8 @@ public PointerArrayMarshaller(T*[]? array, int sizeOfNativeElement) /// /// The must not be movable - that is, it should not be /// on the managed heap or it should be pinned. - /// /// + /// public PointerArrayMarshaller(T*[]? array, Span buffer, int sizeOfNativeElement) { _allocatedMemory = default; @@ -83,9 +83,7 @@ public PointerArrayMarshaller(T*[]? array, Span buffer, int sizeOfNativeEl /// Gets a span that points to the memory where the managed values of the array are stored. /// /// Span over managed values of the array. - /// /// - /// public ReadOnlySpan GetManagedValuesSource() => Unsafe.As(_managedArray); /// @@ -93,9 +91,7 @@ public PointerArrayMarshaller(T*[]? array, Span buffer, int sizeOfNativeEl /// /// Length of the array. /// Span where managed values of the array should be stored. - /// /// - /// public Span GetManagedValuesDestination(int length) { if (_allocatedMemory == IntPtr.Zero) @@ -110,9 +106,7 @@ public Span GetManagedValuesDestination(int length) /// /// Length of the array. /// Span over the native values of the array. - /// /// - /// public ReadOnlySpan GetNativeValuesSource(int length) { if (_allocatedMemory == IntPtr.Zero) @@ -127,9 +121,7 @@ public ReadOnlySpan GetNativeValuesSource(int length) /// Returns a span that points to the memory where the native values of the array should be stored. /// /// Span where native values of the array should be stored. - /// /// - /// public Span GetNativeValuesDestination() => _span; /// @@ -140,34 +132,26 @@ public ReadOnlySpan GetNativeValuesSource(int length) /// /// Returns the native value representing the array. /// - /// /// - /// public byte* ToNativeValue() => (byte*)Unsafe.AsPointer(ref GetPinnableReference()); /// /// Sets the native value representing the array. /// /// The native value. - /// /// - /// public void FromNativeValue(byte* value) => _allocatedMemory = (IntPtr)value; /// /// Returns the managed array. /// - /// /// - /// public T*[]? ToManaged() => _managedArray; /// /// Frees native resources. /// - /// /// - /// public void FreeNative() { Marshal.FreeCoTaskMem(_allocatedMemory); diff --git a/src/libraries/System.Private.CoreLib/src/System/Runtime/InteropServices/Marshalling/Utf16StringMarshaller.cs b/src/libraries/System.Private.CoreLib/src/System/Runtime/InteropServices/Marshalling/Utf16StringMarshaller.cs index 04ada6d9d5132f..22d317a9cb17d2 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Runtime/InteropServices/Marshalling/Utf16StringMarshaller.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Runtime/InteropServices/Marshalling/Utf16StringMarshaller.cs @@ -31,34 +31,26 @@ public Utf16StringMarshaller(string? str) /// /// Returns the native value representing the string. /// - /// /// - /// public void* ToNativeValue() => _nativeValue; /// /// Sets the native value representing the string. /// /// The native value. - /// /// - /// public void FromNativeValue(void* value) => _nativeValue = value; /// /// Returns the managed string. /// - /// /// - /// public string? ToManaged() => Marshal.PtrToStringUni((IntPtr)_nativeValue); /// /// Frees native resources. /// - /// /// - /// public void FreeNative() { Marshal.FreeCoTaskMem((IntPtr)_nativeValue); diff --git a/src/libraries/System.Private.CoreLib/src/System/Runtime/InteropServices/Marshalling/Utf8StringMarshaller.cs b/src/libraries/System.Private.CoreLib/src/System/Runtime/InteropServices/Marshalling/Utf8StringMarshaller.cs index e60db5416dfa64..955e6b7b62f70f 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Runtime/InteropServices/Marshalling/Utf8StringMarshaller.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Runtime/InteropServices/Marshalling/Utf8StringMarshaller.cs @@ -33,8 +33,8 @@ public Utf8StringMarshaller(string? str) /// /// The must not be movable - that is, it should not be /// on the managed heap or it should be pinned. - /// /// + /// public Utf8StringMarshaller(string? str, Span buffer) { _allocated = false; @@ -69,18 +69,14 @@ public Utf8StringMarshaller(string? str, Span buffer) /// /// Returns the native value representing the string. /// - /// /// - /// public byte* ToNativeValue() => _nativeValue; /// /// Sets the native value representing the string. /// /// The native value. - /// /// - /// public void FromNativeValue(byte* value) { _nativeValue = value; @@ -90,17 +86,13 @@ public void FromNativeValue(byte* value) /// /// Returns the managed string. /// - /// /// - /// public string? ToManaged() => Marshal.PtrToStringUTF8((IntPtr)_nativeValue); /// /// Frees native resources. /// - /// /// - /// public void FreeNative() { if (_allocated) From 7d0e198608def6ae6ebbcbcedc859a2b10f7f7c2 Mon Sep 17 00:00:00 2001 From: carlossanlop <1175054+carlossanlop@users.noreply.github.com> Date: Tue, 5 Jul 2022 15:29:47 -0700 Subject: [PATCH 2/2] One more --- .../Marshalling/CustomTypeMarshallerAttribute.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/libraries/System.Private.CoreLib/src/System/Runtime/InteropServices/Marshalling/CustomTypeMarshallerAttribute.cs b/src/libraries/System.Private.CoreLib/src/System/Runtime/InteropServices/Marshalling/CustomTypeMarshallerAttribute.cs index 8017d3d667b40e..1bdad6fbaedc09 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Runtime/InteropServices/Marshalling/CustomTypeMarshallerAttribute.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Runtime/InteropServices/Marshalling/CustomTypeMarshallerAttribute.cs @@ -9,8 +9,8 @@ namespace System.Runtime.InteropServices.Marshalling /// /// This attribute is recognized by the runtime-provided source generators for source-generated interop scenarios. /// It is not used by the interop marshalling system at runtime. - /// /// + /// [AttributeUsage(AttributeTargets.Struct)] public sealed class CustomTypeMarshallerAttribute : Attribute {