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<T>) from UnsafeList<T>. Increment assembly version to 1.6.4. Ensure arrcpy is disposed in Program.cs.
This commit is contained in:
@@ -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
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the total newSize of all currently tracked allocations.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// Always returns 0 if MHP_ENABLE_SAFETY_CHECKS is disabled.
|
||||
/// </remarks>
|
||||
/// <returns>The total newSize of all currently tracked allocations.</returns>
|
||||
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
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Disposes of the AllocationManager, freeing all allocated memory and resources.
|
||||
/// </summary>
|
||||
|
||||
@@ -719,6 +719,7 @@ public unsafe struct HashMapHelper<TKey> : IDisposable
|
||||
{
|
||||
if (!IsCreated)
|
||||
{
|
||||
Debug.Fail("The UnsafeArray is not created or already disposed.");
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
|
||||
@@ -418,6 +418,7 @@ public unsafe struct UnsafeArray<T> : IUnsafeCollection<T>
|
||||
{
|
||||
if (!IsCreated)
|
||||
{
|
||||
Debug.Fail("The UnsafeArray is not created or already disposed.");
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
@@ -363,15 +363,6 @@ public unsafe struct UnsafeList<T> : IUnsafeCollection<T>
|
||||
_count += values.Length;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Adds a range of elements to the collection.
|
||||
/// </summary>
|
||||
/// <param name="values">A collection containing the elements to add.</param>
|
||||
public void AddRange(ReadOnlyUnsafeCollection<T> collection)
|
||||
{
|
||||
AddRange((T*)collection.GetUnsafePtr(), collection.Count);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Adds a range of elements from a pointer to the collection.
|
||||
/// </summary>
|
||||
|
||||
@@ -7,7 +7,7 @@
|
||||
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
|
||||
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
|
||||
<Authors>Misaki</Authors>
|
||||
<AssemblyVersion>1.6.3</AssemblyVersion>
|
||||
<AssemblyVersion>1.6.4</AssemblyVersion>
|
||||
<Version>$(AssemblyVersion)</Version>
|
||||
<PackageProjectUrl>https://git.personalnas.com/Misaki/Misaki.HighPerformance.git</PackageProjectUrl>
|
||||
<RepositoryUrl>https://git.personalnas.com/Misaki/Misaki.HighPerformance.git</RepositoryUrl>
|
||||
|
||||
@@ -15,6 +15,7 @@ AllocationManager.Initialize(opts);
|
||||
var arr = new UnsafeArray<int>(10, Allocator.Persistent);
|
||||
var arrcpy = arr;
|
||||
arr.Dispose();
|
||||
arrcpy.Dispose();
|
||||
|
||||
Console.WriteLine(arr.IsCreated);
|
||||
Console.WriteLine(arrcpy.IsCreated);
|
||||
|
||||
Reference in New Issue
Block a user