BsonChunkPool.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /* Copyright 2010-2014 MongoDB Inc.
  2. *
  3. * Licensed under the Apache License, Version 2.0 (the "License");
  4. * you may not use this file except in compliance with the License.
  5. * You may obtain a copy of the License at
  6. *
  7. * http://www.apache.org/licenses/LICENSE-2.0
  8. *
  9. * Unless required by applicable law or agreed to in writing, software
  10. * distributed under the License is distributed on an "AS IS" BASIS,
  11. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. * See the License for the specific language governing permissions and
  13. * limitations under the License.
  14. */
  15. using System;
  16. using System.Collections.Generic;
  17. namespace MongoDB.Bson.IO
  18. {
  19. /// <summary>
  20. /// Represents a pool of chunks used by BsonBuffer.
  21. /// </summary>
  22. public class BsonChunkPool
  23. {
  24. // private static fields
  25. private static BsonChunkPool __default = new BsonChunkPool(2048, 16 * 1024); // 32MiB of 16KiB chunks
  26. // private fields
  27. private readonly object _lock = new object();
  28. private readonly Stack<BsonChunk> _chunks = new Stack<BsonChunk>();
  29. private readonly int _maxPoolSize;
  30. private readonly int _chunkSize;
  31. // constructors
  32. /// <summary>
  33. /// Initializes a new instance of the <see cref="BsonChunkPool"/> class.
  34. /// </summary>
  35. /// <param name="maxPoolSize">The maximum number of chunks to keep in the pool.</param>
  36. /// <param name="chunkSize">The size of each chunk.</param>
  37. public BsonChunkPool(int maxPoolSize, int chunkSize)
  38. {
  39. _maxPoolSize = maxPoolSize;
  40. _chunkSize = chunkSize;
  41. }
  42. // public static properties
  43. /// <summary>
  44. /// Gets the default chunk pool.
  45. /// </summary>
  46. /// <value>
  47. /// The default chunk pool.
  48. /// </value>
  49. public static BsonChunkPool Default
  50. {
  51. get { return __default; }
  52. set
  53. {
  54. if (value == null)
  55. {
  56. throw new ArgumentNullException("Default");
  57. }
  58. __default = value;
  59. }
  60. }
  61. // public properties
  62. /// <summary>
  63. /// Gets the chunk size.
  64. /// </summary>
  65. /// <value>
  66. /// The chunk size.
  67. /// </value>
  68. public int ChunkSize
  69. {
  70. get { return _chunkSize; }
  71. }
  72. /// <summary>
  73. /// Gets or sets the max pool size.
  74. /// </summary>
  75. public int MaxPoolSize
  76. {
  77. get { return _maxPoolSize; }
  78. }
  79. // internal methods
  80. /// <summary>
  81. /// Acquires a chunk.
  82. /// </summary>
  83. /// <returns></returns>
  84. internal BsonChunk AcquireChunk()
  85. {
  86. lock (_lock)
  87. {
  88. if (_chunks.Count > 0)
  89. {
  90. return _chunks.Pop();
  91. }
  92. }
  93. // release the lock before allocating memory
  94. var bytes = new byte[_chunkSize];
  95. return new BsonChunk(bytes, this);
  96. }
  97. /// <summary>
  98. /// Releases a chunk.
  99. /// </summary>
  100. /// <param name="chunk">The chunk.</param>
  101. internal void ReleaseChunk(BsonChunk chunk)
  102. {
  103. if (chunk.ReferenceCount != 0)
  104. {
  105. new BsonInternalException("A chunk is being returned to the pool and the reference count is not zero.");
  106. }
  107. lock (_lock)
  108. {
  109. if (_chunks.Count < _maxPoolSize)
  110. {
  111. _chunks.Push(chunk);
  112. }
  113. // otherwise just let it get garbage collected
  114. }
  115. }
  116. }
  117. }