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:
2025-04-05 16:07:04 +09:00
parent 9eea53d8f1
commit 463735a481
15 changed files with 177 additions and 53 deletions

View File

@@ -1,6 +1,6 @@
using BenchmarkDotNet.Attributes; using BenchmarkDotNet.Attributes;
using Misaki.HighPerformance.Unsafe.Buffer;
using Misaki.HighPerformance.Unsafe.Collections; using Misaki.HighPerformance.Unsafe.Collections;
using Misaki.HighPerformance.Unsafe.Services;
namespace Misaki.HighPerformance.Test; namespace Misaki.HighPerformance.Test;

View File

@@ -1,9 +1,10 @@
using Misaki.HighPerformance.Unsafe.Collections; using Misaki.HighPerformance.Unsafe.Collections;
using Misaki.HighPerformance.Unsafe.Services; using Misaki.HighPerformance.Unsafe.Helpers;
using System.Numerics;
AllocationManager.Initialize(100); unsafe
{
var unfreeArray = new UnsafeArray<int>(10, Allocator.Persistent); Console.WriteLine(sizeof(UnsafeHashMap<int, float>));
var unfreeList = new UnsafeList<int>(10, Allocator.Persistent); Console.WriteLine(MemoryUtilities.AlignOf<UnsafeHashMap<int, float>>());
//unfreeArray.Dispose(); Console.WriteLine(1 << Math.Min(3, BitOperations.TrailingZeroCount(sizeof(UnsafeHashMap<int, float>))));
AllocationManager.Dispose(); }

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

View File

@@ -1,29 +1,15 @@
using Misaki.HighPerformance.Unsafe.Buffer; #define UNSAFE_COLLECTION_CHECK
using Misaki.HighPerformance.Unsafe.Collections;
using Misaki.HighPerformance.Unsafe.Collections;
#if UNSAFE_COLLECTION_CHECK
#if DEBUG #if DEBUG
using System.Diagnostics; using System.Diagnostics;
#endif #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 #endif
}
namespace Misaki.HighPerformance.Unsafe.Buffer;
// TODO: Custom allocator
public static unsafe class AllocationManager public static unsafe class AllocationManager
{ {
private const uint _DEFAULT_ARENA_SIZE = 512 * 1024; // 512 KB 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 DynamicArena _arena;
private static bool _initialized; 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(); private static readonly Lock _lock = new();
@@ -47,7 +35,9 @@ public static unsafe class AllocationManager
} }
_arena = new DynamicArena(initialSize); _arena = new DynamicArena(initialSize);
_allocated = new(32); #if UNSAFE_COLLECTION_CHECK
_allocated = new Dictionary<nint, MemoryLeakExceptionInfo>(32);
#endif
_initialized = true; _initialized = true;
} }
@@ -77,24 +67,28 @@ public static unsafe class AllocationManager
case Allocator.Persistent: case Allocator.Persistent:
var allocationSize = size * (nuint)sizeof(T); var allocationSize = size * (nuint)sizeof(T);
buffer = (T*)AlignedAlloc(allocationSize, alignSize); buffer = (T*)AlignedAlloc(allocationSize, alignSize);
_allocated[(IntPtr)buffer] = new AllocationInfo
#if UNSAFE_COLLECTION_CHECK
_allocated[(IntPtr)buffer] = new MemoryLeakExceptionInfo
{ {
Size = allocationSize, Size = allocationSize,
#if DEBUG #if DEBUG
StackTrace = new StackTrace(true) StackTrace = new StackTrace(true)
#endif #endif
}; };
#endif
if (allocationOption.HasFlag(AllocationOption.Clear))
{
MemClear(buffer, allocationSize);
}
break; break;
default: default:
throw new ArgumentOutOfRangeException(nameof(allocator), "Invalid allocator type."); throw new ArgumentOutOfRangeException(nameof(allocator), "Invalid allocator type.");
} }
if (allocationOption.HasFlag(AllocationOption.Clear))
{
MemClear(buffer, size * (uint)sizeof(T));
}
return buffer; return buffer;
} }
} }
@@ -120,10 +114,11 @@ public static unsafe class AllocationManager
var allocationSize = size * (nuint)sizeof(T); var allocationSize = size * (nuint)sizeof(T);
newBuffer = (T*)AlignedRealloc(buffer, allocationSize, alignSize); 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 the allocation map can not find the old value, it means that it was a untracked allocation
if (_allocated.Remove((IntPtr)buffer)) if (_allocated.Remove((IntPtr)buffer))
{ {
_allocated[(IntPtr)newBuffer] = new AllocationInfo _allocated[(IntPtr)newBuffer] = new MemoryLeakExceptionInfo
{ {
Size = allocationSize, Size = allocationSize,
#if DEBUG #if DEBUG
@@ -131,6 +126,7 @@ public static unsafe class AllocationManager
#endif #endif
}; };
} }
#endif
break; break;
default: default:
@@ -148,6 +144,9 @@ public static unsafe class AllocationManager
if (allocator == Allocator.Persistent) if (allocator == Allocator.Persistent)
{ {
AlignedFree(ptr); AlignedFree(ptr);
#if UNSAFE_COLLECTION_CHECK
_allocated.Remove((IntPtr)ptr);
#endif
} }
} }
} }
@@ -169,7 +168,9 @@ public static unsafe class AllocationManager
/// <summary> /// <summary>
/// Disposes of the AllocationManager, freeing all allocated memory and resources. /// Disposes of the AllocationManager, freeing all allocated memory and resources.
/// </summary> /// </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() public static void Dispose()
{ {
if (!_initialized) if (!_initialized)
@@ -179,6 +180,7 @@ public static unsafe class AllocationManager
_arena.Dispose(); _arena.Dispose();
#if UNSAFE_COLLECTION_CHECK
nuint unfreeBytes = 0u; nuint unfreeBytes = 0u;
foreach (var pair in _allocated) foreach (var pair in _allocated)
{ {
@@ -192,5 +194,8 @@ public static unsafe class AllocationManager
} }
_allocated.Clear(); _allocated.Clear();
#endif
_initialized = false;
} }
} }

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

