dangerous release (possibly memory leak and deadlock)
This commit is contained in:
parent
a1cb6592eb
commit
741d34a5e0
10 changed files with 164 additions and 115 deletions
|
|
@ -7,10 +7,11 @@ public class ArrayOwner<T> : IBuffer<T> where T : unmanaged
|
|||
private readonly ArrayPool<T> _owner;
|
||||
private readonly T[] _buffer;
|
||||
|
||||
public ArrayOwner(ArrayPool<T> owner, int size)
|
||||
public ArrayOwner(ArrayPool<T> owner, int length)
|
||||
{
|
||||
_owner = owner;
|
||||
_buffer = owner.Rent(size);
|
||||
_buffer = owner.Rent(length);
|
||||
Length = length;
|
||||
}
|
||||
|
||||
~ArrayOwner() => Dispose();
|
||||
|
|
@ -26,4 +27,6 @@ public class ArrayOwner<T> : IBuffer<T> where T : unmanaged
|
|||
public Span<T> Span => _buffer;
|
||||
|
||||
public T[] Array => _buffer;
|
||||
|
||||
public int Length { get; }
|
||||
}
|
||||
|
|
@ -5,4 +5,6 @@ public interface IBuffer<T> : IDisposable where T : unmanaged
|
|||
ref T this[int index] { get; }
|
||||
|
||||
Span<T> Span { get; }
|
||||
|
||||
int Length { get; }
|
||||
}
|
||||
|
|
@ -1,4 +1,5 @@
|
|||
using System.Buffers;
|
||||
using System.Runtime.CompilerServices;
|
||||
|
||||
namespace StitchATon2.Infra.Buffers;
|
||||
|
||||
|
|
@ -15,4 +16,29 @@ public static class MemoryAllocator
|
|||
|
||||
public static MemoryManager<T> AllocateImmovable<T>(int count) where T : unmanaged
|
||||
=> new ImmovableMemory<T>(count);
|
||||
|
||||
public static unsafe IBuffer<T> Clone<T>(this IBuffer<T> buffer) where T : unmanaged
|
||||
{
|
||||
if (buffer is UnmanagedMemory<T> unmanagedMemory)
|
||||
{
|
||||
var newBuffer = new UnmanagedMemory<T>(buffer.Length);
|
||||
var byteCount = (uint)(Unsafe.SizeOf<T>() * buffer.Length);
|
||||
Unsafe.CopyBlock(newBuffer.Pointer, unmanagedMemory.Pointer, byteCount);
|
||||
return newBuffer;
|
||||
}
|
||||
|
||||
throw new NotSupportedException();
|
||||
}
|
||||
|
||||
public static unsafe void Copy<T>(this IBuffer<T> source, IBuffer<T> destination, int count) where T : unmanaged
|
||||
{
|
||||
if (source is UnmanagedMemory<T> sourceBuffer && destination is UnmanagedMemory<T> destinationBuffer)
|
||||
{
|
||||
var byteCount = (uint)(Unsafe.SizeOf<T>() * count);
|
||||
Unsafe.CopyBlock(destinationBuffer.Pointer, sourceBuffer.Pointer, byteCount);
|
||||
return;
|
||||
}
|
||||
|
||||
throw new NotSupportedException();
|
||||
}
|
||||
}
|
||||
|
|
@ -9,18 +9,19 @@ namespace StitchATon2.Infra.Buffers;
|
|||
/// <typeparam name="T"></typeparam>
|
||||
internal sealed unsafe class UnmanagedMemory<T> : IBuffer<T> where T : unmanaged
|
||||
{
|
||||
private readonly T* _pointer;
|
||||
private readonly int _count;
|
||||
internal readonly T* Pointer;
|
||||
private bool _disposed;
|
||||
|
||||
public int Length { get; }
|
||||
|
||||
public ref T this[int index] => ref Unsafe.AsRef<T>(_pointer + index);
|
||||
public ref T this[int index] => ref Unsafe.AsRef<T>(Pointer + index);
|
||||
|
||||
public Span<T> Span => new(_pointer, _count);
|
||||
public Span<T> Span => new(Pointer, Length);
|
||||
|
||||
public UnmanagedMemory(int count)
|
||||
public UnmanagedMemory(int length)
|
||||
{
|
||||
_pointer = (T*)NativeMemory.Alloc((nuint)count, (nuint)Unsafe.SizeOf<T>());
|
||||
_count = count;
|
||||
Pointer = (T*)NativeMemory.Alloc((nuint)length, (nuint)Unsafe.SizeOf<T>());
|
||||
Length = length;
|
||||
}
|
||||
|
||||
~UnmanagedMemory() => Dispose();
|
||||
|
|
@ -29,7 +30,7 @@ internal sealed unsafe class UnmanagedMemory<T> : IBuffer<T> where T : unmanaged
|
|||
{
|
||||
if (!_disposed)
|
||||
{
|
||||
NativeMemory.Free(_pointer);
|
||||
NativeMemory.Free(Pointer);
|
||||
GC.SuppressFinalize(this);
|
||||
_disposed = true;
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue