com.alicizax.unity.framework/Runtime/MemoryPool/MemoryPool.cs

127 lines
3.2 KiB
C#
Raw Normal View History

2026-04-21 14:24:36 +08:00
using System;
using System.Runtime.CompilerServices;
2025-10-11 15:18:09 +08:00
namespace AlicizaX
{
public static partial class MemoryPool
{
2026-04-21 14:24:36 +08:00
private static bool _enableStrictCheck;
2026-04-23 18:18:27 +08:00
private static int _strictCheckVersion;
2025-10-11 15:18:09 +08:00
2025-10-11 15:18:09 +08:00
public static bool EnableStrictCheck
{
get => _enableStrictCheck;
2026-04-23 18:18:27 +08:00
set
{
if (_enableStrictCheck == value)
return;
_enableStrictCheck = value;
_strictCheckVersion++;
}
2025-10-11 15:18:09 +08:00
}
2026-04-23 18:18:27 +08:00
internal static int StrictCheckVersion => _strictCheckVersion;
2026-04-21 14:24:36 +08:00
public static int Count => MemoryPoolRegistry.Count;
2025-10-11 15:18:09 +08:00
#if UNITY_EDITOR
2025-10-11 15:18:09 +08:00
public static MemoryPoolInfo[] GetAllMemoryPoolInfos()
{
2026-04-21 14:24:36 +08:00
return MemoryPoolRegistry.GetAllInfos();
2025-10-11 15:18:09 +08:00
}
#endif
2025-10-11 15:18:09 +08:00
2026-04-23 19:09:56 +08:00
public static int GetAllMemoryPoolInfos(MemoryPoolInfo[] infos)
{
return MemoryPoolRegistry.GetAllInfos(infos);
}
2025-10-11 15:18:09 +08:00
public static void ClearAll()
{
2026-04-21 14:24:36 +08:00
MemoryPoolRegistry.ClearAll();
2025-10-11 15:18:09 +08:00
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
2025-10-11 15:18:09 +08:00
public static T Acquire<T>() where T : class, IMemory, new()
{
2026-04-21 14:24:36 +08:00
return MemoryPool<T>.Acquire();
2025-10-11 15:18:09 +08:00
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
2025-10-11 15:18:09 +08:00
public static IMemory Acquire(Type memoryType)
{
2026-04-21 14:24:36 +08:00
return MemoryPoolRegistry.Acquire(memoryType);
2025-10-11 15:18:09 +08:00
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void Release<T>(T memory) where T : class, IMemory, new()
{
MemoryPool<T>.Release(memory);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
2025-10-11 15:18:09 +08:00
public static void Release(IMemory memory)
{
2026-04-21 14:24:36 +08:00
MemoryPoolRegistry.Release(memory);
2025-10-11 15:18:09 +08:00
}
public static void Add<T>(int count) where T : class, IMemory, new()
{
2026-04-21 14:24:36 +08:00
MemoryPool<T>.Prewarm(count);
2025-10-11 15:18:09 +08:00
}
public static void Add(Type memoryType, int count)
{
2026-04-21 14:24:36 +08:00
MemoryPoolRegistry.Prewarm(memoryType, count);
2025-10-11 15:18:09 +08:00
}
2026-04-21 14:24:36 +08:00
public static void Remove<T>(int count) where T : class, IMemory, new()
2025-10-11 15:18:09 +08:00
{
2026-04-21 14:24:36 +08:00
int target = MemoryPool<T>.UnusedCount - count;
MemoryPool<T>.Shrink(target);
2025-10-11 15:18:09 +08:00
}
public static void Remove(Type memoryType, int count)
{
2026-04-21 14:24:36 +08:00
MemoryPoolRegistry.RemoveFromType(memoryType, count);
2025-10-11 15:18:09 +08:00
}
2026-04-21 14:24:36 +08:00
public static void RemoveAll<T>() where T : class, IMemory, new()
2025-10-11 15:18:09 +08:00
{
2026-04-21 14:24:36 +08:00
MemoryPool<T>.ClearAll();
2025-10-11 15:18:09 +08:00
}
public static void SetCapacity<T>(int softCapacity, int hardCapacity) where T : class, IMemory, new()
{
MemoryPool<T>.SetCapacity(softCapacity, hardCapacity);
}
public static void Compact<T>() where T : class, IMemory, new()
{
MemoryPool<T>.Compact();
}
public static void Compact(Type memoryType)
{
MemoryPoolRegistry.CompactType(memoryType);
}
public static void CompactAll()
{
MemoryPoolRegistry.CompactAll();
}
2025-10-11 15:18:09 +08:00
public static void RemoveAll(Type memoryType)
{
2026-04-21 14:24:36 +08:00
MemoryPoolRegistry.ClearType(memoryType);
2025-10-11 15:18:09 +08:00
}
}
}