dangerous release (possibly memory leak and deadlock)

This commit is contained in:
dennisarfan 2025-07-31 07:40:28 +07:00
parent a1cb6592eb
commit 741d34a5e0
10 changed files with 164 additions and 115 deletions

View file

@ -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();
}
}