using System;
namespace AlicizaX.Runtime
{
///
/// 内存池对象基类。
///
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 = Acquire();
memory.InitFromPool();
return memory;
}
///
/// 将内存对象归还内存池。
///
/// 内存对象。
public static void Dealloc(MemoryObject memory)
{
if (memory == null)
{
throw new Exception("Memory is invalid.");
}
memory.RecycleToPool();
Release(memory);
}
}
}