diff --git a/UnityProject/Assets/TEngine/Runtime/Core/MemoryPool/MemoryPoolExtension.cs b/UnityProject/Assets/TEngine/Runtime/Core/MemoryPool/MemoryPoolExtension.cs new file mode 100644 index 00000000..18619890 --- /dev/null +++ b/UnityProject/Assets/TEngine/Runtime/Core/MemoryPool/MemoryPoolExtension.cs @@ -0,0 +1,57 @@ +using System; + +namespace TEngine +{ + /// + /// 内存池对象基类。 + /// + 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); + } + } +} \ No newline at end of file diff --git a/UnityProject/Assets/TEngine/Runtime/Core/MemoryPool/MemoryPoolExtension.cs.meta b/UnityProject/Assets/TEngine/Runtime/Core/MemoryPool/MemoryPoolExtension.cs.meta new file mode 100644 index 00000000..2d4491f4 --- /dev/null +++ b/UnityProject/Assets/TEngine/Runtime/Core/MemoryPool/MemoryPoolExtension.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: ad13ef73c22340058c4420733a22b580 +timeCreated: 1701273442 \ No newline at end of file