/// <summary> /// Creates a pool for the specified type with the specified initial capacity and allocator. /// </summary> /// <typeparam name="T">The type of object for which to create an expanding pool.</typeparam> /// <param name="capacity">The pool's initial capacity.</param> /// <param name="watermark">The pool's watermark value, if it must be created.</param> /// <param name="allocator">The pool's instance allocator, if it must be created.</param> public void Create <T>(Int32 capacity, Int32 watermark, Func <T> allocator = null) { Contract.EnsureRange(capacity >= 0, nameof(capacity)); Contract.EnsureRange(watermark >= 1, nameof(watermark)); Contract.EnsureRange(watermark >= capacity, nameof(watermark)); if (pools.ContainsKey(typeof(T))) { throw new InvalidOperationException(CoreStrings.PoolRegistryAlreadyContainsType.Format(typeof(T))); } pools[typeof(T)] = new ExpandingPool <T>(capacity, watermark, allocator); }
/// <summary> /// Gets the pool for the specified type. If the pool does not exist, it will be created /// with the specified initial capacity and allocator. /// </summary> /// <typeparam name="T">The type of object for which to retrieve a pool.</typeparam> /// <param name="capacity">The initial capacity of the pool, if it must be created.</param> /// <param name="watermark">The pool's watermark value, if it must be created.</param> /// <param name="allocator">The pool's instance allocator, if it must be created.</param> /// <returns>The pool for the specified type.</returns> public IPool <T> Get <T>(Int32 capacity, Int32 watermark, Func <T> allocator = null) { Contract.EnsureRange(capacity >= 0, nameof(capacity)); Contract.EnsureRange(watermark >= 1, nameof(watermark)); Contract.EnsureRange(watermark >= capacity, nameof(watermark)); IPool pool; if (!pools.TryGetValue(typeof(T), out pool)) { pool = new ExpandingPool <T>(capacity, watermark, allocator); pools[typeof(T)] = pool; } return((IPool <T>)pool); }