예제 #1
0
        public static void Dispose(ref T[] disposing)
        {
                        #if DEV_MODE && PI_ASSERTATIONS
            Debug.Assert(!typeof(IDrawer).IsAssignableFrom(typeof(T)), "Use DrawerArrayPool instead");
            Debug.Assert(typeof(T) != Types.UnityObject || !LinkedMemberHierarchy.AnyHierarchyTargetsArrayEquals(disposing as UnityEngine.Object[]));
                        #endif

            int length = disposing.Length;

            //don't pool zero-length arrays since we'll be using ZeroSizeArray field for those purposes
            if (length > 0)
            {
                PolymorphicPool <T[]> pool;

                                #if DEV_MODE && WARN_IF_POOLING_EXTERNALLY_CREATED_ITEMS
                if (!created.TryGetValue(length, out pool) || !pool.Contains(disposing))
                {
                    Debug.LogWarning("ArrayPool<" + StringUtils.ToString(typeof(T)) + ">.Dispose was called for array that was not created by ArrayPool. This could lead to bugs:\ndisposing: " + StringUtils.ToString(disposing));
                }
                else
                {
                    pool.Remove(disposing);
                }
                                #endif

                if (!pools.TryGetValue(length, out pool))
                {
                    pool          = new PolymorphicPool <T[]>(1, 25);
                    pools[length] = pool;
                }
                pool.Pool(ref disposing);
            }
            disposing = null;
        }
예제 #2
0
        /// <summary>
        /// Disposes the array to the pool and sets the reference to it to null,
        /// after either disposing all its children, or simply setting references
        /// to them to null, depending on the value of disposeContent.
        /// </summary>
        /// <param name="disposing"> The array to dispose. This should not be null when the method is called. It will be set to null once the method has finished. </param>
        /// <param name="disposeContent"> If true, Dispose will be called for each drawer inside the array. </param>
        public static void Dispose(ref IDrawer[] disposing, bool disposeContent)
        {
            // Don't pool zero-length arrays since we'll be using ZeroSizeArray field for those purposes.
            int size = disposing.Length;

            if (size == 0)
            {
                disposing = null;
                return;
            }

            if (disposeContent)
            {
                DisposeContent(ref disposing);
            }
            else
            {
                ClearContent(ref disposing);
            }

            PolymorphicPool <IDrawer[]> pool;

            if (!pools.TryGetValue(size, out pool))
            {
                pool = new PolymorphicPool <IDrawer[]>(1, 250);
            }
            pool.Pool(ref disposing);
        }
예제 #3
0
        private static T[] CreateInternal(int length, bool clearContent)
        {
            if (length == 0)
            {
                return(ZeroSizeArray);
            }

                        #if DEV_MODE && WARN_IF_POOLING_EXTERNALLY_CREATED_ITEMS
            PolymorphicPool <T[]> createdPool;
            if (!created.TryGetValue(length, out createdPool))
            {
                createdPool     = new PolymorphicPool <T[]>(1, 1000000);
                created[length] = createdPool;
            }
                        #endif


            PolymorphicPool <T[]> pool;
            if (!pools.TryGetValue(length, out pool))
            {
                                #if DEV_MODE && WARN_IF_POOLING_EXTERNALLY_CREATED_ITEMS
                var createdArray     = new T[length];
                var poolCreatedArray = createdArray;
                createdPool.Pool(ref poolCreatedArray);
                return(createdArray);
                                #else
                return(new T[length]);
                                #endif
            }
            T[] result;
            if (!pool.TryGet(out result))
            {
                                #if DEV_MODE && WARN_IF_POOLING_EXTERNALLY_CREATED_ITEMS
                var createdArray     = new T[length];
                var poolCreatedArray = createdArray;
                createdPool.Pool(ref poolCreatedArray);
                return(createdArray);
                                #else
                return(new T[length]);
                                #endif
            }
            if (clearContent)
            {
                ClearContent(ref result);
            }

                        #if DEV_MODE && WARN_IF_POOLING_EXTERNALLY_CREATED_ITEMS
            {
                var poolArray = result;
                createdPool.Pool(ref poolArray);
            }
                        #endif

            return(result);
        }