Exemple #1
0
        private static void UncheckedEnsureCapacity <T>(ref ArrayPrefix <T> arrayPrefix, int capacity, int minimum,
                                                        bool clearArray)
        {
            Debug.Assert(minimum > capacity, "minimum > capacity");

            int nextCapacity = capacity == 0 ? DefaultCapacity : unchecked (2 * capacity);

            if (unchecked ((uint)nextCapacity > MaxCoreClrArrayLength))
            {
                nextCapacity = Math.Max(capacity + 1, MaxCoreClrArrayLength);
            }

            nextCapacity = Math.Max(nextCapacity, minimum);

            T[] next = ArrayPool <T> .Shared.Rent(nextCapacity);

            if (arrayPrefix.Count > 0)
            {
                Debug.Assert(arrayPrefix.Array != null, "arrayPrefix.Array != null");
                Array.Copy(arrayPrefix.Array, 0, next, 0, arrayPrefix.Count);
                ArrayPool <T> .Shared.Return(arrayPrefix.Array, clearArray);
            }

            arrayPrefix = ArrayPrefix.Create(next, arrayPrefix.Count);
        }
Exemple #2
0
        private const int MaxCoreClrArrayLength = 0x7fefffff; // For byte arrays the limit is slightly larger

        internal static ArrayPrefix <T> Create <T>(int capacity)
        {
            Debug.Assert(capacity >= 0, "capacity >= 0");

            T[] array = ArrayPool <T> .Shared.Rent(capacity);

            return(ArrayPrefix.Create(array, 0));
        }
Exemple #3
0
        private static void UncheckedAdd <T>(ref ArrayPrefix <T> arrayPrefix, T item)
        {
            Debug.Assert(arrayPrefix.Array != null, "arrayPrefix.Array != null");
            Debug.Assert(arrayPrefix.Count < arrayPrefix.Array.Length, "arrayPrefix.Count < arrayPrefix.Array.Length");

            arrayPrefix.Array[arrayPrefix.Count] = item;
            arrayPrefix = ArrayPrefix.Create(arrayPrefix.Array, arrayPrefix.Count + 1);
        }
Exemple #4
0
        private static void UncheckedShrink <T>(ref ArrayPrefix <T> arrayPrefix, int size, bool clearArray)
        {
            Debug.Assert(arrayPrefix.Count > size, "arrayPrefix.Count > size");

            int oldCount = arrayPrefix.Count;

            arrayPrefix = ArrayPrefix.Create(arrayPrefix.Array, size);
            if (clearArray)
            {
                Array.Clear(arrayPrefix.Array, size, oldCount - size);
            }
        }
Exemple #5
0
        private static void UncheckedGrow <T>(ref ArrayPrefix <T> arrayPrefix, int size, bool clearArray)
        {
            Debug.Assert(arrayPrefix.Count < size, "arrayPrefix.Count < size");

            int capacity = (arrayPrefix.Array?.Length).GetValueOrDefault();

            if (capacity < size)
            {
                UncheckedEnsureCapacity(ref arrayPrefix, capacity, size, clearArray);
            }

            int oldCount = arrayPrefix.Count;

            Debug.Assert(arrayPrefix.Array != null, "arrayPrefix.Array != null");
            Array.Clear(arrayPrefix.Array, oldCount, size - oldCount);
            arrayPrefix = ArrayPrefix.Create(arrayPrefix.Array, size);
        }