using System.Runtime.CompilerServices; using System.Runtime.InteropServices; namespace StitchATon2.Infra.Buffers; internal sealed unsafe class UnmanagedMemory : IBuffer where T : unmanaged { private readonly void* _pointer; private readonly int _count; public ref T this[int index] => ref Unsafe.AsRef((T*)_pointer + index); // *((T*)_pointer + index); public Span Span => new(_pointer, _count); public UnmanagedMemory(int count) { _pointer = NativeMemory.Alloc((nuint)count, (nuint)Unsafe.SizeOf()); _count = count; } ~UnmanagedMemory() => Dispose(); public void Dispose() { NativeMemory.Free(_pointer); GC.SuppressFinalize(this); } }