예제 #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)
            {
                InheritedPool <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 InheritedPool <T[]>(1, 25);
                    pools[length] = pool;
                }
                pool.Pool(ref disposing);
            }
            disposing = null;
        }
예제 #2
0
        private static T[] CreateInternal(int length, bool clearContent)
        {
            if (length == 0)
            {
                return(ZeroSizeArray);
            }

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


            InheritedPool <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);
        }