Skip to content

Commit

Permalink
Update 'MarshalNonBlittable' for AOT support
Browse files Browse the repository at this point in the history
  • Loading branch information
Sergio0694 committed Nov 25, 2024
1 parent ebf783f commit 55e16a8
Showing 1 changed file with 60 additions and 15 deletions.
75 changes: 60 additions & 15 deletions src/WinRT.Runtime/Marshalers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1092,7 +1092,7 @@ public void Dispose()

public static new unsafe MarshalerArray CreateMarshalerArray(T[] array)
{
#if NET
#if NET && !NET9_0_OR_GREATER
if (!RuntimeFeature.IsDynamicCodeCompiled)
{
throw new NotSupportedException($"Cannot handle array marshalling for non blittable type '{typeof(T)}'.");
Expand All @@ -1108,7 +1108,12 @@ public void Dispose()
{
int length = array.Length;
#pragma warning disable IL3050 // https://github.com/dotnet/runtime/issues/97273
var abi_element_size = Marshal.SizeOf(AbiType);
var abi_element_size =
#if NET9_0_OR_GREATER
RuntimeHelpers.SizeOf(AbiType.TypeHandle);
#else
Marshal.SizeOf(AbiType);
#endif
#pragma warning restore IL3050
var byte_length = length * abi_element_size;
m._array = Marshal.AllocCoTaskMem(byte_length);
Expand Down Expand Up @@ -1140,7 +1145,7 @@ public void Dispose()

public static new unsafe T[] FromAbiArray(object box)
{
#if NET
#if NET && !NET9_0_OR_GREATER
if (!RuntimeFeature.IsDynamicCodeCompiled)
{
throw new NotSupportedException($"Cannot handle array marshalling for non blittable type '{typeof(T)}'.");
Expand All @@ -1158,10 +1163,20 @@ public void Dispose()
var array = new T[abi.length];
var data = (byte*)abi.data.ToPointer();
#pragma warning disable IL3050 // https://github.com/dotnet/runtime/issues/97273
var abi_element_size = Marshal.SizeOf(AbiType);
var abi_element_size =
#if NET9_0_OR_GREATER
RuntimeHelpers.SizeOf(AbiType.TypeHandle);
#else
Marshal.SizeOf(AbiType);
#endif
for (int i = 0; i < abi.length; i++)
{
var abi_element = Marshal.PtrToStructure((IntPtr)data, AbiType);
var abi_element =
#if NET9_0_OR_GREATER
RuntimeHelpers.Box(ref *data, AbiType.TypeHandle);
#else
Marshal.PtrToStructure((IntPtr)data, AbiType);
#endif
#pragma warning restore IL3050
array[i] = Marshaler<T>.FromAbi(abi_element);
data += abi_element_size;
Expand All @@ -1171,7 +1186,7 @@ public void Dispose()

public static unsafe void CopyAbiArray(T[] array, object box)
{
#if NET
#if NET && !NET9_0_OR_GREATER
if (!RuntimeFeature.IsDynamicCodeCompiled)
{
throw new NotSupportedException($"Cannot handle array marshalling for non blittable type '{typeof(T)}'.");
Expand All @@ -1184,10 +1199,20 @@ public static unsafe void CopyAbiArray(T[] array, object box)
}
var data = (byte*)abi.data.ToPointer();
#pragma warning disable IL3050 // https://github.com/dotnet/runtime/issues/97273
var abi_element_size = Marshal.SizeOf(AbiType);
var abi_element_size =
#if NET9_0_OR_GREATER
RuntimeHelpers.SizeOf(AbiType.TypeHandle);
#else
Marshal.SizeOf(AbiType);
#endif
for (int i = 0; i < abi.length; i++)
{
var abi_element = Marshal.PtrToStructure((IntPtr)data, AbiType);
var abi_element =
#if NET9_0_OR_GREATER
RuntimeHelpers.Box(ref *data, AbiType.TypeHandle);
#else
Marshal.PtrToStructure((IntPtr)data, AbiType);
#endif
#pragma warning restore IL3050
array[i] = Marshaler<T>.FromAbi(abi_element);
data += abi_element_size;
Expand All @@ -1196,7 +1221,7 @@ public static unsafe void CopyAbiArray(T[] array, object box)

public static new unsafe (int length, IntPtr data) FromManagedArray(T[] array)
{
#if NET
#if NET && !NET9_0_OR_GREATER
if (!RuntimeFeature.IsDynamicCodeCompiled)
{
throw new NotSupportedException($"Cannot handle array marshalling for non blittable type '{typeof(T)}'.");
Expand All @@ -1213,7 +1238,12 @@ public static unsafe void CopyAbiArray(T[] array, object box)
{
int length = array.Length;
#pragma warning disable IL3050 // https://github.com/dotnet/runtime/issues/97273
var abi_element_size = Marshal.SizeOf(AbiType);
var abi_element_size =
#if NET9_0_OR_GREATER
RuntimeHelpers.SizeOf(AbiType.TypeHandle);
#else
Marshal.SizeOf(AbiType);
#endif
#pragma warning restore IL3050
var byte_length = length * abi_element_size;
data = Marshal.AllocCoTaskMem(byte_length);
Expand All @@ -1237,7 +1267,7 @@ public static unsafe void CopyAbiArray(T[] array, object box)

public static unsafe void CopyManagedArray(T[] array, IntPtr data)
{
#if NET
#if NET && !NET9_0_OR_GREATER
if (!RuntimeFeature.IsDynamicCodeCompiled)
{
throw new NotSupportedException($"Cannot handle array marshalling for non blittable type '{typeof(T)}'.");
Expand All @@ -1254,7 +1284,12 @@ public static unsafe void CopyManagedArray(T[] array, IntPtr data)
{
int length = array.Length;
#pragma warning disable IL3050 // https://github.com/dotnet/runtime/issues/97273
var abi_element_size = Marshal.SizeOf(AbiType);
var abi_element_size =
#if NET9_0_OR_GREATER
RuntimeHelpers.SizeOf(AbiType.TypeHandle);
#else
Marshal.SizeOf(AbiType);
#endif
#pragma warning restore IL3050
var byte_length = length * abi_element_size;
var bytes = (byte*)data.ToPointer();
Expand All @@ -1278,18 +1313,28 @@ public static unsafe void CopyManagedArray(T[] array, IntPtr data)

public static unsafe void DisposeAbiArrayElements((int length, IntPtr data) abi)
{
#if NET
#if NET && !NET9_0_OR_GREATER
if (!RuntimeFeature.IsDynamicCodeCompiled)
{
throw new NotSupportedException($"Cannot handle array marshalling for non blittable type '{typeof(T)}'.");
}
#endif
var data = (byte*)abi.data.ToPointer();
#pragma warning disable IL3050 // https://github.com/dotnet/runtime/issues/97273
var abi_element_size = Marshal.SizeOf(AbiType);
var abi_element_size =
#if NET9_0_OR_GREATER
RuntimeHelpers.SizeOf(AbiType.TypeHandle);
#else
Marshal.SizeOf(AbiType);
#endif
for (int i = 0; i < abi.length; i++)
{
var abi_element = Marshal.PtrToStructure((IntPtr)data, AbiType);
var abi_element =
#if NET9_0_OR_GREATER
RuntimeHelpers.Box(ref *data, AbiType.TypeHandle);
#else
Marshal.PtrToStructure((IntPtr)data, AbiType);
#endif
#pragma warning restore IL3050
Marshaler<T>.DisposeAbi(abi_element);
data += abi_element_size;
Expand Down

0 comments on commit 55e16a8

Please sign in to comment.