System.IO.ByteBufferPool.ReturnBuffer C# (CSharp) Méthode

ReturnBuffer() public méthode

public ReturnBuffer ( byte buffer ) : void
buffer byte
Résultat void
        public void ReturnBuffer(byte[] buffer)
        {
            if (buffer == null)
                throw new ArgumentNullException("buffer");
                
        
            // The purpose of the buffer pool is to try to reduce the 
            //   amount of garbage generated, so it doesn't matter  if
            //   the buffer gets tossed out. Since we don't want to
            //   take the perf hit of taking a lock, we only return
            //   the buffer if we can grab the control cookie.
            
            Object cookie = null;

            try
            {
                // If a ThreadAbortException gets thrown after the exchange,
                //   but before the result is assigned to cookie, then the
                //   control cookie is lost forever. However, the buffer pool
                //   will still function normally and return everybody a new
                //   buffer each time (that isn't very likely to happen,
                //   so we don't really care).
                cookie = Interlocked.Exchange(ref _controlCookie, null);
                
                if (cookie != null)
                {
                    if (_current == -1)
                    {
                        _bufferPool[0] = buffer;
                        _current = 0;
                        _last = 0;
                    }
                    else
                    {
                        int newLast = (_last + 1) % _max;
                        if (newLast != _current)
                        {
                            // the pool isn't full so store this buffer
                            _last = newLast;
                            _bufferPool[_last] = buffer;
                        }
                    }

                    _controlCookie = cookie;
                }            
            }
            catch (ThreadAbortException)
            {
                if (cookie != null)
                {
                    // This should be rare, so just reset
                    //   everything to the initial state.
                    _current = -1;
                    _last = -1;

                    // restore cookie
                    _controlCookie = cookie;
                }

                throw;            
            }
        } // ReturnBuffer