/// <summary>
        ///     Gets an instance of an <see cref="AutoRecyclingArray"/>.
        /// </summary>
        /// <param name="minLength">The minimumn number of bytes to start with.</param>
        /// <returns>The array.</returns>
        // TODO DR3 use Caller Information Attributes to improve debugging
        internal static AutoRecyclingArray Create(int minLength)
        {
            AutoRecyclingArray array = ObjectCache.GetAutoRecyclingArray();

            array.isCurrentlyLoungingInAPool = false;
            array.Buffer = ObjectCache.GetMemory(minLength);
            Interlocked.Exchange(ref array.referenceCount, 1);

            return(array);
        }
Пример #2
0
        public static void ReturnAutoRecyclingArray(AutoRecyclingArray autoRecyclingArray)
        {
            if (!initialized)
            {
                ThreadInitialize();
            }

#if DEBUG
            ObjectCacheTestHelper.AutoRecyclingArrayWasReturned();
#endif

            autoRecyclingArrayPool.ReturnInstance(autoRecyclingArray);
        }
Пример #3
0
        /// <summary>
        ///     Ensures the buffer is greater than or equal to the specified length.
        /// </summary>
        /// <param name="newLength">The desired length.</param>
        internal void EnsureLength(int newLength)
        {
            if (newLength > backingBuffer.Buffer.Length)
            {
                //Get a new buffer and copy over data
                AutoRecyclingArray newBackingBuffer = AutoRecyclingArray.Create(newLength);
                Array.Copy(backingBuffer.Buffer, newBackingBuffer.Buffer, backingBuffer.Buffer.Length);

                // Swap out the buffer
                //We're removing a reference to a reference counted object so we need to decrement the count
                backingBuffer.DecrementReference();
                backingBuffer = newBackingBuffer;
            }
        }
Пример #4
0
        /// <summary>
        ///     Creates a new message buffer with a given minimum capacity in the backing buffer.
        /// </summary>
        /// <param name="minCapacity">The minimum number of bytes in the buffer.</param>
        /// <returns>The new message buffer.</returns>
        public static MessageBuffer Create(int minCapacity)
        {
            AutoRecyclingArray array = AutoRecyclingArray.Create(minCapacity);

            MessageBuffer buffer = ObjectCache.GetMessageBuffer();

            buffer.isCurrentlyLoungingInAPool = false;

            buffer.backingBuffer = array;
            buffer.Offset        = 0;
            buffer.Count         = 0;

            return(buffer);
        }