Skip to content

Commit d29e7b9

Browse files
Merge branch 'master' into test-chop-imageex
2 parents b386021 + 065ebe4 commit d29e7b9

File tree

166 files changed

+92
-11173
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

166 files changed

+92
-11173
lines changed

Microsoft.Toolkit.HighPerformance/Buffers/Internals/RawObjectMemoryManager{T}.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
using System.Runtime.CompilerServices;
1010
using System.Runtime.InteropServices;
1111
using Microsoft.Toolkit.HighPerformance.Extensions;
12+
using Microsoft.Toolkit.HighPerformance.Helpers;
1213

1314
namespace Microsoft.Toolkit.HighPerformance.Buffers.Internals
1415
{
@@ -49,7 +50,7 @@ public RawObjectMemoryManager(object instance, IntPtr offset, int length)
4950
/// <inheritdoc/>
5051
public override Span<T> GetSpan()
5152
{
52-
ref T r0 = ref this.instance.DangerousGetObjectDataReferenceAt<T>(this.offset);
53+
ref T r0 = ref ObjectMarshal.DangerousGetObjectDataReferenceAt<T>(this.instance, this.offset);
5354

5455
return MemoryMarshal.CreateSpan(ref r0, this.length);
5556
}
@@ -68,7 +69,7 @@ public override unsafe MemoryHandle Pin(int elementIndex = 0)
6869
// traditional means (eg. via the implicit T[] array conversion), if T is a
6970
// reference type or a type containing some references.
7071
GCHandle handle = GCHandle.Alloc(this.instance, GCHandleType.Pinned);
71-
ref T r0 = ref this.instance.DangerousGetObjectDataReferenceAt<T>(this.offset);
72+
ref T r0 = ref ObjectMarshal.DangerousGetObjectDataReferenceAt<T>(this.instance, this.offset);
7273
ref T r1 = ref Unsafe.Add(ref r0, (nint)(uint)elementIndex);
7374
void* p = Unsafe.AsPointer(ref r1);
7475

Microsoft.Toolkit.HighPerformance/Extensions/ArrayExtensions.1D.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@
99
using System.Runtime.InteropServices;
1010
#endif
1111
using Microsoft.Toolkit.HighPerformance.Enumerables;
12+
#if !NETCORE_RUNTIME && !NET5_0
13+
using Microsoft.Toolkit.HighPerformance.Helpers;
14+
#endif
1215
using Microsoft.Toolkit.HighPerformance.Helpers.Internals;
1316
using RuntimeHelpers = Microsoft.Toolkit.HighPerformance.Helpers.Internals.RuntimeHelpers;
1417

@@ -40,7 +43,7 @@ public static ref T DangerousGetReference<T>(this T[] array)
4043
#else
4144
IntPtr offset = RuntimeHelpers.GetArrayDataByteOffset<T>();
4245

43-
return ref array.DangerousGetObjectDataReferenceAt<T>(offset);
46+
return ref ObjectMarshal.DangerousGetObjectDataReferenceAt<T>(array, offset);
4447
#endif
4548
}
4649

@@ -69,7 +72,7 @@ public static ref T DangerousGetReferenceAt<T>(this T[] array, int i)
6972
return ref ri;
7073
#else
7174
IntPtr offset = RuntimeHelpers.GetArrayDataByteOffset<T>();
72-
ref T r0 = ref array.DangerousGetObjectDataReferenceAt<T>(offset);
75+
ref T r0 = ref ObjectMarshal.DangerousGetObjectDataReferenceAt<T>(array, offset);
7376
ref T ri = ref Unsafe.Add(ref r0, (nint)(uint)i);
7477

7578
return ref ri;

Microsoft.Toolkit.HighPerformance/Extensions/ArrayExtensions.2D.cs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
using Microsoft.Toolkit.HighPerformance.Buffers.Internals;
1111
#endif
1212
using Microsoft.Toolkit.HighPerformance.Enumerables;
13+
using Microsoft.Toolkit.HighPerformance.Helpers;
1314
using Microsoft.Toolkit.HighPerformance.Helpers.Internals;
1415
using RuntimeHelpers = Microsoft.Toolkit.HighPerformance.Helpers.Internals.RuntimeHelpers;
1516

@@ -39,7 +40,7 @@ public static ref T DangerousGetReference<T>(this T[,] array)
3940
#else
4041
IntPtr offset = RuntimeHelpers.GetArray2DDataByteOffset<T>();
4142

