using System; using System.Runtime.CompilerServices; namespace AlicizaX { public static partial class MemoryPool { private static bool _enableStrictCheck; private static int _strictCheckVersion; public static bool EnableStrictCheck { get => _enableStrictCheck; set { if (_enableStrictCheck == value) return; _enableStrictCheck = value; _strictCheckVersion++; } } internal static int StrictCheckVersion => _strictCheckVersion; public static int Count => MemoryPoolRegistry.Count; #if UNITY_EDITOR public static MemoryPoolInfo[] GetAllMemoryPoolInfos() { return MemoryPoolRegistry.GetAllInfos(); } #endif public static int GetAllMemoryPoolInfos(MemoryPoolInfo[] infos) { return MemoryPoolRegistry.GetAllInfos(infos); } public static void ClearAll() { MemoryPoolRegistry.ClearAll(); } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static T Acquire() where T : class, IMemory, new() { return MemoryPool.Acquire(); } /// /// 获取动态内存类型的缓存句柄。运行时热路径应提前缓存该句柄,避免反复使用 Type 查找。 /// [MethodImpl(MethodImplOptions.AggressiveInlining)] public static MemoryPoolHandle GetHandle(Type memoryType) { return MemoryPoolRegistry.GetHandle(memoryType); } /// /// 慢速动态路径。禁止在运行时热路径调用;请提前通过 GetHandle(Type) 缓存 MemoryPoolHandle 后再获取对象。 /// [Obsolete("慢速动态路径,禁止在运行时热路径使用。请缓存 MemoryPoolHandle,或改用 MemoryPool.Acquire / MemoryPool.Acquire。", false)] [MethodImpl(MethodImplOptions.AggressiveInlining)] public static IMemory Acquire(Type memoryType) { return MemoryPoolRegistry.Acquire(memoryType); } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void Release(T memory) where T : class, IMemory, new() { MemoryPool.Release(memory); } /// /// 慢速动态路径。禁止在运行时热路径调用;请通过缓存的 MemoryPoolHandle 或 Release<T> 回收对象。 /// [Obsolete("慢速动态路径,禁止在运行时热路径使用。请通过缓存的 MemoryPoolHandle,或改用 MemoryPool.Release / MemoryPool.Release。", false)] [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void Release(IMemory memory) { MemoryPoolRegistry.Release(memory); } public static void Add(int count) where T : class, IMemory, new() { MemoryPool.Prewarm(count); } public static void Add(Type memoryType, int count) { MemoryPoolRegistry.Prewarm(memoryType, count); } public static void Remove(int count) where T : class, IMemory, new() { int target = MemoryPool.UnusedCount - count; MemoryPool.Shrink(target); } public static void Remove(Type memoryType, int count) { MemoryPoolRegistry.RemoveFromType(memoryType, count); } public static void RemoveAll() where T : class, IMemory, new() { MemoryPool.ClearAll(); } public static void SetCapacity(int softCapacity, int hardCapacity) where T : class, IMemory, new() { MemoryPool.SetCapacity(softCapacity, hardCapacity); } public static void Compact() where T : class, IMemory, new() { MemoryPool.Compact(); } public static void Compact(Type memoryType) { MemoryPoolRegistry.CompactType(memoryType); } public static void CompactAll() { MemoryPoolRegistry.CompactAll(); } public static void RemoveAll(Type memoryType) { MemoryPoolRegistry.ClearType(memoryType); } } }