Skip to content

Commit

Permalink
Merge branch 'master' into test-chop-imageex
Browse files Browse the repository at this point in the history
  • Loading branch information
michael-hawker authored Feb 17, 2021
2 parents b386021 + 065ebe4 commit d29e7b9
Show file tree
Hide file tree
Showing 166 changed files with 92 additions and 11,173 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -49,7 +50,7 @@ public RawObjectMemoryManager(object instance, IntPtr offset, int length)
/// <inheritdoc/>
public override Span<T> GetSpan()
{
ref T r0 = ref this.instance.DangerousGetObjectDataReferenceAt<T>(this.offset);
ref T r0 = ref ObjectMarshal.DangerousGetObjectDataReferenceAt<T>(this.instance, this.offset);

return MemoryMarshal.CreateSpan(ref r0, this.length);
}
Expand All @@ -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<T>(this.offset);
ref T r0 = ref ObjectMarshal.DangerousGetObjectDataReferenceAt<T>(this.instance, this.offset);
ref T r1 = ref Unsafe.Add(ref r0, (nint)(uint)elementIndex);
void* p = Unsafe.AsPointer(ref r1);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

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

return ref array.DangerousGetObjectDataReferenceAt<T>(offset);
return ref ObjectMarshal.DangerousGetObjectDataReferenceAt<T>(array, offset);
#endif
}

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

return ref ri;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

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

return ref array.DangerousGetObjectDataReferenceAt<T>(offset);
return ref ObjectMarshal.DangerousGetObjectDataReferenceAt<T>(array, offset);
#endif
}

Expand Down Expand Up @@ -72,7 +73,7 @@ public static ref T DangerousGetReferenceAt<T>(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<T>();
ref T r0 = ref array.DangerousGetObjectDataReferenceAt<T>(offset);
ref T r0 = ref ObjectMarshal.DangerousGetObjectDataReferenceAt<T>(array, offset);
ref T ri = ref Unsafe.Add(ref r0, index);

return ref ri;
Expand Down Expand Up @@ -137,7 +138,7 @@ public static RefEnumerable<T> GetRow<T>(this T[,] array, int row)
return new RefEnumerable<T>(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<T>(array, offset, width, 1);
#endif
Expand Down Expand Up @@ -191,7 +192,7 @@ public static RefEnumerable<T> GetColumn<T>(this T[,] array, int column)
return new RefEnumerable<T>(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<T>(array, offset, height, width);
#endif
Expand Down Expand Up @@ -324,7 +325,7 @@ public static Memory<T> GetRowMemory<T>(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<T>(array, offset, array.GetLength(1)).Memory;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -38,7 +39,7 @@ public static ref T DangerousGetReference<T>(this T[,,] array)
#else
IntPtr offset = RuntimeHelpers.GetArray3DDataByteOffset<T>();

return ref array.DangerousGetObjectDataReferenceAt<T>(offset);
return ref ObjectMarshal.DangerousGetObjectDataReferenceAt<T>(array, offset);
#endif
}

Expand Down Expand Up @@ -78,7 +79,7 @@ public static ref T DangerousGetReferenceAt<T>(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<T>();
ref T r0 = ref array.DangerousGetObjectDataReferenceAt<T>(offset);
ref T r0 = ref ObjectMarshal.DangerousGetObjectDataReferenceAt<T>(array, offset);
ref T ri = ref Unsafe.Add(ref r0, index);

return ref ri;
Expand Down Expand Up @@ -212,7 +213,7 @@ public static Memory<T> AsMemory<T>(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<T>(array, offset, length).Memory;
Expand Down
14 changes: 0 additions & 14 deletions Microsoft.Toolkit.HighPerformance/Extensions/BoolExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,20 +33,6 @@ public static unsafe byte ToByte(this bool flag)
return *(byte*)&copy;
}

/// <summary>
/// Converts the given <see cref="bool"/> value into an <see cref="int"/>.
/// </summary>
/// <param name="flag">The input value to convert.</param>
/// <returns>1 if <paramref name="flag"/> is <see langword="true"/>, 0 otherwise.</returns>
/// <remarks>This method does not contain branching instructions.</remarks>
[Pure]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
[Obsolete("Use ToByte instead.")]
public static unsafe int ToInt(this bool flag)
{
return *(byte*)&flag;
}

/// <summary>
/// Converts the given <see cref="bool"/> value to an <see cref="int"/> mask with
/// all bits representing the value of the input flag (either 0xFFFFFFFF or 0x00000000).
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
using System.Reflection;
#endif
using System.Runtime.CompilerServices;
using Microsoft.Toolkit.HighPerformance.Extensions;

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

return obj.DangerousGetObjectDataByteOffset(ref data);
return ObjectMarshal.DangerousGetObjectDataByteOffset(obj, ref data);
}

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

return ref obj.DangerousGetObjectDataReferenceAt<T>(offset);
return ref ObjectMarshal.DangerousGetObjectDataReferenceAt<T>(obj, offset);
}

/// <summary>
Expand Down Expand Up @@ -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]);
}

