using System.Runtime.InteropServices; namespace Misaki.HighPerformance.LowLevel.Buffer; /// /// Represents an allocated memory block with metadata. /// [StructLayout(LayoutKind.Sequential)] public readonly unsafe struct MemoryBlock { /// /// Pointer to the actual allocated memory. /// public void* Ptr { get; } /// /// The heap from which the memory was allocated. /// public void* Heap { get; } /// /// Size of the allocated memory in bytes. /// public nuint Size { get; } /// /// Alignment of the allocated memory. /// public nuint Alignment { get; } /// /// Indicates whether this memory block is valid. /// public readonly bool IsValid => Ptr != null && Size > 0; /// /// Creates a new MemoryBlock with the specified parameters. /// /// Pointer to the allocated memory. /// Size of the allocated memory. /// Alignment of the allocated memory. public MemoryBlock(void* ptr, void* heap, nuint size, nuint alignment) { Ptr = ptr; Heap = heap; Size = size; Alignment = alignment; } /// /// Creates an invalid MemoryBlock. /// public static MemoryBlock Invalid => new(null, null, 0, 0); public Span AsSpan() where T : unmanaged { if (!IsValid) { throw new InvalidOperationException("Cannot create span from invalid MemoryBlock."); } return new Span(Ptr, (int)(Size / SizeOf())); } }