Skip to content

Commit

Permalink
Get rid of P/Invoke SizeOf.
Browse files Browse the repository at this point in the history
  • Loading branch information
ericwj committed Jul 21, 2022
1 parent 072c997 commit a7c5597
Show file tree
Hide file tree
Showing 46 changed files with 185 additions and 176 deletions.
3 changes: 2 additions & 1 deletion sources/core/Stride.Core.Mathematics/BoundingSphere.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
*/
using System;
using System.Globalization;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;

namespace Stride.Core.Mathematics
Expand Down Expand Up @@ -194,7 +195,7 @@ public static unsafe void FromPoints(Vector3[] points, out BoundingSphere result
if (points == null) throw new ArgumentNullException("points");
fixed (void* pointsPtr = points)
{
FromPoints((IntPtr)pointsPtr, 0, points.Length, Utilities.SizeOf<Vector3>(), out result);
FromPoints((IntPtr)pointsPtr, 0, points.Length, Unsafe.SizeOf<Vector3>(), out result);
}
}

Expand Down
2 changes: 1 addition & 1 deletion sources/core/Stride.Core.Mathematics/Double2.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public struct Double2 : IEquatable<Double2>, IFormattable
/// <summary>
/// The size of the <see cref="Stride.Core.Mathematics.Double2"/> type, in bytes.
/// </summary>
public static readonly int SizeInBytes = Utilities.SizeOf<Double2>();
public static readonly int SizeInBytes = Unsafe.SizeOf<Double2>();

/// <summary>
/// A <see cref="Stride.Core.Mathematics.Double2"/> with all of its components set to zero.
Expand Down
2 changes: 1 addition & 1 deletion sources/core/Stride.Core.Mathematics/Double3.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public struct Double3 : IEquatable<Double3>, IFormattable
/// <summary>
/// The size of the <see cref="Stride.Core.Mathematics.Double3"/> type, in bytes.
/// </summary>
public static readonly int SizeInBytes = Utilities.SizeOf<Double3>();
public static readonly int SizeInBytes = Unsafe.SizeOf<Double3>();

/// <summary>
/// A <see cref="Stride.Core.Mathematics.Double3"/> with all of its components set to zero.
Expand Down
2 changes: 1 addition & 1 deletion sources/core/Stride.Core.Mathematics/Double4.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public struct Double4 : IEquatable<Double4>, IFormattable
/// <summary>
/// The size of the <see cref="Stride.Core.Mathematics.Double4"/> type, in bytes.
/// </summary>
public static readonly int SizeInBytes = Utilities.SizeOf<Double4>();
public static readonly int SizeInBytes = Unsafe.SizeOf<Double4>();

/// <summary>
/// A <see cref="Stride.Core.Mathematics.Double4"/> with all of its components set to zero.
Expand Down
3 changes: 2 additions & 1 deletion sources/core/Stride.Core.Mathematics/Half2.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

using System;
using System.ComponentModel;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using Stride.Core.Serialization;

Expand All @@ -38,7 +39,7 @@ public struct Half2 : IEquatable<Half2>
/// <summary>
/// The size of the <see cref="Stride.Core.Mathematics.Half2"/> type, in bytes.
/// </summary>
public static readonly int SizeInBytes = Utilities.SizeOf<Half2>();
public static readonly int SizeInBytes = Unsafe.SizeOf<Half2>();


/// <summary>
Expand Down
3 changes: 2 additions & 1 deletion sources/core/Stride.Core.Mathematics/Half3.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

using System;
using System.ComponentModel;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using Stride.Core.Serialization;

Expand All @@ -38,7 +39,7 @@ public struct Half3 : IEquatable<Half3>
/// <summary>
/// The size of the <see cref="Stride.Core.Mathematics.Half3"/> type, in bytes.
/// </summary>
public static readonly int SizeInBytes = Utilities.SizeOf<Half3>();
public static readonly int SizeInBytes = Unsafe.SizeOf<Half3>();

/// <summary>
/// A <see cref="Stride.Core.Mathematics.Half3"/> with all of its components set to zero.
Expand Down
3 changes: 2 additions & 1 deletion sources/core/Stride.Core.Mathematics/Half4.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

using System;
using System.ComponentModel;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using Stride.Core.Serialization;

Expand All @@ -38,7 +39,7 @@ public struct Half4 : IEquatable<Half4>
/// <summary>
/// The size of the <see cref="Stride.Core.Mathematics.Half4"/> type, in bytes.
/// </summary>
public static readonly int SizeInBytes = Utilities.SizeOf<Half4>();
public static readonly int SizeInBytes = Unsafe.SizeOf<Half4>();

