Added new numeric types for unsigned integers, including uint2, uint3, and uint4, along with their matrix types. Added a new `quaternion` struct with constructors and methods for creating and manipulating quaternions. Added methods for projecting and reflecting vectors, enhancing geometric operations. Added utility functions for generating orthonormal bases and changing vector signs. Added comprehensive unit tests for new mathematical functions and quaternion operations. Added a high-performance job scheduling system with job management features and worker thread management. Added new structs for job execution, allowing efficient job scheduling and execution. Added utility functions for job execution, including methods for obtaining unique job IDs. Changed access modifiers and property definitions in several files for improved clarity and maintainability. Changed property definitions and method implementations in `ImageInfo.cs`, `ImageResult.cs`, and `ImageResultFloat.cs` for better readability. Changed memory management functions in `CRuntime.cs` and improved memory allocation tracking in `MemoryStats.cs`. Changed the project file to include references to necessary projects and enable unsafe code blocks. Removed the `WorkerThreadPool.cs` file, integrating worker thread management directly into the `JobScheduler`. Removed the `float4` struct and its associated methods and properties, transitioning to a new code generation strategy. Removed the `float4.tt` template and other related files, indicating a shift in code generation approach. Removed the `Vectorize.cs` file, indicating a change in how vector operations are handled. Updated the `.gitignore` file to include IDE-specific settings. Updated various XML files to define project components and structure. Updated the `AllocationManager.cs` to improve memory allocation management and introduce new strategies. Updated the `UnsafeArray.cs`, `UnsafeHashMap.cs`, and `UnsafeList.cs` to enhance performance and safety in unsafe contexts. Updated error handling and function pointer management in `MemoryLeakException.cs` and `FunctionPointer.cs`. Updated the `AssemblyInfo.cs` file to include global using directives for better code organization.
159 lines
8.2 KiB
C#
159 lines
8.2 KiB
C#
using Misaki.HighPerformance.LowLevel.Collections;
|
|
using System.Runtime.CompilerServices;
|
|
|
|
namespace Misaki.HighPerformance.LowLevel.Helpers;
|
|
|
|
public static unsafe class UnsafeUtilities
|
|
{
|
|
/// <summary>
|
|
/// Converts a pointer to a reference of a specified type.
|
|
/// </summary>
|
|
/// <typeparam name="T">Specifies the type of the reference to be created from the pointer.</typeparam>
|
|
/// <param name="ptr">Represents the memory address to be converted into a reference.</param>
|
|
/// <returns>Returns a reference of the specified type pointing to the given memory address.</returns>
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
public static ref T AsRef<T>(void* ptr)
|
|
where T : unmanaged
|
|
{
|
|
return ref *(T*)ptr;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Returns the address of a specified variable in memory.
|
|
/// </summary>
|
|
/// <typeparam name="T">Represents the type of the variable whose address is being retrieved.</typeparam>
|
|
/// <param name="value">The variable whose memory address is to be obtained.</param>
|
|
/// <returns>A pointer to the memory address of the specified variable.</returns>
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
public static void* AddressOf<T>(ref T value)
|
|
where T : unmanaged
|
|
{
|
|
return Unsafe.AsPointer(ref value);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Reads an element from an unmanaged array at a specified index using a pointer.
|
|
/// </summary>
|
|
/// <typeparam name="T">Specifies the type of elements in the unmanaged array.</typeparam>
|
|
/// <param name="ptr">Points to the start of the unmanaged array from which the element is read.</param>
|
|
/// <param name="index">Indicates the position of the element to be accessed within the array.</param>
|
|
/// <returns>Returns a pointer to the element located at the specified index.</returns>
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
public static T* ReadArrayElementUnsafe<T>(void* ptr, int index)
|
|
where T : unmanaged
|
|
{
|
|
return (T*)((byte*)ptr + index * sizeof(T));
|
|
}
|
|
|
|
/// <summary>
|
|
/// Reads an element from an unmanaged array at a specified index using a pointer.
|
|
/// </summary>
|
|
/// <typeparam name="T">Specifies the type of elements in the unmanaged array.</typeparam>
|
|
/// <param name="ptr">Points to the start of the unmanaged array from which the element is read.</param>
|
|
/// <param name="index">Indicates the position of the element to be accessed within the array.</param>
|
|
/// <returns>Returns a pointer to the element located at the specified index.</returns>
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
public static T* ReadArrayElementUnsafe<T>(void* ptr, uint index)
|
|
where T : unmanaged
|
|
{
|
|
return (T*)((byte*)ptr + index * sizeof(T));
|
|
}
|
|
|
|
/// <summary>
|
|
/// Reads an element from an unmanaged array using a pointer and index, returning a reference to the element.
|
|
/// </summary>
|
|
/// <typeparam name="T">Specifies the type of the elements in the unmanaged array.</typeparam>
|
|
/// <param name="ptr">Points to the start of the unmanaged array from which the element is read.</param>
|
|
/// <param name="index">Indicates the position of the element to be accessed in the array.</param>
|
|
/// <returns>A reference to the specified element in the unmanaged array.</returns>
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
public static ref T ReadArrayElementRef<T>(void* ptr, int index)
|
|
where T : unmanaged
|
|
{
|
|
return ref AsRef<T>(ReadArrayElementUnsafe<T>(ptr, index));
|
|
}
|
|
|
|
/// <summary>
|
|
/// Reads an element from an unmanaged array using a pointer and index, returning a reference to the element.
|
|
/// </summary>
|
|
/// <typeparam name="T">Specifies the type of the elements in the unmanaged array.</typeparam>
|
|
/// <param name="ptr">Points to the start of the unmanaged array from which the element is read.</param>
|
|
/// <param name="index">Indicates the position of the element to be accessed in the array.</param>
|
|
/// <returns>A reference to the specified element in the unmanaged array.</returns>
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
public static ref T ReadArrayElementRef<T>(void* ptr, uint index)
|
|
where T : unmanaged
|
|
{
|
|
return ref AsRef<T>(ReadArrayElementUnsafe<T>(ptr, index));
|
|
}
|
|
|
|
/// <summary>
|
|
/// Reads an element from an array at a specified index using a pointer to the array.
|
|
/// </summary>
|
|
/// <typeparam name="T">Specifies the type of the elements in the array, which must be unmanaged.</typeparam>
|
|
/// <param name="ptr">Points to the start of the array from which an element will be read.</param>
|
|
/// <param name="index">Indicates the position of the element to be accessed within the array.</param>
|
|
/// <returns>The element located at the specified index in the array.</returns>
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
public static T ReadArrayElement<T>(void* ptr, int index)
|
|
where T : unmanaged
|
|
{
|
|
return *ReadArrayElementUnsafe<T>(ptr, index);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Reads an element from an array at a specified index using a pointer to the array.
|
|
/// </summary>
|
|
/// <typeparam name="T">Specifies the type of the elements in the array, which must be unmanaged.</typeparam>
|
|
/// <param name="ptr">Points to the start of the array from which an element will be read.</param>
|
|
/// <param name="index">Indicates the position of the element to be accessed within the array.</param>
|
|
/// <returns>The element located at the specified index in the array.</returns>
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
public static T ReadArrayElement<T>(void* ptr, uint index)
|
|
where T : unmanaged
|
|
{
|
|
return *ReadArrayElementUnsafe<T>(ptr, index);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Writes a value to a specified index of an unmanaged array using a pointer.
|
|
/// </summary>
|
|
/// <typeparam name="T">Specifies the type of the value being written to the array, which must be an unmanaged type.</typeparam>
|
|
/// <param name="ptr">Points to the beginning of the unmanaged array where the value will be written.</param>
|
|
/// <param name="index">Indicates the position in the array where the value should be stored.</param>
|
|
/// <param name="value">Represents the value to be written to the specified index of the array.</param>
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
public static void WriteArrayElement<T>(void* ptr, int index, T value)
|
|
where T : unmanaged
|
|
{
|
|
*ReadArrayElementUnsafe<T>(ptr, index) = value;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Writes a value to a specified index of an unmanaged array using a pointer.
|
|
/// </summary>
|
|
/// <typeparam name="T">Specifies the type of the value being written to the array, which must be an unmanaged type.</typeparam>
|
|
/// <param name="ptr">Points to the beginning of the unmanaged array where the value will be written.</param>
|
|
/// <param name="index">Indicates the position in the array where the value should be stored.</param>
|
|
/// <param name="value">Represents the value to be written to the specified index of the array.</param>
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
public static void WriteArrayElement<T>(void* ptr, uint index, T value)
|
|
where T : unmanaged
|
|
{
|
|
*ReadArrayElementUnsafe<T>(ptr, index) = value;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Converts an UnsafeArray of one unmanaged type to another unmanaged type without copying the elements.
|
|
/// </summary>
|
|
/// <typeparam name="TIn">Represents the type of elements in the input array.</typeparam>
|
|
/// <typeparam name="TOut">Represents the type of elements in the output array.</typeparam>
|
|
/// <param name="array">The input array containing elements of the specified input type.</param>
|
|
/// <returns>An UnsafeArray containing elements of the specified output type.</returns>
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
public static UnsafeArray<TOut> CastArray<TIn, TOut>(UnsafeArray<TIn> array)
|
|
where TIn : unmanaged where TOut : unmanaged
|
|
{
|
|
return new UnsafeArray<TOut>((TOut*)array.GetUnsafePtr(), array.Count * sizeof(TIn) / sizeof(TOut));
|
|
}
|
|
} |