You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Add ObjectiveCMarshal.GetOrCreateReferenceTrackingMemory API (#128508)
Fixes#128476
Adds `ObjectiveCMarshal.GetOrCreateReferenceTrackingMemory(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.
GetOrCreateReferenceTrackingMemory(object)` with full XML documentation
- Mono: stub throwing `NotImplementedException`
- Ref assembly updated
- Tests added to `ObjectiveCMarshalAPI`
---------
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Copy file name to clipboardExpand all lines: src/coreclr/nativeaot/System.Private.CoreLib/src/System/Runtime/InteropServices/ObjectiveCMarshal.NativeAot.cs
Copy file name to clipboardExpand all lines: src/libraries/System.Private.CoreLib/src/System/Runtime/InteropServices/ObjectiveC/ObjectiveCMarshal.PlatformNotSupported.cs
+28Lines changed: 28 additions & 0 deletions
Original file line number
Diff line number
Diff line change
@@ -94,6 +94,34 @@ public static GCHandle CreateReferenceTrackingHandle(
94
94
outSpan<IntPtr>taggedMemory)
95
95
=>thrownewPlatformNotSupportedException();
96
96
97
+
/// <summary>
98
+
/// Gets reference tracking memory for the supplied object.
99
+
/// </summary>
100
+
/// <param name="obj">The object whose tracking memory to return.</param>
101
+
/// <returns>A span of tracking memory associated with <paramref name="obj"/>.</returns>
102
+
/// <exception cref="InvalidOperationException">Thrown if the ObjectiveCMarshal API has not been initialized.</exception>
103
+
/// <remarks>
104
+
/// The Initialize() must be called prior to calling this function.
105
+
///
106
+
/// The <paramref name="obj"/> must have a type in its hierarchy marked with
107
+
/// <see cref="ObjectiveCTrackedTypeAttribute"/>.
108
+
///
109
+
/// The "Is Referenced" callback passed to <see cref="Initialize" />
110
+
/// will be passed the memory returned from this function.
111
+
/// The memory it points at is defined by the length in the <see cref="Span{IntPtr}"/> and
112
+
/// will be zeroed out. It will be available until <paramref name="obj"/> is collected by the GC.
113
+
/// The returned memory can be used for any purpose by the caller of this function and is usable
114
+
/// during the "Is Referenced" callback.
115
+
///
116
+
/// Calling this function multiple times with the same <paramref name="obj"/> will
117
+
/// return the same tracking memory. It is only guaranteed to be zero initialized on
118
+
/// the first call of this or <see cref="CreateReferenceTrackingHandle" />.
119
+
///
120
+
/// The return value is the same as the tracking memory returned from <see cref="CreateReferenceTrackingHandle" />.
0 commit comments