拓展细分内存对象生命周期。

拓展细分内存对象生命周期。
This commit is contained in:
ALEXTANG
2023-11-30 00:13:23 +08:00
parent fb38e96e9e
commit e7f0636f30
2 changed files with 60 additions and 0 deletions

View File

@@ -0,0 +1,57 @@
using System;
namespace TEngine
{
/// <summary>
/// 内存池对象基类。
/// </summary>
public abstract class MemoryObject : IMemory
{
/// <summary>
/// 清理内存对象回收入池。
/// </summary>
public virtual void Clear()
{
}
/// <summary>
/// 从内存池中初始化。
/// </summary>
public abstract void InitFromPool();
/// <summary>
/// 回收到内存池。
/// </summary>
public abstract void RecycleToPool();
}
public static partial class MemoryPool
{
/// <summary>
/// 从内存池获取内存对象。
/// </summary>
/// <typeparam name="T">内存对象类型。</typeparam>
/// <returns>内存对象。</returns>
public static T Alloc<T>() where T : MemoryObject, new()
{
T memory = Acquire<T>();
memory.InitFromPool();
return memory;
}
/// <summary>
/// 将内存对象归还内存池。
/// </summary>
/// <param name="memory">内存对象。</param>
public static void Dealloc(MemoryObject memory)
{
if (memory == null)
{
throw new Exception("Memory is invalid.");
}
memory.RecycleToPool();
Release(memory);
}
}
}

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: ad13ef73c22340058c4420733a22b580
timeCreated: 1701273442