Skip to content

Add ObjectiveCMarshal.GetOrCreateTaggedMemory API#128508

Draft
AaronRobinsonMSFT wants to merge 2 commits into
dotnet:mainfrom
AaronRobinsonMSFT:AaronRobinsonMSFT/objcmarshal-get-or-create-tagged-memory
Draft

Add ObjectiveCMarshal.GetOrCreateTaggedMemory API#128508
AaronRobinsonMSFT wants to merge 2 commits into
dotnet:mainfrom
AaronRobinsonMSFT:AaronRobinsonMSFT/objcmarshal-get-or-create-tagged-memory

Conversation

@AaronRobinsonMSFT
Copy link
Copy Markdown
Member

@AaronRobinsonMSFT AaronRobinsonMSFT commented May 22, 2026

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 to CreateReferenceTrackingHandle when only the tagged memory is needed and no reference tracking handle is required at that point in time.

Changes

  • Added public API ObjectiveCMarshal.GetOrCreateTaggedMemory(object) with full XML documentation
  • CoreCLR: new ObjCMarshal_GetOrCreateTaggedMemory QCall backed by a shared AllocTaggedMemoryForObject helper that both QCalls use to eliminate duplicate logic
  • NativeAOT: CreateReferenceTrackingHandleInternal delegates to GetOrCreateTaggedMemoryInternal and appends the handle allocation
  • Mono: stub throwing NotImplementedException
  • PlatformNotSupported stub
  • Ref assembly updated
  • Tests added to ObjectiveCMarshalAPI

AaronRobinsonMSFT and others added 2 commits May 22, 2026 15:59
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>
Copilot AI review requested due to automatic review settings May 22, 2026 23:34
@AaronRobinsonMSFT AaronRobinsonMSFT added this to the 11.0.0 milestone May 22, 2026
@AaronRobinsonMSFT AaronRobinsonMSFT changed the title Add ObjectiveCMarshal.GetOrCreateTaggedMemory API Add ObjectiveCMarshal.GetOrCreateTaggedMemory API May 22, 2026
@dotnet-policy-service
Copy link
Copy Markdown
Contributor

Tagging subscribers to this area: @dotnet/interop-contrib
See info in area-owners.md if you want to be subscribed.

@AaronRobinsonMSFT AaronRobinsonMSFT added the NO-MERGE The PR is not ready for merge yet (see discussion for detailed reasons) label May 22, 2026
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 in ObjectiveCMarshal.
  • CoreCLR: add ObjCMarshal_GetOrCreateTaggedMemory QCall and a shared helper to reuse the tagged-memory allocation logic.
  • Tests: extend ObjectiveCMarshalAPI coverage 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"));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area-System.Runtime.InteropServices NO-MERGE The PR is not ready for merge yet (see discussion for detailed reasons)

Projects

Status: No status

Development

Successfully merging this pull request may close these issues.

[API Proposal]: Add ObjectiveCMarshal.GetTaggedMemory

2 participants