42-
return ref array.DangerousGetObjectDataReferenceAt<T>(offset);
43+
return ref ObjectMarshal.DangerousGetObjectDataReferenceAt<T>(array, offset);
4344
#endif
4445
}
4546

@@ -72,7 +73,7 @@ public static ref T DangerousGetReferenceAt<T>(this T[,] array, int i, int j)
7273
int width = array.GetLength(1);
7374
nint index = ((nint)(uint)i * (nint)(uint)width) + (nint)(uint)j;
7475
IntPtr offset = RuntimeHelpers.GetArray2DDataByteOffset<T>();
75-
ref T r0 = ref array.DangerousGetObjectDataReferenceAt<T>(offset);
76+
ref T r0 = ref ObjectMarshal.DangerousGetObjectDataReferenceAt<T>(array, offset);
7677
ref T ri = ref Unsafe.Add(ref r0, index);
7778

7879
return ref ri;
@@ -137,7 +138,7 @@ public static RefEnumerable<T> GetRow<T>(this T[,] array, int row)
137138
return new RefEnumerable<T>(ref r0, width, 1);
138139
#else
139140
ref T r0 = ref array.DangerousGetReferenceAt(row, 0);
140-
IntPtr offset = array.DangerousGetObjectDataByteOffset(ref r0);
141+
IntPtr offset = ObjectMarshal.DangerousGetObjectDataByteOffset(array, ref r0);
141142

142143
return new RefEnumerable<T>(array, offset, width, 1);
143144
#endif
@@ -191,7 +192,7 @@ public static RefEnumerable<T> GetColumn<T>(this T[,] array, int column)
191192
return new RefEnumerable<T>(ref r0, height, width);
192193
#else
193194
ref T r0 = ref array.DangerousGetReferenceAt(0, column);
194-
IntPtr offset = array.DangerousGetObjectDataByteOffset(ref r0);
195+
IntPtr offset = ObjectMarshal.DangerousGetObjectDataByteOffset(array, ref r0);
195196

196197
return new RefEnumerable<T>(array, offset, height, width);
197198
#endif
@@ -324,7 +325,7 @@ public static Memory<T> GetRowMemory<T>(this T[,] array, int row)
324325
}
325326

326327
ref T r0 = ref array.DangerousGetReferenceAt(row, 0);
327-
IntPtr offset = array.DangerousGetObjectDataByteOffset(ref r0);
328+
IntPtr offset = ObjectMarshal.DangerousGetObjectDataByteOffset(array, ref r0);
328329

329330
return new RawObjectMemoryManager<T>(array, offset, array.GetLength(1)).Memory;
330331
}

Microsoft.Toolkit.HighPerformance/Extensions/ArrayExtensions.3D.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using System;
66
using System.Diagnostics.Contracts;
77
using System.Runtime.CompilerServices;
8+
using Microsoft.Toolkit.HighPerformance.Helpers;
89
#if SPAN_RUNTIME_SUPPORT
910
using System.Runtime.InteropServices;
1011
using Microsoft.Toolkit.HighPerformance.Buffers.Internals;
@@ -38,7 +39,7 @@ public static ref T DangerousGetReference<T>(this T[,,] array)
3839
#else
3940
IntPtr offset = RuntimeHelpers.GetArray3DDataByteOffset<T>();
4041

41-
return ref array.DangerousGetObjectDataReferenceAt<T>(offset);
42+
return ref ObjectMarshal.DangerousGetObjectDataReferenceAt<T>(array, offset);
4243
#endif
4344
}
4445

@@ -78,7 +79,7 @@ public static ref T DangerousGetReferenceAt<T>(this T[,,] array, int i, int j, i
7879
((nint)(uint)i * (nint)(uint)height * (nint)(uint)width) +
7980
((nint)(uint)j * (nint)(uint)width) + (nint)(uint)k;
8081
IntPtr offset = RuntimeHelpers.GetArray3DDataByteOffset<T>();
81-
ref T r0 = ref array.DangerousGetObjectDataReferenceAt<T>(offset);
82+
ref T r0 = ref ObjectMarshal.DangerousGetObjectDataReferenceAt<T>(array, offset);
8283
ref T ri = ref Unsafe.Add(ref r0, index);
8384

8485
return ref ri;
@@ -212,7 +213,7 @@ public static Memory<T> AsMemory<T>(this T[,,] array, int depth)
212213
}
213214

214215
ref T r0 = ref array.DangerousGetReferenceAt(depth, 0, 0);
215-
IntPtr offset = array.DangerousGetObjectDataByteOffset(ref r0);
216+
IntPtr offset = ObjectMarshal.DangerousGetObjectDataByteOffset(array, ref r0);
216217
int length = checked(array.GetLength(1) * array.GetLength(2));
217218

