Files
Misaki.HighPerformance/Misaki.HighPerformance.LowLevel/MemoryLeakException.cs
Misaki d3e497c7d8 Add TLSF allocator and refactor allocation API
- Introduced TLSF allocator with thread-safe wrapper and integrated into AllocationManager.
- Extended AllocationManagerDesc for TLSF config; made properties settable.
- Refactored AllocationHandle to encapsulate function pointers and state, replacing direct field access with methods.
- Updated all memory-related structs to use new AllocationHandle API.
- Added ReplaceIfZeros utility to MemoryUtility.
- Improved IndexOfNullByte performance.
- Minor fix in MemoryLeakException output order.
- FreeList now uses a fixed 64KB refill budget.
- Bumped version to 1.6.21; removed MHP_ENABLE_STACKTRACE from Debug.
- Updated Program.cs to test TLSF allocator and manage allocation lifecycle.
2026-05-05 22:13:58 +09:00

62 lines
1.9 KiB
C#

using Misaki.HighPerformance.LowLevel.Buffer;
using System.Diagnostics;
using System.Text;
namespace Misaki.HighPerformance.LowLevel;
/// <summary>
/// An exception that is thrown when a memory leak is detected.
/// </summary>
/// <param name="Infos">An array of AllocationInfo containing details about the memory leaks.</param>
public class MemoryLeakException : Exception
{
private readonly string _message;
public override string Message => _message;
public MemoryLeakException(IEnumerable<AllocationInfo> infos)
{
var stringBuilder = new StringBuilder();
stringBuilder.AppendLine($"Found {infos.Count()} memory lakes!");
#if MHP_ENABLE_STACKTRACE
foreach (var info in infos)
{
stringBuilder.AppendLine();
GetMessage(stringBuilder, info.StackTrace);
}
#else
stringBuilder.AppendLine("No stack trace information available. Please enable MHP_ENABLE_STACKTRACE for detailed leak information.");
#endif
_message = stringBuilder.ToString();
}
public MemoryLeakException(string message)
{
_message = message;
}
private static void GetMessage(StringBuilder stringBuilder, StackTrace? stackTrace)
{
if (stackTrace == null)
{
stringBuilder.AppendLine("No stack trace available.");
return;
}
stringBuilder.AppendLine("Memory leak detected at: ");
for (var i = 0; i < stackTrace.FrameCount; i++)
{
var frame = stackTrace.GetFrame(i);
var fileName = frame?.GetFileName();
if (frame != null)
{
var methodInfo = DiagnosticMethodInfo.Create(frame);
stringBuilder.AppendLine($"Type: {methodInfo?.DeclaringTypeName}, Method: {methodInfo?.Name}, File: {fileName}, Line: {frame.GetFileLineNumber()}");
}
}
}
}