Remove unused methods and project file

Removed the `CeilPow2` method and its associated using directive from `MathUtilities.cs`.
Removed the entire content of the `Misaki.HighPerformance.Mathematics.csproj` file.

Added new classes and structures

Added a new `MemoryLeakException` class to handle memory leak reporting in `MemoryLeakException.cs`.
Added a new `AllocationInfo` struct to store allocation details in `AllocationManager.cs`.

Changed memory management logic

Changed memory allocation handling in `Program.cs` by introducing `unfreeArray` and `unfreeList`.
Changed the `_allocated` dictionary in `AllocationManager.cs` from `UnsafeHashMap` to `Dictionary` and updated allocation logic to store `AllocationInfo`.
Modified allocation and reallocation logic in `AllocationManager` to include stack trace information in debug mode.
Updated disposal logic in `AllocationManager` to throw a `MemoryLeakException` for unfreed allocations.
This commit is contained in:
2025-04-03 22:55:48 +09:00
parent 791be1bed2
commit 9eea53d8f1
5 changed files with 94 additions and 39 deletions

View File

@@ -1,19 +0,0 @@
using System.Runtime.CompilerServices;
namespace Misaki.HighPerformance.Mathematics;
public static class MathUtilities
{
/// <summary>Returns the smallest power of two that is greater than or equal to the specified number.</summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static int CeilPow2(int x)
{
x -= 1;
x |= x >> 1;
x |= x >> 2;
x |= x >> 4;
x |= x >> 8;
x |= x >> 16;
return x + 1;
}
}

View File

@@ -1,9 +0,0 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net9.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
</Project>

View File

@@ -2,7 +2,8 @@
using Misaki.HighPerformance.Unsafe.Services; using Misaki.HighPerformance.Unsafe.Services;
AllocationManager.Initialize(100); AllocationManager.Initialize(100);
var unfreeArray = new UnsafeArray<int>(10, Allocator.Persistent);
//unfreeArray.Dispose();
var unfreeArray = new UnsafeArray<int>(10, Allocator.Persistent);
var unfreeList = new UnsafeList<int>(10, Allocator.Persistent);
//unfreeArray.Dispose();
AllocationManager.Dispose(); AllocationManager.Dispose();

View File

@@ -0,0 +1,49 @@
using Misaki.HighPerformance.Unsafe.Services;
using System.Diagnostics;
using System.Text;
namespace Misaki.HighPerformance.Unsafe;
internal class MemoryLeakException(params AllocationInfo[] Infos) : Exception
{
private static string GetMessage(StackTrace? stackTrace)
{
if (stackTrace == null)
{
return "No stack trace available.";
}
var stringBuilder = new StringBuilder();
stringBuilder.AppendLine("Memory leak detected at: ");
for (var i = 0; i < stackTrace.FrameCount; i++)
{
var frame = stackTrace.GetFrame(i);
if (frame != null)
{
stringBuilder.AppendLine($"File: {frame.GetFileName()}, Line: {frame.GetFileLineNumber()}");
}
}
return stringBuilder.ToString();
}
public override string Message
{
get
{
#if DEBUG
var stringBuilder = new StringBuilder();
stringBuilder.AppendLine($"Found {Infos.Length} memory lakes!");
foreach (var info in Infos)
{
stringBuilder.AppendLine(GetMessage(info.StackTrace));
}
return stringBuilder.ToString();
#else
return $"There are still {Infos.Length} buffers that hold {Infos.Sum(i => (uint)i.Size)} bytes in total are not freed yet. Please free them before disposing. Switch to debug mode for more information.";
#endif
}
}
}

View File

@@ -1,8 +1,29 @@
using Misaki.HighPerformance.Unsafe.Buffer; using Misaki.HighPerformance.Unsafe.Buffer;
using Misaki.HighPerformance.Unsafe.Collections; using Misaki.HighPerformance.Unsafe.Collections;
#if DEBUG
using System.Diagnostics;
#endif
namespace Misaki.HighPerformance.Unsafe.Services; namespace Misaki.HighPerformance.Unsafe.Services;
internal readonly struct AllocationInfo
{
public readonly nuint Size
{
get;
init;
}
#if DEBUG
public readonly StackTrace StackTrace
{
get;
init;
}
#endif
}
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
@@ -10,7 +31,7 @@ public static unsafe class AllocationManager
private static DynamicArena _arena; private static DynamicArena _arena;
private static bool _initialized; private static bool _initialized;
private static UnsafeHashMap<IntPtr, nuint> _allocated; private static Dictionary<IntPtr, AllocationInfo> _allocated = null!;
private static readonly Lock _lock = new(); private static readonly Lock _lock = new();
@@ -26,7 +47,7 @@ public static unsafe class AllocationManager
} }
_arena = new DynamicArena(initialSize); _arena = new DynamicArena(initialSize);
_allocated = new(32, Allocator.Persistent, AllocationOption.UnTracked); _allocated = new(32);
_initialized = true; _initialized = true;
} }
@@ -56,7 +77,13 @@ 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] = allocationSize; _allocated[(IntPtr)buffer] = new AllocationInfo
{
Size = allocationSize,
#if DEBUG
StackTrace = new StackTrace(true)
#endif
};
break; break;
default: default:
@@ -93,10 +120,16 @@ 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 the allocation map can not find the old value, which means that it's 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.Add((IntPtr)newBuffer, allocationSize); _allocated[(IntPtr)newBuffer] = new AllocationInfo
{
Size = allocationSize,
#if DEBUG
StackTrace = new StackTrace(true)
#endif
};
} }
break; break;
@@ -149,15 +182,15 @@ public static unsafe class AllocationManager
nuint unfreeBytes = 0u; nuint unfreeBytes = 0u;
foreach (var pair in _allocated) foreach (var pair in _allocated)
{ {
unfreeBytes += pair.Value; unfreeBytes += pair.Value.Size;
AlignedFree((void*)pair.Key); AlignedFree((void*)pair.Key);
} }
_allocated.Dispose();
if (unfreeBytes > 0u) if (unfreeBytes > 0u)
{ {
throw new InvalidOperationException($"There are still {unfreeBytes} bytes allocated buffers are not freed yet. Please free them before disposing."); throw new MemoryLeakException([.. _allocated.Values]);
} }
_allocated.Clear();
} }
} }