Exemple #1
0
        public int CalculateNewCapacity(int minNewCapacity, int maxCapacity)
        {
            if (minNewCapacity < 0)
            {
                ThrowHelper.ThrowArgumentOutOfRangeException_MinNewCapacity(minNewCapacity);
            }
            if (minNewCapacity > maxCapacity)
            {
                ThrowHelper.ThrowArgumentOutOfRangeException_MaxCapacity(minNewCapacity, maxCapacity);
            }

            const int Threshold = CalculateThreshold; // 4 MiB page

            if (minNewCapacity == CalculateThreshold)
            {
                return(Threshold);
            }

            int newCapacity;

            // If over threshold, do not double but just increase by threshold.
            if (minNewCapacity > Threshold)
            {
                newCapacity = minNewCapacity / Threshold * Threshold;
                if (newCapacity > maxCapacity - Threshold)
                {
                    newCapacity = maxCapacity;
                }
                else
                {
                    newCapacity += Threshold;
                }

                return(newCapacity);
            }

            // Not over threshold. Double up to 4 MiB, starting from 64.
            newCapacity = 64;
            while (newCapacity < minNewCapacity)
            {
                newCapacity <<= 1;
            }

            return(Math.Min(newCapacity, maxCapacity));
        }