Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 5.6k
Move the cache for the marshaler methods to be stored on the RuntimeType instead of in a ConditionalWeakTable#126621
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
Merged
Uh oh!
There was an error while loading. Please reload this page.
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
20eb643
Move the cache for the marshaler methods to be stored on the RuntimeT…
jkoritzinsky 8cc2f74
Only validate that the type has layout the first time we set up the l…
jkoritzinsky d6dc96a
Keep the delegates (less need for unsafe)
jkoritzinsky 7b317a7
Fix test failures.
jkoritzinsky eefb95d
Remove trailing whitespace
jkoritzinsky 288cd77
Fix failures
jkoritzinsky File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
98 changes: 57 additions & 41 deletions
98 src/coreclr/System.Private.CoreLib/src/System/Runtime/InteropServices/Marshal.CoreCLR.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -234,7 +234,7 @@ private static void PrelinkCore(MethodInfo m) | ||
| [MethodImpl(MethodImplOptions.InternalCall)] | ||
| public static extern int GetExceptionCode(); | ||
| internal sealed class LayoutTypeMarshalerMethods | ||
| internal sealed class LayoutTypeMarshalerMethods : RuntimeType.IGenericCacheEntry<LayoutTypeMarshalerMethods> | ||
| { | ||
| private static MemberInfo ConvertToUnmanagedMethod => field ??= typeof(BoxedLayoutTypeMarshaler<>).GetMethod(nameof(BoxedLayoutTypeMarshaler<object>.ConvertToUnmanaged), BindingFlags.Public | BindingFlags.Static)!; | ||
| private static MemberInfo ConvertToManagedMethod => field ??= typeof(BoxedLayoutTypeMarshaler<>).GetMethod(nameof(BoxedLayoutTypeMarshaler<object>.ConvertToManaged), BindingFlags.Public | BindingFlags.Static)!; | ||
| @@ -248,40 +248,54 @@ internal sealed class LayoutTypeMarshalerMethods | ||
| private readonly ConvertToManagedDelegate _convertToManaged; | ||
| private readonly FreeDelegate _free; | ||
| internal LayoutTypeMarshalerMethods(Type instantiatedType) | ||
| private readonly int _nativeSize; | ||
| internal LayoutTypeMarshalerMethods(Type instantiatedType, int nativeSize) | ||
| { | ||
| _convertToUnmanaged = ((MethodInfo)instantiatedType.GetMemberWithSameMetadataDefinitionAs(ConvertToUnmanagedMethod)).CreateDelegate<ConvertToUnmanagedDelegate>(); | ||
| _convertToManaged = ((MethodInfo)instantiatedType.GetMemberWithSameMetadataDefinitionAs(ConvertToManagedMethod)).CreateDelegate<ConvertToManagedDelegate>(); | ||
| _free = ((MethodInfo)instantiatedType.GetMemberWithSameMetadataDefinitionAs(FreeMethod)).CreateDelegate<FreeDelegate>(); | ||
| _nativeSize = nativeSize; | ||
| } | ||
| public unsafe void ConvertToManaged(object obj, byte* native, ref CleanupWorkListElement? cleanupWorkList) | ||
| public unsafe void ConvertToManaged(object obj, byte* native) | ||
| { | ||
| _convertToManaged(obj, native, ref cleanupWorkList); | ||
| _convertToManaged(obj, native, ref Unsafe.NullRef<CleanupWorkListElement?>()); | ||
| } | ||
| public unsafe void ConvertToUnmanaged(object obj, byte* native, int nativeSize, ref CleanupWorkListElement? cleanupWorkList) | ||
| public unsafe void ConvertToUnmanaged(object obj, byte* native, ref CleanupWorkListElement? cleanupWorkList) | ||
| { | ||
| _convertToUnmanaged(obj, native, nativeSize, ref cleanupWorkList); | ||
| _convertToUnmanaged(obj, native, _nativeSize, ref cleanupWorkList); | ||
| } | ||
| public unsafe void Free(object? obj, byte* native, int nativeSize, ref CleanupWorkListElement? cleanupWorkList) | ||
| public unsafe void Free(byte* native) | ||
| { | ||
| _free(obj, native, nativeSize, ref cleanupWorkList); | ||
| _free(null, native, _nativeSize, ref Unsafe.NullRef<CleanupWorkListElement?>()); | ||
| } | ||
| private static readonly ConditionalWeakTable<Type, LayoutTypeMarshalerMethods> s_marshalerCache = []; | ||
| internal static LayoutTypeMarshalerMethods GetMarshalMethodsForType(RuntimeType t) | ||
| { | ||
| return t.GetOrCreateCacheEntry<LayoutTypeMarshalerMethods>(); | ||
| } | ||
| [RequiresDynamicCode("Marshalling code for the object might not be available.")] | ||
| internal static LayoutTypeMarshalerMethods GetMarshalMethodsForType(Type t) | ||
| public static LayoutTypeMarshalerMethods Create(RuntimeType type) | ||
| { | ||
| return s_marshalerCache.GetOrAdd(t, CreateMarshalMethods); | ||
| if (!HasLayout(new QCallTypeHandle(ref type), out _, out int size)) | ||
| throw new ArgumentException(SR.Argument_MustHaveLayoutOrBeBlittable, nameof(type)); | ||
| static LayoutTypeMarshalerMethods CreateMarshalMethods(Type type) | ||
| { | ||
| Type instantiatedMarshaler = typeof(BoxedLayoutTypeMarshaler<>).MakeGenericType([type]); | ||
| return new LayoutTypeMarshalerMethods(instantiatedMarshaler); | ||
| } | ||
| Type instantiatedMarshaler = typeof(BoxedLayoutTypeMarshaler<>).MakeGenericType([type]); | ||
| return new LayoutTypeMarshalerMethods(instantiatedMarshaler, size); | ||
| } | ||
AaronRobinsonMSFT marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| public static ref LayoutTypeMarshalerMethods? GetStorageRef(RuntimeType.CompositeCacheEntry compositeEntry) | ||
| { | ||
| return ref compositeEntry._marshalerMethods; | ||
| } | ||
| public void InitializeCompositeCache(RuntimeType.CompositeCacheEntry compositeEntry) | ||
| { | ||
| compositeEntry._marshalerMethods = this; | ||
| } | ||
| } | ||
| @@ -306,23 +320,23 @@ public static unsafe void StructureToPtr(object structure, IntPtr ptr, bool fDel | ||
| if (type.IsGenericType) | ||
| throw new ArgumentException(SR.Argument_NeedNonGenericObject, nameof(structure)); | ||
| if (!HasLayout(new QCallTypeHandle(ref type), out bool isBlittable, out int size)) | ||
| throw new ArgumentException(SR.Argument_MustHaveLayoutOrBeBlittable, nameof(structure)); | ||
| if (isBlittable) | ||
| LayoutTypeMarshalerMethods methods; | ||
| try | ||
| { | ||
| SpanHelpers.Memmove(ref *(byte*)ptr, ref structure.GetRawData(), (nuint)size); | ||
| return; | ||
| methods = LayoutTypeMarshalerMethods.GetMarshalMethodsForType(type); | ||
| } | ||
| catch (ArgumentException e) | ||
| { | ||
| // COMPAT: preserve legacy ParamName for non-marshalable structure values. | ||
| throw new ArgumentException(e.Message, nameof(structure), e); | ||
| } | ||
| LayoutTypeMarshalerMethods methods = LayoutTypeMarshalerMethods.GetMarshalMethodsForType(type); | ||
| if (fDeleteOld) | ||
| { | ||
| methods.Free(structure, (byte*)ptr, size, ref Unsafe.NullRef<CleanupWorkListElement?>()); | ||
| methods.Free((byte*)ptr); | ||
| } | ||
| methods.ConvertToUnmanaged(structure, (byte*)ptr, size, ref Unsafe.NullRef<CleanupWorkListElement?>()); | ||
| methods.ConvertToUnmanaged(structure, (byte*)ptr, ref Unsafe.NullRef<CleanupWorkListElement?>()); | ||
| } | ||
| /// <summary> | ||
| @@ -335,18 +349,18 @@ private static unsafe void PtrToStructureHelper(IntPtr ptr, object structure, bo | ||
| if (!allowValueClasses && type.IsValueType) | ||
| throw new ArgumentException(SR.Argument_StructMustNotBeValueClass, nameof(structure)); | ||
| if (!HasLayout(new QCallTypeHandle(ref type), out bool isBlittable, out int size)) | ||
| throw new ArgumentException(SR.Argument_MustHaveLayoutOrBeBlittable, nameof(structure)); | ||
| if (isBlittable) | ||
| LayoutTypeMarshalerMethods methods; | ||
| try | ||
| { | ||
| SpanHelpers.Memmove(ref structure.GetRawData(), ref *(byte*)ptr, (nuint)size); | ||
| return; | ||
| methods = LayoutTypeMarshalerMethods.GetMarshalMethodsForType(type); | ||
| } | ||
| catch (ArgumentException e) | ||
| { | ||
| // COMPAT: preserve legacy ParamName for non-marshalable structure values. | ||
| throw new ArgumentException(e.Message, nameof(structure), e); | ||
| } | ||
| LayoutTypeMarshalerMethods methods = LayoutTypeMarshalerMethods.GetMarshalMethodsForType(type); | ||
| methods.ConvertToManaged(structure, (byte*)ptr, ref Unsafe.NullRef<CleanupWorkListElement?>()); | ||
| methods.ConvertToManaged(structure, (byte*)ptr); | ||
| } | ||
| /// <summary> | ||
| @@ -366,14 +380,16 @@ public static unsafe void DestroyStructure(IntPtr ptr, Type structuretype) | ||
| if (rt.IsGenericType) | ||
| throw new ArgumentException(SR.Argument_NeedNonGenericType, nameof(structuretype)); | ||
| if (!HasLayout(new QCallTypeHandle(ref rt), out bool isBlittable, out int size)) | ||
| throw new ArgumentException(SR.Argument_MustHaveLayoutOrBeBlittable, nameof(structuretype)); | ||
| if (!isBlittable) | ||
| try | ||
| { | ||
| LayoutTypeMarshalerMethods methods = LayoutTypeMarshalerMethods.GetMarshalMethodsForType(structuretype); | ||
| LayoutTypeMarshalerMethods methods = LayoutTypeMarshalerMethods.GetMarshalMethodsForType(rt); | ||
| methods.Free(null, (byte*)ptr, size, ref Unsafe.NullRef<CleanupWorkListElement?>()); | ||
| methods.Free((byte*)ptr); | ||
| } | ||
| catch (ArgumentException) | ||
| { | ||
| // COMPAT: rethrow the argument exception with the correct argument name. | ||
| throw new ArgumentException(SR.Argument_MustHaveLayoutOrBeBlittable, nameof(structuretype)); | ||
| } | ||
| } | ||
1 change: 1 addition & 0 deletions
1 src/coreclr/System.Private.CoreLib/src/System/RuntimeType.GenericCache.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
38 changes: 14 additions & 24 deletions
38 src/coreclr/System.Private.CoreLib/src/System/StubHelpers.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1457,8 +1457,11 @@ private static void FreeCore(ref T managed, byte* unmanaged, ref CleanupWorkList | ||
| public static void Free(ref T managed, byte* unmanaged, int nativeSize, ref CleanupWorkListElement? cleanupWorkList) | ||
| { | ||
| FreeCore(ref managed, unmanaged, ref cleanupWorkList); | ||
| NativeMemory.Clear(unmanaged, (nuint)nativeSize); | ||
| if (unmanaged != null) | ||
| { | ||
| FreeCore(ref managed, unmanaged, ref cleanupWorkList); | ||
| NativeMemory.Clear(unmanaged, (nuint)nativeSize); | ||
| } | ||
| } | ||
| } | ||
| @@ -1597,8 +1600,11 @@ static void CallFree(T? managed, byte* unmanaged, ref CleanupWorkListElement? cl | ||
| public static void Free(T? managed, byte* unmanaged, int nativeSize, ref CleanupWorkListElement? cleanupWorkList) | ||
| { | ||
| FreeCore(managed, unmanaged, ref cleanupWorkList); | ||
| NativeMemory.Clear(unmanaged, (nuint)nativeSize); | ||
| if (unmanaged != null) | ||
| { | ||
| FreeCore(managed, unmanaged, ref cleanupWorkList); | ||
| NativeMemory.Clear(unmanaged, (nuint)nativeSize); | ||
| } | ||
| } | ||
| } | ||
| @@ -1947,21 +1953,13 @@ internal static void CheckStringLength(uint length) | ||
| internal static unsafe void LayoutTypeConvertToUnmanaged(object obj, byte* pNative, ref CleanupWorkListElement? pCleanupWorkList) | ||
| { | ||
| RuntimeType type = (RuntimeType)obj.GetType(); | ||
| bool hasLayout = Marshal.HasLayout(new QCallTypeHandle(ref type), out bool isBlittable, out int size); | ||
| Debug.Assert(hasLayout); | ||
| if (isBlittable) | ||
| { | ||
| SpanHelpers.Memmove(ref *pNative, ref obj.GetRawData(), (nuint)size); | ||
| return; | ||
| } | ||
| Marshal.LayoutTypeMarshalerMethods methods = Marshal.LayoutTypeMarshalerMethods.GetMarshalMethodsForType(type); | ||
| methods.ConvertToUnmanaged(obj, pNative, size, ref pCleanupWorkList); | ||
| methods.ConvertToUnmanaged(obj, pNative, ref pCleanupWorkList); | ||
| } | ||
AaronRobinsonMSFT marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| [UnmanagedCallersOnly] | ||
| [RequiresUnsafe] | ||
| internal static unsafe void LayoutTypeConvertToUnmanaged(object* obj, byte* pNative, Exception* pException) | ||
| { | ||
| try | ||
| @@ -1978,21 +1976,13 @@ internal static unsafe void LayoutTypeConvertToUnmanaged(object* obj, byte* pNat | ||
| internal static unsafe void LayoutTypeConvertToManaged(object obj, byte* pNative) | ||
| { | ||
| RuntimeType type = (RuntimeType)obj.GetType(); | ||
| bool hasLayout = Marshal.HasLayout(new QCallTypeHandle(ref type), out bool isBlittable, out int size); | ||
| Debug.Assert(hasLayout); | ||
| if (isBlittable) | ||
| { | ||
| SpanHelpers.Memmove(ref obj.GetRawData(), ref *pNative, (nuint)size); | ||
| return; | ||
| } | ||
| Marshal.LayoutTypeMarshalerMethods methods = Marshal.LayoutTypeMarshalerMethods.GetMarshalMethodsForType(type); | ||
| methods.ConvertToManaged(obj, pNative, ref Unsafe.NullRef<CleanupWorkListElement?>()); | ||
| methods.ConvertToManaged(obj, pNative); | ||
| } | ||
| [UnmanagedCallersOnly] | ||
| [RequiresUnsafe] | ||
| internal static unsafe void LayoutTypeConvertToManaged(object* obj, byte* pNative, Exception* pException) | ||
| { | ||
| try | ||
Oops, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.