/// <summary>
/// A <see cref="Stride.Core.Mathematics.Half4"/> with all of its components set to zero.
Expand Down
3 changes: 2 additions & 1 deletion sources/core/Stride.Core.Mathematics/Int2.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
*/
using System;
using System.Globalization;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;

namespace Stride.Core.Mathematics
Expand All @@ -43,7 +44,7 @@ public struct Int2 : IEquatable<Int2>, IFormattable
/// <summary>
/// The size of the <see cref="Stride.Core.Mathematics.Int2"/> type, in bytes.
/// </summary>
public static readonly int SizeInBytes = Utilities.SizeOf<Int2>();
public static readonly int SizeInBytes = Unsafe.SizeOf<Int2>();

/// <summary>
/// A <see cref="Stride.Core.Mathematics.Int2"/> with all of its components set to zero.
Expand Down
3 changes: 2 additions & 1 deletion sources/core/Stride.Core.Mathematics/Int3.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
*/
using System;
using System.Globalization;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;

namespace Stride.Core.Mathematics
Expand All @@ -43,7 +44,7 @@ public struct Int3 : IEquatable<Int3>, IFormattable
/// <summary>
/// The size of the <see cref="Int3"/> type, in bytes.
/// </summary>
public static readonly int SizeInBytes = Utilities.SizeOf<Int3>();
public static readonly int SizeInBytes = Unsafe.SizeOf<Int3>();

/// <summary>
/// A <see cref="Int3"/> with all of its components set to zero.
Expand Down
3 changes: 2 additions & 1 deletion sources/core/Stride.Core.Mathematics/Int4.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
// THE SOFTWARE.
using System;
using System.Globalization;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;

namespace Stride.Core.Mathematics
Expand All @@ -37,7 +38,7 @@ public struct Int4 : IEquatable<Int4>, IFormattable
/// <summary>
/// The size of the <see cref = "Int4" /> type, in bytes.
/// </summary>
public static readonly int SizeInBytes = Utilities.SizeOf<Int4>();
public static readonly int SizeInBytes = Unsafe.SizeOf<Int4>();

/// <summary>
/// A <see cref = "Int4" /> with all of its components set to zero.
Expand Down
2 changes: 1 addition & 1 deletion sources/core/Stride.Core.Mathematics/Matrix.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public struct Matrix : IEquatable<Matrix>, IFormattable
/// <summary>
/// The size of the <see cref="Stride.Core.Mathematics.Matrix"/> type, in bytes.
/// </summary>
public static readonly int SizeInBytes = Utilities.SizeOf<Matrix>();
public static readonly int SizeInBytes = Unsafe.SizeOf<Matrix>();

/// <summary>
/// A <see cref="Stride.Core.Mathematics.Matrix"/> with all of its components set to zero.
Expand Down
3 changes: 2 additions & 1 deletion sources/core/Stride.Core.Mathematics/Quaternion.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
*/
using System;
using System.Globalization;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using static System.MathF;

Expand All @@ -44,7 +45,7 @@ public struct Quaternion : IEquatable<Quaternion>, IFormattable
/// <summary>
/// The size of the <see cref="Stride.Core.Mathematics.Quaternion"/> type, in bytes.
/// </summary>
public static readonly int SizeInBytes = Utilities.SizeOf<Quaternion>();
public static readonly int SizeInBytes = Unsafe.SizeOf<Quaternion>();

/// <summary>
/// A <see cref="Stride.Core.Mathematics.Quaternion"/> with all of its components set to zero.
Expand Down
3 changes: 2 additions & 1 deletion sources/core/Stride.Core.Mathematics/UInt4.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
// THE SOFTWARE.
using System;
using System.Globalization;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using Stride.Core.Serialization;

Expand All @@ -37,7 +38,7 @@ public struct UInt4 : IEquatable<UInt4>, IFormattable
/// <summary>
/// The size of the <see cref = "UInt4" /> type, in bytes.
/// </summary>
public static readonly int SizeInBytes = Utilities.SizeOf<UInt4>();
public static readonly int SizeInBytes = Unsafe.SizeOf<UInt4>();