View File

@@ -31,4 +31,8 @@ public enum Allocator : byte
/// Allocator for persistent allocations. Allocations are not cleared after use. /// Allocator for persistent allocations. Allocations are not cleared after use.
/// </summary> /// </summary>
Persistent, Persistent,
/// <summary>
/// Allocator for external memory. Allocations are not cleared after use.
/// </summary>
External
} }

View File

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

View File

@@ -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.Helpers;
using Misaki.HighPerformance.Unsafe.Services;
using System.Collections; using System.Collections;
using System.Runtime.CompilerServices; using System.Runtime.CompilerServices;
@@ -86,12 +86,18 @@ public unsafe struct UnsafeArray<T> : IUnsafeCollection<T>
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator(); IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
/// <summary> /// <summary>
/// Initializes a new instance of UnsafeArray with a specified number of elements and an allocation type. It /// Constructs an UnsafeArray with a default size of 1 and uses the Persistent allocator.
/// allocates memory and optionally clears it. /// </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> /// </summary>
/// <param name="count">Specifies the number of elements to allocate in the array, which must be greater than zero.</param> /// <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="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> /// <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) 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); _buffer = AllocationManager.Allocate<T>((uint)count, (uint)AlignOf<T>(), allocator, allocationOption);
_count = count; _count = count;
_allocator = allocator; _allocator = allocator;
if (allocationOption == AllocationOption.Clear)
{
Clear();
}
} }
/// <summary> /// <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> /// </summary>
/// <param name="buffer">A pointer to the memory location that holds the elements of the array.</param> /// <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> /// <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) public UnsafeArray(void* buffer, int count)
{ {
_buffer = (T*)buffer; _buffer = (T*)buffer;
_count = count; _count = count;
_allocator = Allocator.External;
} }
public void Resize(int newSize) public void Resize(int newSize)
@@ -128,7 +135,7 @@ public unsafe struct UnsafeArray<T> : IUnsafeCollection<T>
return; return;
} }
_buffer = AllocationManager.Realloc<T>(_buffer, (uint)newSize, (uint)AlignOf<T>(), _allocator); _buffer = AllocationManager.Realloc(_buffer, (uint)newSize, (uint)AlignOf<T>(), _allocator);
_count = newSize; _count = newSize;
} }

