Skip to content

Commit

Permalink
拓展细分内存对象生命周期。
Browse files Browse the repository at this point in the history
拓展细分内存对象生命周期。
  • Loading branch information
Alex-Rachel committed Nov 29, 2023
1 parent fb38e96 commit e7f0636
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -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);
}
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit e7f0636

Please sign in to comment.