Ejemplo n.º 1
0
        public void Release(byte[] buffer)
        {
            var size      = buffer.Length;
            var sizeIndex = GetIndexOf(size);

            BufferLevel level;

            _sem.Wait();
            try {
                level = _bufferLevels[sizeIndex];
                if (level == null)
                {
                    level = new BufferLevel(size);
                    _bufferLevels[sizeIndex] = level;
                    if (_upperIndex < sizeIndex)
                    {
                        _upperIndex = sizeIndex;
                    }
                }
            }
            finally {
                _sem.Release();
            }
            level.Release(buffer);
            if (_slidingExpiration <= TimeSpan.Zero)
            {
                return;
            }
            _slidingQueue.Enqueue(DateTime.Now.Add(_slidingExpiration), level);
            SetupTimer();
        }
Ejemplo n.º 2
0
        public BinaryBufferPool(int size, TimeSpan slidingExpiration)
        {
            if (size < 1024)
            {
                throw new ArgumentException("Size may not be under 1024.");
            }
            _size = size;
            _slidingExpiration = slidingExpiration;
            _bufferLevels      = new BufferLevel[20];
            _bufferLevels[0]   = new BufferLevel(size);
            _slidingQueue      = new DateTimeQueue <BufferLevel>();

            _maxIndex = (int)Math.Log(int.MaxValue / _size, 2);
            _size1    = _size * 2;
            _size2    = _size1 * 2;
            _size3    = _size2 * 2;
            _size4    = _size3 * 2;
            _size5    = _size4 * 2;
            _size6    = _size5 * 2;
            _size7    = _size6 * 2;
            _size8    = _size7 * 2;
            _size9    = _size8 * 2;
            _size10   = _size9 * 2;
            _size11   = _size10 * 2;
            _size12   = _size11 * 2;
            _size13   = _size12 * 2;
            _size14   = _size13 * 2;
            _size15   = _size14 * 2;
            _size16   = _size15 * 2;
            _size17   = _size16 * 2;
            _size18   = _size17 * 2;
            _size19   = _size18 * 2;
            _size20   = _size19 * 2;
        }
Ejemplo n.º 3
0
        public byte[] AcquireBuffer(int minSize)
        {
            var rest = minSize % _size;

            if (rest > 0)
            {
                // Makes all sized divisable by _size
                // So that we have less buffer levels
                minSize += _size - rest;
            }
            var         sizeIndex = GetIndexOf(minSize);
            BufferLevel level     = null;

            _sem.Wait();
            try {
                for (var i = _upperIndex; i >= sizeIndex; i--)
                {
                    level = _bufferLevels[i];
                    if (i == sizeIndex)
                    {
                        break;
                    }
                    if (level == null)
                    {
                        continue;
                    }

                    if (level.TryPop(out var buffer))
                    {
                        return(buffer);
                    }
                }
            }
            finally {
                _sem.Release();
            }
            return(level == null
                ? new byte[minSize]
                : level.PopOrCreate());
        }