コード例 #1
0
        /// <summary>
        /// Adds segments, returning the initial segment of the newly added chain.
        /// </summary>
        /// <param name="segmentsToObtain"></param>
        /// <returns>Returns the initial segment of the newly added chain.</returns>
        private Segment *AddSegments(int segmentsToObtain)
        {
            Segment *segment;

            if (_pool.TryPop(segmentsToObtain, out segment) == segmentsToObtain)
            {
                if (Head == null)
                {
                    Head  = segment;
                    _tail = GetTailOrThis(segment);
                }
                else
                {
                    // attach the new segment to the end O(1)
                    _tail->Next = segment;
                    _tail       = GetTailOrThis(segment); // rewrite tail O(number of segments added)
                }

                var s = segment;
                while (s != null)
                {
                    _capacity += s->Length;
                    s          = s->Next;
                }

                return(segment);
            }
            else
            {
                // not enough elements taken, release and throw
                _pool.Push(segment);
                throw new Exception("Not enough memory in the pool");
            }
        }
コード例 #2
0
        public static unsafe Segment *Pop(this ISegmentPool pool)
        {
            Segment *result;

            if (pool.TryPop(out result) == false)
            {
                throw new InvalidOperationException("Pool cannot provide more memory");
            }

            return(result);
        }