Upgrade to .net9
This commit is contained in:
parent
eb97cfb57c
commit
a1cb6592eb
15 changed files with 395 additions and 54 deletions
37
Infra/Buffers/ImmovableMemory.cs
Normal file
37
Infra/Buffers/ImmovableMemory.cs
Normal file
|
|
@ -0,0 +1,37 @@
|
|||
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()
|
||||
{
|
||||
}
|
||||
}
|
||||
|
|
@ -12,4 +12,7 @@ public static class MemoryAllocator
|
|||
|
||||
public static ArrayOwner<T> AllocateArray<T>(int count) where T : unmanaged
|
||||
=> new(ArrayPool<T>.Shared, count);
|
||||
|
||||
public static MemoryManager<T> AllocateImmovable<T>(int count) where T : unmanaged
|
||||
=> new ImmovableMemory<T>(count);
|
||||
}
|
||||
|
|
@ -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