Skip to content

Commit

Permalink
Merge branch 'master' into v53
Browse files Browse the repository at this point in the history
  • Loading branch information
hyazinthh committed Jun 17, 2024
2 parents be3d88e + 2142e3f commit 66fdecc
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 27 deletions.
2 changes: 1 addition & 1 deletion src/Aardvark.Base.IO/Aardvark.Base.IO.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<NoWarn>612;1591</NoWarn>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<LangVersion>8.0</LangVersion>
<LangVersion>9.0</LangVersion>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
<OutputPath>..\..\bin\Debug</OutputPath>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<PropertyGroup>
<OutputType>Library</OutputType>
<TargetFramework>netstandard2.0</TargetFramework>
<LangVersion>8.0</LangVersion>
<LangVersion>9.0</LangVersion>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<NoWarn>1591</NoWarn>
</PropertyGroup>
Expand Down
2 changes: 1 addition & 1 deletion src/Aardvark.Base/Aardvark.Base.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks>net8.0;netstandard2.0</TargetFrameworks>
<LangVersion>8.0</LangVersion>
<LangVersion>9.0</LangVersion>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
Expand Down
21 changes: 6 additions & 15 deletions src/Aardvark.Base/Extensions/ArrayExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2265,7 +2265,7 @@ public static void CopyTo(this Array input, int offset, int length, IntPtr targe
try
{
var typeSize = input.GetType().GetElementType().GetCLRSize();
var dataSize = input.Length * typeSize;
var dataSize = length * typeSize;

unsafe
{
Expand All @@ -2281,14 +2281,10 @@ public static void CopyTo(this Array input, int offset, int length, IntPtr targe
}

public static void CopyTo(this Array input, int length, IntPtr target)
{
CopyTo(input, 0, length, target);
}
=> CopyTo(input, 0, length, target);

public static void CopyTo(this Array input, IntPtr target)
{
CopyTo(input, 0, input.Length, target);
}
=> CopyTo(input, 0, input.Length, target);

public static void CopyTo(this IntPtr input, Array target, int offset, int length)
{
Expand All @@ -2297,7 +2293,7 @@ public static void CopyTo(this IntPtr input, Array target, int offset, int lengt
try
{
var typeSize = target.GetType().GetElementType().GetCLRSize();
var dataSize = target.Length * typeSize;
var dataSize = length * typeSize;

unsafe
{
Expand All @@ -2312,15 +2308,10 @@ public static void CopyTo(this IntPtr input, Array target, int offset, int lengt
}

public static void CopyTo(this IntPtr input, Array target, int length)
{
CopyTo(input, target, 0, length);
}
=> CopyTo(input, target, 0, length);

public static void CopyTo(this IntPtr input, Array target)
{
CopyTo(input, target, target.Length);
}

=> CopyTo(input, target, target.Length);

public static void CopyTo(this IntPtr input, IntPtr target, int size)
{
Expand Down
29 changes: 20 additions & 9 deletions src/Aardvark.Base/Extensions/UnsafeCoerce.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,31 @@
using System;
using System.Collections.Concurrent;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;

namespace Aardvark.Base
{
public static class ArrayUnsafeCoerceExtensions
{
private static readonly MethodInfo unsafeSizeOfMeth = typeof(Unsafe).GetMethod(nameof(Unsafe.SizeOf), BindingFlags.Public | BindingFlags.Static);

private static readonly ConcurrentDictionary<Type, int> unsafeTypeSizes = new();

/// <summary>
/// Returns the managed size of the given type.
/// </summary>
public static int GetCLRSize(this Type t)
{
return unsafeTypeSizes.GetOrAdd(t, t =>
{
var mi = unsafeSizeOfMeth.MakeGenericMethod(t);
return (int)mi.Invoke(null, null);
});
}

#region UnsafeCoerce

[Obsolete("breaks net8.0+")]
public static IntPtr GetTypeIdUncached<T>()
where T : struct
Expand All @@ -32,14 +51,6 @@ private static IntPtr GetTypeId<T>()
return typeId;
}

public static int GetCLRSize(this Type t)
{
// TODO: somehow make use of sizeof operator -> requires compile time type -> cannot use ILGenerator in .net standard
if (t == typeof(char)) return 2; // Marshal.SizeOf = 1
if (t == typeof(bool)) return 1; // Marshal.SizeOf = 4
return Marshal.SizeOf(t);
}

[Obsolete("breaks net8.0+")]
internal static TR[] UnsafeCoerce<TR>(this Array input, IntPtr targetId)
where TR : struct
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
<Compile Include="SortedSetExt.fs" />
<Compile Include="MapExt.fs" />
<Compile Include="OrderMaintenanceTrie.fs" />
<Compile Include="SizeOfTests.fs" />
<Compile Include="Program.fs" />
</ItemGroup>
<ItemGroup>
Expand Down
25 changes: 25 additions & 0 deletions src/Tests/Aardvark.Base.FSharp.Tests/SizeOfTests.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
namespace Aardvark.Base.FSharp.Tests

open Aardvark.Base
open System.Runtime.InteropServices

open FsUnit
open NUnit.Framework

module SizeOfTests =

[<Struct; StructLayout(LayoutKind.Sequential)>]
type MyStruct =
val A : bool
val B : bool
val C : char

[<Test>]
let ``[GetCLRSize] Primitive types``() =
typeof<int64>.GetCLRSize() |> should equal 8
typeof<bool>.GetCLRSize() |> should equal 1
typeof<char>.GetCLRSize() |> should equal 2

[<Test>]
let ``[GetCLRSize] Struct``() =
typeof<MyStruct>.GetCLRSize() |> should equal 4

0 comments on commit 66fdecc

Please sign in to comment.