SegmentBufferManager.cs 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. using System;
  2. using System.Collections.Concurrent;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. namespace MECF.Framework.Common.Communications.Tcp.Buffer
  6. {
  7. /// <summary>
  8. /// A manager to handle buffers for the socket connections.
  9. /// </summary>
  10. /// <remarks>
  11. /// When used in an async call a buffer is pinned. Large numbers of pinned buffers
  12. /// cause problem with the GC (in particular it causes heap fragmentation).
  13. /// This class maintains a set of large segments and gives clients pieces of these
  14. /// segments that they can use for their buffers. The alternative to this would be to
  15. /// create many small arrays which it then maintained. This methodology should be slightly
  16. /// better than the many small array methodology because in creating only a few very
  17. /// large objects it will force these objects to be placed on the LOH. Since the
  18. /// objects are on the LOH they are at this time not subject to compacting which would
  19. /// require an update of all GC roots as would be the case with lots of smaller arrays
  20. /// that were in the normal heap.
  21. /// </remarks>
  22. public class SegmentBufferManager : ISegmentBufferManager
  23. {
  24. private const int TrialsCount = 100;
  25. private static SegmentBufferManager _defaultBufferManager;
  26. private readonly int _segmentChunks;
  27. private readonly int _chunkSize;
  28. private readonly int _segmentSize;
  29. private readonly bool _allowedToCreateMemory;
  30. private readonly ConcurrentStack<ArraySegment<byte>> _buffers = new ConcurrentStack<ArraySegment<byte>>();
  31. private readonly List<byte[]> _segments;
  32. private readonly object _creatingNewSegmentLock = new object();
  33. public static SegmentBufferManager Default
  34. {
  35. get
  36. {
  37. // default to 1024 1kb buffers if people don't want to manage it on their own;
  38. if (_defaultBufferManager == null)
  39. _defaultBufferManager = new SegmentBufferManager(1024, 1024, 1);
  40. return _defaultBufferManager;
  41. }
  42. }
  43. public static void SetDefaultBufferManager(SegmentBufferManager manager)
  44. {
  45. if (manager == null)
  46. throw new ArgumentNullException("manager");
  47. _defaultBufferManager = manager;
  48. }
  49. public int ChunkSize
  50. {
  51. get { return _chunkSize; }
  52. }
  53. public int SegmentsCount
  54. {
  55. get { return _segments.Count; }
  56. }
  57. public int SegmentChunksCount
  58. {
  59. get { return _segmentChunks; }
  60. }
  61. public int AvailableBuffers
  62. {
  63. get { return _buffers.Count; }
  64. }
  65. public int TotalBufferSize
  66. {
  67. get { return _segments.Count * _segmentSize; }
  68. }
  69. public SegmentBufferManager(int segmentChunks, int chunkSize)
  70. : this(segmentChunks, chunkSize, 1) { }
  71. public SegmentBufferManager(int segmentChunks, int chunkSize, int initialSegments)
  72. : this(segmentChunks, chunkSize, initialSegments, true) { }
  73. /// <summary>
  74. /// Constructs a new <see cref="SegmentBufferManager"></see> object
  75. /// </summary>
  76. /// <param name="segmentChunks">The number of chunks to create per segment</param>
  77. /// <param name="chunkSize">The size of a chunk in bytes</param>
  78. /// <param name="initialSegments">The initial number of segments to create</param>
  79. /// <param name="allowedToCreateMemory">If false when empty and checkout is called an exception will be thrown</param>
  80. public SegmentBufferManager(int segmentChunks, int chunkSize, int initialSegments, bool allowedToCreateMemory)
  81. {
  82. if (segmentChunks <= 0)
  83. throw new ArgumentException("segmentChunks");
  84. if (chunkSize <= 0)
  85. throw new ArgumentException("chunkSize");
  86. if (initialSegments < 0)
  87. throw new ArgumentException("initialSegments");
  88. _segmentChunks = segmentChunks;
  89. _chunkSize = chunkSize;
  90. _segmentSize = _segmentChunks * _chunkSize;
  91. _segments = new List<byte[]>();
  92. _allowedToCreateMemory = true;
  93. for (int i = 0; i < initialSegments; i++)
  94. {
  95. CreateNewSegment(true);
  96. }
  97. _allowedToCreateMemory = allowedToCreateMemory;
  98. }
  99. private void CreateNewSegment(bool forceCreation)
  100. {
  101. if (!_allowedToCreateMemory)
  102. throw new UnableToCreateMemoryException();
  103. lock (_creatingNewSegmentLock)
  104. {
  105. if (!forceCreation && _buffers.Count > _segmentChunks / 2)
  106. return;
  107. var bytes = new byte[_segmentSize];
  108. _segments.Add(bytes);
  109. for (int i = 0; i < _segmentChunks; i++)
  110. {
  111. var chunk = new ArraySegment<byte>(bytes, i * _chunkSize, _chunkSize);
  112. _buffers.Push(chunk);
  113. }
  114. }
  115. }
  116. public ArraySegment<byte> BorrowBuffer()
  117. {
  118. int trial = 0;
  119. while (trial < TrialsCount)
  120. {
  121. ArraySegment<byte> result;
  122. if (_buffers.TryPop(out result))
  123. return result;
  124. CreateNewSegment(false);
  125. trial++;
  126. }
  127. throw new UnableToAllocateBufferException();
  128. }
  129. public IEnumerable<ArraySegment<byte>> BorrowBuffers(int count)
  130. {
  131. var result = new ArraySegment<byte>[count];
  132. var trial = 0;
  133. var totalReceived = 0;
  134. try
  135. {
  136. while (trial < TrialsCount)
  137. {
  138. ArraySegment<byte> piece;
  139. while (totalReceived < count)
  140. {
  141. if (!_buffers.TryPop(out piece))
  142. break;
  143. result[totalReceived] = piece;
  144. ++totalReceived;
  145. }
  146. if (totalReceived == count)
  147. return result;
  148. CreateNewSegment(false);
  149. trial++;
  150. }
  151. throw new UnableToAllocateBufferException();
  152. }
  153. catch
  154. {
  155. if (totalReceived > 0)
  156. ReturnBuffers(result.Take(totalReceived));
  157. throw;
  158. }
  159. }
  160. public void ReturnBuffer(ArraySegment<byte> buffer)
  161. {
  162. if (ValidateBuffer(buffer))
  163. {
  164. _buffers.Push(buffer);
  165. }
  166. }
  167. public void ReturnBuffers(IEnumerable<ArraySegment<byte>> buffers)
  168. {
  169. if (buffers == null)
  170. throw new ArgumentNullException("buffers");
  171. foreach (var buf in buffers)
  172. {
  173. if (ValidateBuffer(buf))
  174. {
  175. _buffers.Push(buf);
  176. }
  177. }
  178. }
  179. public void ReturnBuffers(params ArraySegment<byte>[] buffers)
  180. {
  181. if (buffers == null)
  182. throw new ArgumentNullException("buffers");
  183. foreach (var buf in buffers)
  184. {
  185. if (ValidateBuffer(buf))
  186. {
  187. _buffers.Push(buf);
  188. }
  189. }
  190. }
  191. private bool ValidateBuffer(ArraySegment<byte> buffer)
  192. {
  193. if (buffer.Array == null || buffer.Count == 0 || buffer.Array.Length < buffer.Offset + buffer.Count)
  194. return false;
  195. if (buffer.Count != _chunkSize)
  196. return false;
  197. return true;
  198. }
  199. }
  200. }