diff --git a/Microsoft.Toolkit.HighPerformance/Buffers/Internals/RawObjectMemoryManager{T}.cs b/Microsoft.Toolkit.HighPerformance/Buffers/Internals/RawObjectMemoryManager{T}.cs
index 89ce81ea208..1a173c7ce3b 100644
--- a/Microsoft.Toolkit.HighPerformance/Buffers/Internals/RawObjectMemoryManager{T}.cs
+++ b/Microsoft.Toolkit.HighPerformance/Buffers/Internals/RawObjectMemoryManager{T}.cs
@@ -9,6 +9,7 @@
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using Microsoft.Toolkit.HighPerformance.Extensions;
+using Microsoft.Toolkit.HighPerformance.Helpers;
namespace Microsoft.Toolkit.HighPerformance.Buffers.Internals
{
@@ -49,7 +50,7 @@ public RawObjectMemoryManager(object instance, IntPtr offset, int length)
///
public override Span GetSpan()
{
- ref T r0 = ref this.instance.DangerousGetObjectDataReferenceAt(this.offset);
+ ref T r0 = ref ObjectMarshal.DangerousGetObjectDataReferenceAt(this.instance, this.offset);
return MemoryMarshal.CreateSpan(ref r0, this.length);
}
@@ -68,7 +69,7 @@ public override unsafe MemoryHandle Pin(int elementIndex = 0)
// traditional means (eg. via the implicit T[] array conversion), if T is a
// reference type or a type containing some references.
GCHandle handle = GCHandle.Alloc(this.instance, GCHandleType.Pinned);
- ref T r0 = ref this.instance.DangerousGetObjectDataReferenceAt(this.offset);
+ ref T r0 = ref ObjectMarshal.DangerousGetObjectDataReferenceAt(this.instance, this.offset);
ref T r1 = ref Unsafe.Add(ref r0, (nint)(uint)elementIndex);
void* p = Unsafe.AsPointer(ref r1);
diff --git a/Microsoft.Toolkit.HighPerformance/Extensions/ArrayExtensions.1D.cs b/Microsoft.Toolkit.HighPerformance/Extensions/ArrayExtensions.1D.cs
index 75b292d4208..6f851c7a968 100644
--- a/Microsoft.Toolkit.HighPerformance/Extensions/ArrayExtensions.1D.cs
+++ b/Microsoft.Toolkit.HighPerformance/Extensions/ArrayExtensions.1D.cs
@@ -9,6 +9,9 @@
using System.Runtime.InteropServices;
#endif
using Microsoft.Toolkit.HighPerformance.Enumerables;
+#if !NETCORE_RUNTIME && !NET5_0
+using Microsoft.Toolkit.HighPerformance.Helpers;
+#endif
using Microsoft.Toolkit.HighPerformance.Helpers.Internals;
using RuntimeHelpers = Microsoft.Toolkit.HighPerformance.Helpers.Internals.RuntimeHelpers;
@@ -40,7 +43,7 @@ public static ref T DangerousGetReference(this T[] array)
#else
IntPtr offset = RuntimeHelpers.GetArrayDataByteOffset();
- return ref array.DangerousGetObjectDataReferenceAt(offset);
+ return ref ObjectMarshal.DangerousGetObjectDataReferenceAt(array, offset);
#endif
}
@@ -69,7 +72,7 @@ public static ref T DangerousGetReferenceAt(this T[] array, int i)
return ref ri;
#else
IntPtr offset = RuntimeHelpers.GetArrayDataByteOffset();
- ref T r0 = ref array.DangerousGetObjectDataReferenceAt(offset);
+ ref T r0 = ref ObjectMarshal.DangerousGetObjectDataReferenceAt(array, offset);
ref T ri = ref Unsafe.Add(ref r0, (nint)(uint)i);
return ref ri;
diff --git a/Microsoft.Toolkit.HighPerformance/Extensions/ArrayExtensions.2D.cs b/Microsoft.Toolkit.HighPerformance/Extensions/ArrayExtensions.2D.cs
index 6b571a68ef1..635451b9c53 100644
--- a/Microsoft.Toolkit.HighPerformance/Extensions/ArrayExtensions.2D.cs
+++ b/Microsoft.Toolkit.HighPerformance/Extensions/ArrayExtensions.2D.cs
@@ -10,6 +10,7 @@
using Microsoft.Toolkit.HighPerformance.Buffers.Internals;
#endif
using Microsoft.Toolkit.HighPerformance.Enumerables;
+using Microsoft.Toolkit.HighPerformance.Helpers;
using Microsoft.Toolkit.HighPerformance.Helpers.Internals;
using RuntimeHelpers = Microsoft.Toolkit.HighPerformance.Helpers.Internals.RuntimeHelpers;
@@ -39,7 +40,7 @@ public static ref T DangerousGetReference(this T[,] array)
#else
IntPtr offset = RuntimeHelpers.GetArray2DDataByteOffset();
- return ref array.DangerousGetObjectDataReferenceAt(offset);
+ return ref ObjectMarshal.DangerousGetObjectDataReferenceAt(array, offset);
#endif
}
@@ -72,7 +73,7 @@ public static ref T DangerousGetReferenceAt(this T[,] array, int i, int j)
int width = array.GetLength(1);
nint index = ((nint)(uint)i * (nint)(uint)width) + (nint)(uint)j;
IntPtr offset = RuntimeHelpers.GetArray2DDataByteOffset();
- ref T r0 = ref array.DangerousGetObjectDataReferenceAt(offset);
+ ref T r0 = ref ObjectMarshal.DangerousGetObjectDataReferenceAt(array, offset);
ref T ri = ref Unsafe.Add(ref r0, index);
return ref ri;
@@ -137,7 +138,7 @@ public static RefEnumerable GetRow(this T[,] array, int row)
return new RefEnumerable(ref r0, width, 1);
#else
ref T r0 = ref array.DangerousGetReferenceAt(row, 0);
- IntPtr offset = array.DangerousGetObjectDataByteOffset(ref r0);
+ IntPtr offset = ObjectMarshal.DangerousGetObjectDataByteOffset(array, ref r0);
return new RefEnumerable(array, offset, width, 1);
#endif
@@ -191,7 +192,7 @@ public static RefEnumerable GetColumn(this T[,] array, int column)
return new RefEnumerable(ref r0, height, width);
#else
ref T r0 = ref array.DangerousGetReferenceAt(0, column);
- IntPtr offset = array.DangerousGetObjectDataByteOffset(ref r0);
+ IntPtr offset = ObjectMarshal.DangerousGetObjectDataByteOffset(array, ref r0);
return new RefEnumerable(array, offset, height, width);
#endif
@@ -324,7 +325,7 @@ public static Memory GetRowMemory(this T[,] array, int row)
}
ref T r0 = ref array.DangerousGetReferenceAt(row, 0);
- IntPtr offset = array.DangerousGetObjectDataByteOffset(ref r0);
+ IntPtr offset = ObjectMarshal.DangerousGetObjectDataByteOffset(array, ref r0);
return new RawObjectMemoryManager(array, offset, array.GetLength(1)).Memory;
}
diff --git a/Microsoft.Toolkit.HighPerformance/Extensions/ArrayExtensions.3D.cs b/Microsoft.Toolkit.HighPerformance/Extensions/ArrayExtensions.3D.cs
index ba4f0bd498d..b5671469da9 100644
--- a/Microsoft.Toolkit.HighPerformance/Extensions/ArrayExtensions.3D.cs
+++ b/Microsoft.Toolkit.HighPerformance/Extensions/ArrayExtensions.3D.cs
@@ -5,6 +5,7 @@
using System;
using System.Diagnostics.Contracts;
using System.Runtime.CompilerServices;
+using Microsoft.Toolkit.HighPerformance.Helpers;
#if SPAN_RUNTIME_SUPPORT
using System.Runtime.InteropServices;
using Microsoft.Toolkit.HighPerformance.Buffers.Internals;
@@ -38,7 +39,7 @@ public static ref T DangerousGetReference(this T[,,] array)
#else
IntPtr offset = RuntimeHelpers.GetArray3DDataByteOffset();
- return ref array.DangerousGetObjectDataReferenceAt(offset);
+ return ref ObjectMarshal.DangerousGetObjectDataReferenceAt(array, offset);
#endif
}
@@ -78,7 +79,7 @@ public static ref T DangerousGetReferenceAt(this T[,,] array, int i, int j, i
((nint)(uint)i * (nint)(uint)height * (nint)(uint)width) +
((nint)(uint)j * (nint)(uint)width) + (nint)(uint)k;
IntPtr offset = RuntimeHelpers.GetArray3DDataByteOffset();
- ref T r0 = ref array.DangerousGetObjectDataReferenceAt(offset);
+ ref T r0 = ref ObjectMarshal.DangerousGetObjectDataReferenceAt(array, offset);
ref T ri = ref Unsafe.Add(ref r0, index);
return ref ri;
@@ -212,7 +213,7 @@ public static Memory AsMemory(this T[,,] array, int depth)
}
ref T r0 = ref array.DangerousGetReferenceAt(depth, 0, 0);
- IntPtr offset = array.DangerousGetObjectDataByteOffset(ref r0);
+ IntPtr offset = ObjectMarshal.DangerousGetObjectDataByteOffset(array, ref r0);
int length = checked(array.GetLength(1) * array.GetLength(2));
return new RawObjectMemoryManager(array, offset, length).Memory;
diff --git a/Microsoft.Toolkit.HighPerformance/Extensions/BoolExtensions.cs b/Microsoft.Toolkit.HighPerformance/Extensions/BoolExtensions.cs
index e4fa0a6df73..388529f15dc 100644
--- a/Microsoft.Toolkit.HighPerformance/Extensions/BoolExtensions.cs
+++ b/Microsoft.Toolkit.HighPerformance/Extensions/BoolExtensions.cs
@@ -33,20 +33,6 @@ public static unsafe byte ToByte(this bool flag)
return *(byte*)©
}
- ///
- /// Converts the given value into an .
- ///
- /// The input value to convert.
- /// 1 if is , 0 otherwise.
- /// This method does not contain branching instructions.
- [Pure]
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- [Obsolete("Use ToByte instead.")]
- public static unsafe int ToInt(this bool flag)
- {
- return *(byte*)&flag;
- }
-
///
/// Converts the given value to an mask with
/// all bits representing the value of the input flag (either 0xFFFFFFFF or 0x00000000).
diff --git a/Microsoft.Toolkit.HighPerformance/Helpers/Internals/RuntimeHelpers.cs b/Microsoft.Toolkit.HighPerformance/Helpers/Internals/RuntimeHelpers.cs
index 4a0d464b0af..49817edc31a 100644
--- a/Microsoft.Toolkit.HighPerformance/Helpers/Internals/RuntimeHelpers.cs
+++ b/Microsoft.Toolkit.HighPerformance/Helpers/Internals/RuntimeHelpers.cs
@@ -13,7 +13,6 @@
using System.Reflection;
#endif
using System.Runtime.CompilerServices;
-using Microsoft.Toolkit.HighPerformance.Extensions;
namespace Microsoft.Toolkit.HighPerformance.Helpers.Internals
{
@@ -152,7 +151,7 @@ public static unsafe IntPtr GetObjectDataOrReferenceByteOffset(object? obj, r
return (IntPtr)Unsafe.AsPointer(ref data);
}
- return obj.DangerousGetObjectDataByteOffset(ref data);
+ return ObjectMarshal.DangerousGetObjectDataByteOffset(obj, ref data);
}
///
@@ -174,7 +173,7 @@ public static unsafe ref T GetObjectDataAtOffsetOrPointerReference(object? ob
return ref Unsafe.AsRef((void*)offset);
}
- return ref obj.DangerousGetObjectDataReferenceAt(offset);
+ return ref ObjectMarshal.DangerousGetObjectDataReferenceAt(obj, offset);
}
///
@@ -274,7 +273,7 @@ private static IntPtr MeasureArrayDataByteOffset()
{
var array = new T[1];
- return array.DangerousGetObjectDataByteOffset(ref array[0]);
+ return ObjectMarshal.DangerousGetObjectDataByteOffset(array, ref array[0]);
}
///
@@ -286,7 +285,7 @@ private static IntPtr MeasureArray2DDataByteOffset()
{
var array = new T[1, 1];
- return array.DangerousGetObjectDataByteOffset(ref array[0, 0]);
+ return ObjectMarshal.DangerousGetObjectDataByteOffset(array, ref array[0, 0]);
}
///
@@ -298,7 +297,7 @@ private static IntPtr MeasureArray3DDataByteOffset()
{
var array = new T[1, 1, 1];
- return array.DangerousGetObjectDataByteOffset(ref array[0, 0, 0]);
+ return ObjectMarshal.DangerousGetObjectDataByteOffset(array, ref array[0, 0, 0]);
}
}
}
diff --git a/Microsoft.Toolkit.HighPerformance/Extensions/ObjectExtensions.cs b/Microsoft.Toolkit.HighPerformance/Helpers/ObjectMarshal.cs
similarity index 95%
rename from Microsoft.Toolkit.HighPerformance/Extensions/ObjectExtensions.cs
rename to Microsoft.Toolkit.HighPerformance/Helpers/ObjectMarshal.cs
index 0a61350a17e..9e451917a7e 100644
--- a/Microsoft.Toolkit.HighPerformance/Extensions/ObjectExtensions.cs
+++ b/Microsoft.Toolkit.HighPerformance/Helpers/ObjectMarshal.cs
@@ -7,12 +7,12 @@
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
-namespace Microsoft.Toolkit.HighPerformance.Extensions
+namespace Microsoft.Toolkit.HighPerformance.Helpers
{
///
/// Helpers for working with instances.
///
- public static class ObjectExtensions
+ public static class ObjectMarshal
{
///
/// Calculates the byte offset to a specific field within a given .
@@ -29,7 +29,7 @@ public static class ObjectExtensions
///
[Pure]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
- public static IntPtr DangerousGetObjectDataByteOffset(this object obj, ref T data)
+ public static IntPtr DangerousGetObjectDataByteOffset(object obj, ref T data)
{
var rawObj = Unsafe.As(obj)!;
ref byte r0 = ref rawObj.Data;
@@ -53,7 +53,7 @@ public static IntPtr DangerousGetObjectDataByteOffset(this object obj, ref T
///
[Pure]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
- public static ref T DangerousGetObjectDataReferenceAt(this object obj, IntPtr offset)
+ public static ref T DangerousGetObjectDataReferenceAt(object obj, IntPtr offset)
{
var rawObj = Unsafe.As(obj)!;
ref byte r0 = ref rawObj.Data;
@@ -97,7 +97,7 @@ private sealed class RawObjectData
///
/// This extension behaves just like the following method:
///
- /// public static bool TryUnbox<T>(this object obj, out T value)
+ /// public static bool TryUnbox<T>(object obj, out T value)
/// {
/// if (obj is T)
/// {
@@ -139,7 +139,7 @@ public static bool TryUnbox(this object obj, out T value)
/// Thrown when is not of type .
[Pure]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
- public static ref T DangerousUnbox(this object obj)
+ public static ref T DangerousUnbox(object obj)
where T : struct
{
return ref Unsafe.Unbox(obj);
diff --git a/Microsoft.Toolkit.HighPerformance/Memory/Memory2D{T}.cs b/Microsoft.Toolkit.HighPerformance/Memory/Memory2D{T}.cs
index 6f6a7262222..3bc114a380d 100644
--- a/Microsoft.Toolkit.HighPerformance/Memory/Memory2D{T}.cs
+++ b/Microsoft.Toolkit.HighPerformance/Memory/Memory2D{T}.cs
@@ -13,6 +13,7 @@
using Microsoft.Toolkit.HighPerformance.Buffers.Internals;
#endif
using Microsoft.Toolkit.HighPerformance.Extensions;
+using Microsoft.Toolkit.HighPerformance.Helpers;
using Microsoft.Toolkit.HighPerformance.Memory.Internals;
using Microsoft.Toolkit.HighPerformance.Memory.Views;
using static Microsoft.Toolkit.HighPerformance.Helpers.Internals.RuntimeHelpers;
@@ -129,7 +130,7 @@ public Memory2D(T[] array, int offset, int height, int width, int pitch)
}
this.instance = array;
- this.offset = array.DangerousGetObjectDataByteOffset(ref array.DangerousGetReferenceAt(offset));
+ this.offset = ObjectMarshal.DangerousGetObjectDataByteOffset(array, ref array.DangerousGetReferenceAt(offset));
this.height = height;
this.width = width;
this.pitch = pitch;
@@ -222,7 +223,7 @@ public Memory2D(T[,]? array, int row, int column, int height, int width)
}
this.instance = array;
- this.offset = array.DangerousGetObjectDataByteOffset(ref array.DangerousGetReferenceAt(row, column));
+ this.offset = ObjectMarshal.DangerousGetObjectDataByteOffset(array, ref array.DangerousGetReferenceAt(row, column));
this.height = height;
this.width = width;
this.pitch = columns - width;
@@ -250,7 +251,7 @@ public Memory2D(T[,,] array, int depth)
}
this.instance = array;
- this.offset = array.DangerousGetObjectDataByteOffset(ref array.DangerousGetReferenceAt(depth, 0, 0));
+ this.offset = ObjectMarshal.DangerousGetObjectDataByteOffset(array, ref array.DangerousGetReferenceAt(depth, 0, 0));
this.height = array.GetLength(1);
this.width = array.GetLength(2);
this.pitch = 0;
@@ -306,7 +307,7 @@ public Memory2D(T[,,] array, int depth, int row, int column, int height, int wid
}
this.instance = array;
- this.offset = array.DangerousGetObjectDataByteOffset(ref array.DangerousGetReferenceAt(depth, row, column));
+ this.offset = ObjectMarshal.DangerousGetObjectDataByteOffset(array, ref array.DangerousGetReferenceAt(depth, row, column));
this.height = height;
this.width = width;
this.pitch = columns - width;
@@ -465,7 +466,7 @@ internal Memory2D(Memory memory, int offset, int height, int width, int pitch
ref char r0 = ref text.DangerousGetReferenceAt(textStart + offset);
this.instance = text;
- this.offset = text.DangerousGetObjectDataByteOffset(ref r0);
+ this.offset = ObjectMarshal.DangerousGetObjectDataByteOffset(text, ref r0);
}
else if (MemoryMarshal.TryGetArray(memory, out ArraySegment segment))
{
@@ -477,7 +478,7 @@ internal Memory2D(Memory memory, int offset, int height, int width, int pitch
T[] array = segment.Array!;
this.instance = array;
- this.offset = array.DangerousGetObjectDataByteOffset(ref array.DangerousGetReferenceAt(segment.Offset + offset));
+ this.offset = ObjectMarshal.DangerousGetObjectDataByteOffset(array, ref array.DangerousGetReferenceAt(segment.Offset + offset));
}
else if (MemoryMarshal.TryGetMemoryManager>(memory, out var memoryManager, out int memoryManagerStart, out _))
{
@@ -549,7 +550,7 @@ public static Memory2D DangerousCreate(object instance, ref T value, int heig
OverflowHelper.EnsureIsInNativeIntRange(height, width, pitch);
- IntPtr offset = instance.DangerousGetObjectDataByteOffset(ref value);
+ IntPtr offset = ObjectMarshal.DangerousGetObjectDataByteOffset(instance, ref value);
return new Memory2D(instance, offset, height, width, pitch);
}
@@ -615,7 +616,7 @@ public Span2D Span
}
else
{
- ref T r0 = ref this.instance.DangerousGetObjectDataReferenceAt(this.offset);
+ ref T r0 = ref ObjectMarshal.DangerousGetObjectDataReferenceAt(this.instance, this.offset);
return new Span2D(ref r0, this.height, this.width, this.pitch);
}
@@ -749,7 +750,7 @@ public unsafe MemoryHandle Pin()
GCHandle handle = GCHandle.Alloc(this.instance, GCHandleType.Pinned);
- void* pointer = Unsafe.AsPointer(ref this.instance.DangerousGetObjectDataReferenceAt(this.offset));
+ void* pointer = Unsafe.AsPointer(ref ObjectMarshal.DangerousGetObjectDataReferenceAt(this.instance, this.offset));
return new MemoryHandle(pointer, handle);
}
@@ -775,7 +776,7 @@ public bool TryGetMemory(out Memory memory)
else if (typeof(T) == typeof(char) && this.instance.GetType() == typeof(string))
{
string text = Unsafe.As(this.instance)!;
- int index = text.AsSpan().IndexOf(in text.DangerousGetObjectDataReferenceAt(this.offset));
+ int index = text.AsSpan().IndexOf(in ObjectMarshal.DangerousGetObjectDataReferenceAt(text, this.offset));
ReadOnlyMemory temp = text.AsMemory(index, (int)Length);
// The string type could still be present if a user ends up creating a
@@ -795,7 +796,7 @@ public bool TryGetMemory(out Memory memory)
{
// If it's a T[] array, also handle the initial offset
T[] array = Unsafe.As(this.instance)!;
- int index = array.AsSpan().IndexOf(ref array.DangerousGetObjectDataReferenceAt(this.offset));
+ int index = array.AsSpan().IndexOf(ref ObjectMarshal.DangerousGetObjectDataReferenceAt(array, this.offset));
memory = array.AsMemory(index, this.height * this.width);
}
diff --git a/Microsoft.Toolkit.HighPerformance/Memory/ReadOnlyMemory2D{T}.cs b/Microsoft.Toolkit.HighPerformance/Memory/ReadOnlyMemory2D{T}.cs
index 80e8839cb85..3666755e533 100644
--- a/Microsoft.Toolkit.HighPerformance/Memory/ReadOnlyMemory2D{T}.cs
+++ b/Microsoft.Toolkit.HighPerformance/Memory/ReadOnlyMemory2D{T}.cs
@@ -13,6 +13,7 @@
using Microsoft.Toolkit.HighPerformance.Buffers.Internals;
#endif
using Microsoft.Toolkit.HighPerformance.Extensions;
+using Microsoft.Toolkit.HighPerformance.Helpers;
using Microsoft.Toolkit.HighPerformance.Memory.Internals;
using Microsoft.Toolkit.HighPerformance.Memory.Views;
using static Microsoft.Toolkit.HighPerformance.Helpers.Internals.RuntimeHelpers;
@@ -115,7 +116,7 @@ public ReadOnlyMemory2D(string text, int offset, int height, int width, int pitc
}
this.instance = text;
- this.offset = text.DangerousGetObjectDataByteOffset(ref text.DangerousGetReferenceAt(offset));
+ this.offset = ObjectMarshal.DangerousGetObjectDataByteOffset(text, ref text.DangerousGetReferenceAt(offset));
this.height = height;
this.width = width;
this.pitch = pitch;
@@ -182,7 +183,7 @@ public ReadOnlyMemory2D(T[] array, int offset, int height, int width, int pitch)
}
this.instance = array;
- this.offset = array.DangerousGetObjectDataByteOffset(ref array.DangerousGetReferenceAt(offset));
+ this.offset = ObjectMarshal.DangerousGetObjectDataByteOffset(array, ref array.DangerousGetReferenceAt(offset));
this.height = height;
this.width = width;
this.pitch = pitch;
@@ -259,7 +260,7 @@ public ReadOnlyMemory2D(T[,]? array, int row, int column, int height, int width)
}
this.instance = array;
- this.offset = array.DangerousGetObjectDataByteOffset(ref array.DangerousGetReferenceAt(row, column));
+ this.offset = ObjectMarshal.DangerousGetObjectDataByteOffset(array, ref array.DangerousGetReferenceAt(row, column));
this.height = height;
this.width = width;
this.pitch = columns - width;
@@ -279,7 +280,7 @@ public ReadOnlyMemory2D(T[,,] array, int depth)
}
this.instance = array;
- this.offset = array.DangerousGetObjectDataByteOffset(ref array.DangerousGetReferenceAt(depth, 0, 0));
+ this.offset = ObjectMarshal.DangerousGetObjectDataByteOffset(array, ref array.DangerousGetReferenceAt(depth, 0, 0));
this.height = array.GetLength(1);
this.width = array.GetLength(2);
this.pitch = 0;
@@ -327,7 +328,7 @@ public ReadOnlyMemory2D(T[,,] array, int depth, int row, int column, int height,
}
this.instance = array;
- this.offset = array.DangerousGetObjectDataByteOffset(ref array.DangerousGetReferenceAt(depth, row, column));
+ this.offset = ObjectMarshal.DangerousGetObjectDataByteOffset(array, ref array.DangerousGetReferenceAt(depth, row, column));
this.height = height;
this.width = width;
this.pitch = columns - width;
@@ -484,14 +485,14 @@ internal ReadOnlyMemory2D(ReadOnlyMemory memory, int offset, int height, int
ref char r0 = ref text.DangerousGetReferenceAt(textStart + offset);
this.instance = text;
- this.offset = text.DangerousGetObjectDataByteOffset(ref r0);
+ this.offset = ObjectMarshal.DangerousGetObjectDataByteOffset(text, ref r0);
}
else if (MemoryMarshal.TryGetArray(memory, out ArraySegment segment))
{
T[] array = segment.Array!;
this.instance = array;
- this.offset = array.DangerousGetObjectDataByteOffset(ref array.DangerousGetReferenceAt(segment.Offset + offset));
+ this.offset = ObjectMarshal.DangerousGetObjectDataByteOffset(array, ref array.DangerousGetReferenceAt(segment.Offset + offset));
}
else if (MemoryMarshal.TryGetMemoryManager>(memory, out var memoryManager, out int memoryManagerStart, out _))
{
@@ -563,7 +564,7 @@ public static ReadOnlyMemory2D DangerousCreate(object instance, ref T value,
OverflowHelper.EnsureIsInNativeIntRange(height, width, pitch);
- IntPtr offset = instance.DangerousGetObjectDataByteOffset(ref value);
+ IntPtr offset = ObjectMarshal.DangerousGetObjectDataByteOffset(instance, ref value);
return new ReadOnlyMemory2D(instance, offset, height, width, pitch);
}
@@ -630,7 +631,7 @@ public ReadOnlySpan2D Span
else
{
// This handles both arrays and strings
- ref T r0 = ref this.instance.DangerousGetObjectDataReferenceAt(this.offset);
+ ref T r0 = ref ObjectMarshal.DangerousGetObjectDataReferenceAt(this.instance, this.offset);
return new ReadOnlySpan2D(in r0, this.height, this.width, this.pitch);
}
@@ -764,7 +765,7 @@ public unsafe MemoryHandle Pin()
GCHandle handle = GCHandle.Alloc(this.instance, GCHandleType.Pinned);
- void* pointer = Unsafe.AsPointer(ref this.instance.DangerousGetObjectDataReferenceAt(this.offset));
+ void* pointer = Unsafe.AsPointer(ref ObjectMarshal.DangerousGetObjectDataReferenceAt(this.instance, this.offset));
return new MemoryHandle(pointer, handle);
}
@@ -797,7 +798,7 @@ public bool TryGetMemory(out ReadOnlyMemory memory)
// within the string), and the input reference, which we can get from the byte offset in use. The result
// is the character index which we can use to create the final Memory instance.
string text = Unsafe.As(this.instance)!;
- int index = text.AsSpan().IndexOf(in text.DangerousGetObjectDataReferenceAt(this.offset));
+ int index = text.AsSpan().IndexOf(in ObjectMarshal.DangerousGetObjectDataReferenceAt(text, this.offset));
ReadOnlyMemory temp = text.AsMemory(index, (int)Length);
memory = Unsafe.As, ReadOnlyMemory>(ref temp);
@@ -811,7 +812,7 @@ public bool TryGetMemory(out ReadOnlyMemory memory)
{
// If it's a T[] array, also handle the initial offset
T[] array = Unsafe.As(this.instance)!;
- int index = array.AsSpan().IndexOf(ref array.DangerousGetObjectDataReferenceAt(this.offset));
+ int index = array.AsSpan().IndexOf(ref ObjectMarshal.DangerousGetObjectDataReferenceAt(array, this.offset));
memory = array.AsMemory(index, this.height * this.width);
}
diff --git a/Microsoft.Toolkit.HighPerformance/Memory/ReadOnlySpan2D{T}.cs b/Microsoft.Toolkit.HighPerformance/Memory/ReadOnlySpan2D{T}.cs
index 17c9f5df51b..cd38069dad4 100644
--- a/Microsoft.Toolkit.HighPerformance/Memory/ReadOnlySpan2D{T}.cs
+++ b/Microsoft.Toolkit.HighPerformance/Memory/ReadOnlySpan2D{T}.cs
@@ -9,6 +9,9 @@
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using Microsoft.Toolkit.HighPerformance.Extensions;
+#if !SPAN_RUNTIME_SUPPORT
+using Microsoft.Toolkit.HighPerformance.Helpers;
+#endif
using Microsoft.Toolkit.HighPerformance.Memory.Internals;
using Microsoft.Toolkit.HighPerformance.Memory.Views;
#if !SPAN_RUNTIME_SUPPORT
@@ -210,7 +213,7 @@ public ReadOnlySpan2D(T[] array, int offset, int height, int width, int pitch)
this.span = MemoryMarshal.CreateReadOnlySpan(ref array.DangerousGetReferenceAt(offset), height);
#else
this.instance = array;
- this.offset = array.DangerousGetObjectDataByteOffset(ref array.DangerousGetReferenceAt(offset));
+ this.offset = ObjectMarshal.DangerousGetObjectDataByteOffset(array, ref array.DangerousGetReferenceAt(offset));
this.height = height;
#endif
this.width = width;
@@ -234,7 +237,7 @@ public ReadOnlySpan2D(T[,]? array)
this.span = MemoryMarshal.CreateReadOnlySpan(ref array.DangerousGetReference(), array.GetLength(0));
#else
this.instance = array;
- this.offset = array.DangerousGetObjectDataByteOffset(ref array.DangerousGetReferenceAt(0, 0));
+ this.offset = ObjectMarshal.DangerousGetObjectDataByteOffset(array, ref array.DangerousGetReferenceAt(0, 0));
this.height = array.GetLength(0);
#endif
this.width = this.stride = array.GetLength(1);
@@ -294,7 +297,7 @@ public ReadOnlySpan2D(T[,]? array, int row, int column, int height, int width)
this.span = MemoryMarshal.CreateReadOnlySpan(ref array.DangerousGetReferenceAt(row, column), height);
#else
this.instance = array;
- this.offset = array.DangerousGetObjectDataByteOffset(ref array.DangerousGetReferenceAt(row, column));
+ this.offset = ObjectMarshal.DangerousGetObjectDataByteOffset(array, ref array.DangerousGetReferenceAt(row, column));
this.height = height;
#endif
this.width = width;
@@ -318,7 +321,7 @@ public ReadOnlySpan2D(T[,,] array, int depth)
this.span = MemoryMarshal.CreateReadOnlySpan(ref array.DangerousGetReferenceAt(depth, 0, 0), array.GetLength(1));
#else
this.instance = array;
- this.offset = array.DangerousGetObjectDataByteOffset(ref array.DangerousGetReferenceAt(depth, 0, 0));
+ this.offset = ObjectMarshal.DangerousGetObjectDataByteOffset(array, ref array.DangerousGetReferenceAt(depth, 0, 0));
this.height = array.GetLength(1);
#endif
this.width = this.stride = array.GetLength(2);
@@ -369,7 +372,7 @@ public ReadOnlySpan2D(T[,,] array, int depth, int row, int column, int height, i
this.span = MemoryMarshal.CreateReadOnlySpan(ref array.DangerousGetReferenceAt(depth, row, column), height);
#else
this.instance = array;
- this.offset = array.DangerousGetObjectDataByteOffset(ref array.DangerousGetReferenceAt(depth, row, column));
+ this.offset = ObjectMarshal.DangerousGetObjectDataByteOffset(array, ref array.DangerousGetReferenceAt(depth, row, column));
this.height = height;
#endif
this.width = width;
diff --git a/Microsoft.Toolkit.HighPerformance/Memory/Span2D{T}.cs b/Microsoft.Toolkit.HighPerformance/Memory/Span2D{T}.cs
index ecdeeccfb58..88ac3c2c214 100644
--- a/Microsoft.Toolkit.HighPerformance/Memory/Span2D{T}.cs
+++ b/Microsoft.Toolkit.HighPerformance/Memory/Span2D{T}.cs
@@ -9,6 +9,9 @@
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using Microsoft.Toolkit.HighPerformance.Extensions;
+#if !SPAN_RUNTIME_SUPPORT
+using Microsoft.Toolkit.HighPerformance.Helpers;
+#endif
using Microsoft.Toolkit.HighPerformance.Memory.Internals;
using Microsoft.Toolkit.HighPerformance.Memory.Views;
#if !SPAN_RUNTIME_SUPPORT
@@ -249,7 +252,7 @@ public Span2D(T[] array, int offset, int height, int width, int pitch)
this.span = MemoryMarshal.CreateSpan(ref array.DangerousGetReferenceAt(offset), height);
#else
this.Instance = array;
- this.Offset = array.DangerousGetObjectDataByteOffset(ref array.DangerousGetReferenceAt(offset));
+ this.Offset = ObjectMarshal.DangerousGetObjectDataByteOffset(array, ref array.DangerousGetReferenceAt(offset));
this.height = height;
#endif
this.width = width;
@@ -281,7 +284,7 @@ public Span2D(T[,]? array)
this.span = MemoryMarshal.CreateSpan(ref array.DangerousGetReference(), array.GetLength(0));
#else
this.Instance = array;
- this.Offset = array.DangerousGetObjectDataByteOffset(ref array.DangerousGetReferenceAt(0, 0));
+ this.Offset = ObjectMarshal.DangerousGetObjectDataByteOffset(array, ref array.DangerousGetReferenceAt(0, 0));
this.height = array.GetLength(0);
#endif
this.width = this.Stride = array.GetLength(1);
@@ -349,7 +352,7 @@ public Span2D(T[,]? array, int row, int column, int height, int width)
this.span = MemoryMarshal.CreateSpan(ref array.DangerousGetReferenceAt(row, column), height);
#else
this.Instance = array;
- this.Offset = array.DangerousGetObjectDataByteOffset(ref array.DangerousGetReferenceAt(row, column));
+ this.Offset = ObjectMarshal.DangerousGetObjectDataByteOffset(array, ref array.DangerousGetReferenceAt(row, column));
this.height = height;
#endif
this.width = width;
@@ -381,7 +384,7 @@ public Span2D(T[,,] array, int depth)
this.span = MemoryMarshal.CreateSpan(ref array.DangerousGetReferenceAt(depth, 0, 0), array.GetLength(1));
#else
this.Instance = array;
- this.Offset = array.DangerousGetObjectDataByteOffset(ref array.DangerousGetReferenceAt(depth, 0, 0));
+ this.Offset = ObjectMarshal.DangerousGetObjectDataByteOffset(array, ref array.DangerousGetReferenceAt(depth, 0, 0));
this.height = array.GetLength(1);
#endif
this.width = this.Stride = array.GetLength(2);
@@ -440,7 +443,7 @@ public Span2D(T[,,] array, int depth, int row, int column, int height, int width
this.span = MemoryMarshal.CreateSpan(ref array.DangerousGetReferenceAt(depth, row, column), height);
#else
this.Instance = array;
- this.Offset = array.DangerousGetObjectDataByteOffset(ref array.DangerousGetReferenceAt(depth, row, column));
+ this.Offset = ObjectMarshal.DangerousGetObjectDataByteOffset(array, ref array.DangerousGetReferenceAt(depth, row, column));
this.height = height;
#endif
this.width = width;
diff --git a/Microsoft.Toolkit.HighPerformance/ReadOnlyRef{T}.cs b/Microsoft.Toolkit.HighPerformance/ReadOnlyRef{T}.cs
index 37401184821..4ca386500a2 100644
--- a/Microsoft.Toolkit.HighPerformance/ReadOnlyRef{T}.cs
+++ b/Microsoft.Toolkit.HighPerformance/ReadOnlyRef{T}.cs
@@ -7,7 +7,7 @@
#if SPAN_RUNTIME_SUPPORT
using System.Runtime.InteropServices;
#else
-using Microsoft.Toolkit.HighPerformance.Extensions;
+using Microsoft.Toolkit.HighPerformance.Helpers;
#endif
namespace Microsoft.Toolkit.HighPerformance
@@ -98,7 +98,7 @@ private ReadOnlyRef(object owner, IntPtr offset)
public ReadOnlyRef(object owner, in T value)
{
this.owner = owner;
- this.offset = owner.DangerousGetObjectDataByteOffset(ref Unsafe.AsRef(value));
+ this.offset = ObjectMarshal.DangerousGetObjectDataByteOffset(owner, ref Unsafe.AsRef(value));
}
///
@@ -107,7 +107,7 @@ public ReadOnlyRef(object owner, in T value)
public ref readonly T Value
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
- get => ref this.owner.DangerousGetObjectDataReferenceAt(this.offset);
+ get => ref ObjectMarshal.DangerousGetObjectDataReferenceAt(this.owner, this.offset);
}
///
diff --git a/Microsoft.Toolkit.HighPerformance/Ref{T}.cs b/Microsoft.Toolkit.HighPerformance/Ref{T}.cs
index e821a365969..1ba880437d7 100644
--- a/Microsoft.Toolkit.HighPerformance/Ref{T}.cs
+++ b/Microsoft.Toolkit.HighPerformance/Ref{T}.cs
@@ -7,7 +7,7 @@
#if SPAN_RUNTIME_SUPPORT
using System.Runtime.InteropServices;
#else
-using Microsoft.Toolkit.HighPerformance.Extensions;
+using Microsoft.Toolkit.HighPerformance.Helpers;
#endif
namespace Microsoft.Toolkit.HighPerformance
@@ -77,7 +77,7 @@ public ref T Value
public Ref(object owner, ref T value)
{
Owner = owner;
- Offset = owner.DangerousGetObjectDataByteOffset(ref value);
+ Offset = ObjectMarshal.DangerousGetObjectDataByteOffset(owner, ref value);
}
///
@@ -86,7 +86,7 @@ public Ref(object owner, ref T value)
public ref T Value
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
- get => ref Owner.DangerousGetObjectDataReferenceAt(Offset);
+ get => ref ObjectMarshal.DangerousGetObjectDataReferenceAt(Owner, Offset);
}
#endif
diff --git a/UnitTests/UnitTests.HighPerformance.Shared/Extensions/Test_ObjectExtensions.cs b/UnitTests/UnitTests.HighPerformance.Shared/Helpers/Test_ObjectMarshal.cs
similarity index 77%
rename from UnitTests/UnitTests.HighPerformance.Shared/Extensions/Test_ObjectExtensions.cs
rename to UnitTests/UnitTests.HighPerformance.Shared/Helpers/Test_ObjectMarshal.cs
index 11d4ec2f4a3..192ae7d4431 100644
--- a/UnitTests/UnitTests.HighPerformance.Shared/Extensions/Test_ObjectExtensions.cs
+++ b/UnitTests/UnitTests.HighPerformance.Shared/Helpers/Test_ObjectMarshal.cs
@@ -4,35 +4,35 @@
using System;
using System.Runtime.CompilerServices;
-using Microsoft.Toolkit.HighPerformance.Extensions;
+using Microsoft.Toolkit.HighPerformance.Helpers;
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace UnitTests.HighPerformance.Extensions
{
[TestClass]
- public class Test_ObjectExtensions
+ public class Test_ObjectMarshal
{
- [TestCategory("ObjectExtensions")]
+ [TestCategory("ObjectMarshal")]
[TestMethod]
public void Test_DangerousGetObjectDataByteOffset()
{
var a = new TestClass { Number = 42, Character = 'a', Text = "Hello" };
- IntPtr ptr = a.DangerousGetObjectDataByteOffset(ref a.Number);
+ IntPtr ptr = ObjectMarshal.DangerousGetObjectDataByteOffset(a, ref a.Number);
- ref int number = ref a.DangerousGetObjectDataReferenceAt(ptr);
+ ref int number = ref ObjectMarshal.DangerousGetObjectDataReferenceAt(a, ptr);
Assert.IsTrue(Unsafe.AreSame(ref a.Number, ref number));
- ptr = a.DangerousGetObjectDataByteOffset(ref a.Character);
+ ptr = ObjectMarshal.DangerousGetObjectDataByteOffset(a, ref a.Character);
- ref char character = ref a.DangerousGetObjectDataReferenceAt(ptr);
+ ref char character = ref ObjectMarshal.DangerousGetObjectDataReferenceAt(a, ptr);
Assert.IsTrue(Unsafe.AreSame(ref a.Character, ref character));
- ptr = a.DangerousGetObjectDataByteOffset(ref a.Text);
+ ptr = ObjectMarshal.DangerousGetObjectDataByteOffset(a, ref a.Text);
- ref string text = ref a.DangerousGetObjectDataReferenceAt(ptr);
+ ref string text = ref ObjectMarshal.DangerousGetObjectDataReferenceAt(a, ptr);
Assert.IsTrue(Unsafe.AreSame(ref a.Text, ref text));
}
@@ -46,7 +46,7 @@ internal class TestClass
#pragma warning restore SA1401
}
- [TestCategory("ObjectExtensions")]
+ [TestCategory("ObjectMarshal")]
[TestMethod]
public void Test_BoxOfT_PrimitiveTypes()
{
@@ -60,7 +60,7 @@ public void Test_BoxOfT_PrimitiveTypes()
Test(184013.234324);
}
- [TestCategory("ObjectExtensions")]
+ [TestCategory("ObjectMarshal")]
[TestMethod]
public void Test_BoxOfT_OtherTypes()
{
@@ -84,7 +84,7 @@ public bool Equals(TestStruct other)
}
}
- [TestCategory("ObjectExtensions")]
+ [TestCategory("ObjectMarshal")]
[TestMethod]
public void TestBoxOfT_CustomStruct()
{
@@ -115,7 +115,7 @@ private static void Test(T value)
Assert.IsFalse(success);
Assert.AreEqual(test, default);
- result = obj.DangerousUnbox();
+ result = ObjectMarshal.DangerousUnbox(obj);
Assert.AreEqual(value, result);
}
diff --git a/UnitTests/UnitTests.HighPerformance.Shared/UnitTests.HighPerformance.Shared.projitems b/UnitTests/UnitTests.HighPerformance.Shared/UnitTests.HighPerformance.Shared.projitems
index 7efff4b2ef4..2a0bdf27505 100644
--- a/UnitTests/UnitTests.HighPerformance.Shared/UnitTests.HighPerformance.Shared.projitems
+++ b/UnitTests/UnitTests.HighPerformance.Shared/UnitTests.HighPerformance.Shared.projitems
@@ -25,7 +25,6 @@
-
@@ -36,6 +35,7 @@
+