View File

@@ -129,6 +129,10 @@ public unsafe struct UnsafeList<T> : IUnsafeCollection<T>
public ParallelWriter AsParallelWriter() => new((UnsafeList<T>*)UnsafeUtilities.AddressOf(ref this)); 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) public UnsafeList(int capacity, Allocator allocator, AllocationOption allocationType = AllocationOption.None)
{ {
_array = new UnsafeArray<T>(capacity, allocator, allocationType); _array = new UnsafeArray<T>(capacity, allocator, allocationType);

View File

@@ -79,6 +79,10 @@ public unsafe struct UnsafeQueue<T> : IUnsafeCollection<T>
public IEnumerator<T> GetEnumerator() => new Enumerator((UnsafeQueue<T>*)UnsafeUtilities.AddressOf(ref this)); public IEnumerator<T> GetEnumerator() => new Enumerator((UnsafeQueue<T>*)UnsafeUtilities.AddressOf(ref this));
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator(); IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
public UnsafeQueue() : this(1, Allocator.Persistent)
{
}
public UnsafeQueue(int capacity, Allocator allocator, AllocationOption allocationType = AllocationOption.None) public UnsafeQueue(int capacity, Allocator allocator, AllocationOption allocationType = AllocationOption.None)
{ {
_array = new UnsafeArray<T>(capacity, allocator, allocationType); _array = new UnsafeArray<T>(capacity, allocator, allocationType);

View File

@@ -23,6 +23,10 @@ public unsafe struct UnsafeStack<T> : IUnsafeCollection<T>
return GetEnumerator(); return GetEnumerator();
} }
public UnsafeStack() : this(1, Allocator.Persistent)
{
}
public UnsafeStack(int initialSize, Allocator allocator, AllocationOption allocationOption = AllocationOption.None) public UnsafeStack(int initialSize, Allocator allocator, AllocationOption allocationOption = AllocationOption.None)
{ {
_array = new UnsafeArray<T>(initialSize, allocator, allocationOption); _array = new UnsafeArray<T>(initialSize, allocator, allocationOption);

View File

@@ -1,11 +1,27 @@
using Misaki.HighPerformance.Unsafe.Services; #if DEBUG
using System.Diagnostics; using System.Diagnostics;
using System.Text; using System.Text;
#endif
namespace Misaki.HighPerformance.Unsafe; 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) private static string GetMessage(StackTrace? stackTrace)
{ {
if (stackTrace == null) if (stackTrace == null)
@@ -27,6 +43,7 @@ internal class MemoryLeakException(params AllocationInfo[] Infos) : Exception
return stringBuilder.ToString(); return stringBuilder.ToString();
} }
#endif
public override string Message public override string Message
{ {

View File

@@ -1,5 +1,5 @@
using Misaki.HighPerformance.Unsafe.Buffer;
using Misaki.HighPerformance.Unsafe.Collections; using Misaki.HighPerformance.Unsafe.Collections;
using Misaki.HighPerformance.Unsafe.Services;
using System.Numerics; using System.Numerics;
using System.Runtime.CompilerServices; using System.Runtime.CompilerServices;

View File

@@ -69,6 +69,11 @@ public static unsafe class MemoryUtilities
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void Free(void* ptr) public static void Free(void* ptr)
{ {
if (ptr == null)
{
return;
}
NativeMemory.Free(ptr); NativeMemory.Free(ptr);
} }
@@ -80,6 +85,11 @@ public static unsafe class MemoryUtilities
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void AlignedFree(void* ptr) public static void AlignedFree(void* ptr)
{ {
if (ptr == null)
{
return;
}
NativeMemory.AlignedFree(ptr); NativeMemory.AlignedFree(ptr);
} }

View File

@@ -19,4 +19,8 @@
<ProjectReference Include="..\Misaki.HighPerformance\Misaki.HighPerformance.csproj" /> <ProjectReference Include="..\Misaki.HighPerformance\Misaki.HighPerformance.csproj" />
</ItemGroup> </ItemGroup>
<ItemGroup>
<Folder Include="Collections\Allocator\" />
</ItemGroup>
</Project> </Project>