From 78c565e4289cc165cd283793c228cbefc8ca7a0d Mon Sep 17 00:00:00 2001 From: Misaki Date: Thu, 2 Apr 2026 19:11:37 +0900 Subject: [PATCH] feat(buffer): add allocation tracking and diagnostics Add AllocationManager.GetTotalAllocatedMemory() to track total allocations when safety checks are enabled. Improve diagnostics by calling Debug.Fail in Dispose methods of HashMapHelper, UnTypedArray, and UnsafeArray when disposing uninitialized or already disposed arrays. Remove AddRange(ReadOnlyUnsafeCollection) from UnsafeList. Increment assembly version to 1.6.4. Ensure arrcpy is disposed in Program.cs. --- .../Buffer/AllocationManager.cs | 26 +++++++++++++++++++ .../Collections/HashMapHelper.cs | 1 + .../Collections/UnTypedArray.cs | 2 ++ .../Collections/UnsafeArray.cs | 1 + .../Collections/UnsafeList.cs | 9 ------- .../Misaki.HighPerformance.LowLevel.csproj | 2 +- Misaki.HighPerformance.Test/Program.cs | 1 + 7 files changed, 32 insertions(+), 10 deletions(-) diff --git a/Misaki.HighPerformance.LowLevel/Buffer/AllocationManager.cs b/Misaki.HighPerformance.LowLevel/Buffer/AllocationManager.cs index 83c2419..2b0b765 100644 --- a/Misaki.HighPerformance.LowLevel/Buffer/AllocationManager.cs +++ b/Misaki.HighPerformance.LowLevel/Buffer/AllocationManager.cs @@ -1,5 +1,7 @@ #if MHP_ENABLE_SAFETY_CHECKS using Misaki.HighPerformance.Collections; +using System.Collections.Concurrent; + #endif using System.Diagnostics; using System.Runtime.CompilerServices; @@ -804,6 +806,30 @@ public static unsafe class AllocationManager #endif } + /// + /// Gets the total newSize of all currently tracked allocations. + /// + /// + /// Always returns 0 if MHP_ENABLE_SAFETY_CHECKS is disabled. + /// + /// The total newSize of all currently tracked allocations. + public static nuint GetTotalAllocatedMemory() + { +#if MHP_ENABLE_SAFETY_CHECKS + Debug.Assert(s_initialized, "AllocationManager is not initialized."); + + nuint total = 0; + foreach (var allocation in s_allocations) + { + total += allocation.Size; + } + + return total; +#else + return 0; +#endif + } + /// /// Disposes of the AllocationManager, freeing all allocated memory and resources. /// diff --git a/Misaki.HighPerformance.LowLevel/Collections/HashMapHelper.cs b/Misaki.HighPerformance.LowLevel/Collections/HashMapHelper.cs index 1cca1db..9741167 100644 --- a/Misaki.HighPerformance.LowLevel/Collections/HashMapHelper.cs +++ b/Misaki.HighPerformance.LowLevel/Collections/HashMapHelper.cs @@ -719,6 +719,7 @@ public unsafe struct HashMapHelper : IDisposable { if (!IsCreated) { + Debug.Fail("The UnsafeArray is not created or already disposed."); return; } diff --git a/Misaki.HighPerformance.LowLevel/Collections/UnTypedArray.cs b/Misaki.HighPerformance.LowLevel/Collections/UnTypedArray.cs index fb43d56..136e084 100644 --- a/Misaki.HighPerformance.LowLevel/Collections/UnTypedArray.cs +++ b/Misaki.HighPerformance.LowLevel/Collections/UnTypedArray.cs @@ -1,6 +1,7 @@ using Misaki.HighPerformance.LowLevel.Buffer; using Misaki.HighPerformance.LowLevel.Collections.Contracts; using Misaki.HighPerformance.LowLevel.Utilities; +using System.Diagnostics; using System.Runtime.CompilerServices; namespace Misaki.HighPerformance.LowLevel.Collections; @@ -272,6 +273,7 @@ public unsafe struct UnTypedArray : IUnTypedCollection { if (!IsCreated) { + Debug.Fail("The UnsafeArray is not created or already disposed."); return; } diff --git a/Misaki.HighPerformance.LowLevel/Collections/UnsafeArray.cs b/Misaki.HighPerformance.LowLevel/Collections/UnsafeArray.cs index b0e82a7..4143ec0 100644 --- a/Misaki.HighPerformance.LowLevel/Collections/UnsafeArray.cs +++ b/Misaki.HighPerformance.LowLevel/Collections/UnsafeArray.cs @@ -418,6 +418,7 @@ public unsafe struct UnsafeArray : IUnsafeCollection { if (!IsCreated) { + Debug.Fail("The UnsafeArray is not created or already disposed."); return; } diff --git a/Misaki.HighPerformance.LowLevel/Collections/UnsafeList.cs b/Misaki.HighPerformance.LowLevel/Collections/UnsafeList.cs index 9cf5ab9..be69dfc 100644 --- a/Misaki.HighPerformance.LowLevel/Collections/UnsafeList.cs +++ b/Misaki.HighPerformance.LowLevel/Collections/UnsafeList.cs @@ -363,15 +363,6 @@ public unsafe struct UnsafeList : IUnsafeCollection _count += values.Length; } - /// - /// Adds a range of elements to the collection. - /// - /// A collection containing the elements to add. - public void AddRange(ReadOnlyUnsafeCollection collection) - { - AddRange((T*)collection.GetUnsafePtr(), collection.Count); - } - /// /// Adds a range of elements from a pointer to the collection. /// diff --git a/Misaki.HighPerformance.LowLevel/Misaki.HighPerformance.LowLevel.csproj b/Misaki.HighPerformance.LowLevel/Misaki.HighPerformance.LowLevel.csproj index a605db0..51f5c07 100644 --- a/Misaki.HighPerformance.LowLevel/Misaki.HighPerformance.LowLevel.csproj +++ b/Misaki.HighPerformance.LowLevel/Misaki.HighPerformance.LowLevel.csproj @@ -7,7 +7,7 @@ true true Misaki - 1.6.3 + 1.6.4 $(AssemblyVersion) https://git.personalnas.com/Misaki/Misaki.HighPerformance.git https://git.personalnas.com/Misaki/Misaki.HighPerformance.git diff --git a/Misaki.HighPerformance.Test/Program.cs b/Misaki.HighPerformance.Test/Program.cs index ce94a9c..b432621 100644 --- a/Misaki.HighPerformance.Test/Program.cs +++ b/Misaki.HighPerformance.Test/Program.cs @@ -15,6 +15,7 @@ AllocationManager.Initialize(opts); var arr = new UnsafeArray(10, Allocator.Persistent); var arrcpy = arr; arr.Dispose(); +arrcpy.Dispose(); Console.WriteLine(arr.IsCreated); Console.WriteLine(arrcpy.IsCreated);