Update memory management and collection structures
Added `AllocationHandler` struct for memory allocation management. Added `UnsafeArrayPool` class for pooling `UnsafeArray<T>` instances. Added new `External` option to `Allocator` enum. Added default constructors for `UnsafeList`, `UnsafeQueue`, and `UnsafeStack` using `Persistent` allocator. Changed namespace in `AllocationManager` to `Misaki.HighPerformance.Unsafe.Buffer`. Changed `MemoryLeakException` to use `MemoryLeakExceptionInfo` for better debugging. Changed constructor behavior in `UnsafeArray` to clarify memory management responsibilities. Changed `MemoryUtilities` to include null checks in `Free` and `AlignedFree` methods. Removed unused using directive in `CollectionBenchmark.cs`. Removed initialization of `AllocationManager` in `Program.cs`.
This commit is contained in:
32
Misaki.HighPerformance.Unsafe/Buffer/AllocationHandler.cs
Normal file
32
Misaki.HighPerformance.Unsafe/Buffer/AllocationHandler.cs
Normal file
@@ -0,0 +1,32 @@
|
||||
using Misaki.HighPerformance.Unsafe.Collections;
|
||||
using Misaki.HighPerformance.Unsafe.Collections.Contracts;
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
namespace Misaki.HighPerformance.Unsafe.Buffer;
|
||||
|
||||
[StructLayout(LayoutKind.Sequential)]
|
||||
public unsafe struct AllocationHandler : IAllocator
|
||||
{
|
||||
public unsafe T* Allocate<T>(uint size, uint alignSize, AllocationOption allocationOption)
|
||||
where T : unmanaged
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public unsafe T* Reallocate<T>(T* buffer, uint size, uint alignSize)
|
||||
where T : unmanaged
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public unsafe void Free<T>(T* buffer, uint size, uint alignSize)
|
||||
where T : unmanaged
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
}
|
||||
@@ -1,29 +1,15 @@
|
||||
using Misaki.HighPerformance.Unsafe.Buffer;
|
||||
using Misaki.HighPerformance.Unsafe.Collections;
|
||||
#define UNSAFE_COLLECTION_CHECK
|
||||
|
||||
using Misaki.HighPerformance.Unsafe.Collections;
|
||||
#if UNSAFE_COLLECTION_CHECK
|
||||
#if DEBUG
|
||||
using System.Diagnostics;
|
||||
#endif
|
||||
|
||||
namespace Misaki.HighPerformance.Unsafe.Services;
|
||||
|
||||
internal readonly struct AllocationInfo
|
||||
{
|
||||
public readonly nuint Size
|
||||
{
|
||||
get;
|
||||
init;
|
||||
}
|
||||
|
||||
#if DEBUG
|
||||
public readonly StackTrace StackTrace
|
||||
{
|
||||
get;
|
||||
init;
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
namespace Misaki.HighPerformance.Unsafe.Buffer;
|
||||
|
||||
// TODO: Custom allocator
|
||||
public static unsafe class AllocationManager
|
||||
{
|
||||
private const uint _DEFAULT_ARENA_SIZE = 512 * 1024; // 512 KB
|
||||
@@ -31,7 +17,9 @@ public static unsafe class AllocationManager
|
||||
private static DynamicArena _arena;
|
||||
private static bool _initialized;
|
||||
|
||||
private static Dictionary<IntPtr, AllocationInfo> _allocated = null!;
|
||||
#if UNSAFE_COLLECTION_CHECK
|
||||
private static Dictionary<IntPtr, MemoryLeakExceptionInfo> _allocated = null!;
|
||||
#endif
|
||||
|
||||
private static readonly Lock _lock = new();
|
||||
|
||||
@@ -47,7 +35,9 @@ public static unsafe class AllocationManager
|
||||
}
|
||||
|
||||
_arena = new DynamicArena(initialSize);
|
||||
_allocated = new(32);
|
||||
#if UNSAFE_COLLECTION_CHECK
|
||||
_allocated = new Dictionary<nint, MemoryLeakExceptionInfo>(32);
|
||||
#endif
|
||||
|
||||
_initialized = true;
|
||||
}
|
||||
@@ -77,24 +67,28 @@ public static unsafe class AllocationManager
|
||||
case Allocator.Persistent:
|
||||
var allocationSize = size * (nuint)sizeof(T);
|
||||
buffer = (T*)AlignedAlloc(allocationSize, alignSize);
|
||||
_allocated[(IntPtr)buffer] = new AllocationInfo
|
||||
|
||||
#if UNSAFE_COLLECTION_CHECK
|
||||
_allocated[(IntPtr)buffer] = new MemoryLeakExceptionInfo
|
||||
{
|
||||
Size = allocationSize,
|
||||
#if DEBUG
|
||||
StackTrace = new StackTrace(true)
|
||||
#endif
|
||||
};
|
||||
#endif
|
||||
|
||||
if (allocationOption.HasFlag(AllocationOption.Clear))
|
||||
{
|
||||
MemClear(buffer, allocationSize);
|
||||
}
|
||||
|
||||
break;
|
||||
|
||||
default:
|
||||
throw new ArgumentOutOfRangeException(nameof(allocator), "Invalid allocator type.");
|
||||
}
|
||||
|
||||
if (allocationOption.HasFlag(AllocationOption.Clear))
|
||||
{
|
||||
MemClear(buffer, size * (uint)sizeof(T));
|
||||
}
|
||||
|
||||
return buffer;
|
||||
}
|
||||
}
|
||||
@@ -120,10 +114,11 @@ public static unsafe class AllocationManager
|
||||
var allocationSize = size * (nuint)sizeof(T);
|
||||
newBuffer = (T*)AlignedRealloc(buffer, allocationSize, alignSize);
|
||||
|
||||
#if UNSAFE_COLLECTION_CHECK
|
||||
// If the allocation map can not find the old value, it means that it was a untracked allocation
|
||||
if (_allocated.Remove((IntPtr)buffer))
|
||||
{
|
||||
_allocated[(IntPtr)newBuffer] = new AllocationInfo
|
||||
_allocated[(IntPtr)newBuffer] = new MemoryLeakExceptionInfo
|
||||
{
|
||||
Size = allocationSize,
|
||||
#if DEBUG
|
||||
@@ -131,6 +126,7 @@ public static unsafe class AllocationManager
|
||||
#endif
|
||||
};
|
||||
}
|
||||
#endif
|
||||
break;
|
||||
|
||||
default:
|
||||
@@ -148,6 +144,9 @@ public static unsafe class AllocationManager
|
||||
if (allocator == Allocator.Persistent)
|
||||
{
|
||||
AlignedFree(ptr);
|
||||
#if UNSAFE_COLLECTION_CHECK
|
||||
_allocated.Remove((IntPtr)ptr);
|
||||
#endif
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -169,7 +168,9 @@ public static unsafe class AllocationManager
|
||||
/// <summary>
|
||||
/// Disposes of the AllocationManager, freeing all allocated memory and resources.
|
||||
/// </summary>
|
||||
/// <exception cref="InvalidOperationException">Thrown if there are still allocated buffers that have not been freed.</exception>
|
||||
#if UNSAFE_COLLECTION_CHECK
|
||||
/// <exception cref="MemoryLeakException">Thrown if there are still allocated buffers that have not been freed.</exception>
|
||||
#endif
|
||||
public static void Dispose()
|
||||
{
|
||||
if (!_initialized)
|
||||
@@ -179,6 +180,7 @@ public static unsafe class AllocationManager
|
||||
|
||||
_arena.Dispose();
|
||||
|
||||
#if UNSAFE_COLLECTION_CHECK
|
||||
nuint unfreeBytes = 0u;
|
||||
foreach (var pair in _allocated)
|
||||
{
|
||||
@@ -192,5 +194,8 @@ public static unsafe class AllocationManager
|
||||
}
|
||||
|
||||
_allocated.Clear();
|
||||
#endif
|
||||
|
||||
_initialized = false;
|
||||
}
|
||||
}
|
||||
19
Misaki.HighPerformance.Unsafe/Buffer/UnsafeArrayPool.cs
Normal file
19
Misaki.HighPerformance.Unsafe/Buffer/UnsafeArrayPool.cs
Normal file
@@ -0,0 +1,19 @@
|
||||
using Misaki.HighPerformance.Unsafe.Collections;
|
||||
|
||||
namespace Misaki.HighPerformance.Unsafe.Buffer;
|
||||
|
||||
// TODO: Implement a pool for UnsafeArray<T>.
|
||||
public unsafe static class UnsafeArrayPool
|
||||
{
|
||||
public static UnsafeArray<T> Rent<T>(int minimalSize)
|
||||
where T : unmanaged
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public static void Return<T>(UnsafeArray<T> array)
|
||||
where T : unmanaged
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
}
|
||||
@@ -31,4 +31,8 @@ public enum Allocator : byte
|
||||
/// Allocator for persistent allocations. Allocations are not cleared after use.
|
||||
/// </summary>
|
||||
Persistent,
|
||||
/// <summary>
|
||||
/// Allocator for external memory. Allocations are not cleared after use.
|
||||
/// </summary>
|
||||
External
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
namespace Misaki.HighPerformance.Unsafe.Collections.Contracts;
|
||||
|
||||
internal unsafe interface IAllocator : IDisposable
|
||||
{
|
||||
public T* Allocate<T>(uint size, uint alignSize, AllocationOption allocationOption)
|
||||
where T : unmanaged;
|
||||
|
||||
public T* Reallocate<T>(T* buffer, uint size, uint alignSize)
|
||||
where T : unmanaged;
|
||||
|
||||
public void Free<T>(T* buffer, uint size, uint alignSize)
|
||||
where T : unmanaged;
|
||||
}
|
||||
@@ -1,6 +1,6 @@
|
||||
using Misaki.HighPerformance.Unsafe.Collections.Contracts;
|
||||
using Misaki.HighPerformance.Unsafe.Buffer;
|
||||
using Misaki.HighPerformance.Unsafe.Collections.Contracts;
|
||||
using Misaki.HighPerformance.Unsafe.Helpers;
|
||||
using Misaki.HighPerformance.Unsafe.Services;
|
||||
using System.Collections;
|
||||
using System.Runtime.CompilerServices;
|
||||
|
||||
@@ -86,12 +86,18 @@ public unsafe struct UnsafeArray<T> : IUnsafeCollection<T>
|
||||
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of UnsafeArray with a specified number of elements and an allocation type. It
|
||||
/// allocates memory and optionally clears it.
|
||||
/// Constructs an UnsafeArray with a default size of 1 and uses the Persistent allocator.
|
||||
/// </summary>
|
||||
public UnsafeArray() : this(1, Allocator.Persistent)
|
||||
{
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of UnsafeArray with a specified number of elements and an allocation type.
|
||||
/// </summary>
|
||||
/// <param name="count">Specifies the number of elements to allocate in the array, which must be greater than zero.</param>
|
||||
/// <param name="allocator">Specifies the allocator to use for memory allocation, which determines the memory management strategy.</param>
|
||||
/// <param name="allocationOption">Determines how the allocated memory should be initialized, either uninitialized or cleared.</param>
|
||||
/// <param name="allocationOption">Determines how the memory should be allocated.</param>
|
||||
/// <exception cref="ArgumentOutOfRangeException">Thrown when the specified number of elements is less than or equal to zero.</exception>
|
||||
public UnsafeArray(int count, Allocator allocator, AllocationOption allocationOption = AllocationOption.None)
|
||||
{
|
||||
@@ -103,22 +109,23 @@ public unsafe struct UnsafeArray<T> : IUnsafeCollection<T>
|
||||
_buffer = AllocationManager.Allocate<T>((uint)count, (uint)AlignOf<T>(), allocator, allocationOption);
|
||||
_count = count;
|
||||
_allocator = allocator;
|
||||
|
||||
if (allocationOption == AllocationOption.Clear)
|
||||
{
|
||||
Clear();
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Initializes an UnsafeArray with a pointer to a buffer and a count of elements.
|
||||
/// Initializes an UnsafeArray with a pointer to a buffer and a count of elements. This does not copy the data.
|
||||
/// </summary>
|
||||
/// <param name="buffer">A pointer to the memory location that holds the elements of the array.</param>
|
||||
/// <param name="count">The total size of the data.</param>
|
||||
/// <remarks>
|
||||
/// When using this constructor, the user is responsible for managing the memory pointed to by the buffer.
|
||||
/// Disposing of the UnsafeArray does not free the memory and only release the reference. The memory should be freed manually when no longer needed.
|
||||
/// Use <see cref="UnsafeArray(int, Allocator, AllocationOption)"/> constructor and <see cref="MemCpy(void*, void*, nuint)"/> if you are not sure what you are doing.
|
||||
/// </remarks>
|
||||
public UnsafeArray(void* buffer, int count)
|
||||
{
|
||||
_buffer = (T*)buffer;
|
||||
_count = count;
|
||||
_allocator = Allocator.External;
|
||||
}
|
||||
|
||||
public void Resize(int newSize)
|
||||
@@ -128,7 +135,7 @@ public unsafe struct UnsafeArray<T> : IUnsafeCollection<T>
|
||||
return;
|
||||
}
|
||||
|
||||
_buffer = AllocationManager.Realloc<T>(_buffer, (uint)newSize, (uint)AlignOf<T>(), _allocator);
|
||||
_buffer = AllocationManager.Realloc(_buffer, (uint)newSize, (uint)AlignOf<T>(), _allocator);
|
||||
_count = newSize;
|
||||
}
|
||||
|
||||
|
||||
@@ -129,6 +129,10 @@ public unsafe struct UnsafeList<T> : IUnsafeCollection<T>
|
||||
|
||||
public ParallelWriter AsParallelWriter() => new((UnsafeList<T>*)UnsafeUtilities.AddressOf(ref this));
|
||||
|
||||
public UnsafeList() : this(1, Allocator.Persistent)
|
||||
{
|
||||
}
|
||||
|
||||
public UnsafeList(int capacity, Allocator allocator, AllocationOption allocationType = AllocationOption.None)
|
||||
{
|
||||
_array = new UnsafeArray<T>(capacity, allocator, allocationType);
|
||||
|
||||
@@ -79,6 +79,10 @@ public unsafe struct UnsafeQueue<T> : IUnsafeCollection<T>
|
||||
public IEnumerator<T> GetEnumerator() => new Enumerator((UnsafeQueue<T>*)UnsafeUtilities.AddressOf(ref this));
|
||||
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
|
||||
|
||||
public UnsafeQueue() : this(1, Allocator.Persistent)
|
||||
{
|
||||
}
|
||||
|
||||
public UnsafeQueue(int capacity, Allocator allocator, AllocationOption allocationType = AllocationOption.None)
|
||||
{
|
||||
_array = new UnsafeArray<T>(capacity, allocator, allocationType);
|
||||
|
||||
@@ -23,6 +23,10 @@ public unsafe struct UnsafeStack<T> : IUnsafeCollection<T>
|
||||
return GetEnumerator();
|
||||
}
|
||||
|
||||
public UnsafeStack() : this(1, Allocator.Persistent)
|
||||
{
|
||||
}
|
||||
|
||||
public UnsafeStack(int initialSize, Allocator allocator, AllocationOption allocationOption = AllocationOption.None)
|
||||
{
|
||||
_array = new UnsafeArray<T>(initialSize, allocator, allocationOption);
|
||||
|
||||
@@ -1,11 +1,27 @@
|
||||
using Misaki.HighPerformance.Unsafe.Services;
|
||||
#if DEBUG
|
||||
using System.Diagnostics;
|
||||
using System.Text;
|
||||
#endif
|
||||
|
||||
namespace Misaki.HighPerformance.Unsafe;
|
||||
|
||||
internal class MemoryLeakException(params AllocationInfo[] Infos) : Exception
|
||||
public readonly struct MemoryLeakExceptionInfo
|
||||
{
|
||||
public nuint Size
|
||||
{
|
||||
get; init;
|
||||
}
|
||||
#if DEBUG
|
||||
public StackTrace StackTrace
|
||||
{
|
||||
get; init;
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
public class MemoryLeakException(params MemoryLeakExceptionInfo[] Infos) : Exception
|
||||
{
|
||||
#if DEBUG
|
||||
private static string GetMessage(StackTrace? stackTrace)
|
||||
{
|
||||
if (stackTrace == null)
|
||||
@@ -27,6 +43,7 @@ internal class MemoryLeakException(params AllocationInfo[] Infos) : Exception
|
||||
|
||||
return stringBuilder.ToString();
|
||||
}
|
||||
#endif
|
||||
|
||||
public override string Message
|
||||
{
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
using Misaki.HighPerformance.Unsafe.Buffer;
|
||||
using Misaki.HighPerformance.Unsafe.Collections;
|
||||
using Misaki.HighPerformance.Unsafe.Services;
|
||||
using System.Numerics;
|
||||
using System.Runtime.CompilerServices;
|
||||
|
||||
|
||||
@@ -69,6 +69,11 @@ public static unsafe class MemoryUtilities
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static void Free(void* ptr)
|
||||
{
|
||||
if (ptr == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
NativeMemory.Free(ptr);
|
||||
}
|
||||
|
||||
@@ -80,6 +85,11 @@ public static unsafe class MemoryUtilities
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static void AlignedFree(void* ptr)
|
||||
{
|
||||
if (ptr == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
NativeMemory.AlignedFree(ptr);
|
||||
}
|
||||
|
||||
|
||||
@@ -19,4 +19,8 @@
|
||||
<ProjectReference Include="..\Misaki.HighPerformance\Misaki.HighPerformance.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Folder Include="Collections\Allocator\" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
|
||||
Reference in New Issue
Block a user