218219
return new RawObjectMemoryManager<T>(array, offset, length).Memory;

Microsoft.Toolkit.HighPerformance/Extensions/BoolExtensions.cs

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -33,20 +33,6 @@ public static unsafe byte ToByte(this bool flag)
3333
return *(byte*)&copy;
3434
}
3535

36-
/// <summary>
37-
/// Converts the given <see cref="bool"/> value into an <see cref="int"/>.
38-
/// </summary>
39-
/// <param name="flag">The input value to convert.</param>
40-
/// <returns>1 if <paramref name="flag"/> is <see langword="true"/>, 0 otherwise.</returns>
41-
/// <remarks>This method does not contain branching instructions.</remarks>
42-
[Pure]
43-
[MethodImpl(MethodImplOptions.AggressiveInlining)]
44-
[Obsolete("Use ToByte instead.")]
45-
public static unsafe int ToInt(this bool flag)
46-
{
47-
return *(byte*)&flag;
48-
}
49-
5036
/// <summary>
5137
/// Converts the given <see cref="bool"/> value to an <see cref="int"/> mask with
5238
/// all bits representing the value of the input flag (either 0xFFFFFFFF or 0x00000000).

Microsoft.Toolkit.HighPerformance/Helpers/Internals/RuntimeHelpers.cs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
using System.Reflection;
1414
#endif
1515
using System.Runtime.CompilerServices;
16-
using Microsoft.Toolkit.HighPerformance.Extensions;
1716

1817
namespace Microsoft.Toolkit.HighPerformance.Helpers.Internals
1918
{
@@ -152,7 +151,7 @@ public static unsafe IntPtr GetObjectDataOrReferenceByteOffset<T>(object? obj, r
152151
return (IntPtr)Unsafe.AsPointer(ref data);
153152
}
154153

155-
return obj.DangerousGetObjectDataByteOffset(ref data);
154+
return ObjectMarshal.DangerousGetObjectDataByteOffset(obj, ref data);
156155
}
157156

158157
/// <summary>
@@ -174,7 +173,7 @@ public static unsafe ref T GetObjectDataAtOffsetOrPointerReference<T>(object? ob
174173
return ref Unsafe.AsRef<T>((void*)offset);
175174
}
176175

177-
return ref obj.DangerousGetObjectDataReferenceAt<T>(offset);
176+
return ref ObjectMarshal.DangerousGetObjectDataReferenceAt<T>(obj, offset);
178177
}
179178

180179
/// <summary>
@@ -274,7 +273,7 @@ private static IntPtr MeasureArrayDataByteOffset()
274273
{
275274
var array = new T[1];
276275

277-
return array.DangerousGetObjectDataByteOffset(ref array[0]);
276+
return ObjectMarshal.DangerousGetObjectDataByteOffset(array, ref array[0]);
278277
}
279278

280279
/// <summary>
@@ -286,7 +285,7 @@ private static IntPtr MeasureArray2DDataByteOffset()
286285
{
287286
var array = new T[1, 1];
288287

289-
return array.DangerousGetObjectDataByteOffset(ref array[0, 0]);
288+
return ObjectMarshal.DangerousGetObjectDataByteOffset(array, ref array[0, 0]);
290289
}
291290

292291
/// <summary>
@@ -298,7 +297,7 @@ private static IntPtr MeasureArray3DDataByteOffset()
298297
{
299298
var array = new T[1, 1, 1];
300299

301-
return array.DangerousGetObjectDataByteOffset(ref array[0, 0, 0]);
300+
return ObjectMarshal.DangerousGetObjectDataByteOffset(array, ref array[0, 0, 0]);
302301
}
303302
}
304303
}

Microsoft.Toolkit.HighPerformance/Extensions/ObjectExtensions.cs renamed to Microsoft.Toolkit.HighPerformance/Helpers/ObjectMarshal.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@
77
using System.Runtime.CompilerServices;
88
using System.Runtime.InteropServices;
99

