using Misaki.HighPerformance.LowLevel.Collections; using System.Runtime.CompilerServices; namespace Misaki.HighPerformance.LowLevel.Helpers; public static unsafe class UnsafeUtilities { /// /// Converts a pointer to a reference of a specified type. /// /// Specifies the type of the reference to be created from the pointer. /// Represents the memory address to be converted into a reference. /// Returns a reference of the specified type pointing to the given memory address. [MethodImpl(MethodImplOptions.AggressiveInlining)] public static ref T AsRef(void* ptr) { return ref SystemUnsfae.AsRef(ptr); } /// /// Returns the address of a specified variable in memory. /// /// Represents the type of the variable whose address is being retrieved. /// The variable whose memory address is to be obtained. /// A pointer to the memory address of the specified variable. [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void* AddressOf(ref T value) { return SystemUnsfae.AsPointer(ref value); } /// /// Reads an element from an unmanaged array at a specified index using a pointer. /// /// Specifies the type of elements in the unmanaged array. /// Points to the start of the unmanaged array from which the element is read. /// Indicates the position of the element to be accessed within the array. /// Returns a pointer to the element located at the specified index. [MethodImpl(MethodImplOptions.AggressiveInlining)] public static T* ReadArrayElementUnsafe(void* ptr, int index) where T : unmanaged { return (T*)((byte*)ptr + index * sizeof(T)); } /// /// Reads an element from an unmanaged array using a pointer and index, returning a reference to the element. /// /// Specifies the type of the elements in the unmanaged array. /// Points to the start of the unmanaged array from which the element is read. /// Indicates the position of the element to be accessed in the array. /// A reference to the specified element in the unmanaged array. [MethodImpl(MethodImplOptions.AggressiveInlining)] public static ref T ReadArrayElementRef(void* ptr, int index) where T : unmanaged { return ref AsRef(ReadArrayElementUnsafe(ptr, index)); } /// /// Reads an element from an array at a specified index using a pointer to the array. /// /// Specifies the type of the elements in the array, which must be unmanaged. /// Points to the start of the array from which an element will be read. /// Indicates the position of the element to be accessed within the array. /// The element located at the specified index in the array. [MethodImpl(MethodImplOptions.AggressiveInlining)] public static T ReadArrayElement(void* ptr, int index) where T : unmanaged { return *ReadArrayElementUnsafe(ptr, index); } /// /// Writes a value to a specified index of an unmanaged array using a pointer. /// /// Specifies the type of the value being written to the array, which must be an unmanaged type. /// Points to the beginning of the unmanaged array where the value will be written. /// Indicates the position in the array where the value should be stored. /// Represents the value to be written to the specified index of the array. [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void WriteArrayElement(void* ptr, int index, T value) where T : unmanaged { *ReadArrayElementUnsafe(ptr, index) = value; } /// /// Converts an UnsafeArray of one unmanaged type to another unmanaged type without copying the elements. /// /// Represents the type of elements in the input array. /// Represents the type of elements in the output array. /// The input array containing elements of the specified input type. /// An UnsafeArray containing elements of the specified output type. [MethodImpl(MethodImplOptions.AggressiveInlining)] public static UnsafeArray CastArray(UnsafeArray array) where TIn : unmanaged where TOut : unmanaged { return new UnsafeArray(array.GetUnsafePtr(), array.Count * sizeof(TIn) / sizeof(TOut)); } }