Added a TLSF (Two-Level Segregated Fit) memory allocator with O(1) allocation, free, and reallocation in `TLSF.cs`, plus comprehensive unit tests. Introduced a cache-line-padded `SeqLock` synchronization primitive. Refactored vector extension code for conciseness and fixed its usage to `extension(ref ...)`. Updated namespaces, removed unused code, and improved assertion and diagnostics. Updated NuGet dependencies and project files.
62 lines
1.9 KiB
C#
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($"File: {fileName}, Type: {methodInfo?.DeclaringTypeName}, Method: {methodInfo?.Name}, Line: {frame.GetFileLineNumber()}");
|
|
}
|
|
}
|
|
}
|
|
} |