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