Example #1
0
        public void Clear()
        {
            if (m_ActionOnDestroy != null)
            {
                for (var itr = m_PoolFirst; itr != null; itr = itr.poolNext)
                {
                    m_ActionOnDestroy(itr.value);
                }
            }

            m_PoolFirst             = null;
            m_NextAvailableListItem = null;
            CountInactive           = 0;
        }
Example #2
0
        ///<inheritdoc/>
        public override void Return(T element)
        {
            // Never put an instance to the stack, if pooling is disabled
            if (!IsPoolingEnabled)
            {
                return;
            }

            var listItem = PoolFirst;

            while (listItem != null)
            {
                // This is a safe, but expensive
                if (PoolSettings.CheckReturnedObjectsExistInPool && ReferenceEquals(listItem.Value, element))
                {
                    throw new PoolingException("Trying to return an object that has already been returned to the pool.", GetType());
                }
                listItem = listItem.PoolNext;
            }

            PoolPolicy.ActionOnReturn?.Invoke(element);

            if (CountInactive < PoolPolicy.MaximumPoolSize)
            {
                var poolItem = NextAvailableListItem;
                if (poolItem == null)
                {
                    poolItem = new LinkedPoolItem();
                }
                else
                {
                    NextAvailableListItem = poolItem.PoolNext;
                }

                poolItem.Value    = element;
                poolItem.PoolNext = PoolFirst;
                PoolFirst         = poolItem;
                _countInactive++;
            }
            else
            {
                PoolPolicy.ActionOnDestroy?.Invoke(element);
            }
        }
Example #3
0
        public void Release(T item)
        {
            #if UNITY_EDITOR // keep heavy checks in editor
            if (m_CollectionCheck)
            {
                var listItem = m_PoolFirst;
                while (listItem != null)
                {
                    if (ReferenceEquals(listItem.value, item))
                    {
                        throw new InvalidOperationException("Trying to release an object that has already been released to the pool.");
                    }
                    listItem = listItem.poolNext;
                }
            }
            #endif

            m_ActionOnRelease?.Invoke(item);

            if (CountInactive < m_Limit)
            {
                LinkedPoolItem poolItem = m_NextAvailableListItem;
                if (poolItem == null)
                {
                    poolItem = new LinkedPoolItem();
                }
                else
                {
                    m_NextAvailableListItem = poolItem.poolNext;
                }

                poolItem.value    = item;
                poolItem.poolNext = m_PoolFirst;
                m_PoolFirst       = poolItem;
                ++CountInactive;
            }
            else
            {
                m_ActionOnDestroy?.Invoke(item);
            }
        }
Example #4
0
        public T Get()
        {
            T item = null;

            if (m_PoolFirst == null)
            {
                item = m_CreateFunc();
            }
            else
            {
                var first = m_PoolFirst;
                item        = first.value;
                m_PoolFirst = first.poolNext;

                // Add the empty node to our pool for reuse and to prevent GC
                first.poolNext                = m_NextAvailableListItem;
                m_NextAvailableListItem       = first;
                m_NextAvailableListItem.value = null;
                --CountInactive;
            }
            m_ActionOnGet?.Invoke(item);
            return(item);
        }