    001    package net.minecraft.src;
    002
    003    import java.util.ArrayList;
    004    import java.util.List;
    005
    006    public class IntCache
    007    {
    008        private static int intCacheSize = 256;
    009
    010        /**
    011         * A list of pre-allocated int[256] arrays that are currently unused and can be returned by getIntCache()
    012         */
    013        private static List freeSmallArrays = new ArrayList();
    014
    015        /**
    016         * A list of pre-allocated int[256] arrays that were previously returned by getIntCache() and which will not be re-
    017         * used again until resetIntCache() is called.
    018         */
    019        private static List inUseSmallArrays = new ArrayList();
    020
    021        /**
    022         * A list of pre-allocated int[cacheSize] arrays that are currently unused and can be returned by getIntCache()
    023         */
    024        private static List freeLargeArrays = new ArrayList();
    025
    026        /**
    027         * A list of pre-allocated int[cacheSize] arrays that were previously returned by getIntCache() and which will not
    028         * be re-used again until resetIntCache() is called.
    029         */
    030        private static List inUseLargeArrays = new ArrayList();
    031
    032        public static synchronized int[] getIntCache(int par0)
    033        {
    034            int[] var1;
    035
    036            if (par0 <= 256)
    037            {
    038                if (freeSmallArrays.isEmpty())
    039                {
    040                    var1 = new int[256];
    041                    inUseSmallArrays.add(var1);
    042                    return var1;
    043                }
    044                else
    045                {
    046                    var1 = (int[])freeSmallArrays.remove(freeSmallArrays.size() - 1);
    047                    inUseSmallArrays.add(var1);
    048                    return var1;
    049                }
    050            }
    051            else if (par0 > intCacheSize)
    052            {
    053                intCacheSize = par0;
    054                freeLargeArrays.clear();
    055                inUseLargeArrays.clear();
    056                var1 = new int[intCacheSize];
    057                inUseLargeArrays.add(var1);
    058                return var1;
    059            }
    060            else if (freeLargeArrays.isEmpty())
    061            {
    062                var1 = new int[intCacheSize];
    063                inUseLargeArrays.add(var1);
    064                return var1;
    065            }
    066            else
    067            {
    068                var1 = (int[])freeLargeArrays.remove(freeLargeArrays.size() - 1);
    069                inUseLargeArrays.add(var1);
    070                return var1;
    071            }
    072        }
    073
    074        /**
    075         * Mark all pre-allocated arrays as available for re-use by moving them to the appropriate free lists.
    076         */
    077        public static synchronized void resetIntCache()
    078        {
    079            if (!freeLargeArrays.isEmpty())
    080            {
    081                freeLargeArrays.remove(freeLargeArrays.size() - 1);
    082            }
    083
    084            if (!freeSmallArrays.isEmpty())
    085            {
    086                freeSmallArrays.remove(freeSmallArrays.size() - 1);
    087            }
    088
    089            freeLargeArrays.addAll(inUseLargeArrays);
    090            freeSmallArrays.addAll(inUseSmallArrays);
    091            inUseLargeArrays.clear();
    092            inUseSmallArrays.clear();
    093        }
    094    }