Exemple #1
0
        /// <summary>
        /// Clears and returns objects to the pool.
        /// </summary>
        /// <param name="pool">The object pool to return to.</param>
        /// <param name="map">The <see cref="Dictionary{TKey,TValue}"/> to release.</param>
        public static void ClearAndFree <TKey, TValue>([NotNull] this ObjectPool <Dictionary <TKey, TValue> > pool, Dictionary <TKey, TValue> map)
        {
            if (map == null)
            {
                return;
            }

            // if map grew too big, don't put it back to pool
            if (map.Count > Threshold)
            {
                pool.ForgetTrackedObject(map);
                return;
            }

            map.Clear();
            pool.Free(map);
        }
        /// <summary>
        /// Returns object to the pool.
        /// </summary>
        public void Dispose()
        {
            if (_pooledObject == null)
            {
                return;
            }

            if (_releaser != null)
            {
                _releaser(_pool, _pooledObject);
            }
            else
            {
                _pool.Free(_pooledObject);
            }

            _pooledObject = null;
        }
Exemple #3
0
        /// <summary>
        /// Clears and returns objects to the pool.
        /// </summary>
        /// <param name="pool">The object pool to return to.</param>
        /// <param name="queue">The <see cref="Queue{T}"/> to release.</param>
        public static void ClearAndFree <T>([NotNull] this ObjectPool <Queue <T> > pool, Queue <T> queue)
        {
            if (queue == null)
            {
                return;
            }

            var count = queue.Count;

            queue.Clear();

            if (count > _threshold)
            {
                queue.TrimExcess();
            }

            pool.Free(queue);
        }
Exemple #4
0
        /// <summary>
        /// Clears and returns objects to the pool.
        /// </summary>
        /// <param name="pool">The object pool to return to.</param>
        /// <param name="stack">The <see cref="Stack{T}"/> to release.</param>
        public static void ClearAndFree <T>([NotNull] this ObjectPool <Stack <T> > pool, Stack <T> stack)
        {
            if (stack == null)
            {
                return;
            }

            var count = stack.Count;

            stack.Clear();

            if (count > _threshold)
            {
                stack.TrimExcess();
            }

            pool.Free(stack);
        }
Exemple #5
0
        /// <summary>
        /// Clears and returns objects to the pool.
        /// </summary>
        /// <param name="pool">The object pool to return to.</param>
        /// <param name="set">The <see cref="HashSet{T}"/> to release.</param>
        public static void ClearAndFree <T>([NotNull] this ObjectPool <HashSet <T> > pool, HashSet <T> set)
        {
            if (set == null)
            {
                return;
            }

            var count = set.Count;

            set.Clear();

            if (count > _threshold)
            {
                set.TrimExcess();
            }

            pool.Free(set);
        }
Exemple #6
0
        /// <summary>
        /// Clears and returns objects to the pool.
        /// </summary>
        /// <param name="pool">The object pool to return to.</param>
        /// <param name="sb">The <see cref="StringBuilder"/> to release.</param>
        public static void ClearAndFree([NotNull] this ObjectPool <StringBuilder> pool, StringBuilder sb)
        {
            if (sb == null)
            {
                return;
            }

#if FW35
            sb.Length = 0;
#else
            sb.Clear();
#endif

            if (sb.Capacity > _threshold)
            {
                sb.Capacity = _threshold;
            }

            pool.Free(sb);
        }