fix mem allock debugging

This commit is contained in:
Mikhail 2026-03-18 15:44:47 +08:00
parent 1c6edc6e6b
commit b1244e42a0
2 changed files with 47 additions and 58 deletions

View File

@ -10,6 +10,7 @@ namespace DCFApixels.DragonECS.Core.Internal
internal unsafe static class MemoryAllocator internal unsafe static class MemoryAllocator
{ {
#if DEBUG #if DEBUG
private static ulong _inrement = 0;
private static IdDispenser _idDispenser; private static IdDispenser _idDispenser;
private static HandlerDebugInfo[] _debugInfos; private static HandlerDebugInfo[] _debugInfos;
#endif #endif
@ -25,6 +26,21 @@ namespace DCFApixels.DragonECS.Core.Internal
_debugInfos = new HandlerDebugInfo[32]; _debugInfos = new HandlerDebugInfo[32];
#endif #endif
} }
private static HandlerDebugInfo[] CurrentHandlersList
{
get { return CreateCurrentHandlersList_Debug(); }
}
internal static HandlerDebugInfo[] CreateCurrentHandlersList_Debug()
{
var result = new HandlerDebugInfo[_idDispenser.Count];
int i = 0;
foreach (var id in _idDispenser)
{
result[i++] = _debugInfos[id];
}
SortHalper.SortBy<HandlerDebugInfo, ulong>(result, o => o.increment);
return result;
}
#region AllocAndInit #region AllocAndInit
public static HMem<T> AllocAndInit<T>(int count) where T : unmanaged public static HMem<T> AllocAndInit<T>(int count) where T : unmanaged
@ -76,6 +92,7 @@ namespace DCFApixels.DragonECS.Core.Internal
#if DRAGONECS_DEEP_DEBUG #if DRAGONECS_DEEP_DEBUG
_debugInfos[id].stackTrace = new System.Diagnostics.StackTrace(); _debugInfos[id].stackTrace = new System.Diagnostics.StackTrace();
#endif #endif
_debugInfos[id].increment = ++_inrement;
_debugInfos[id].type = type; _debugInfos[id].type = type;
_debugInfos[id].handler = handler; _debugInfos[id].handler = handler;
#endif #endif
@ -143,17 +160,19 @@ namespace DCFApixels.DragonECS.Core.Internal
{ {
return Alloc_Internal(newByteLength, newType); return Alloc_Internal(newByteLength, newType);
} }
#if DEBUG //#if DEBUG
int id = 0; // int id = 0;
lock (_idDispenser) // lock (_idDispenser)
{ // {
if (_debugInfos.Length <= _idDispenser.Count) // if (_debugInfos.Length <= _idDispenser.Count)
{ // {
Array.Resize(ref _debugInfos, ArrayUtility.NextPow2(_idDispenser.Count)); // Array.Resize(ref _debugInfos, ArrayUtility.NextPow2(_idDispenser.Count));
} // }
id = _idDispenser.UseFree(); // id = _idDispenser.UseFree();
} // }
#endif //#endif
var id = target.GetHandledPtr()->ID;
Meta* newHandledPtr = (Meta*)Marshal.ReAllocHGlobal( Meta* newHandledPtr = (Meta*)Marshal.ReAllocHGlobal(
(IntPtr)target.GetHandledPtr(), (IntPtr)target.GetHandledPtr(),
(IntPtr)newByteLength + sizeof(Meta)); (IntPtr)newByteLength + sizeof(Meta));
@ -162,10 +181,11 @@ namespace DCFApixels.DragonECS.Core.Internal
newHandledPtr->ID = id; newHandledPtr->ID = id;
newHandledPtr->ByteLength = newByteLength; newHandledPtr->ByteLength = newByteLength;
#if DRAGONECS_DEEP_DEBUG #if DRAGONECS_DEEP_DEBUG
_debugInfos[newHandledPtr->ID].stackTrace = new System.Diagnostics.StackTrace(); _debugInfos[id].stackTrace = new System.Diagnostics.StackTrace();
#endif #endif
_debugInfos[newHandledPtr->ID].type = newType; _debugInfos[id].increment = ++_inrement;
_debugInfos[newHandledPtr->ID].handler = handler; _debugInfos[id].type = newType;
_debugInfos[id].handler = handler;
#endif #endif
return handler; return handler;
} }
@ -265,6 +285,7 @@ namespace DCFApixels.DragonECS.Core.Internal
#if DRAGONECS_DEEP_DEBUG #if DRAGONECS_DEEP_DEBUG
public System.Diagnostics.StackTrace stackTrace; public System.Diagnostics.StackTrace stackTrace;
#endif #endif
public ulong increment;
public Type type; public Type type;
public Handler handler; public Handler handler;
#endif #endif
@ -276,6 +297,7 @@ namespace DCFApixels.DragonECS.Core.Internal
} }
#endregion #endregion
#region Handlers
public readonly struct HMem<T> : IDisposable, IEquatable<HMem<T>> public readonly struct HMem<T> : IDisposable, IEquatable<HMem<T>>
where T : unmanaged where T : unmanaged
{ {
@ -508,50 +530,7 @@ namespace DCFApixels.DragonECS.Core.Internal
#endif #endif
#endregion #endregion
} }
} #endregion
internal ref struct AllocBuilder
{
//[ThreadStatic]
//private static Stack<AllocBuilder> _buildersPool;
//private AllocBuilder() { }
//public static AllocBuilder New()
//{
// if (_buildersPool == null) { _buildersPool = new Stack<AllocBuilder>(4); }
// if(_buildersPool.TryPop(out var result) == false)
// {
// result = new AllocBuilder();
// }
// return result;
//}
public static AllocBuilder New()
{
return new AllocBuilder(0);
}
private int _byteLength;
private AllocBuilder(int byteLength)
{
_byteLength = byteLength;
}
public void Add<T>(int count) where T : unmanaged
{
Add(Marshal.SizeOf<T>());
}
public void Add(int byteLength)
{
_byteLength += byteLength;
}
public MemoryAllocator.Handler Alloc()
{
return MemoryAllocator.Alloc(_byteLength);
}
public MemoryAllocator.Handler Realloc(MemoryAllocator.Handler handler)
{
return MemoryAllocator.Realloc(handler, _byteLength);
}
} }
internal static class MemoryAllocatorHandlerExtensions internal static class MemoryAllocatorHandlerExtensions

View File

@ -17,6 +17,16 @@ namespace DCFApixels.DragonECS.Core.Internal
#endif #endif
internal static unsafe class SortHalper internal static unsafe class SortHalper
{ {
#region OrderBy
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void SortBy<T, TKey>(Span<T> span, Func<T, TKey> keySelector)
where TKey : IComparable<TKey>
{
var c = new ComparisonWrapper<T>((a, b) => keySelector(a).CompareTo(keySelector(b)));
SortHalper<T, ComparisonWrapper<T>>.Sort(span, ref c);
}
#endregion
#region Span #region Span
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void Sort<T>(Span<T> span) public static void Sort<T>(Span<T> span)