Upgrade to .net9
This commit is contained in:
parent
eb97cfb57c
commit
a1cb6592eb
15 changed files with 395 additions and 54 deletions
|
|
@ -3,18 +3,23 @@ using System.Runtime.InteropServices;
|
|||
|
||||
namespace StitchATon2.Infra.Buffers;
|
||||
|
||||
/// <summary>
|
||||
/// Provide non-thread safe anti GC contiguous memory.
|
||||
/// </summary>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
internal sealed unsafe class UnmanagedMemory<T> : IBuffer<T> where T : unmanaged
|
||||
{
|
||||
private readonly void* _pointer;
|
||||
private readonly T* _pointer;
|
||||
private readonly int _count;
|
||||
private bool _disposed;
|
||||
|
||||
public ref T this[int index] => ref Unsafe.AsRef<T>((T*)_pointer + index); // *((T*)_pointer + index);
|
||||
public ref T this[int index] => ref Unsafe.AsRef<T>(_pointer + index);
|
||||
|
||||
public Span<T> Span => new(_pointer, _count);
|
||||
|
||||
public UnmanagedMemory(int count)
|
||||
{
|
||||
_pointer = NativeMemory.Alloc((nuint)count, (nuint)Unsafe.SizeOf<T>());
|
||||
_pointer = (T*)NativeMemory.Alloc((nuint)count, (nuint)Unsafe.SizeOf<T>());
|
||||
_count = count;
|
||||
}
|
||||
|
||||
|
|
@ -22,7 +27,11 @@ internal sealed unsafe class UnmanagedMemory<T> : IBuffer<T> where T : unmanaged
|
|||
|
||||
public void Dispose()
|
||||
{
|
||||
NativeMemory.Free(_pointer);
|
||||
GC.SuppressFinalize(this);
|
||||
if (!_disposed)
|
||||
{
|
||||
NativeMemory.Free(_pointer);
|
||||
GC.SuppressFinalize(this);
|
||||
_disposed = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue