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 Oct 24, 2024
1 parent de396b6 commit 2ac8ec3
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 @@ -1077,7 +1077,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 @@ -1093,7 +1093,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 @@ -1125,7 +1130,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 @@ -1143,10 +1148,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 @@ -1156,7 +1171,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 @@ -1169,10 +1184,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 @@ -1181,7 +1206,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 @@ -1198,7 +1223,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 @@ -1222,7 +1252,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 @@ -1239,7 +1269,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 @@ -1263,18 +1298,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 2ac8ec3

Please sign in to comment.