37 lines
889 B
C#
37 lines
889 B
C#
|
|
using System.Buffers;
|
||
|
|
using System.Runtime.CompilerServices;
|
||
|
|
using System.Runtime.InteropServices;
|
||
|
|
|
||
|
|
namespace StitchATon2.Infra.Buffers;
|
||
|
|
|
||
|
|
internal sealed unsafe class ImmovableMemory<T> : MemoryManager<T> where T : unmanaged
|
||
|
|
{
|
||
|
|
private readonly T* _pointer;
|
||
|
|
private readonly int _length;
|
||
|
|
private bool _disposed;
|
||
|
|
|
||
|
|
public ImmovableMemory(int count)
|
||
|
|
{
|
||
|
|
_pointer = (T*)NativeMemory.Alloc((nuint)count, (nuint)Unsafe.SizeOf<T>());
|
||
|
|
_length = count;
|
||
|
|
}
|
||
|
|
|
||
|
|
protected override void Dispose(bool disposing)
|
||
|
|
{
|
||
|
|
if (!_disposed)
|
||
|
|
{
|
||
|
|
NativeMemory.Free(_pointer);
|
||
|
|
_disposed = true;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
public override Span<T> GetSpan()
|
||
|
|
=> new(_pointer, _length);
|
||
|
|
|
||
|
|
public override MemoryHandle Pin(int elementIndex = 0)
|
||
|
|
=> new(_pointer + elementIndex);
|
||
|
|
|
||
|
|
public override void Unpin()
|
||
|
|
{
|
||
|
|
}
|
||
|
|
}
|