using System.Numerics; using System.Runtime.CompilerServices; using System.Runtime.InteropServices; using System.Runtime.Intrinsics; 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) { try { return NativeMemory.Alloc(size); } catch (Exception) { return null; } } /// /// Allocates a block of memory of the specified size in bytes and initializes it to zero. /// /// Specifies the number of bytes to allocate in memory. /// Returns a pointer to the allocated and zero-initialized memory block. [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void* Calloc(nuint size) { try { return NativeMemory.AllocZeroed(size); } catch (Exception) { return null; } } /// /// 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) { try { return NativeMemory.AlignedAlloc(size, alignment); } catch (Exception) { return null; } } /// /// 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) { try { return NativeMemory.Realloc(ptr, size); } catch (Exception) { return null; } } /// /// 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) { try { return NativeMemory.AlignedRealloc(ptr, size, alignment); } catch (Exception) { return null; } } /// /// 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) { 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) { 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* destination, void* source, nuint size) { NativeMemory.Copy(source, destination, size); } /// /// Moves a block of memory from a source location to a destination location, handling overlapping regions correctly. /// /// Indicates the memory address where the data will be moved to. /// Specifies the memory address from which data will be moved. /// Defines the number of bytes to be moved from the source to the destination. [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void MemMove(void* destination, void* source, nuint size) { // NativeMemory.Copy use memmove internally. NativeMemory.Copy(source, destination, size); } /// /// Compares two blocks of memory byte by byte for a specified length. /// /// A pointer to the first block of memory to compare. /// A pointer to the second block of memory to compare. /// The number of bytes to compare. Must not exceed the length of either memory block. /// A signed integer that indicates the relative order of the memory blocks: less than zero if the first differing /// byte in ptr1 is less than the corresponding byte in ptr2; zero if all compared bytes are equal; greater than /// zero if the first differing byte in ptr1 is greater than the corresponding byte in ptr2. [MethodImpl(MethodImplOptions.AggressiveInlining)] public static int MemCmp(void* ptr1, void* ptr2, nuint size) { if (ptr1 == ptr2) { return 0; } var span1 = new ReadOnlySpan(ptr1, (int)size); var span2 = new ReadOnlySpan(ptr2, (int)size); return span1.SequenceCompareTo(span2); } /// /// 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(); } }