/// <summary>
Expand All @@ -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]);
}

/// <summary>
Expand All @@ -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]);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;

namespace Microsoft.Toolkit.HighPerformance.Extensions
namespace Microsoft.Toolkit.HighPerformance.Helpers
{
/// <summary>
/// Helpers for working with <see cref="object"/> instances.
/// </summary>
public static class ObjectExtensions
public static class ObjectMarshal
{
/// <summary>
/// Calculates the byte offset to a specific field within a given <see cref="object"/>.
Expand All @@ -29,7 +29,7 @@ public static class ObjectExtensions
/// </remarks>
[Pure]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static IntPtr DangerousGetObjectDataByteOffset<T>(this object obj, ref T data)
public static IntPtr DangerousGetObjectDataByteOffset<T>(object obj, ref T data)
{
var rawObj = Unsafe.As<RawObjectData>(obj)!;
ref byte r0 = ref rawObj.Data;
Expand All @@ -53,7 +53,7 @@ public static IntPtr DangerousGetObjectDataByteOffset<T>(this object obj, ref T
/// </remarks>
[Pure]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static ref T DangerousGetObjectDataReferenceAt<T>(this object obj, IntPtr offset)
public static ref T DangerousGetObjectDataReferenceAt<T>(object obj, IntPtr offset)
{
var rawObj = Unsafe.As<RawObjectData>(obj)!;
ref byte r0 = ref rawObj.Data;
Expand Down Expand Up @@ -97,7 +97,7 @@ private sealed class RawObjectData
/// <remarks>
/// This extension behaves just like the following method:
/// <code>
/// public static bool TryUnbox&lt;T>(this object obj, out T value)
/// public static bool TryUnbox&lt;T>(object obj, out T value)
/// {
/// if (obj is T)
/// {
Expand Down Expand Up @@ -139,7 +139,7 @@ public static bool TryUnbox<T>(this object obj, out T value)
/// <exception cref="InvalidCastException">Thrown when <paramref name="obj"/> is not of type <typeparamref name="T"/>.</exception>
[Pure]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static ref T DangerousUnbox<T>(this object obj)
public static ref T DangerousUnbox<T>(object obj)
where T : struct
{
return ref Unsafe.Unbox<T>(obj);
Expand Down
23 changes: 12 additions & 11 deletions Microsoft.Toolkit.HighPerformance/Memory/Memory2D{T}.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -465,7 +466,7 @@ internal Memory2D(Memory<T> 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<T> segment))
{
Expand All @@ -477,7 +478,7 @@ internal Memory2D(Memory<T> 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<T, MemoryManager<T>>(memory, out var memoryManager, out int memoryManagerStart, out _))
{
Expand Down Expand Up @@ -549,7 +550,7 @@ public static Memory2D<T> 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<T>(instance, offset, height, width, pitch);
}
Expand Down Expand Up @@ -615,7 +616,7 @@ public Span2D<T> Span
}
else
{
ref T r0 = ref this.instance.DangerousGetObjectDataReferenceAt<T>(this.offset);
ref T r0 = ref ObjectMarshal.DangerousGetObjectDataReferenceAt<T>(this.instance, this.offset);

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

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

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

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

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

memory = array.AsMemory(index, this.height * this.width);
}
Expand Down
Loading

0 comments on commit d29e7b9

Please sign in to comment.