using System.Runtime.CompilerServices; namespace Misaki.HighPerformance.LowLevel.Utilities; public static unsafe class UnsafeUtility { /// /// 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) where T : unmanaged { return ref *(T*)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) where T : unmanaged { return Unsafe.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, nint index) where T : unmanaged { return (T*)((byte*)ptr + index * sizeof(T)); } /// /// 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, nuint index) where T : unmanaged { return (T*)((byte*)ptr + index * (nuint)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, nint index) where T : unmanaged { return ref AsRef(ReadArrayElementUnsafe(ptr, index)); } /// /// 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, nuint 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, nint index) where T : unmanaged { return *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, nuint 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, nint index, T value) where T : unmanaged { *ReadArrayElementUnsafe(ptr, index) = value; } /// /// 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, nuint index, T value) where T : unmanaged { *ReadArrayElementUnsafe(ptr, index) = value; } /// /// Returns a pointer to the first element of the specified span. This method enables direct, unsafe access to the underlying data of the span. /// /// The type of elements in the span. Must be an unmanaged type. /// The span whose underlying data pointer is to be obtained. /// A pointer to the first element of the span. If the span is empty, the returned pointer is undefined and must not be dereferenced. public static T* GetUnsafePtr(this Span span) where T : unmanaged { fixed (T* ptr = span) { return ptr; } } /// /// Returns a pointer to the first element of the specified span. This method enables direct, unsafe access to the underlying data of the span. /// /// The type of elements in the span. Must be an unmanaged type. /// The span whose underlying data pointer is to be obtained. /// A pointer to the first element of the span. If the span is empty, the returned pointer is undefined and must not be dereferenced. public static T* GetUnsafePtr(this ReadOnlySpan span) where T : unmanaged { fixed (T* ptr = span) { return ptr; } } }