using System;
namespace AlicizaX
{
///
/// 内存池对象基类。
///
public abstract class MemoryObject : IMemory
{
///
/// 清理内存对象回收入池。
///
public virtual void Clear()
{
}
///
/// 从内存池中初始化。
///
public abstract void InitFromPool();
///
/// 回收到内存池。
///
public abstract void RecycleToPool();
}
public static partial class MemoryPool
{
///
/// 从内存池获取内存对象。
///
public static T Alloc() where T : MemoryObject, new()
{
T memory = MemoryPool.Acquire();
memory.InitFromPool();
return memory;
}
///
/// 将内存对象归还内存池。
///
public static void Dealloc(MemoryObject memory)
{
if (memory == null)
{
throw new ArgumentNullException(nameof(memory));
}
memory.RecycleToPool();
MemoryPoolRegistry.Release(memory);
}
}
}