001package net.minecraft.world.gen.layer;
002
003import java.util.ArrayList;
004import java.util.List;
005
006public 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[] aint;
035
036        if (par0 <= 256)
037        {
038            if (freeSmallArrays.isEmpty())
039            {
040                aint = new int[256];
041                inUseSmallArrays.add(aint);
042                return aint;
043            }
044            else
045            {
046                aint = (int[])freeSmallArrays.remove(freeSmallArrays.size() - 1);
047                inUseSmallArrays.add(aint);
048                return aint;
049            }
050        }
051        else if (par0 > intCacheSize)
052        {
053            intCacheSize = par0;
054            freeLargeArrays.clear();
055            inUseLargeArrays.clear();
056            aint = new int[intCacheSize];
057            inUseLargeArrays.add(aint);
058            return aint;
059        }
060        else if (freeLargeArrays.isEmpty())
061        {
062            aint = new int[intCacheSize];
063            inUseLargeArrays.add(aint);
064            return aint;
065        }
066        else
067        {
068            aint = (int[])freeLargeArrays.remove(freeLargeArrays.size() - 1);
069            inUseLargeArrays.add(aint);
070            return aint;
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
095    public static synchronized String func_85144_b()
096    {
097        return "cache: " + freeLargeArrays.size() + ", tcache: " + freeSmallArrays.size() + ", allocated: " + inUseLargeArrays.size() + ", tallocated: " + inUseSmallArrays.size();
098    }
099}