Add ObjectiveCMarshal.GetOrCreateTaggedMemory API#128508
Draft
AaronRobinsonMSFT wants to merge 2 commits into
Draft
Add ObjectiveCMarshal.GetOrCreateTaggedMemory API#128508AaronRobinsonMSFT wants to merge 2 commits into
ObjectiveCMarshal.GetOrCreateTaggedMemory API#128508AaronRobinsonMSFT wants to merge 2 commits into
Conversation
Adds ObjectiveCMarshal.GetOrCreateTaggedMemory(object) which returns the tagged memory span for an object without creating a GCHandle. This is a more efficient alternative when only the tagged memory is needed and no reference tracking handle is required. Implements the API across CoreCLR, NativeAOT, Mono, and the PlatformNotSupported stub. The CoreCLR native implementation shares logic with CreateReferenceTrackingHandle via a common helper. Fixes dotnet#128476 Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
ObjectiveCMarshal.GetOrCreateTaggedMemory API
Contributor
|
Tagging subscribers to this area: @dotnet/interop-contrib |
Contributor
There was a problem hiding this comment.
Pull request overview
This PR introduces a new public System.Runtime.InteropServices.ObjectiveC.ObjectiveCMarshal.GetOrCreateTaggedMemory(object) API and wires it up across CoreCLR (new QCall), NativeAOT (shared internal implementation), Mono (stub), plus adds interop tests to validate behavior parity with CreateReferenceTrackingHandle.
Changes:
- Add the new public API to the contract (
ref/) and to the managed implementation inObjectiveCMarshal. - CoreCLR: add
ObjCMarshal_GetOrCreateTaggedMemoryQCall and a shared helper to reuse the tagged-memory allocation logic. - Tests: extend
ObjectiveCMarshalAPIcoverage for invalid args, pre-initialize behavior, and memory identity/zero-init semantics.
Show a summary per file
| File | Description |
|---|---|
| src/tests/Interop/ObjectiveC/ObjectiveCMarshalAPI/Program.cs | Adds new test coverage for GetOrCreateTaggedMemory and reorganizes pre-initialize validation. |
| src/mono/System.Private.CoreLib/src/System/Runtime/InteropServices/ObjectiveCMarshal.Mono.cs | Adds a Mono internal stub for the new API. |
| src/libraries/System.Runtime.InteropServices/ref/System.Runtime.InteropServices.cs | Adds the new public API to the reference contract. |
| src/libraries/System.Private.CoreLib/src/System/Runtime/InteropServices/ObjectiveC/ObjectiveCMarshal.PlatformNotSupported.cs | Adds PNSE stub for the new public method. |
| src/libraries/System.Private.CoreLib/src/System/Runtime/InteropServices/ObjectiveC/ObjectiveCMarshal.cs | Adds the new public managed entrypoint and XML docs. |
| src/coreclr/vm/qcallentrypoints.cpp | Registers the new Objective-C marshal QCall entrypoint. |
| src/coreclr/vm/interoplibinterface.h | Declares the new QCall in the VM ↔ interop interface. |
| src/coreclr/vm/interoplibinterface_objc.cpp | Implements the new QCall and factors shared tagged-memory logic into a helper. |
| src/coreclr/System.Private.CoreLib/src/System/Runtime/InteropServices/ObjectiveCMarshal.CoreCLR.cs | Adds the new LibraryImport for the QCall. |
| src/coreclr/nativeaot/System.Private.CoreLib/src/System/Runtime/InteropServices/ObjectiveCMarshal.NativeAot.cs | Factors NativeAOT implementation so handle creation reuses the tagged-memory path. |
Copilot's findings
- Files reviewed: 10/10 changed files
- Comments generated: 4
| public static GCHandle CreateReferenceTrackingHandle( | ||
| object obj, | ||
| out System.Span<System.IntPtr> taggedMemory) => throw null; | ||
| public static System.Span<System.IntPtr> GetOrCreateTaggedMemory(object obj) => throw null; |
| @@ -94,6 +94,9 @@ public static GCHandle CreateReferenceTrackingHandle( | |||
| out Span<IntPtr> taggedMemory) | |||
| => throw new PlatformNotSupportedException(); | |||
|
|
|||
Comment on lines
+62
to
+66
| void* TaggedMemoryForObjectHelper( | ||
| _In_ QCall::ObjectHandleOnStack obj, | ||
| _Out_ size_t* memInSizeT, | ||
| _Out_opt_ OBJECTHANDLE* instHandle) | ||
| { |
Comment on lines
+76
to
+78
| // The reference tracking system must be initialized. | ||
| if (!g_ReferenceTrackerInitialized) | ||
| COMPlusThrow(kInvalidOperationException, W("InvalidOperation_ObjectiveCMarshalNotInitialized")); |
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Fixes #128476
Adds
ObjectiveCMarshal.GetOrCreateTaggedMemory(object)which returns the tagged memory span for an object without creating a GCHandle. This is a more efficient alternative toCreateReferenceTrackingHandlewhen only the tagged memory is needed and no reference tracking handle is required at that point in time.Changes
ObjectiveCMarshal.GetOrCreateTaggedMemory(object)with full XML documentationObjCMarshal_GetOrCreateTaggedMemoryQCall backed by a sharedAllocTaggedMemoryForObjecthelper that both QCalls use to eliminate duplicate logicCreateReferenceTrackingHandleInternaldelegates toGetOrCreateTaggedMemoryInternaland appends the handle allocationNotImplementedExceptionObjectiveCMarshalAPI