Exemple #1
0
            public PoolBufferInstance Allocate(int actualSize)
            {
#if BUFFER_POOL_STATS
                Interlocked.Increment(ref _allocationCount);
                Interlocked.Add(ref _allocationActualSize, actualSize);
#endif
                Debug.Assert(actualSize <= Size);

                lock (_pool)
                {
                    if (_pool.Count > 0)
                        return _pool.Pop();
                }

                var bufferEntry = new PoolBufferInstance(Size);
#if BUFFER_POOL_STATS
                Interlocked.Increment(ref _newAllocationCount);
                lock (_allocationTracker)
                {
                    _allocationTracker.Add(bufferEntry);
                }
#endif

                return bufferEntry;
            }
Exemple #2
0
            public void Free(PoolBufferInstance buffer)
            {
#if BUFFER_POOL_STATS
                Interlocked.Increment(ref _freeCount);
#endif

                if (Size != buffer.Buffer.Length)
                    throw new ArgumentException("Invalid buffer size", nameof(buffer));

                lock (_pool)
                {
                    _pool.Push(buffer);
                }
            }
Exemple #3
0
        public BufferInstance Allocate(int minSize)
        {
#if BUFFER_POOL_STATS
            Interlocked.Increment(ref _allocationCount);
            Interlocked.Add(ref _requestedAllocationBytes, minSize);
#endif
            var pool = FindPool(minSize);

            PoolBufferInstance bufferEntry;
            if (null != pool)
                bufferEntry = pool.Allocate(minSize);
            else
            {
                bufferEntry = new PoolBufferInstance(minSize);
#if BUFFER_POOL_STATS
                Interlocked.Increment(ref _nonPoolAllocationCount);
#endif
            }

#if BUFFER_POOL_STATS
            Interlocked.Add(ref _actualAllocationBytes, bufferEntry.Buffer.Length);
#endif

            bufferEntry.Reference();

#if DEBUG
            //Debug.WriteLine("Allocated Buffer {0}", bufferEntry);
#endif

            return bufferEntry;
        }