ObjectPool.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. namespace NRKernal
  2. {
  3. using System;
  4. using System.Collections.Generic;
  5. using UnityEngine;
  6. /// <summary> An object pool. </summary>
  7. public class ObjectPool
  8. {
  9. /// <summary> Dictionary of cache pools. </summary>
  10. private Dictionary<Type, Queue<object>> m_CachePoolDict = new Dictionary<Type, Queue<object>>();
  11. /// <summary> Number of initializes. </summary>
  12. public int InitCount = 100;
  13. /// <summary> Expansions this object. </summary>
  14. /// <typeparam name="T"> Generic type parameter.</typeparam>
  15. public void Expansion<T>() where T : new()
  16. {
  17. var queue = GetQueue<T>();
  18. for (int i = 0; i < InitCount; i++)
  19. {
  20. T data = new T();
  21. queue.Enqueue(data);
  22. }
  23. }
  24. /// <summary> Gets the get. </summary>
  25. /// <typeparam name="T"> Generic type parameter.</typeparam>
  26. /// <returns> A T. </returns>
  27. public T Get<T>() where T : new()
  28. {
  29. var queue = GetQueue<T>();
  30. if (queue.Count == 0)
  31. {
  32. Expansion<T>();
  33. }
  34. return (T)queue.Dequeue();
  35. }
  36. /// <summary> Puts the given data. </summary>
  37. /// <typeparam name="T"> Generic type parameter.</typeparam>
  38. /// <param name="data"> The data.</param>
  39. public void Put<T>(T data) where T : new()
  40. {
  41. var queue = GetQueue<T>();
  42. queue.Enqueue(data);
  43. }
  44. /// <summary> Gets the queue. </summary>
  45. /// <typeparam name="T"> Generic type parameter.</typeparam>
  46. /// <returns> The queue. </returns>
  47. private Queue<object> GetQueue<T>() where T : new()
  48. {
  49. Queue<object> queue = null;
  50. m_CachePoolDict.TryGetValue(typeof(T), out queue);
  51. if (queue == null)
  52. {
  53. queue = new Queue<object>();
  54. m_CachePoolDict.Add(typeof(T), queue);
  55. }
  56. return queue;
  57. }
  58. public void Clear()
  59. {
  60. m_CachePoolDict.Clear();
  61. }
  62. }
  63. /// <summary> The bytes pool. </summary>
  64. public class BytesPool
  65. {
  66. /// <summary> Dictionary of bytes. </summary>
  67. public Dictionary<int, Queue<byte[]>> BytesDict = new Dictionary<int, Queue<byte[]>>();
  68. /// <summary> Gets a byte[] using the given length. </summary>
  69. /// <param name="len"> The Length to get.</param>
  70. /// <returns> A byte[]. </returns>
  71. public byte[] Get(int len)
  72. {
  73. if (len <= 0)
  74. {
  75. NRDebugger.Info("BytesPool get len is not valid :" + len);
  76. return null;
  77. }
  78. Queue<byte[]> que = null;
  79. BytesDict.TryGetValue(len, out que);
  80. if (que == null)
  81. {
  82. que = new Queue<byte[]>();
  83. byte[] temp = new byte[len];
  84. que.Enqueue(temp);
  85. BytesDict.Add(len, que);
  86. }
  87. else if (que.Count == 0)
  88. {
  89. byte[] temp = new byte[len];
  90. que.Enqueue(temp);
  91. }
  92. return que.Dequeue();
  93. }
  94. /// <summary> Puts the given data. </summary>
  95. /// <param name="data"> The data to put.</param>
  96. public void Put(byte[] data)
  97. {
  98. if (data == null || data.Length == 0)
  99. {
  100. NRDebugger.Info("BytesPool retrieve data is null.");
  101. return;
  102. }
  103. Queue<byte[]> que = null;
  104. BytesDict.TryGetValue(data.Length, out que);
  105. if (que == null)
  106. {
  107. que = new Queue<byte[]>();
  108. BytesDict.Add(data.Length, que);
  109. }
  110. que.Enqueue(data);
  111. }
  112. public void Clear()
  113. {
  114. BytesDict.Clear();
  115. }
  116. }
  117. }