2026-04-21 14:24:36 +08:00
|
|
|
using System;
|
2025-10-11 15:18:09 +08:00
|
|
|
|
|
|
|
|
namespace AlicizaX
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
2026-04-21 14:24:36 +08:00
|
|
|
/// 内存池。保留旧 API 签名,内部转发到 MemoryPool<T> / MemoryPoolRegistry。
|
2025-10-11 15:18:09 +08:00
|
|
|
/// </summary>
|
|
|
|
|
public static partial class MemoryPool
|
|
|
|
|
{
|
2026-04-21 14:24:36 +08:00
|
|
|
private static bool _enableStrictCheck;
|
2025-10-11 15:18:09 +08:00
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 获取或设置是否开启强制检查。
|
|
|
|
|
/// </summary>
|
|
|
|
|
public static bool EnableStrictCheck
|
|
|
|
|
{
|
|
|
|
|
get => _enableStrictCheck;
|
|
|
|
|
set => _enableStrictCheck = value;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 获取内存池的数量。
|
|
|
|
|
/// </summary>
|
2026-04-21 14:24:36 +08:00
|
|
|
public static int Count => MemoryPoolRegistry.Count;
|
2025-10-11 15:18:09 +08:00
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 获取所有内存池的信息。
|
|
|
|
|
/// </summary>
|
|
|
|
|
public static MemoryPoolInfo[] GetAllMemoryPoolInfos()
|
|
|
|
|
{
|
2026-04-21 14:24:36 +08:00
|
|
|
return MemoryPoolRegistry.GetAllInfos();
|
2025-10-11 15:18:09 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 清除所有内存池。
|
|
|
|
|
/// </summary>
|
|
|
|
|
public static void ClearAll()
|
|
|
|
|
{
|
2026-04-21 14:24:36 +08:00
|
|
|
MemoryPoolRegistry.ClearAll();
|
2025-10-11 15:18:09 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 从内存池获取内存对象。
|
|
|
|
|
/// </summary>
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 从内存池获取内存对象。
|
|
|
|
|
/// </summary>
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 将内存对象归还内存池。
|
|
|
|
|
/// </summary>
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2026-04-21 14:24:36 +08:00
|
|
|
/// 向内存池中预热指定数量的内存对象。
|
2025-10-11 15:18:09 +08:00
|
|
|
/// </summary>
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2026-04-21 14:24:36 +08:00
|
|
|
/// 向内存池中预热指定数量的内存对象。
|
2025-10-11 15:18:09 +08:00
|
|
|
/// </summary>
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 从内存池中移除指定数量的内存对象。
|
|
|
|
|
/// </summary>
|
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
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 从内存池中移除指定数量的内存对象。
|
|
|
|
|
/// </summary>
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 从内存池中移除所有的内存对象。
|
|
|
|
|
/// </summary>
|
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
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 从内存池中移除所有的内存对象。
|
|
|
|
|
/// </summary>
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|