using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
namespace StitchATon2.Infra.Buffers;
///
/// Provide non-thread safe anti GC contiguous memory.
///
///
[Obsolete("Use immovable memory instead")]
internal sealed unsafe class UnmanagedMemory : IBuffer where T : unmanaged
{
internal readonly T* Pointer;
private bool _disposed;
public Memory Memory => throw new NotImplementedException();
public int Length { get; }
public ref T this[int index] => ref Unsafe.AsRef(Pointer + index);
public Span Span => new(Pointer, Length);
public UnmanagedMemory(int length)
{
Pointer = (T*)NativeMemory.Alloc((nuint)length, (nuint)Unsafe.SizeOf());
Length = length;
}
~UnmanagedMemory() => Dispose();
public void Dispose()
{
if (!_disposed)
{
NativeMemory.Free(Pointer);
GC.SuppressFinalize(this);
_disposed = true;
}
}
}