/// <summary>
/// A <see cref = "UInt4" /> with all of its components set to zero.
Expand Down
2 changes: 1 addition & 1 deletion sources/core/Stride.Core.Mathematics/Vector2.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public struct Vector2 : IEquatable<Vector2>, IFormattable
/// <summary>
/// The size of the <see cref="Stride.Core.Mathematics.Vector2"/> type, in bytes.
/// </summary>
public static readonly int SizeInBytes = Utilities.SizeOf<Vector2>();
public static readonly int SizeInBytes = Unsafe.SizeOf<Vector2>();

/// <summary>
/// A <see cref="Stride.Core.Mathematics.Vector2"/> with all of its components set to zero.
Expand Down
2 changes: 1 addition & 1 deletion sources/core/Stride.Core.Mathematics/Vector3.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public struct Vector3 : IEquatable<Vector3>, IFormattable
/// <summary>
/// The size of the <see cref="Stride.Core.Mathematics.Vector3"/> type, in bytes.
/// </summary>
public static readonly int SizeInBytes = Utilities.SizeOf<Vector3>();
public static readonly int SizeInBytes = Unsafe.SizeOf<Vector3>();

/// <summary>
/// A <see cref="Stride.Core.Mathematics.Vector3"/> with all of its components set to zero.
Expand Down
2 changes: 1 addition & 1 deletion sources/core/Stride.Core.Mathematics/Vector4.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public struct Vector4 : IEquatable<Vector4>, IFormattable
/// <summary>
/// The size of the <see cref="Stride.Core.Mathematics.Vector4"/> type, in bytes.
/// </summary>
public static readonly int SizeInBytes = Utilities.SizeOf<Vector4>();
public static readonly int SizeInBytes = Unsafe.SizeOf<Vector4>();

