From b1244e42a00cfa0ae4a73f194ba98ffd3815dc7b Mon Sep 17 00:00:00 2001 From: Mikhail <99481254+DCFApixels@users.noreply.github.com> Date: Wed, 18 Mar 2026 15:44:47 +0800 Subject: [PATCH] fix mem allock debugging --- src/Internal/Allocators/MemoryAllocator.cs | 95 +++++++++------------- src/Internal/SortHalper.cs | 10 +++ 2 files changed, 47 insertions(+), 58 deletions(-) diff --git a/src/Internal/Allocators/MemoryAllocator.cs b/src/Internal/Allocators/MemoryAllocator.cs index 4dfceb1..e35b8ff 100644 --- a/src/Internal/Allocators/MemoryAllocator.cs +++ b/src/Internal/Allocators/MemoryAllocator.cs @@ -10,6 +10,7 @@ namespace DCFApixels.DragonECS.Core.Internal internal unsafe static class MemoryAllocator { #if DEBUG + private static ulong _inrement = 0; private static IdDispenser _idDispenser; private static HandlerDebugInfo[] _debugInfos; #endif @@ -25,6 +26,21 @@ namespace DCFApixels.DragonECS.Core.Internal _debugInfos = new HandlerDebugInfo[32]; #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(result, o => o.increment); + return result; + } #region AllocAndInit public static HMem AllocAndInit(int count) where T : unmanaged @@ -76,6 +92,7 @@ namespace DCFApixels.DragonECS.Core.Internal #if DRAGONECS_DEEP_DEBUG _debugInfos[id].stackTrace = new System.Diagnostics.StackTrace(); #endif + _debugInfos[id].increment = ++_inrement; _debugInfos[id].type = type; _debugInfos[id].handler = handler; #endif @@ -143,17 +160,19 @@ namespace DCFApixels.DragonECS.Core.Internal { return Alloc_Internal(newByteLength, newType); } -#if DEBUG - int id = 0; - lock (_idDispenser) - { - if (_debugInfos.Length <= _idDispenser.Count) - { - Array.Resize(ref _debugInfos, ArrayUtility.NextPow2(_idDispenser.Count)); - } - id = _idDispenser.UseFree(); - } -#endif + //#if DEBUG + // int id = 0; + // lock (_idDispenser) + // { + // if (_debugInfos.Length <= _idDispenser.Count) + // { + // Array.Resize(ref _debugInfos, ArrayUtility.NextPow2(_idDispenser.Count)); + // } + // id = _idDispenser.UseFree(); + // } + //#endif + var id = target.GetHandledPtr()->ID; + Meta* newHandledPtr = (Meta*)Marshal.ReAllocHGlobal( (IntPtr)target.GetHandledPtr(), (IntPtr)newByteLength + sizeof(Meta)); @@ -162,10 +181,11 @@ namespace DCFApixels.DragonECS.Core.Internal newHandledPtr->ID = id; newHandledPtr->ByteLength = newByteLength; #if DRAGONECS_DEEP_DEBUG - _debugInfos[newHandledPtr->ID].stackTrace = new System.Diagnostics.StackTrace(); + _debugInfos[id].stackTrace = new System.Diagnostics.StackTrace(); #endif - _debugInfos[newHandledPtr->ID].type = newType; - _debugInfos[newHandledPtr->ID].handler = handler; + _debugInfos[id].increment = ++_inrement; + _debugInfos[id].type = newType; + _debugInfos[id].handler = handler; #endif return handler; } @@ -265,6 +285,7 @@ namespace DCFApixels.DragonECS.Core.Internal #if DRAGONECS_DEEP_DEBUG public System.Diagnostics.StackTrace stackTrace; #endif + public ulong increment; public Type type; public Handler handler; #endif @@ -276,6 +297,7 @@ namespace DCFApixels.DragonECS.Core.Internal } #endregion + #region Handlers public readonly struct HMem : IDisposable, IEquatable> where T : unmanaged { @@ -508,50 +530,7 @@ namespace DCFApixels.DragonECS.Core.Internal #endif #endregion } - } - - internal ref struct AllocBuilder - { - //[ThreadStatic] - //private static Stack _buildersPool; - //private AllocBuilder() { } - //public static AllocBuilder New() - //{ - // if (_buildersPool == null) { _buildersPool = new Stack(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(int count) where T : unmanaged - { - Add(Marshal.SizeOf()); - } - 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); - } + #endregion } internal static class MemoryAllocatorHandlerExtensions diff --git a/src/Internal/SortHalper.cs b/src/Internal/SortHalper.cs index 31e4c49..6d53e4a 100644 --- a/src/Internal/SortHalper.cs +++ b/src/Internal/SortHalper.cs @@ -17,6 +17,16 @@ namespace DCFApixels.DragonECS.Core.Internal #endif internal static unsafe class SortHalper { + #region OrderBy + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void SortBy(Span span, Func keySelector) + where TKey : IComparable + { + var c = new ComparisonWrapper((a, b) => keySelector(a).CompareTo(keySelector(b))); + SortHalper>.Sort(span, ref c); + } + #endregion + #region Span [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void Sort(Span span)