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:
@@ -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
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,8 +1,29 @@
|
||||
using Misaki.HighPerformance.Unsafe.Buffer;
|
||||
using Misaki.HighPerformance.Unsafe.Collections;
|
||||
|
||||
#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
|
||||
}
|
||||
|
||||
public static unsafe class AllocationManager
|
||||
{
|
||||
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 bool _initialized;
|
||||
|
||||
private static UnsafeHashMap<IntPtr, nuint> _allocated;
|
||||
private static Dictionary<IntPtr, AllocationInfo> _allocated = null!;
|
||||
|
||||
private static readonly Lock _lock = new();
|
||||
|
||||
@@ -26,7 +47,7 @@ public static unsafe class AllocationManager
|
||||
}
|
||||
|
||||
_arena = new DynamicArena(initialSize);
|
||||
_allocated = new(32, Allocator.Persistent, AllocationOption.UnTracked);
|
||||
_allocated = new(32);
|
||||
|
||||
_initialized = true;
|
||||
}
|
||||
@@ -56,7 +77,13 @@ public static unsafe class AllocationManager
|
||||
case Allocator.Persistent:
|
||||
var allocationSize = size * (nuint)sizeof(T);
|
||||
buffer = (T*)AlignedAlloc(allocationSize, alignSize);
|
||||
_allocated[(IntPtr)buffer] = allocationSize;
|
||||
_allocated[(IntPtr)buffer] = new AllocationInfo
|
||||
{
|
||||
Size = allocationSize,
|
||||
#if DEBUG
|
||||
StackTrace = new StackTrace(true)
|
||||
#endif
|
||||
};
|
||||
break;
|
||||
|
||||
default:
|
||||
@@ -93,10 +120,16 @@ public static unsafe class AllocationManager
|
||||
var allocationSize = size * (nuint)sizeof(T);
|
||||
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))
|
||||
{
|
||||
_allocated.Add((IntPtr)newBuffer, allocationSize);
|
||||
_allocated[(IntPtr)newBuffer] = new AllocationInfo
|
||||
{
|
||||
Size = allocationSize,
|
||||
#if DEBUG
|
||||
StackTrace = new StackTrace(true)
|
||||
#endif
|
||||
};
|
||||
}
|
||||
break;
|
||||
|
||||
@@ -149,15 +182,15 @@ public static unsafe class AllocationManager
|
||||
nuint unfreeBytes = 0u;
|
||||
foreach (var pair in _allocated)
|
||||
{
|
||||
unfreeBytes += pair.Value;
|
||||
unfreeBytes += pair.Value.Size;
|
||||
AlignedFree((void*)pair.Key);
|
||||
}
|
||||
|
||||
_allocated.Dispose();
|
||||
|
||||
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();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user