/// <summary>
/// A <see cref="Stride.Core.Mathematics.Vector4"/> with all of its components set to zero.
Expand Down
7 changes: 4 additions & 3 deletions sources/core/Stride.Core.Tests/TestUtilities.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright (c) .NET Foundation and Contributors (https://dotnetfoundation.org/ & https://stride3d.net) and Silicon Studio Corp. (https://www.siliconstudio.co.jp)
// Distributed under the MIT license. See the LICENSE.md file in the project root for more information.
using System;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using Xunit;

Expand Down Expand Up @@ -28,7 +29,7 @@ public unsafe void Base()
var s = new S { A = 32, B = 33 };

// Check SizeOf
Assert.Equal(8, Utilities.SizeOf<S>());
Assert.Equal(8, Unsafe.SizeOf<S>());

// Write
Utilities.Write(data + 4, ref s);
Expand All @@ -37,11 +38,11 @@ public unsafe void Base()
Assert.Equal(s, s2);

// CopyMemory+Fixed (with offset)
Utilities.CopyMemory(data + 12, (IntPtr)Interop.Fixed(ref s) + 4, Utilities.SizeOf<int>());
Utilities.CopyMemory(data + 12, (IntPtr)Interop.Fixed(ref s) + 4, sizeof(int));
int b = 0;
Utilities.Read(data + 12, ref b);
Assert.Equal(s.B, b);

// FreeMemory
Utilities.FreeMemory(data);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System;
using System.Collections.Generic;
using System.Reflection;
using System.Runtime.CompilerServices;
using Stride.Core.Annotations;

namespace Stride.Core.Serialization.Serializers
Expand Down Expand Up @@ -319,7 +320,7 @@ public class BlittableArraySerializer<T> : ArraySerializer<T>
/// <inheritdoc/>
public override void Initialize(SerializerSelector serializerSelector)
{
elementSize = Interop.SizeOf<T>();
elementSize = Unsafe.SizeOf<T>();
}

/// <inheritdoc/>
Expand Down
8 changes: 4 additions & 4 deletions sources/core/Stride.Core/Storage/ObjectIdBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -245,10 +245,10 @@ public void Write<T>(T data) where T : struct
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void Write<T>(T[] buffer, int offset, int count) where T : struct
{
var ptr = (IntPtr)Interop.Fixed(buffer) + offset * Interop.SizeOf<T>();
Write((byte*)ptr, count * Interop.SizeOf<T>());
var ptr = (IntPtr)Interop.Fixed(buffer) + offset * Unsafe.SizeOf<T>();
Write((byte*)ptr, count * Unsafe.SizeOf<T>());
}

/// <summary>
/// Writes the specified buffer to this instance.
/// </summary>
Expand All @@ -258,7 +258,7 @@ public void Write<T>(T[] buffer, int offset, int count) where T : struct
public void Write<T>(ref T data) where T : struct
{
var ptr = (byte*)Interop.Fixed(ref data);
Write(ptr, Interop.SizeOf<T>());
Write(ptr, Unsafe.SizeOf<T>());
}
#endif

Expand Down
2 changes: 1 addition & 1 deletion sources/core/Stride.Core/Storage/ObjectIdSimpleBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ public void Write(int data)
public void Write<T>(T data) where T : struct
{
var pData = (int*)Interop.Fixed(ref data);
var count = Utilities.SizeOf<T>() >> 2;
var count = Unsafe.SizeOf<T>() >> 2;
for (var i = 0; i < count; i++)
{
Write(*pData++);
Expand Down
5 changes: 3 additions & 2 deletions sources/core/Stride.Core/UnmanagedArray.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright (c) .NET Foundation and Contributors (https://dotnetfoundation.org/ & https://stride3d.net) and Silicon Studio Corp. (https://www.siliconstudio.co.jp)
// Distributed under the MIT license. See the LICENSE.md file in the project root for more information.
using System;
using System.Runtime.CompilerServices;
using Stride.Core.Annotations;

namespace Stride.Core
Expand All @@ -13,7 +14,7 @@ public class UnmanagedArray<T> : IDisposable where T : struct
public UnmanagedArray(int length)
{
Length = length;
sizeOfT = Utilities.SizeOf<T>();
sizeOfT = Unsafe.SizeOf<T>();
var finalSize = length * sizeOfT;
Pointer = Utilities.AllocateMemory(finalSize);
isShared = false;
Expand All @@ -22,7 +23,7 @@ public UnmanagedArray(int length)
public UnmanagedArray(int length, IntPtr unmanagedDataPtr)
{
Length = length;
sizeOfT = Utilities.SizeOf<T>();
sizeOfT = Unsafe.SizeOf<T>();
Pointer = unmanagedDataPtr;
isShared = true;
}
Expand Down
7 changes: 4 additions & 3 deletions sources/core/Stride.Core/Utilities.Interop.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,17 @@
// Distributed under the MIT license. See the LICENSE.md file in the project root for more information.
//
// Copyright (c) 2010-2012 SharpDX - Alexandre Mutel
//
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
Expand Down Expand Up @@ -141,6 +141,7 @@ public static unsafe void CopyInlineOut<T>(out T data, void* pSrc)
throw new NotImplementedException();
}

[Obsolete("Use System.Runtime.CompilerServices.Unsafe.SizeOf<T>()", error: true)]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static int SizeOf<T>()
{
Expand Down
26 changes: 3 additions & 23 deletions sources/core/Stride.Core/Utilities.cs
Original file line number Diff line number Diff line change
Expand Up @@ -111,20 +111,10 @@ public static void ClearMemory(IntPtr dest, byte value, int sizeInBytesToClear)
/// </summary>
/// <typeparam name="T">a struct to evaluate</typeparam>
/// <returns>sizeof this struct</returns>
[Obsolete("Use System.Runtime.CompilerServices.Unsafe.SizeOf<T>()")]
public static int SizeOf<T>() where T : struct
{
return Interop.SizeOf<T>();
}

/// <summary>
/// Return the sizeof an array of struct. Equivalent to sizeof operator but works on generics too.
/// </summary>
/// <typeparam name="T">a struct</typeparam>
/// <param name="array">The array of struct to evaluate.</param>
/// <returns>sizeof in bytes of this array of struct</returns>
public static int SizeOf<T>(T[] array) where T : struct
{
return array == null ? 0 : array.Length * Interop.SizeOf<T>();
return Unsafe.SizeOf<T>();
}

/// <summary>
Expand Down Expand Up @@ -164,7 +154,7 @@ public static byte[] ToByteArray<T>(T[] source) where T : struct
{
if (source == null) return null;

var buffer = new byte[SizeOf<T>() * source.Length];
var buffer = new byte[Unsafe.SizeOf<T>() * source.Length];

if (source.Length == 0)
return buffer;
Expand Down Expand Up @@ -804,16 +794,6 @@ internal static void UnsafeReadOut<T>(IntPtr source, out T data)
}
}

/// <summary>
/// Return the sizeof a struct from a CLR. Equivalent to sizeof operator but works on generics too.
/// </summary>
/// <typeparam name="T">a struct to evaluate</typeparam>
/// <returns>sizeof this struct</returns>
internal static int UnsafeSizeOf<T>()
{
return Interop.SizeOf<T>();
}

/// <summary>
/// Linq assisted full tree iteration and collection in a single line.
/// Warning, could be slow.
Expand Down
Loading

0 comments on commit a7c5597

Please sign in to comment.