-
Notifications
You must be signed in to change notification settings - Fork 297
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
2 changed files
with
60 additions
and
0 deletions.
There are no files selected for viewing
57 changes: 57 additions & 0 deletions
57
UnityProject/Assets/TEngine/Runtime/Core/MemoryPool/MemoryPoolExtension.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
3 changes: 3 additions & 0 deletions
3
UnityProject/Assets/TEngine/Runtime/Core/MemoryPool/MemoryPoolExtension.cs.meta
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.