/// <summary>
 /// Manages a memory pool of poolSize blocks.
 /// Whenever we've allocated more blocks than the poolSize, we signal the oldest allocated block to purge
 ///   itself and return to the pool.
 /// </summary>
 /// <param name="poolSize"></param>
 /// <param name="factoryFunc"></param>
 /// <param name="monitor"></param>
 /// <param name="monitorWriteInterval"></param>
 public FixedSizeObjectPool(Func <T> factoryFunc, int poolSize, IObjectPoolMonitor monitor = null, TimeSpan?monitorWriteInterval = null)
     : base(factoryFunc, poolSize, monitor, monitorWriteInterval)
 {
     if (poolSize < MinObjectCount)
     {
         throw new ArgumentOutOfRangeException("poolSize", "Minimum object count is " + MinObjectCount);
     }
     maxObjectCount = poolSize;
 }
Exemple #2
0
        /// <summary>
        /// Simple object pool
        /// </summary>
        /// <param name="factoryFunc">Function used to create new resources of type T</param>
        /// <param name="monitor">monitor to report statistics for object pool</param>
        /// <param name="monitorWriteInterval"></param>
        public ObjectPool(Func <T> factoryFunc, IObjectPoolMonitor monitor = null, TimeSpan?monitorWriteInterval = null)
        {
            if (factoryFunc == null)
            {
                throw new ArgumentNullException("factoryFunc");
            }

            this.factoryFunc = factoryFunc;
            pool             = new ConcurrentStack <T>();
            this.monitor     = monitor;

            if (this.monitor != null && monitorWriteInterval.HasValue)
            {
                this.timer = new Timer(this.ReportObjectPoolStatistics, null, monitorWriteInterval.Value, monitorWriteInterval.Value);
            }
            this.totalObjects = 0;
        }
Exemple #3
0
        /// <summary>
        /// Simple object pool
        /// </summary>
        /// <param name="factoryFunc">Function used to create new resources of type T</param>
        /// <param name="initialCapacity">Initial number of items to allocate</param>
        /// <param name="monitor">monitor to report statistics for object pool</param>
        /// <param name="monitorWriteInterval"></param>
        public ObjectPool(Func <T> factoryFunc, int initialCapacity = DefaultPoolCapacity, IObjectPoolMonitor monitor = null, TimeSpan?monitorWriteInterval = null)
        {
            if (factoryFunc == null)
            {
                throw new ArgumentNullException("factoryFunc");
            }
            if (initialCapacity < 0)
            {
                throw new ArgumentOutOfRangeException("initialCapacity");
            }
            this.factoryFunc = factoryFunc;
            pool             = new Stack <T>(initialCapacity);
            this.monitor     = monitor;

            if (this.monitor != null && monitorWriteInterval.HasValue)
            {
                this.timer = new Timer(this.ReportObjectPoolStatistics, null, monitorWriteInterval.Value, monitorWriteInterval.Value);
            }
            this.totalObjects = 0;
        }