using System; using System.Collections.Generic; namespace AlicizaX { /// /// 内存池。 /// public static partial class MemoryPool { private static readonly Dictionary _memoryCollections = new Dictionary(); private static bool _enableStrictCheck = false; /// /// 获取或设置是否开启强制检查。 /// public static bool EnableStrictCheck { get => _enableStrictCheck; set => _enableStrictCheck = value; } /// /// 获取内存池的数量。 /// // ReSharper disable once InconsistentlySynchronizedField public static int Count => _memoryCollections.Count; /// /// 获取所有内存池的信息。 /// /// 所有内存池的信息。 public static MemoryPoolInfo[] GetAllMemoryPoolInfos() { int index = 0; MemoryPoolInfo[] results = null; lock (_memoryCollections) { results = new MemoryPoolInfo[_memoryCollections.Count]; foreach (KeyValuePair memoryCollection in _memoryCollections) { results[index++] = new MemoryPoolInfo(memoryCollection.Key, memoryCollection.Value.UnusedMemoryCount, memoryCollection.Value.UsingMemoryCount, memoryCollection.Value.AcquireMemoryCount, memoryCollection.Value.ReleaseMemoryCount, memoryCollection.Value.AddMemoryCount, memoryCollection.Value.RemoveMemoryCount); } } return results; } /// /// 清除所有内存池。 /// public static void ClearAll() { lock (_memoryCollections) { foreach (KeyValuePair memoryCollection in _memoryCollections) { memoryCollection.Value.RemoveAll(); } _memoryCollections.Clear(); } } /// /// 从内存池获取内存对象。 /// /// 内存对象类型。 /// 内存对象。 public static T Acquire() where T : class, IMemory, new() { return GetMemoryCollection(typeof(T)).Acquire(); } /// /// 从内存池获取内存对象。 /// /// 内存对象类型。 /// 内存对象。 public static IMemory Acquire(Type memoryType) { InternalCheckMemoryType(memoryType); return GetMemoryCollection(memoryType).Acquire(); } /// /// 将内存对象归还内存池。 /// /// 内存对象。 public static void Release(IMemory memory) { if (memory == null) { throw new Exception("Memory is invalid."); } Type memoryType = memory.GetType(); InternalCheckMemoryType(memoryType); GetMemoryCollection(memoryType).Release(memory); } /// /// 向内存池中追加指定数量的内存对象。 /// /// 内存对象类型。 /// 追加数量。 public static void Add(int count) where T : class, IMemory, new() { GetMemoryCollection(typeof(T)).Add(count); } /// /// 向内存池中追加指定数量的内存对象。 /// /// 内存对象类型。 /// 追加数量。 public static void Add(Type memoryType, int count) { InternalCheckMemoryType(memoryType); GetMemoryCollection(memoryType).Add(count); } /// /// 从内存池中移除指定数量的内存对象。 /// /// 内存对象类型。 /// 移除数量。 public static void Remove(int count) where T : class, IMemory { GetMemoryCollection(typeof(T)).Remove(count); } /// /// 从内存池中移除指定数量的内存对象。 /// /// 内存对象类型。 /// 移除数量。 public static void Remove(Type memoryType, int count) { InternalCheckMemoryType(memoryType); GetMemoryCollection(memoryType).Remove(count); } /// /// 从内存池中移除所有的内存对象。 /// /// 内存对象类型。 public static void RemoveAll() where T : class, IMemory { GetMemoryCollection(typeof(T)).RemoveAll(); } /// /// 从内存池中移除所有的内存对象。 /// /// 内存对象类型。 public static void RemoveAll(Type memoryType) { InternalCheckMemoryType(memoryType); GetMemoryCollection(memoryType).RemoveAll(); } private static void InternalCheckMemoryType(Type memoryType) { if (!_enableStrictCheck) { return; } if (memoryType == null) { throw new Exception("Memory type is invalid."); } if (!memoryType.IsClass || memoryType.IsAbstract) { throw new Exception("Memory type is not a non-abstract class type."); } if (!typeof(IMemory).IsAssignableFrom(memoryType)) { throw new Exception(string.Format("Memory type '{0}' is invalid.", memoryType.FullName)); } } private static MemoryCollection GetMemoryCollection(Type memoryType) { if (memoryType == null) { throw new Exception("MemoryType is invalid."); } MemoryCollection memoryCollection = null; lock (_memoryCollections) { if (!_memoryCollections.TryGetValue(memoryType, out memoryCollection)) { memoryCollection = new MemoryCollection(memoryType); _memoryCollections.Add(memoryType, memoryCollection); } } return memoryCollection; } } }