using System.Runtime.CompilerServices; using System.Runtime.InteropServices; namespace Misaki.HighPerformance.LowLevel.Utilities; public static unsafe partial class MemoryUtility { [StructLayout(LayoutKind.Sequential)] private struct AlignOfHelper where T : struct { public byte dummy; public T data; } /// /// Allocates a block of memory of the specified size in bytes. /// /// Specifies the number of bytes to allocate in memory. /// Returns a pointer to the allocated memory block. [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void* Malloc(nuint size) { return NativeMemory.Alloc(size); } /// /// Allocates a block of memory with a specified size and alignment. /// /// Specifies the total number of bytes to allocate for the memory block. /// Defines the required alignment for the allocated memory address. /// Returns a pointer to the allocated memory block. [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void* AlignedAlloc(nuint size, nuint alignment) { return NativeMemory.AlignedAlloc(size, alignment); } /// /// Resizes a previously allocated memory block to a new size. It returns a pointer to the reallocated memory. /// /// The pointer to the memory block that needs to be resized. /// The new size for the memory block after resizing. /// A pointer to the reallocated memory block, or null if the operation fails. [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void* Realloc(void* ptr, nuint size) { return NativeMemory.Realloc(ptr, size); } /// /// Reallocates memory to a specified size with a given alignment. It returns a pointer to the newly allocated /// memory. /// /// The pointer to the existing memory block that needs to be reallocated. /// The new size for the memory allocation. /// The required alignment for the new memory allocation. /// A pointer to the reallocated memory block, or null if the allocation fails. [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void* AlignedRealloc(void* ptr, nuint size, nuint alignment) { return NativeMemory.AlignedRealloc(ptr, size, alignment); } /// /// Releases the allocated memory pointed to by the given pointer. This helps in managing memory usage effectively. /// /// The pointer to the memory block that needs to be freed. [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void Free(void* ptr) { NativeMemory.Free(ptr); } /// /// Releases memory that was allocated with alignment requirements. It ensures proper deallocation of aligned memory /// blocks. /// /// The pointer to the memory block that needs to be freed. [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void AlignedFree(void* ptr) { NativeMemory.AlignedFree(ptr); } /// /// Clears a block of memory by setting it to zero. It initializes a specified number of bytes at a given memory /// address. /// /// Specifies the memory address where the clearing operation will begin. /// Indicates the number of bytes to be cleared in the memory block. [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void MemClear(void* ptr, nuint size) { if (ptr == null || size == 0) { return; } NativeMemory.Clear(ptr, size); } /// /// Sets a block of memory to a specified byte value for a given size. /// /// The memory address where the byte value will be set. /// The number of bytes to set to the specified value. /// The byte value to which the memory block will be initialized. [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void MemSet(void* ptr, byte value, nuint size) { if (ptr == null || size == 0) { return; } NativeMemory.Fill(ptr, size, value); } /// /// Copies a block of memory from a source location to a destination location. /// /// Indicates the memory address from which data will be copied. /// Specifies the memory address where the copied data will be stored. /// Defines the number of bytes to be copied from the source to the destination. [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void MemCpy(void* source, void* destination, nuint size) { if (source == null || destination == null || size == 0) { return; } NativeMemory.Copy(source, destination, size); } /// /// Calculates the size in bytes of a specified unmanaged type. /// /// Represents an unmanaged type for which the size is being calculated. /// Returns the size of the specified type as an unsigned integer. [MethodImpl(MethodImplOptions.AggressiveInlining)] public static nuint SizeOf() where T : unmanaged { return (nuint)sizeof(T); } /// /// Calculates the alignment size of a specified unmanaged type. /// /// Represents an unmanaged type for which the alignment size is being calculated. /// Returns the difference in size between a helper structure and the specified type. public static nuint AlignOf() where T : unmanaged { return (nuint)(sizeof(AlignOfHelper) - sizeof(T)); } /// /// Calculates the alignment size of a specified struct. /// /// Represents a value type that is used to determine the alignment size. /// Returns the size difference in bytes as an integer. public static int MarshalAlignOf() where T : struct { return Marshal.SizeOf>() - Marshal.SizeOf(); } }