10-
namespace Microsoft.Toolkit.HighPerformance.Extensions
10+
namespace Microsoft.Toolkit.HighPerformance.Helpers
1111
{
1212
/// <summary>
1313
/// Helpers for working with <see cref="object"/> instances.
1414
/// </summary>
15-
public static class ObjectExtensions
15+
public static class ObjectMarshal
1616
{
1717
/// <summary>
1818
/// Calculates the byte offset to a specific field within a given <see cref="object"/>.
@@ -29,7 +29,7 @@ public static class ObjectExtensions
2929
/// </remarks>
3030
[Pure]
3131
[MethodImpl(MethodImplOptions.AggressiveInlining)]
32-
public static IntPtr DangerousGetObjectDataByteOffset<T>(this object obj, ref T data)
32+
public static IntPtr DangerousGetObjectDataByteOffset<T>(object obj, ref T data)
3333
{
3434
var rawObj = Unsafe.As<RawObjectData>(obj)!;
3535
ref byte r0 = ref rawObj.Data;
@@ -53,7 +53,7 @@ public static IntPtr DangerousGetObjectDataByteOffset<T>(this object obj, ref T
5353
/// </remarks>
5454
[Pure]
5555
[MethodImpl(MethodImplOptions.AggressiveInlining)]
56-
public static ref T DangerousGetObjectDataReferenceAt<T>(this object obj, IntPtr offset)
56+
public static ref T DangerousGetObjectDataReferenceAt<T>(object obj, IntPtr offset)
5757
{
5858
var rawObj = Unsafe.As<RawObjectData>(obj)!;
5959
ref byte r0 = ref rawObj.Data;
@@ -97,7 +97,7 @@ private sealed class RawObjectData
9797
/// <remarks>
9898
/// This extension behaves just like the following method:
9999
/// <code>
100-
/// public static bool TryUnbox&lt;T>(this object obj, out T value)
100+
/// public static bool TryUnbox&lt;T>(object obj, out T value)
101101
/// {
102102
/// if (obj is T)
103103
/// {
@@ -139,7 +139,7 @@ public static bool TryUnbox<T>(this object obj, out T value)
139139
/// <exception cref="InvalidCastException">Thrown when <paramref name="obj"/> is not of type <typeparamref name="T"/>.</exception>
140140
[Pure]
141141
[MethodImpl(MethodImplOptions.AggressiveInlining)]
142-
public static ref T DangerousUnbox<T>(this object obj)
142+
public static ref T DangerousUnbox<T>(object obj)
143143
where T : struct
144144
{
145145
return ref Unsafe.Unbox<T>(obj);

Microsoft.Toolkit.HighPerformance/Memory/Memory2D{T}.cs

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
using Microsoft.Toolkit.HighPerformance.Buffers.Internals;
1414
#endif
1515
using Microsoft.Toolkit.HighPerformance.Extensions;
16+
using Microsoft.Toolkit.HighPerformance.Helpers;
1617
using Microsoft.Toolkit.HighPerformance.Memory.Internals;
1718
using Microsoft.Toolkit.HighPerformance.Memory.Views;
1819
using static Microsoft.Toolkit.HighPerformance.Helpers.Internals.RuntimeHelpers;
@@ -129,7 +130,7 @@ public Memory2D(T[] array, int offset, int height, int width, int pitch)
129130
}
130131

131132
this.instance = array;
132-
this.offset = array.DangerousGetObjectDataByteOffset(ref array.DangerousGetReferenceAt(offset));
133+
this.offset = ObjectMarshal.DangerousGetObjectDataByteOffset(array, ref array.DangerousGetReferenceAt(offset));
133134
this.height = height;
134135
this.width = width;
135136
this.pitch = pitch;
@@ -222,7 +223,7 @@ public Memory2D(T[,]? array, int row, int column, int height, int width)
222223
}
223224

224225
this.instance = array;
225-
this.offset = array.DangerousGetObjectDataByteOffset(ref array.DangerousGetReferenceAt(row, column));
226+
this.offset = ObjectMarshal.DangerousGetObjectDataByteOffset(array, ref array.DangerousGetReferenceAt(row, column));
226227
this.height = height;
227228
this.width = width;
228229
this.pitch = columns - width;
@@ -250,7 +251,7 @@ public Memory2D(T[,,] array, int depth)
250251
}
251252

252253
this.instance = array;
253-
this.offset = array.DangerousGetObjectDataByteOffset(ref array.DangerousGetReferenceAt(depth, 0, 0));
254+
this.offset = ObjectMarshal.DangerousGetObjectDataByteOffset(array, ref array.DangerousGetReferenceAt(depth, 0, 0));
254255
this.height = array.GetLength(1);
255256
this.width = array.GetLength(2);
256257
this.pitch = 0;
@@ -306,7 +307,7 @@ public Memory2D(T[,,] array, int depth, int row, int column, int height, int wid
306307
}
307308

308309
this.instance = array;
309-
this.offset = array.DangerousGetObjectDataByteOffset(ref array.DangerousGetReferenceAt(depth, row, column));
310+
this.offset = ObjectMarshal.DangerousGetObjectDataByteOffset(array, ref array.DangerousGetReferenceAt(depth, row, column));
310311
this.height = height;
311312
this.width = width;
312313
this.pitch = columns - width;
@@ -465,7 +466,7 @@ internal Memory2D(Memory<T> memory, int offset, int height, int width, int pitch
465466
ref char r0 = ref text.DangerousGetReferenceAt(textStart + offset);
466467

467468
this.instance = text;
468-
this.offset = text.DangerousGetObjectDataByteOffset(ref r0);
469+
this.offset = ObjectMarshal.DangerousGetObjectDataByteOffset(text, ref r0);
469470
}
470471
else if (MemoryMarshal.TryGetArray(memory, out ArraySegment<T> segment))
471472
{
@@ -477,7 +478,7 @@ internal Memory2D(Memory<T> memory, int offset, int height, int width, int pitch
477478
T[] array = segment.Array!;
478479

479480
this.instance = array;
480-
this.offset = array.DangerousGetObjectDataByteOffset(ref array.DangerousGetReferenceAt(segment.Offset + offset));
481+
this.offset = ObjectMarshal.DangerousGetObjectDataByteOffset(array, ref array.DangerousGetReferenceAt(segment.Offset + offset));
481482
}
482483
else if (MemoryMarshal.TryGetMemoryManager<T, MemoryManager<T>>(memory, out var memoryManager, out int memoryManagerStart, out _))
483484
{
@@ -549,7 +550,7 @@ public static Memory2D<T> DangerousCreate(object instance, ref T value, int heig
549550

550551
OverflowHelper.EnsureIsInNativeIntRange(height, width, pitch);
551552

552-
IntPtr offset = instance.DangerousGetObjectDataByteOffset(ref value);
553+
IntPtr offset = ObjectMarshal.DangerousGetObjectDataByteOffset(instance, ref value);
553554

554555
return new Memory2D<T>(instance, offset, height, width, pitch);
555556
}
@@ -615,7 +616,7 @@ public Span2D<T> Span
615616
}
616617
else
617618
{
618-
ref T r0 = ref this.instance.DangerousGetObjectDataReferenceAt<T>(this.offset);
619+
ref T r0 = ref ObjectMarshal.DangerousGetObjectDataReferenceAt<T>(this.instance, this.offset);
619620

620621
return new Span2D<T>(ref r0, this.height, this.width, this.pitch);
621622
}
@@ -749,7 +750,7 @@ public unsafe MemoryHandle Pin()
749750

750751
GCHandle handle = GCHandle.Alloc(this.instance, GCHandleType.Pinned);
751752

752-
void* pointer = Unsafe.AsPointer(ref this.instance.DangerousGetObjectDataReferenceAt<T>(this.offset));
753+
void* pointer = Unsafe.AsPointer(ref ObjectMarshal.DangerousGetObjectDataReferenceAt<T>(this.instance, this.offset));
753754

754755
return new MemoryHandle(pointer, handle);
755756
}
@@ -775,7 +776,7 @@ public bool TryGetMemory(out Memory<T> memory)
775776
else if (typeof(T) == typeof(char) && this.instance.GetType() == typeof(string))
776777
{
777778
string text = Unsafe.As<string>(this.instance)!;
778-
int index = text.AsSpan().IndexOf(in text.DangerousGetObjectDataReferenceAt<char>(this.offset));
779+
int index = text.AsSpan().IndexOf(in ObjectMarshal.DangerousGetObjectDataReferenceAt<char>(text, this.offset));
779780
ReadOnlyMemory<char> temp = text.AsMemory(index, (int)Length);
780781

781782
// The string type could still be present if a user ends up creating a
@@ -795,7 +796,7 @@ public bool TryGetMemory(out Memory<T> memory)
795796
{
796797
// If it's a T[] array, also handle the initial offset
797798
T[] array = Unsafe.As<T[]>(this.instance)!;
798-
int index = array.AsSpan().IndexOf(ref array.DangerousGetObjectDataReferenceAt<T>(this.offset));
799+
int index = array.AsSpan().IndexOf(ref ObjectMarshal.DangerousGetObjectDataReferenceAt<T>(array, this.offset));
799800

800801
memory = array.AsMemory(index, this.height * this.width);
801802
}

0 commit comments

Comments
 (0)